Académique Documents
Professionnel Documents
Culture Documents
Timer1_PWM !!!
WHAT IS PWM?
In this modulation technique, the width of the modulated signal is varied in accordance with input.
by
increasing
and
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%.
% DUTY CYCLE=
100
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!!!