garmcqui said:
I'm new to PIC programming, but I want to create a very simple software PWM which will controll 2 servos according to the data received via RS232. I am using the PIC16F628.
Obviously the first byte can be discarded once received. The second byte determines which pin of Port B to use. The third byte determines the position of the servo addressed in byte 2.
The value of byte 3 can range from 0 to 255 (with only odd numbers being used to simplify it) so that 0 = 1ms pulse, 255 = 2ms pulse. I
Is a lookup table with 128 lines needed or is there a simpler way of doing it?
For a start make it simpler!, there's no need to have full 8 bit resolution for the servo pulses, the servo itself doesn't give that degree of precision, so it's just a waste.
However, for now I'll assume the MAXIMUM value is 250 (rather than 255), as it makes life much easier. Simply use the received value (from 0 to 250) as the counter in a loop - the inner part of the loop should be a simple delay of 4uS (you should also account for the loop time in the 4uS). You need two actual loops, the first (which is always 250) delays the fixed 1mS, the second (which uses the variable) delays between 0mS and 1mS - giving the required 1mS-2mS pulse width.
So simply set the output pin HIGH, called the fixed delay, call the variable delay, then set the pin LOW again.
You can then repeat the procedure for the second servo pulse.
Bear in mind the pulse repetition frequency of servo pulses is 20mS, however this isn't at all critical, only the width of the pulses is.
Depending on the actual RS232 control data you may have to use interrupts to receive the data, triggered by the hardware USART. It really depends on what is generating the data - if you send updated values every 20mS, that would automatically give you the required repetition timing, and you could use software polling to receive the RS232.