# How can I control a servo using Great Cow Basic?

Status
Not open for further replies.

#### jigsawkiller

##### New Member
I'm trying to control a servo using Great Cow Basic (GCBasic). I will be using the PIC18F4520 to control the servo.

So far, I have the following code which I have ported from BASIC Stamp code that does work:

Code:
#chip 18f4520, 4

dim Cnt as word

dir PORTB.0 OUT

do
'Counterclockwise:
FOR Cnt = 1 to 20
pulseout PORTB.0, 1000 ms
wait 20 ms
NEXT

'Center:
FOR Cnt = 1 to 20
pulseout PORTB.0, 750 ms
wait 20 ms
NEXT

'Clockwise:
FOR Cnt = 1 to 20
pulseout PORTB.0, 500 ms
wait 20 ms
NEXT
loop

However, the above code only makes the servo turn clockwise in even interval.

Please provide some code that could make a servo work correctly on GCBASIC. Everything compiles correctly, I'm sure the Micro controller and the servo are working. I even tried the code in ISIS simulator and get the same results as in real life.

Thank you...

Last edited:

#### Pommie

##### Well-Known Member
Try changing the for loops to something like 200 so it will wait in each position for 4 seconds. That is, FOR Cnt = 1 to 200.

Edit, also make sure that the compiler knows the speed of your chip. The default speed for that chip is 1MHz. To make it 4MHz try adding OSCCON=0x62.

Mike.

Last edited:

#### nickelflippr

##### Member
Need to be shooting for 1ms, 1.5ms, 2ms. So try using 100 10us, 150 10us, and 200 10us in the Pulseout command.

Also GCBasic presumes the oscillator is an external crystal as the default. So if you are using the internal oscillator, the config should be:

Code:
#chip 18f4520, 4
#config OSC=INTIO2

#### jigsawkiller

##### New Member
Try changing the for loops to something like 200 so it will wait in each position for 4 seconds. That is, FOR Cnt = 1 to 200.

Edit, also make sure that the compiler knows the speed of your chip. The default speed for that chip is 1MHz. To make it 4MHz try adding OSCCON=0x62.

Mike.

Thanks for the response, but I tried what you said about changing the loop time and it still works the same way, only a bit slower. I also tried OSCCON = 0x62, still get same results.

The compiler already knows it's running at 4 MHz, hence
Code:
#chip 18F4520a, 4
Where the 4 is the clock frequency in MHz.

I'm using an external oscillator.

Last edited:

#### Pommie

##### Well-Known Member
Whoops, missed the actual delay values. Shouldn't he just do a pulse out of 1000 - 2000uS? Wont 100 10uS pulseout commands send 100 pulses?

I've never used GCB so the above may be completely wrong.

Mike.

#### nickelflippr

##### Member
Whoops, missed the actual delay values. Shouldn't he just do a pulse out of 1000 - 2000uS? Wont 100 10uS pulseout commands send 100 pulses?

I've never used GCB so the above may be completely wrong.

Mike.
The Pulseout command would be a one-shot turn this pin on for xxxx wait/delay cycles, then turn off. So 100 10us is the same as 1000us without the overhead of word sized assembler instructions (presuming that a variable will be used on down the road). Also a fast clock (i.e. 20 Mhz) will be needed if variables will be used in conjunction with us wait/delay variables.

#### Pommie

##### Well-Known Member
I can't see how 100 10uS pulseouts would not be a 1mS long 100kHz pulse.

Mike.

#### nickelflippr

##### Member
Sorry that I did not explain that well enough, check the GCBasic help file here on PulseOut. To put it another way, there is only one pulse, per PulseOut command. To do Qty (100) 10us PulseOuts then:

Code:
For count = 1 to 100
PulseOut PortB.0, 1 10us
next

These statements are all equivalent in GCBasic:
Code:
PulseOut PortB.0, 1 ms
PulseOut PortB.0, 100 10us
PulseOut PortB.0, 1000 us

#### Russ Hensel

##### New Member
Quote: "Need to be shooting for 1ms, 1.5ms, 2ms."

I think that is the problem, program does not, to me, seem to be using these timings.

Status
Not open for further replies.

Replies
6
Views
4K
Replies
3
Views
969
Replies
9
Views
3K
Replies
11
Views
12K
Replies
25
Views
6K