program is running
.
.
.
.
interupt after 10 ms (for example)
set output pin high
reset timer to interupt in 5 ms
will now interupt after that 5 ms
set output pin low
reset time to interupt in 15 ms
will now interupt after that 15 ms
set output pin high again...
etc
etc
In it's interrupt put one counter which incremented each time interrupt is called....
now If we consider the limit of that counter is 100 then if you want to do 50% then when counter reaches to 50 you set the uc pin and reset it after 100 count...
If you set a timer running so that it rolls every 20 ms, then you can do this:-
Wait for the timer to roll.
Start pulse
Wait for the timer to get to the correct number to give the right length of pulse
End pulse
Do everything else
Loop to start
That will give you at least 18 ms every 20 ms to do everything else you need.
If you're using an 18F' device with two CCP modules then you might consider using one of them along with Timer 1 in "special event trigger" mode.
The following example provides 1 usec servo pulse width resolution but Timer 1 prescaler settings limit you to a clock of 4, 8, 16, or 32 MHz for this resolution. Basically on each compare match interrupt you toggle the Servo output pin and load the CCPRx module "compare" register pair with the servo pulse "on" time or "off" time for the next compare match interrupt. The "special event trigger" mode automatically resets the TMR1 register pair to zero after each compare match.
Code:
unsigned int servo = 1500; // 500..2499 usecs
void interrupt()
{ if (PIR1bits.CCP1IF == 1) // if "special event trigger"
{ PIR1bits.CCP1IF = 0; // clear CCP1 interrupt flag
LATB ^= 0b00000001; // toggle Servo pin on RB0
CCPR1 = 20000 - Servo; // setup "off" time match value
if(LATB & 1) // if "on" time
CCPR1 = Servo; // setup "on" time match value
}
}