Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

26/04/2019

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).

Programmable Timers Programmable Timers

 In a microcontroller, programmable timers operate


 Example independently of software execution.
– Counter mod of 10 with a Frequency of 1 kHz.  They operate as a background task.
– Period of 1kHz is 1ms.  This avoids slowing down the code.
 Timers are controlled by a clock whose frequency can be
– For each 1ms, the counter is incremented by 1. programmed.
– This will count from 0 to 9 and then will restart  The mod value for the counter (the maximum number of counts
from 0. before reset), depends on the size of the counter.
 The ATMega2560 has both 8 and 16 bit counters.
– So we have a timer which counts at 1kHz or at  We can have mod values of 2^8 (256) and 2^16 (65536).
1ms per count.

3 4

Determining Frequency System Clock

 In order to set the frequency of a timer, you  Frequency = 16MHz


will need to determine the clock input.  Period = 1/16MHz = 62.5ns
 Microcontroller Clock (Our board used a  Mod Range = 2^8 (8Bit) = 256
16MHz crystal oscillator).  256 * 62.5ns = 15.94us

 You can use an external clock.


 Therefore using the System Clock, from 0 to
255, it takes 15.94us, then resets.
 Either of these clocks are known as System  Is 15.94us practical use for a Timer is all
Clocks. applications?
– What do we do if we need a timer of 50ms?
5 6

1
26/04/2019

Prescaler Prescaler

 Microcontrollers provide the ability to  Prescalers available for the ATMega2560


‘Prescale’ their System Clock frequencies. – None
– 8
 This provides more control of the resolution
– 16
of timer lengths. – 64
– 128
– 256
– 1024

7 8

Timer Resolution – Overflow Mode Timer Range

 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

Example Output Compare Mode


 What will be the time needed by a 16-bit counter which counts  This mode, the programmable timer will compare
with a frequency of 16MHz and a prescaler factor of 8 to
overflow 10 times? the current count with a given value.
– fclk = fmicrocontroller / prescale factor  When they come equal, the timer will generate an
– fclk = 16MHz / 8 = 2 MHz – this is the frequency of the clock after interrupt.
it was prescaled.
– Tclk = 1/fclk = 500ns (Timer Resolution)  This mode of operation may be used for
 This means that every single count of the timer/counter will implementing delays which are shorter than the
take 500ns. period of an overflow.
 Now find the overflow time period for 10 counts.
– 16 bit which counts from 0 to 65535.  To use this mode, you need to:
– 1 Overflow is 2^16 * 500ns = 32.8ms – Calculate the Timer Resolution.
– 10 overflows the timer will need 328ms. – Decide what the compare value should be.

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 Uses ATMega2560 Timers

 Timed Outputs  Timer/Counter0


– Signal Generation – 8-bit Timer/Counter module, with two independent Output
 Music Tones Compare Units, and with PWM support
 PWM
 Driving Spark Ignitions  Timer/Counter1, 3 ,4 and 5
– Referencing Timing Signals (Eg: Delays) – 16-bit Timer/Counter with Separate Prescaler, Compare
Mode, and Capture Mode: Timer/Counter1 with PWM (3
independent output compare units)
 Measuring Inputs
– Period / Frequency Measurement  Timer/Counter2
– PWM – 8-bit Timer/Counter with Separate Prescalers and
Compare Modes and with PWM and asynchronous
operation (Separate Oscillator)
15 16

Main Blocks How it Works

 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

How it Works How it Works

 Normal Mode (Overflow)  Output Compare Mode


– If the programmer does not program a value in – The programmer can write a value in the OCR0A register
OCR0A, then the counter TCNT0 will count from (the Output Compare Register). This is the compare value.
0 to its max. value: 255 (8-bit for TCNT0, it will The register will remain programmed with this value until
be different for 16-bit TCNT1). When the counter the programmer changes it again in the code. However,
the counter TCNT0 will start counting at 0 all the way up to
reaches the maximum value an overflow
its maximum value.
interrupt will be generated.
– The value of the TCNT0 is compared to the value of
– The count direction and whether to be reset or OCR0. When found equal, an interrupt request is
not is dictated by the control logic. The control generated (OC0A (Int. Req) ) and the pin OC0A is toggled
logic can be programmed by setting up various (see pins OC0A, OC0B, etc on the ATMega2560 Pinout).
bits in TCCR0A, the Timer/Counter Control
19 Register. 20

