PWM stands for pulse wide modulation. In AVR family there are three types of waveform generating modes
- Fast PWM
- Phase Correct PWM
- Frequency and Phase Correct PWM

The relationship between output voltage and duty cycle is

A PWM signal is nothing but a square wave in which the tine period (Ton+Toff) remain the same but the duty cycle changes. Basically all PWM mode operate with two modes
- Non inverting mode(set at BOTTOM, clear at compare match)
- Inverting mode (set at compare match, clear at BOTTOM)

In this mode Timer generate a saw tooth wave and compare it with OCRx(Output compare register) and we get a PWM output.


In this mode Timer generates a triangular waveform and compare it with OCRx(Output compare register), thus we get a PWM signal as shown in figure-


In this mode Timer generates a variable TOP sawtooth wave and compare it with OCRx(Output compare register), thus we get a PWM output at OCn pin. In this PWM mode the frequency continuously changing as shown in picture-

The basic difference between PWM mode and CTC mode is that in PWM mode the OCRx register is used to store the duty cycle. The procedure is just multiply the duty cycle with the TOP/MAXimum value and store in the OCRx register.
Thus for 8bit PWM mode 45% duty cycle would be obtain as follow-
OCRx=(2^8-1)*45%
=114.75≈115
Let make a program to control the brightness of an LED using Timer0. The Timer should operate in Fast PWM using 256 prescalling non inverting mode.

TCCR0|=(1<<WGM01)|(1<<WGM00); /*** Fast PWM with TOP value 255 *********/
TCCR0|=(1<<COM01); //NON Inverting mode
/********** In non inverting mode if we increase the OCRx register value PWM output voltage will increase ********/
TCCR0|=(1<<CS02); // Prescaler = 256
DDRB|=(1<<DDB3); //Output OC0 pin of ATmega8
Since in non inverting PWM mode output voltage increases by increasing the OCRx register value. So the brightness and darkness condition would be-

for(uint8_t i=0;i<255;i++)
{
OCR2=i;_delay_ms(50);
}

for(uint8_t i=255;i>0;i--)
{
OCR2=i;_delay_ms(50);
}
Download the pwm.c and pwm.h file and the main program as follow-
#include<avr/io.h>
#include<util/delay.h>
#include"pwm.h"
int main(void)
{
init_PWM();
while(1)
{
brightness();
_delay_ms(200);
darkness();
}
return 0;
}






Visit Today : 33
Total Visit : 28448