CP316: Microprocessor Systems and Interfacing
Timers and Counters
Objectives
Since creating square waves and pulses is common in circuits,
hardware timers make this easy. Once set up, they can
run
indefinitely
without software intervention.
Equipment
- Arduino Uno
- AVR Data Sheet [pdf,660pp;
©2015]
- oscilloscope
- function generator
Procedure
Arduino timer "poll overflow flag" program
- Connect the Arduino board.
- Create a new sketch, and download the
timer poll overflow flag
sketch.
- Examine the code, and based on the timer configuration registers,
identify which mode the timer is in; i.e.
normal or
CTC. Which is it?
Also, determine what prescaler value is being used. Which
is it?
- Connect the oscilloscope to output pins 9 and 13.
(This is because each timer has certain pins which it can control.
Timer 1 can control pins 9 and 10. None of the timers
can directly control pin 13.)
- Run the program to see that the square wave appears on the scope.
- Change the value in OCR1A (by at least a factor of 2) and rerun
the program to see if the frequency changes. Does it?
Based on the mode, is this what you would expect? Explain.
- Without changing the mode, change the prescaler value. Record the
output frequency observed. Does it make sense? Explain.
Try a couple of other prescaler values to see that it operates as
expected.
- Return to the original prescaler value, and rerun the
program.
- Sketch the output, and note the oscilloscope settings.
Demonstration - summarize what you have learned
about this timing mode and the prescaler.
Arduino timer "poll compare flag" program
- Create a new sketch, and download the
timer poll compare flag
sketch.
- Examine the code, and based on the timer configuration registers,
identify which mode the timer is in; i.e.
normal or
CTC. Which is it?
Also, determine what prescaler value is being used. Which
is it?
- Connect the oscilloscope to output pins 9 and 13.
(This is because each timer has certain pins which it can control.
Timer 1 can control pins 9 and 10. None of the timers
can directly control pin 13.)
- Run the program to see that the square wave appears on the scope.
Determine the delay between the two output pins.
What is the cause of the delay?
- Change the value in OCR1A (by at least a factor of 2) and rerun
the program to see if the frequency changes. Does it?
Based on the mode, is this what you would expect? Explain.
- Return to the original value in OCR1A.
- Configure the program to omit the prescaler so
you can see the maximum frequency that can be produced.
Sketch the output, and note the oscilloscope settings.
- Without changing the mode, change the prescaler value. Record the
output frequency observed. Does it make sense? Explain.
Try a couple of other prescaler values to see that it operates as
expected.
- Return to the original prescaler value, and rerun the
program.
- Press the Run/Stop button on the oscilloscope to freeze
the image, and note how consistent the pulses are. Repeat this 5 or 6
times to find the maximum variation between pulses that you observe.
Sketch the output, and note the oscilloscope settings.
Demonstration - demonstrate the screen showing the
variation.
Because there is no operating system,
your program
is the only task running.
Using low-level operations
- Look at the blinkfast sketch to see how access was made
to
Arduino
pin 13.
- In this week's sketches, toggling a pin is done using the
following instruction:
digitalWrite(LED_PIN,!digitalRead(LED_PIN));
Follow the example from the blinkfast sketch to replace
this line of code with a low-level command. (Hint: The exclusive OR
operator is '^'.)
- Sketch the oscilloscope output showing the delay, and note the
oscilloscope settings.
Determine the delay between the two output pins.
How does it compare to the previous delay?
Demonstration - demonstrate the modified program working.
- Disassemble both the original code and the modified version so
that the timing can be compared.
Counter operation
-
Set up the function generator to produce a 0-5V 1kHz square wave.
-
Change the timer configuration to make it count pulses on its input pin
instead.
-
Configure the input pin, and connect the function generator.
-
Observe the output, and see that it is actually acting as a counter by
changing the frequency of the function generator and seeing the result.
Demonstration - demonstrate the operation of the the counter
using the modified code.
Multiple timers; adapt for Timer 2
-
Adapt the code from the "low level operations" section to use Timer 2
instead.
Demonstration - demonstrate the operation of Timer 2
using the modified code.
Wilfrid Laurier University
© 2019 Wilfrid Laurier University