Pulse an output pin continuously to drive a radio-control style servo. Note that on 14M2, 18M2, 20M2 and X2 parts the servo commands only function on portB (B.0 to B.7)
Servos, as commonly found in radio control toys, are a very accurate motor/ gearbox assembly that can be repeatedly moved to the same position due to their internal position sensor. Generally servos require a pulse of 0.75 to 2.25ms every 20ms, and this pulse must be constantly repeated every 20ms. Once the pulse is lost the servo will lose its position. The servo command starts a pin pulsing high for length of time pulse (x0.01 ms) every 20ms.
This command is different to most other BASIC commands in that the pulsing mode continues until another servo, high or low command is executed. High and low commands stop the pulsing immediately. Servo commands adjust the pulse length to the new pulse value, hence moving the servo. Servo cannot be used at the same time as timer or pwmout/hpwm as they share a common internal timer resource. The servo command initialises the pin for servo operation and starts the timer.
Once a pin has been initialised, it is recommended to use the servopos command to adjust position. This prevents resetting of the timer, which could cause 'jitter' Do not generally use a pulse value less than 75 or greater than 225, as this may cause the servo to malfunction. Due to tolerances in servo manufacture all values are approximate and will require fine-tuning by experimentation (e.g. 60 to 200). Always use a separate 6V (e.g 4x AA cells) power supply for the servo, as they generate a lot of electrical noise.
Note that the overhead processing time required for processing the servo commands every 20ms causes the other commands to be slightly extended i.e. a pause command will take slightly longer than expected. The servo pulses are also temporarily disabled during timing sensitive commands like serin, serout, sertxd, debug etc.
On X2 parts servo will only function at 8MHz or 32MHz. On M2 and X1 parts servo will only function at 4MHz or 16MHz. On all other parts servo will only function at 4MHz.
On X2 parts it is also possible to change the 20ms delay between pulses. This is achieved via the 'preload' value, which is the number to preload into timer 1 before it starts counting. On X2 parts timer 1 increments every 0.5us, so for a delay of 20ms (20,000us) we need 40,000 increments.
Therefore the preload value is 65,536 - 40,000 = 25,536. As an example, for digital servos, you may wish to increase the pulse frequency to every 10ms (note the delay must be longer than the total of all pulses to all servos, so 10ms is only suitable for up to 4 servos (maximum delay for 4 servos is when pulse length is 2.25ms, so 4x2.25 = 9ms).
10ms = 10,000 us = 20,000 steps 65536-20,000 = 45536 So the command is servo ,1,75
Effect of increased clock speed:
The servo command will function correctly at 4MHz on all parts (except X2 parts, which only function at 8 or 32MHz). 16MHz is also additionally supported on M2 and X1 parts. No other frequency will work correctly.