Documente Academic
Documente Profesional
Documente Cultură
Introduction
The first PWM mode we’ll look at is called Fast PWM. Let’s consider the additional
capabilities of Fast PWM Mode one step at a time. Fast PWM Mode works very much
like CTC mode, but adds another register for controlling the pulses on the OC0n pins..
Now the value placed in the OCR0A register will be continuously compared to COUNT
(the timer/counter value) as it increments. When the COUNT is equal to the value in
OCR0A, the output on OC0A will go high. As soon as the count reaches TOP, OC0A will
be cleared. So we can again generate a square wave, but now we can easily vary the duty
cycle. The duty cycle is simply the ratio of the time the output is high divided by the total
length of one cycle.
#include <avr/io.h>
int main(void)
{
DDRD |= (1 << DDD6);
// PD6 is now an output
OCR0A = 128;
// set PWM for 50% duty cycle
while (1);
{
// we have a working Fast PWM
}
}