Lab 07 MIP
Lab 07 MIP
EL-3002
LABORATORY MANUAL
Fall 2024
LAB 07
Implementation of Timer programming in Atmega32
LAB #7
Lab Title: Implementation of Timer / Counter programming in
Atmega32
Learning Introduction to:
Objectives Timers and Counter Programming to generate delays in Atmel Studio 6
and its simulation on Proteus 8
Software Atmel Studio 6
Required: Proteus 8.5
Super Pro Device Programmer
Introduction:
Instruction cycle time for AVR:
The duration of the machine cycle depends on the frequency of the oscillator connected to the
AVR. One machine cycle consists of one oscillator period. Therefore, to calculate the
machine cycle for the AVR, we take the inverse of the crystal frequency.
For crystals of 1, 8, 10, 16 MHz have machine cycles 1s, 125s, 100s and 62.5s respectively.
• Where n = 0,1,2
Timer 0:
Timer 1:
Timer 1 is 16 bit timer. Timer 1 is a 16-bit Timer and has many different capabilities. It has 16-
bit count registers split into two 8-bit registers. These are referred to as TCNT1L and
TCNT1H.
• It has two 8-bit control registers TCCR1A and TCCR1B. The TOV1 flag bit goes HIGH
when overflow occurs.
• Timer 1 also has the pre-scalar options of 1:1, 1:8, 1:64, 1:256, and 1:1024.
• There are two 16-bit OCR registers OCR1A and OCR1B. There are two separate flags for
each of the OCR registers which work independently.
• Whenever TCNT1 equals OCR1A, the OCF1A flag will be set on the next timer clock
• When TCNT1 equals OCR1B, the OCF1B flag will be set on the next clock
The two 8-bit control registers TCCR1A and TCCR1B allow Timer to be programmed in 16
different modes.
Modes are selected by programming bits WGM10, WGM11, WGM12, WGM13 found in
TCCR1A and TCCR1B registers.
Timer 2:
Timer 2 can be used as a real time counter to do so, a crystal of 32.768 kHz is connected to the
TOSCl and TOSC2 pins of AVR and the AS2 bit is set in ASSR (Asynchronous Status
Register).
TCCR2 Register
Finding count value to generate the desired delay:
Assuming that we know the amount of timer delay we need to find the value needed for the
TCNT0 register follow the following steps:
• Calculate the period of the timer clock using the following formula: Tclock = l /fTimer
• where fTimer is the frequency of the clock used for the timer
• For example, in no prescaler mode, fTimer = foscillator
• Divide the desired time delay by Tclock
• This says how many clocks are needed
• Perform 255 – n + 1, where n is the number of clocks calculated in step 2
• + 1 is one extra clock for roll-over
• Convert the result of Step 3 to hex, where xx is the initial hex value to be loaded into
the timer's register
• Set TCNT0 = xx
Calculating the time delay:
For no prescalar:
Lab Tasks:
1. Write a program for the AVR chip to toggle all the bits of PORTA, PORTB,
PORTC, and PORTD after every 0.25 second using internal oscillator. Assume all
the ports have value 0x0F initially. Generate delay by using timer 0 (normal mode).
Assume a crystal frequency of 8 MHz. Show your code working on Atmel Studio
and Proteus.
2. Generate a square wave of frequency 10 kHz using timer 2 (normal mode) on pin 6
of PORTB. Assume the crystal frequency to be 8MHz.
a. Generate the wave with 25 % duty cycle.
b. Generate a wave with same frequency but 75% duty cycle.
Show the working of both the codes on Atmel Studio and Proteus (show your wave on
oscilloscope).
a)
b)
3. Write a program for the AVR chip to generate running lights on PORTA. There
should be a delay of 0.5 sec after every step by using timer 0(CTC mode). Assume
a crystal frequency of 1 MHz. Show the working of both the codes on Atmel Studio
and Proteus.
Hint: Running lights can be implemented by outputting the following sequence on
any port of AVR:
10000000
01000000
00100000
.
.
0000001
0
00000001