AVR Tutorials Timer/Counter

In AVR ATMega32 there are 3 timers ,in that two 8bit timers(Timer0 & Timer 2)and one 16 bit timer(Timer1).By learning the timers of ATMega 32 we can use timers in any AVR controllers. In AVR the Timer/Counter registers are      TCNTn TCCRn OCRn TIMSK TIFR Timer counter register n Timer Counter Control Register Output Compare Register Timer Counter Interrupt Mask Register 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/counter register. For each tick in the oscillator the counter register counts one, so the register holds the number of ticks, since we know the oscillator speed we can calculate the total time. If we want to count an external event, the source of the event will be connect the clock pin of the timer/counter register
E-Dazzling Technologies Page 1

so from 0X9C the hundredth count will overflow Timer/Counter0 Block Diagram E-Dazzling Technologies Page 2 . So if we want to generate 100 micro second of time delay in a microcontroller who’s operating at 1MHZ we have to load the counter register(TCNTN)with 0X9C(156).AVR Tutorials In this section we are going to learn how to generate time delay by using timer/counter register In 8bit timer the upper most value is 0XFF.the counter register increase from its content up to 0XFF linearly and the rolls back to zero cause the overflow. Because for each tick it takes 1 micro seconds.

Output Compare Register (OCR0) The content of the register is continuously compared with TCNT0 content.AVR Tutorials Register Descriptions: Timer counter register 0(TCNT0) This register is used to hold counting values .It is a 8bit register and it can hold up to 255(0XFF). WGM0 1:0: Timer 0 Mode selecting bit COM0 1:0: This bit controls the OC0 pin operation during CTC. Timer Counter Control Register (TCCR0) FOC0: This bit is used while wave generation when written one to this bit when compare match is occurred. Page 3 E-Dazzling Technologies . PWM phase correct and Fast PWM modes. if both are equal it can be used to generate compare match interrupt occurs or waveform generation.

AVR Tutorials Compare Match Mode Fast PWM Mode PWM Phase Correct Mode CS0 2:0: These three bits are used to select the clock source E-Dazzling Technologies Page 4 .

Timer/Counter Interrupt Mask Register (TIMSK) TOIE0: OCIE0: By writing one to this bit enables Timer/Counter Overflow0 interrupt if global interrupt is enabled. when TCNT0 is incremented from 0XFF it becomes 0X100) This bit is set when the Content of TCNT0 and OCR0 are equal. By writing one to this bit enables Timer/Counter CompareMatch0 interrupt if global interrupt is enabled. E-Dazzling Technologies Page 5 .AVR Tutorials Timer/Counter Interrupt Flag Register (TIFR) TOV0: OCF0: This bits is set when overflow occurs in TCNT0 (i.e.

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.e. If the interrupt happens for 4 times it’s nearly equal to 1 sec. E-Dazzling Technologies Page 6 .      Enable global interrupt (i.26112sec = 261msec time taken for each interrupt to occur. the function must begin with a keyword interrupt followed by the vector address with in the square bracket followed by the name of function with prototype. Write the operation what we want to do when interrupt occurs in the ISR function For writing an Interrupt Service Routine function. # asm (“sei”)).AVR Tutorials Procedure for Generating Time delay Formula for calculation time for interrupt to occur Time = (1 / Fosc) * TCNT0 value * Prescalar. Interrupt [Vector address] Return type fn_name (Arguments) { } Vector address can be found in the microcontroller pdf or in the compilers header file Program Write a program to toggle the pin PB0 for every one second approximately. Time = (1/1000000)*255*1024 =.

AVR Tutorials Code E-Dazzling Technologies Page 7 .

AVR Tutorials Circuit: E-Dazzling Technologies Page 8 .