PDA

View Full Version : contorlling servo motor



shirleyz
- 28th May 2008, 04:36
hi

i'm using PIC16F877A for my line follower. there will be a robotic arm too... i'm decided to use picbasic as it is easier to control the servo.

for the line follower, i'm planning to use 20MHz osc. so, izzit possible for my robotic arm.
this is info that i know:

PIC16F877A microcontroller is used and the microcontroller is operated
with a 4 MHz crystal. The pulse required to operate a servomotor can very easily be obtained using the PULSOUT
statement of the PicBasic and PicBasic Pro compilers. When a 4 MHz crystal is used, the time
interval of PULSOUT is in units of 10s. For example, the following PicBasic statement generates
a pulse with a width of 1.3 ms from bit 0 of PortB (1.3 ms = 1300us and 1300/10 = 130):
PULSOUT 0, 130

With an 20Mhz OSC, the resolution of PULSOUT is??.

HenrikOlsson
- 28th May 2008, 08:21
Hi,
IIRC the resolution of the pulsout command is 10uS no matter what osciallator frequency you are using -as long as you let PBP know what frequency you are using.

So if you tell PBP that you are using a 10Mhz clock but in reallity it's 20MHz you'll get 6.5ms instead of 13 with Pulsout 0,130. Just remeber that all timing sensetive commands, like Pause, Serin/out, Pulsin etc also is affacted the same way.

/Henrik.

Acetronics2
- 28th May 2008, 08:44
Hi, Henrik

No,NO ...PULSOUT is one of the XTAL freq dependant commands ... There's NO DEFINE to get rid of that !!!

@ 20Mhz, the result will simply be 20/4 times that @ 4Mhz ...

Alain

HenrikOlsson
- 28th May 2008, 09:55
Alain,
Yep, you're right! I didn't remeber correctly..... :-(

To quote the docs:


If a 4MHz oscillator is used, the Period of the generated pulse will be in 10us increments. If a 20MHz oscillator is used, Period will have a 2us resolution. Defining an OSC value has no effect on PULSOUT. The resolution always changes with the actual oscillator speed.

Sorry for the confusion!

/Henrik.

shirleyz
- 28th May 2008, 12:04
so do u all mean this:

When a 20MHz crystal is used, the time
interval of PULSOUT is in units of 2us. For example, the following PicBasic statement generates a pulse with a width of 1.3 ms from bit 0 of PortB (1.3 ms = 1300us and 1300/2 = 650):
PULSOUT 0, 650

izzit like that?? can u all give sample codes, so i can see clearly..
thanks

Acetronics2
- 28th May 2008, 12:35
Hi, Shirleyzzzzzzzz ...

that's it !

Find here an application ...




'************************************************* ***************
'* Name : Glow Driver with Fail Safe - 1 *
'* Author : Ironsides *
'* Notice : Copyright (c) 2004 C *
'* : All Rights Reserved *
'* Date : 24 Feb 2004 *
'* Version : 1.0 *
'* Notes : Turns on glow driver to glow plug at low throttle *
'* If sigal is lost for 5 secs, engine is killed and *
'* glow power turned off *
'* ; An N Channel MOSFET is needed to carry the current*
'* ; See the circuit diagram for details *
'* Caution : Should have a safety cut off switch in the pits *
'* : For PIC 12F675 *
'************************************************* ***************

;The 12F675 has both analog converters and analog comparators.
;You must disable both to use the I/O pins for digital functions.
;In addition, the register that controls the analog converters
;has been named ANSEL, and it has a different format that the ADCON1
;register found on most PICmicro MCUs
PCON.3 = 1
ANSEL = 0 ;needed as as above
CMCON = 7
ASM
errorlevel -302 ; Suppress message 302
bcf STATUS,RP0 ; Bank 0
clrf GPIO ; Init GPIO
movlw 07h ; Set GP<2:0> to
movwf CMCON ; digital IO
bsf STATUS,RP0 ; Bank 1
clrf ANSEL ; Digital IO
movlw 0Ch ; Set GP<3:2> as inputs
movwf TRISIO ; and set up GP<5:4,1:0> as outputs
endasm

DEFINE OSC4 ;sets up 4 meg internal oscillator
THROTTLE VAR GPIO.1 ;Outputs to throttle servo - Pin 6
GLOW VAR GPIO.2 ;Output to MOSFET driving glow driver - Pin 5
RADIO VAR GPIO.3 ;Input from receiver - Pin 4

TEST VAR WORD ; Used to test for loss of signal
P VAR WORD ; This stores the PULSIN value
N VAR WORD ; Loop counter
;************************************************* ********************
TEST = 0
RX: ; This is where we wait for a signal from the Tx
PULSIN RADIO,1,P ; With 4 meg oscillator, PULSIN measures at 10 millisecs
IF (P < 50) and (test = 0) then GOsub LOS ; Loss of signal
PULSIN RADIO,1,P ; Check to see if signal re-established
if (P < 50) and (test = 1) then goto kill_glow
; There is loss of signal - kill the engine
IF (P > 100) and (P < 130) THEN GOSUB SW_ON ; Turn on glow
IF (P > 130) THEN GOSUB SW_OFF ; Turn off glow

goto RX
;************************************************* *******************

SW_ON:
HIGH GLOW ; This tunrs on the glow signal on Pin 5
PULSOUT THROTTLE, p ; send the normal signal to the throttle servo
RETURN

SW_OFF:
LOW GLOW ; This turns off the glow signal on Pin 5
PULSOUT THROTTLE, p ; send the normal signal to the throttle servo
RETURN

LOS:
TEST = 1 ; Set for loss of signal
For N = 1 to 5
Pause 1000 ; one second - 5 secs total
NEXT n
return

KILL_GLOW:
low glow ; Turn off the glow plug

KILL_ENGINE:
PULSOUT throttle, 101 ; Kill engine - adjust this value to get the
; carb closed to prevent dirt from entering
pause 50 ; Needs a pause to stop servo hum
goto KILL_ENGINE ; Keep in loop to stop spurious signals

;************************************************* *******************

END ;Terminate the program




Alain

shirleyz
- 29th May 2008, 12:11
so the code that i write, izzit correct....i mean pulseout code for 20MHz???

Acetronics2
- 29th May 2008, 12:38
Hi, Shirleyzzzzzzzz ...

that's it !

Alain

shirleyz
- 30th May 2008, 03:28
thank you so much....

shirleyz
- 31st May 2008, 11:26
how to make the motor to rotates continously?...when i test this code, the servo motor just move one time only....

define osc 20

trisa.0=0 ''LEFT MOTOR''
trisa.1=0 ''RIGHT MOTOR''

LM var porta.0
RM var porta.1

Forward:
PULSOUT 0, 170 ‘ Left wheel anti-clockwise
PULSOUT 1, 130 ‘ Right wheel clockwise
PAUSE 20 ‘ Wait 20 ms
GOTO Forward ‘ Repeat

mister_e
- 31st May 2008, 23:53
PortA Doesn't Work
http://www.picbasic.co.uk/forum/showthread.php?t=561

Presetting Configuration Fuses (PIC Defines) into your Program
http://www.picbasic.co.uk/forum/showthread.php?t=543