PDA

View Full Version : Servo control with 12F629



achilles03
- 23rd June 2005, 16:24
I'm having some trouble with a continuous rotation servo and a 12F629. I've managed to have a BS2 control the servo at different speeds in both directions, but everytime I try to use the 12F629 to control the servo, it only operates in one direction (counter-clockwise) at the same speed (fast). A typical program I'd use with the BS2 was:

loop
pulsout 0, 750
pause 20
goto loop

The code I used for the 12F629 was:

loop
pulsout 0, 150
pause 20
goto loop

I tried varying the 150 to 100, 180, 750, etc... with the same direction and speed everytime. I'm using the 12F629's internal RC oscillator (4Mhz), which is why I use the 150 instead of the 750 (for the BS2). I even included a "Define OSC 4" to make sure the pulsout was correct.

Does anyone have any suggestions or advice as to what I'm doing wrong?

Thanks in advance!
Dave

P.S. Also, everytime I touch any of the leads with my finger (ground, +5V, etc), the servo stops. Is this because my finger is interfering with the capacitance of the internal RC oscillator, effectively stopping the 12f629?

Bruce
- 23rd June 2005, 20:49
Turn off analog comparators so your pins can be used for digital I/O.

CMCON = 7

PULSOUT toggles the pin twice, so the initial state of the pin determines
the logic of the pulse.

LOW 0 ' We want a high-going pulse or low-high-low with pulsout.



@ DEVICE MCLR_OFF,INTRC_OSC,WDT_OFF,PROTECT_OFF

Define OSCCAL_1K 1 ' Load factory value into OSCCAL
CMCON = 7 ' Turn analog comparators off

X VAR BYTE
LOW 0 ' Setup GPIO,0 to output a high-going pulse with pulsout

Main:
FOR X = 1 to 50
pulsout 0, 100
pause 15
NEXT X

pause 1000

FOR X = 1 to 50
pulsout 0, 200
pause 15
NEXT X

pause 1000

goto Main

end
You may want to tweak the pulsout periods, but that should get you started.

Make sure you use a separate power supply for your servo. Using the same PS as the PIC will cause problems.

achilles03
- 23rd June 2005, 23:04
Bruce,
Thanks for the help! The CMCON = 7 I think is what did the trick (i.e. it works). However, it works only intermittently. It seems that the microchip is only executing the full program when I have my finger on the +5V lead (and then only sometimes). I'm wondering what's causing this? I'm using the INTRC (IN) configuration option. I am also using the same PS for the servo and the chip (wall wart to a 1A, 5V regulator). Is the cause of this most likely due to sharing of the power source, a touchy internal oscillator, or something else?

Thanks again for the help.

Dave


Turn off analog comparators so your pins can be used for digital I/O.

CMCON = 7

PULSOUT toggles the pin twice, so the initial state of the pin determines
the logic of the pulse.

LOW 0 ' We want a high-going pulse or low-high-low with pulsout.



@ DEVICE MCLR_OFF,INTRC_OSC,WDT_OFF,PROTECT_OFF

Define OSCCAL_1K 1 ' Load factory value into OSCCAL
CMCON = 7 ' Turn analog comparators off

X VAR BYTE
LOW 0 ' Setup GPIO,0 to output a high-going pulse with pulsout

Main:
FOR X = 1 to 50
pulsout 0, 100
pause 15
NEXT X

pause 1000

FOR X = 1 to 50
pulsout 0, 200
pause 15
NEXT X

pause 1000

goto Main

end
You may want to tweak the pulsout periods, but that should get you started.

Make sure you use a separate power supply for your servo. Using the same PS as the PIC will cause problems.

Bruce
- 23rd June 2005, 23:34
You don't want to run a servo from the same power supply your PIC runs on unless it's a bench type power supply or one that can handle a ton of current without dropping its output.

Use a separate supply or batteries for the servo motor. Should take care of the problem.

You might also want to strap a 0.1uF cap across the PIC Vcc & gnd pins too. Helps filter spikes from the supply, and it can keep the PIC from resetting at odd times.