Professional Documents
Culture Documents
Microcontroller Applications Programmable Timers
Microcontroller Applications Programmable Timers
Programmable Timers
Microcontroller Applications
In principle, a timer is a simple counter
measuring time by accumulating a number of
clock ticks.
It’s characterised by 2 main parameters.
Programmable Timers – Counting the clock frequency and it’s modulo.
(Mod = The Maximum number of counts before
reset).
3 4
1
26/04/2019
Prescaler Prescaler
7 8
When using Programmable Timers, we must first Once we know the Timer Resolution, we
understand and determine the Timer Resolution. can now determine the Timer Range.
When the counter starts from 0, it will increment its
The Timer Range is the total time needed
count on every clock cycle.
for the counter to count from 0 to the
When it reaches the maximum number of counts, it
will reset itself and start again. maximum value.
The time between counts is the Timer Resolution.
Board Example
Board Example
9 10
11 12
2
26/04/2019
Example Example
We want an LED on PORTA pin 7 to toggle every 200μs. In 200μs, the timer/counter will counter approximately 12
Calculate the Timer Resolution. counts. This is the value you will need to program the counter
Using micros clock of 16MHz and a prescaler of 256. with to achieve an overflow at every 200μs.
– fclk = fmicrocontroller / prescale factor When the timer reaches the value 12, it will reset itself, (Start
fclk = 16MHz / 256 = 62.5 kHz – this is the frequency of the clock
counting from 0), and it will also generate an interrupt.
–
after it was prescaled.
– Tclk = 1/fclk = 16.0μs In the Interrupt Service Routine you can then have the follow
Now decide on the compare value. We need it for 200μs. line of code to toggle the LED.
– Counts = 200μs / 16μs – PORTA = PORTA ^ 0x80;
– Counts = 12.5 counts (Where ‘^’ = XOR)
Approx. 12 counts (round it down)
13 14
Timer/counter performs the counting (TCNTn) Note: This will explain for the 8 bit
register. timer/counter0, but the same applies to
A clock select unit which produces the clock signal other counters.
A register which can be programmed with the
TCNT0 is the free running counter. (TCNT1
compare value for the counter (OCRnX) – this is
for Output Compare mode of operation for 16 bit). Its counts are driven by the
A control logic unit which can stop/start the counter, signal received from the control logic.
change its counting direction, etc. The control logic uses a clock which is
A physical output pin (OCnX) where the counter either internal or external, and is used with
output waveform can be found. or without a prescaler.
17 18
3
26/04/2019
21 22
23 24
4
26/04/2019
25 26
27 28
#define TOGGLECYCLES 31
5
26/04/2019
ISR(TIMER0_OVF_vect)
{
if(overflowCounter == TOGGLECYCLES)
Code Example – Output Compare
{
PORTA = PORTA ^ 0b10000000;
overflowCounter = 0; Create a square waveform with a frequency
}
overflowCounter++;
of 1 kHz.
TIMSK0 = TIMSK0 | (1 << TOIE0); // re-arm interrupt – Use Prescaler 256
}
– Frequency = 1kHz Period = 1ms
– Resolution = 16MHz / 256 = 65.5kHz => 15.26us
– Range = 2^8 * 15.26us = 3.91ms
– We need half of the period => 1ms / 2 = 500us
– OCR = 500us / 15.26us= 32.77 => 33
31 32
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
Match between #include <util/delay.h>
OCR0A and TCNT0A Overflow occurred
int main(void)
{
cli(); // Disable all Interrupts
DDRB = 0x80; // PORTB output
PORTB = 0x00; // Clear PORTB
while(1)
{
;
}
}
35 36
6
26/04/2019
37 38
Debugging Interrupts can be difficult. We will look into use the Analogue to Digital
– Use the inbuilt Arduino ‘Serial’ library.
Converter of the ATMega2560
Eg. Write to the Serial Port and monitor when an ISR is
serviced. Microcontroller.
Question 7 requires outputting a calculated pulse width of a signal
onto the LCD Display. Use the Arduino ‘LiquidCrystal’ library to
display values.
– This library can be directly used within your code without the
need of using the Arduino ‘void setup()’ and ‘void loop()’.
Question 7 also requires you to use ICP4 which is on PORTL. You
may have already designed your Hex Keypad to use this pin on
PORTL You will need to re-design the connection diagram and code
for this modification.
39 40