Output Compare Diagram Timer Registers – TCCR0

21 22

Timer Registers - Prescale Timer Registers – Set Mode

23 24

4
26/04/2019

Timer Registers – Set Mode Timer Registers – Set Mode

25 26

Timer Registers Timer Registers

27 28

/*================ Led05ms.c ======================================


PURPOSE – get PORTB pin 7 to toggle every 0.5ms NORMAL MODE
Timer used is timer/counter0.
Mode of operation used for counter: Mode 0 (Normal); no prescaler used
*/
#include <avr/io.h>
#include <avr/interrupt.h>
Code Example – Overflow #include <avr/pgmspace.h>
#include <util/delay.h>

#define TOGGLECYCLES 31

 Toggle PORTA Pin 7 every 0.5ms. volatile unsigned char overflowCounter = 0;

– Use No Prescaler int main(void)


{
– Timer Resolution = 1/16MHz = 62.5ns cli(); // Disable All Interrupts
DDRA = 0x80; // PORTA output
– Timer Range (Overflow) = 2^8 * 62.5ns = 16us PORTA = 0x00; // Clear PORTA
TCNT0 = 0x00; // Clear the Timer
– To toggle at 0.5ms: TCCR0A = 0x00;
TCCR0B = 0x01;
//
//
Mode 0,
No prescale
 0.5ms / 16us = 31.25 (Use 31)
TIMSK0 = TIMSK0 | (1 << TOIE0); // Enables overflow interrupt
– We add the value into a declared Constant TIFR0 = TIFR0 | (1 << TOV0); // Set the overflow flag
sei(); // Enable all Interrupts
within our code, Eg TOGGLECYCLES.
while(1)
{
;
29 30 }
}

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

/*================ squarewave.c ======================================


PURPOSE - code to generate a squarewave with 1 kHz frequency: OUTPUT
COMPARE MODE
The square wave is generated on pin OC0/PB3.
*/
#include <avr/io.h>
#include <avr/interrupt.h>
PWM Mode
#include <avr/pgmspace.h>
#include <util/delay.h>

int main(void)  ATMega2560 has 2 PWM modes.


{
cli(); // Disable all interrupts – FastPWM
DDRB = 0x80; // PORTB output – Phase-Correct PWM
PORTB = 0x00; // Clear PORTB

TCCR0A = 0b01000010; // Toggle OC0A ,Mode 2


 We are only going to use FastPWM
TCCR0B = 0b00000100; // 256 x prescaler
 Fast PWM mode, the compare unit generates
OCR0A = 33; // Load value into the OCR Register PWM waveforms on the OC0A pin. You can
while(1) choose between an inverted or non-inverted PWM
{
; output.
}
}  NOTE: The actual PWM signal will only be visible
on the OC0A port pin if the data direction for the
33 34 port pin is set as output.

#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

TCCR0A = 0b10000011; // Non inverting 8-Bit Fast PWM ,


TCCR0B = 0b00000001; // No prescaler

OCR0A = xx; // Load a value into the OCR Register

while(1)
{
;
}
}

35 36

6
26/04/2019

Input Capture Input Capture

 The Timer/Counter 1, 3, 4 and 5 incorporate an Input Capture unit


that can capture external events and give them a time-stamp
indicating time of occurrence
 How it works:
– Source of external event: ICPn pin or ACO (analog comparator
output)
– When a change of logic level occurs on this pin, a capture is
triggered
– At this moment in time, the TCNTn value is written into ICRn
– The application (code) needs to read the value of the timer input
capture register, ICRn.
 Further details on Input Capture can be found in the Lecture Notes.

37 38

Next Lecture: Analogue to Digital


Practical Advice Converters

 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

You might also like