0% found this document useful (0 votes)
320 views3 pages

L 9 Timer Programming: Objectives

This document describes a lab experiment on programming an AVR timer. The objectives are to program the AVR timer and generate a square wave using the timer to play music. The lab activities include writing programs to generate square waves at 1000Hz and 500Hz using the timer, using the timer to turn on LEDs after a delay, and toggling an LED a certain number of times when a button is pressed using the timer.

Uploaded by

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

L 9 Timer Programming: Objectives

This document describes a lab experiment on programming an AVR timer. The objectives are to program the AVR timer and generate a square wave using the timer to play music. The lab activities include writing programs to generate square waves at 1000Hz and 500Hz using the timer, using the timer to turn on LEDs after a delay, and toggling an LED a certain number of times when a button is pressed using the timer.

Uploaded by

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

LAB 9

TIMER PROGRAMMING

OBJECTIVES:
 To program the AVR timer.
 To generate a square wave using the AVR timer and play music.

REFERENCE:
 Mazidi & Naimi, “The AVR Microcontroller and Embedded Systems,” Chapter 9.

MATERIALS:
 AVR Trainer
 AVR Assembler
 Oscilloscope
 Speaker (optional)

ACTIVITY 1

a) Write a program using a Timer to generate a 1000 Hz square wave frequency on pin
PORTC.2.
b) Then examine the frequency using the oscilloscope.
c) Modify the count value to make sure that the frequency is exactly 1000 Hz.
#include <avr/io.h>

void delay ( )
{
TCNT0 = -125;
TCCR0A = 0x00;
TCCR0B = 0x03;

while ((TIFR0&(1<<TOV0)) == 0);


TIFR0 = (1<<TOV0);
}

int main ( )
{
DDRC |= (1<<2);

while(1)
{
PORTC |= (1<<2);
delay ( );
PORTC &= ~(1<<2);
delay ( );
}
return 0;
}

www.NicerLand.com
LAB 9
TIMER PROGRAMMING

ATmega328

PORTC.2 Speaker

ACTIVITY 2

a) Modify the Activity 1 to generate a 500 Hz square wave frequency.


b) Rewrite the code, using the CTC mode.

ACTIVITY 3

Write a program that sets the PORTB.2 when the key is pressed and after 3 seconds
PORTB.3 is turned on, as well. Use Timer1.

ATmega328
PB0 PB2
PB3

ACTIVITY 4

Connect a key to PORTD.4. Using Timer/Counter0, write a program to toggle PORTB.2,


when the key is pressed, 10 times.

ATmega328
PD4 (T0) PB2

www.NicerLand.com
WORKSHEET
Name: Class:
Last Name: Lab#:

1) What is the lowest frequency that can be generated using Timer0 if the crystal frequency
is 16 MHz? Show your calculation.

2) What is the lowest frequency that can be generated using Timer1 if the crystal frequency
is 16 MHz? Show your calculation.

3) In Normal mode, when is TOVx set to high?

4) In CTC mode, when is OCFx set to high?

www.NicerLand.com

You might also like