CS-241-MNP S.Y.B.Sc. (Comp. Sci.
Experiment – 3
Title: Study of blinking LED, generate a delay using AVR Timer
Aim: To study of blinking LED, generate a delay using AVR Timer.
Circuit Diagram:
Figure: LED interfacing to AVR
Figure: Timer 0 Block Diagram
@NewMGTechSolution
Contact: 9960277201 / 9822590931
CS-241-MNP S.Y.B.Sc. (Comp. Sci.)
Theory:
Every microcontroller have Timer / counter modules in it, that generally used to generate time
delays, waveforms, or to count events. Also, the timer is used for PWM generation, capturing events,
etc. In AVR ATmega16, there are three timers - Timer0: 8-bit timer, Timer1: 16-bit timer & Timer2:
8-bit timer. In this practical, we use Timer 0 register to generate a delay. For that First, we need to
understand the basic registers of the Timer0.
▪ TCNT0: Timer / Counter Register 0 - It is an 8-bit register. It counts up with each pulse.
▪ TCCR0: Timer / Counter Control register 0 - This is an 8-bit register used for the operation mode
and the clock source selection.
@NewMGTechSolution
Contact: 9960277201 / 9822590931
CS-241-MNP S.Y.B.Sc. (Comp. Sci.)
▪ TIFR: Timer Counter Interrupt Flag register - This is an 8-bit register used for the operation mode
and the clock source selection.
Steps to Program Delay using Timer0:
1. Load the TCNT0 register with the initial value (let’s take 0x25).
2. For normal mode and the pre-scaler option of the clock, set the value in the TCCR0 register. As
soon as the clock Prescaler value gets selected, the timer/counter starts to count, and each clock
tick causes the value of the timer/counter to increment by 1.
3. Timer keeps counting up, so keep monitoring for timer overflow i.e. TOV0 (Timer0 Overflow)
flag to see if it is raised.
4. Stop the timer by putting 0 in the TCCR0 i.e. the clock source will get disconnected and the
timer/counter will get stopped.
5. Clear the TOV0 flag. Note that we have to write 1 to the TOV0 bit to clear the flag.
Initial Value Calculation:
Fosc = 4MHz
Pre-scalar value selected = 1024
So the timer clock source frequency = 4MHz / 1024 = 3906.25
Time Period of 1 cycle = 256 uS.
e.g. Suppose we want generate delay of 20 mS
Therefore, for a delay of 20 ms, the number of pulses required = 20 mS / 256uS = 78
So, we need 78 timer cycles to generate a delay of 20 ms.
Hence, initial value to be loaded in to TCNT0 register = 256 – 78 = 178 (in decimal) = 0xB2 (in Hex).
@NewMGTechSolution
Contact: 9960277201 / 9822590931
CS-241-MNP S.Y.B.Sc. (Comp. Sci.)
Procedure:
▪ Start AVR Studio software on PC and write a program.
▪ Connect the USB cable between USB ASP programmer on the Target board and PC.
▪ Turn ON the power supply of the target board and then load the .hex file into the AVR using
“ProgISP” software.
▪ Turn OFF the power supply while making circuit connections on the board. Make necessary
connections as shown in the circuit diagram.
▪ Switch ON the power supply of the target board and observe the output.
Program:
#include <avr/io.h>
void T0delay();
int main(void)
{
DDRA = 0xFF;
while(1)
{
PORTA=0x00;
T0delay();
PORTA=0xFF;
T0delay();
}
}
void T0delay()
{
TCCR0 = (1<<CS02) | (1<<CS00); /* Timer0, normal mode, /1024 prescalar */
TCNT0 = 0xB2; /* Load TCNT0, count for 20ms */
while((TIFR&0x01)==0); /* Wait for TOV0 to roll over */
TCCR0 = 0;
TIFR = 0x1; /* Clear TOV0 flag */
}
Result: Successfully studied the use of AVR Timer to generate delay and make LED on/off for
different delay.
Teacher’s Sign
-------------------------------------------------*********---------------------------------------------------------
@NewMGTechSolution
Contact: 9960277201 / 9822590931