If i have micro with no PWM module , how to generate output of duty cycle (20%) on pin 1 , and output with duty cycle(80) on pin2 using one counter and interrupt not polling.
2 Answers
Configure a timer to interrupt periodically. The timer period should be 1/100th (or 1/10th or 1/5th) of the desired signal period. Increment a counter in the timer interrupt handler. Reset the counter value to zero when the counter reaches the maximum value of 100 (or 10 or 5). Toggle the appropriate pins when the counter value reaches 20% or 80% of the max counter value. And toggle the pins when the counter value resets to 0.

- 6,643
- 1
- 17
- 30
-
3To reduce interrupt load you should use the lowest possible interrupt rate. The requested duty cycles of 20% and 80% suggest a interrupt rate of f*5. – user5329483 Mar 12 '18 at 17:42
If you've got an AVR, and you can pick the pins, you can get this behavior with no interrupts. You just need to configure one of the timers to use the count-up and clear timer on compare mode. You get to set which register is the clear timer on compare, and what the Waveform generation mode is for the COMPA and COMPB registers. I recommend using the ICR as the timebase (clear on compare register) and then using the two COMP registers to generate the waveforms, by setting the WGM bits to clear the output on reset and set the output on compare match. You can then set the COMPn registers to whatever fraction of the ICR register to achieve whatever duty cycle you want, on the OCxn pins.

- 175
- 7