Vous êtes sur la page 1sur 13

ATMega8 uC

Timer1_PWM !!!

WHAT IS PWM?

In this modulation technique, the width of the modulated signal is varied in accordance with input.

Why it is used in Microcontroller?


Pulse Width Modulation (PWM) is a method of getting intermediate voltage between 0V and 5V for e.g. 3.6V

Intermediate voltage is achieved decreasing the width of digital pulse

by

increasing

and

PWM is defined by Duty Cycle of the pulse

DUTY CYCLE
It is the ratio of active time(ON time) to the total time period of the clock cycle

total time period= ON time(TON) + OFF time(TOFF) For e.g.- If an CPU Clock pulse is ON or HIGH for half time period of the pulse so duty cycle is 50%.

FORMULA OF DUTY CYCLE

% DUTY CYCLE=

100

Where Total Time Period= TON + TOFF

PWM GENERATION in AVR


AVR contains separate hardware
PWM can be generated by Timer1 in ATmega8 microcontroller TCNTx, OCR1 A/B, ICR1 registers generates PWM

FAST PWM GENERATION USING TIMER1


In this mode 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) an output pin associated with output compare register pulled HIGH When TCNT1 reset to Bottom that pin pulled LOW

FAST PWM MODE


For this mode, we have to set TCNT1 (Timer Counter Register) OCR1A (Output Compare Register) ICR1 (Input Compare Register) TCCR1B & TCCR1A (Timer/Counter Control Register) TIMSK (Timer Interrupt Mask Register)

Program to make PWM of 20ms time period using Fast PWM mode of TIMER1
#include<avr/io.h> #include<avr/interrupt.h> #include<util/delay.h> int count =1; void main()
{

DDRB=0xFF; //DIRECTION OF PORT B AS OUTPUT TCCR1B=0b00011001; //Bit 0-2 for CLOCK SELECT(NO PRESCALING) bit 3-4 for fast PWM //WAVEFORM GENERATION MODE
TCNT1=0; //for initializing timer1 count TCCR1A=0b10100010; //bit 0-1 for fast PWMWAVEFORM GENERATION MODE bit 4-5 for //COM1B1:0, bit 6-7 for COM1A1:0 (10 for both) ICR1=20000; // setting upper limit for count register (Input compare register)

TIMSK=0b00000000; //Timer Interrupt mask set register sei(); //Set Interrupt Enable OCR1A=0x0000; //initiating output compare register 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 } else { OCR1A=(100*count); _delay_ms(10); count++; } } // while close } //main close

THANK YOU!!!

Vous aimerez peut-être aussi