PDA

View Full Version : Beginner + Servo



james
- 31st July 2007, 20:17
Hello,

Am in my first month of learning to program, and have completed most projects in the " PicBasic Projects - Book, by : Dogan Ibrahim ".

I am currently trying to control a servo motor(Tower Hobbies: STD TS-53) via a toggle switch. Switch one way to turn the servo Clockwise, opposite position for Counter Clockwise. Center position to center the servo

when power is applied my servo spins to the center and then vibrates and will not respond to switch. I am new and looking to learn, if I am using code improperly please inform me, I dont want to start off on the wrong foot.

using pic16F648, my code is as follows :
Code:
'PICBasic pro manual servo control program

symbol switchCW = porta.1
symbol switchCCW = porta.0
symbol servo = portb.0


Position var byte


start:

'look at switches on Port A
IF switchCW = 1 and switchCCW = 1 then goto loop

Position = 150
Pulsout servo, Position
pause 18

loop:
if SwitchCW = 0 then
Position = 130
Pulsout servo, Position
Pause 18
endif

If SwitchCCW = 0 then
Position = 170
pulsout servo, Position
Pause 18
endif

Pause 18
Goto loop
*****************
end of program....

Thank you, for any feedback

mackrackit
- 31st July 2007, 20:46
Hi,

Could not find the code you are using in my manual?
But, the chip you are using 16F648 does not show up at MICROCHIP, the A version does. If it is the "A" version there are a couple of comparators on PORTA.

You will need to turn the comparators off to make PORTA.0 and PORTA.1 regular I/Os.

CMCON = 7
Should be at the top of your code.

If this is the problem here are two post that will explain more.
http://www.picbasic.co.uk/forum/showthread.php?t=561
http://www.picbasic.co.uk/forum/showthread.php?t=562

POTRA has some different things than the other PORTS.

james
- 31st July 2007, 21:03
thank you for your quick response!

please excuse my typo earlier, PIC16F648A is the chip Im using.

and the "CMCON = 7" at the beginning of my code fixed the problem with my switches not responding.

My servo will now respond to the switch being toggled, but I would like for my servo to center itself when the switch is in it's center position. I have been reading different versions of controlling servo's via switches, and buttons and have tried my best to combine, into my idea of a logical procedure(not to say the others are not), just trying to write my first bit of code :)

again thank you for your response.

mackrackit
- 31st July 2007, 21:31
Try this-- UNTESTED


start:

'look at switches on Port A
IF (switchCW = 1) and (switchCCW = 1) then
GOTO HOME
ELSE
GOTO LOOP
ENDIF
GOTO start

HOME:
Position = 150
Pulsout servo, Position
pause 18
GOTO start

loop:
if SwitchCW = 0 then
Position = 130
Pulsout servo, Position
Pause 18
endif

If SwitchCCW = 0 then
Position = 170
pulsout servo, Position
Pause 18
endif

Pause 18
Goto loop