Say i have 2 timers - Timer1(8-bit), Timer2(16-bit) on a controller running at 5mips, are the following correct or are they flawed?
-> for every instruction the timer is increased once - so every 200 nanoseconds each of the timer is increased by 1
-> so lets say i set Timer1 to start from 251 and Timer2 to start from 65534, and on interrupts from Timer1 set a pin to go high and on interrupt from Timer2 the same pin goes low, i will generate an approx 80% duty cycle.(excluding errors of both interrupts happening at the same time)
IF the clock runs at 5Mz AND the timers use the processor clock with no extra prescaling then the time interval is 200ns. This is not true for all microprocessors running at 5mips.
Using two different timers to generate a wave like that will not work. I assume you will use autoreload values of 251 and 65536 for the two timers. For a nice periodic signal the two timers should have the same period. Then the phase relation, difference in reload time, determines the duty cycle. Thirdly the interrupt latency is usually some 10 to 20 clock cycles even for very simple interrupt handling routines.
So the conclusion is that you should use one timer to generate a output signal with a specified duty cycle, and for fast signals you need some hardware pwm mode, output compare register.
I want to create a function to be able to generate pwm of variable duty cycles on all digital pins. this is what i thought - i make the 16bit timer over flow at a specific duration within which the 8bit timer will overflow 100 times.
The 16bit timer isr sets all the required pins to high and resets a counter to 0. The 8bit timer's isr will be enabled & initial value set from the 16bit timer.
A counter is increased every time the 8bit timer overflows and based on the counter value at any given time pins requiring a duty cycle of that percentage are switched off.
and when the counter reaches the end, i disable the 8bit timer from within the isr.
Now, i think this will enable setting pwm outputs on multiple pins with different duty cycles.
I do understand that the output might not be very precise but then, i haven't thought of how to improvise that.