|
Intro to Physical Computing Syllabus code, circuits, & construction
Programming the PIC using PicBasic Pro Serial in and out (using serin2 and serout2) |
Controlling a Servo using PicBasic Pro |
|||
| The principles of controlling a servo from a PIC asre the same as from a BX-24, Basic stamp or any other microcontroller. See the motor control notes for more on this.You need to give the servo a 1-2 millisecond pulse once very 18-20 milliseconds.
Using PicBasic Pro, the command needed is the PulsOut command. With a 4-MHz clock, the pulsOut can generate a minimum 10-microsecond pulse, so the values below are given with that in mind. With a 20-MHz clock, the PulsOut command can pulse as short as 2 microseconds, so you will need to adjust the minimum and maximum pulsewidths (multiplying by 5 should do the job): start: pulseWidth var byte ' set up constants with the minimum and maximum pulsewidths minPulse CON 50 maxPulse CON 250 ' set up a constant with the time between pulses: refreshPeriod CON 20 ' set an initial pulsewidth: pulseWidth = minPulse main: 'take the output pin low so we can pulse it high Low PORTC.3 ' pulse the pin PulsOut PORTC.3, pulseWidth ' pause for as long as needed: Pause refreshPeriod ' change the angle for the next time around: IF pulseWidth > maxPulse Then pulseWidth = minPulse Else pulseWidth = pulseWidth + 1 Endif GoTo main |
||||