L 9 Timer Programming: Objectives
L 9 Timer Programming: Objectives
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;
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
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
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.
www.NicerLand.com