The Hong Kong Polytechnic University
Department of Electronic and Information Engineering
EIE3373 Microcontroller Systems and Interface
Laboratory Exercise 2: AVR Timer/Counter Programming
(Deadline: Check the course information)
Objective:
1. To develop C programs with timers under the Arduino platform.
2. To develop a C program with counters under the Arduino platform.
Introduction:
This experiment introduces an application of using timers and counters inside the AVR
microcontroller. Students MUST use the AVR timers in the delay function to finish this lab.
Using simple loops to count the delay is not acceptable.
Equipment:
Atmel Studio 6
The Arduino Starter Kit
Procedure:
Section A: Write a C Program to toggle a LED by using a timer
1. Write a C program to toggle a LED connected to PB0 (you should connect the LED to
PB0 with a resistor) in every second. You should use the timer with Normal mode to
generate the time delay of one second. Note that the clock frequency of the Arduino Start
Kit is 16 MHz.
2. Repeat Step 1 but this time the timer is in CTC mode.
Section B: Write a C Program to simulate the traffic lights
1. Write a C program to simulate the traffic lights by using different pins. You should use a
timer in Normal mode. Note that the clock frequency of the Arduino Start Kit is 16 MHz.
A set of traffic lights for cars (Light 3): PB0, PB1, PB2 (3 LEDs)
A set of traffic lights for cars (Light 2): PB3, PB4, PB5 (3 LEDs)
A set of traffic lights for people (Light 1): PC4, PC5 (2 LEDs)
2
Repeat the following:
Light 1 (RED), Light 2 (GREEN), Light 3 (RED), period (around 5s)
Light 1 (RED), Light 2 (YELLOW), Light 3 (RED), period (around 1s)
Light 1 (RED), Light 2 (RED), Light 3 (RED), period (around 1s)
Light 1 (RED), Light 2 (RED), Light 3 (RED+YELLOW), period (around 1s)
Light 1 (GREEN), Light 2 (RED), Light 3 (GREEN), period (around 5s)
Light 1 (GREEN Blinking), Light 2 (RED), Light 3 (YELLOW), period (around 1s)
Light 1 (RED), Light 2 (RED), Light 3 (RED), period (around 1s)
Light 1 (RED), Light 2 RED+YELLOW), Light 3 (RED), period (around 1s)
2. Repeat Step 1 but this time the timer is in CTC mode.
Section C: Write a C program to count a switch
Connect a switch to pin T0 (PD4, Counter 0) or T1 (PD5, Counter 1) and a LED to PC0.
There are two states in the switch: State 0 and 1. When it is in State 0, the LED is off. When
it is in State 1, the LED is on. At the beginning, the switch is in State 0. When the switch is
pressed three times and it is in State 0, it goes to State 1. When the switch is pressed three
times and it is in State 1, it goes to State 0. You should use counter programming in CTC
mode to implement this application.
Demonstrate Section B and C to our tutors or technicians.
Instructions:
1. You are required to demonstrate your programs to our tutor or technicians.
2. Zip all programs (including the whole projects) in Section A, B, and C into a single file,
and submit it to Blackboard.
3. Deadline: Check the course information.