0% found this document useful (0 votes)
13 views

Lab 07 MIP

Uploaded by

i222162
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Lab 07 MIP

Uploaded by

i222162
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Microprocessor Interfacing & Programming

EL-3002
LABORATORY MANUAL
Fall 2024

LAB 07
Implementation of Timer programming in Atmega32

Asma and Aiman 2162, 1429 A

STUDENT NAME ROLL NO SEC

NATIONAL UNIVERSITY OF COMPUTER AND EMERGING SCIENCES (NUCES),


ISLAMABAD

Prepared by: Engr. Warda Shafiq Version: 1.01


Last Edited by: Engr. Aneela Sabir Date: 27 Sep, 2017
Verified by: Shehzad Ahmed
EL-3002 Microprocessor Interfacing & Programming
Lab

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.

Timer and Counter in AVR:


AVR32 has three timers/counters. Timer 0 and Timer 2 are 8-bit timers/counters while Timer
1 is 16-bit timer/counter.

Each timer has the following registers/ bits:

• Where n = 0,1,2

Department of Electrical Engineering


FAST NU, Islamabad 2|Page
EL-3002 Microprocessor Interfacing & Programming
Lab

• All registers mentioned above are 8-bit registers.


• TCTNn registers can be written and read. On reset all TCTNn’s registers contain zeros
and counts up on each pulse.
• When a timer overflows its TOVn flag will be set.
• The contents of the OCRn are compared with the corresponding TCTNn when the
contents are equal the 1-bit OCFn (Output Compare Match Flag) is set
• TIFR (Timer/ Counter Interrupt Flag Register) has TOVn and OCFn bits of each timer/
counter.

How to program any timer/ Counter?


• Load the TCNTn register with the initial count value
• Load the OCRn register if required.
• Load the value into the TCCRn register, indicating which mode is to be used and the
prescaler option
• When clock source is selected, the timer/counter starts to count, and each tick causes
the content of the timer/counter to increment by 1
• Keep monitoring the timer overflow flag (TOVn) / output compare match flag (OCFn)
to see if it is raised.
• Get out of the loop when the respective flag becomes high.
• Stop the timer by disconnecting the clock source, using the following instructions (for
timer 0)
• LDI R20,0x00
• OUT TCCR0, R20 ;timer stopped, mode=Normal • Clear the respective flag for the
next round
• Go back to Step 1 to load TCNTn again.

Timer 0:

• TCCR bit 3 and 6 define the waveform generation mode

• TCCR bit 2, 1 and 0 define the clock source used by Timer/Counter

Department of Electrical Engineering


FAST NU, Islamabad 3|Page
EL-3002 Microprocessor Interfacing & Programming
Lab

• TCCR bit 5 and 4 control OC0 pin

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.

Department of Electrical Engineering


FAST NU, Islamabad 4|Page
EL-3002 Microprocessor Interfacing & Programming
Lab

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).

Department of Electrical Engineering


FAST NU, Islamabad 5|Page
EL-3002 Microprocessor Interfacing & Programming
Lab

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:

Calculate the amount of time delay generated by the timer


Assume that XTAL = 8MHz and we loaded 0xF2 in TCNTn register.

1 Clock period T = 1 /8 MHz = 0.125 μs

No of machine cycle before rollover/ overflow:


0xFF - 0xF2 = 0x0D (13 decimal)
One extra clock needed when it rolls over from FF to 0 and raises the TOV0 flag

This gives 14 x 0.125 μs = 1.75 μs

Department of Electrical Engineering


FAST NU, Islamabad 6|Page
EL-3002 Microprocessor Interfacing & Programming
Lab

For prescalar option enabled:

If prescalar( for timer 1) = 010 i.e, clk/8

1 Clock period T = 8 /8 MHz = 1 μs

No of machine cycle before rollover/ overflow:


0xFF - 0xF2 = 0x0D (13 decimal)
One extra clock needed when it rolls over from FF to 0 and raises the TOV0 flag

This gives 14 x 1 μs = 14 μs.

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.

Department of Electrical Engineering


FAST NU, Islamabad 7|Page
EL-3002 Microprocessor Interfacing & Programming
Lab

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)

Department of Electrical Engineering


FAST NU, Islamabad 8|Page
EL-3002 Microprocessor Interfacing & Programming
Lab

b)

Department of Electrical Engineering


FAST NU, Islamabad 9|Page
EL-3002 Microprocessor Interfacing & Programming
Lab

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

FAST NU, Islamabad 10 | P a g


Department of Electrical Engineering
e

You might also like