Pulse Width Modulation In Microcontroller

post footer border
In this modulation technique, the width of the modulated signal is varies in accordance with input.

But the main question arrives, WHY IT IS USED IN MICROCONTROLLER ?

Pulse width modulation is a method of getting voltage between 0V and 5V for e.g 3.6V.
which means we get desirable input voltage between 0-5V using PWM.
Intermediate voltage is achieved by increasing and decreasing the with of digital pulse.
PWM is defined by " Duty Cycle" of the pulse.

AVR contains separate hardware for PWM generation.

PWM can be generated by Timer1 in ATmega8 microcontroller or we can say, TCNTx, OCR1 A/B, ICR1 registers generates PWM.

For fast PWM generation using TIMER1, TCNTx register count its value from bottom to top. reset to bottom as it reaches top to count again. while counting up when TCNT1 matched with compare register (OCRxx) and output pin associated with output compare register pulled high. when TCNT1 reset to bottom that pin pulled low.
TCNT1 ( timer counter register )
OCR1A (output compare register)
ICR1 (Input compare register)
TCCR1B & TCCR1A (timer/counter control register)
TIMSK (Timer interrupt mask register)

Example Code:

int count =1;

void main()
DDRB=0xff; // Direction of PORT B as output ( 8 LED Output )

TCNT1=0;   //for intializing timer1 count
ICR1=20000; // setting upper limit for count register
TIMSK=0b00000000; //timer interrupt mask set register
sei(); // set intrrupt enable
OCR1A=0x0000; // initiating output compare redister from 0

while (1)
if(OCR1A==20000) // checking if OCR1A==ICR1
OCR1A=0x0000; //resetting OCR1A to 0 value again 
count=1;      //resetting count value to 1 again


Recent Posts

AD Setting