Upload
stephanie-flores
View
35
Download
3
Embed Size (px)
DESCRIPTION
timer
Citation preview
AVR Tutorials
E-Dazzling Technologies Page 1
Timer/CounterIn AVR ATMega32 there are 3 timers ,in that two 8bit timers(Timer0 & Timer 2)and one16 bit timer(Timer1).By learning the timers of ATMega 32 we can use timers in any AVRcontrollers.
In AVR the Timer/Counter registers are
TCNTn Timer counter register n TCCRn Timer Counter Control Register OCRn Output Compare Register TIMSK Timer Counter Interrupt Mask Register TIFR Timer Counter Interrupt Flag Register
The AVR Timer/Counter register are used for
Generating time delays Counting the operation Pulse Width modulation Waveform Generation
If we want to generate time delay we connect oscillator to the timer/counterregister. For each tick in the oscillator the counter register counts one, so the registerholds the number of ticks, since we know the oscillator speed we can calculate the totaltime.
If we want to count an external event, the source of the event will be connect theclock pin of the timer/counter register
AVR Tutorials
E-Dazzling Technologies Page 2
In this section we are going to learn how to generate time delay by using timer/counterregister
In 8bit timer the upper most value is 0XFF,the counter register increase from itscontent up to 0XFF linearly and the rolls back to zero cause the overflow.
So if we want to generate 100 micro second of time delay in a microcontrollerwho’s operating at 1MHZ we have to load the counter register(TCNTN)with 0X9C(156).Because for each tick it takes 1 micro seconds, so from 0X9C the hundredth count willoverflow
Timer/Counter0 Block Diagram
AVR Tutorials
E-Dazzling Technologies Page 3
Register Descriptions:
Timer counter register 0(TCNT0)
This register is used to hold counting values .It is a 8bit register and it can holdup to 255(0XFF).
Output Compare Register (OCR0)
The content of the register is continuously compared with TCNT0 content, if bothare equal it can be used to generate compare match interrupt occurs or waveformgeneration.
Timer Counter Control Register (TCCR0)
FOC0: This bit is used while wave generation when written one to this bit whencompare match is occurred.
WGM0 1:0: Timer 0 Mode selecting bit
COM0 1:0: This bit controls the OC0 pin operation during CTC, PWM phase correctand Fast PWM modes.
AVR Tutorials
E-Dazzling Technologies Page 4
Compare Match Mode
Fast PWM Mode
PWM Phase Correct Mode
CS0 2:0: These three bits are used to select the clock source
AVR Tutorials
E-Dazzling Technologies Page 5
Timer/Counter Interrupt Flag Register (TIFR)
TOV0: This bits is set when overflow occurs in TCNT0 (i.e. when TCNT0 isincremented from 0XFF it becomes 0X100)
OCF0: This bit is set when the Content of TCNT0 and OCR0 are equal.
Timer/Counter Interrupt Mask Register (TIMSK)
TOIE0: By writing one to this bit enables Timer/Counter Overflow0 interrupt ifglobal interrupt is enabled.
OCIE0: By writing one to this bit enables Timer/Counter CompareMatch0 interruptif global interrupt is enabled.
AVR Tutorials
E-Dazzling Technologies Page 6
Procedure for Generating Time delay
Formula for calculation time for interrupt to occur
Time = (1 / Fosc) * TCNT0 value * Prescalar.
Enable global interrupt (i.e. # asm (“sei”)). Select normal mode and the Prescalar for the timer clock in TCCR0 Enable the Timer/Counter0 Overflow in TIMSK Register Load the value to TCNT0. Write the operation what we want to do when interrupt occurs in the
ISR function
For writing an Interrupt Service Routine function, the function must beginwith a keyword interrupt followed by the vector address with in the squarebracket followed by the name of function with prototype.
Interrupt [Vector address] Return type fn_name (Arguments){}
Vector address can be found in the microcontroller pdf or in thecompilers header file
Program
Write a program to toggle the pin PB0 for every one second approximately.
Time = (1/1000000)*255*1024
=.26112sec
= 261msec time taken for each interrupt to occur. If the interrupthappens for 4 times it’s nearly equal to 1 sec.
AVR Tutorials
E-Dazzling Technologies Page 7
Code
AVR Tutorials
E-Dazzling Technologies Page 8
Circuit: