Lecture – Timers and Counters

Hardware Timing, Counters

Ryan Robucci

Table of Contents

References

What are Timers/Counters

Timer/Counter Components

A timer is built from a counter

Timer Hardware

Consider the following scenarios:

  1. You would like to measure time for up to 2 seconds, with a sub 1-ms precision. How many bits of resolution are required?

    Ans: ceil( log2( NUMBER OF LEVELS)) = ceil( log2 (2/.001))
  2. You have a an 8MHz clock available, you would like a timer that
    measures time up to 2 seconds. How large does the counter/timer
    register need to be?

    Ans: ceil( log2( NUMBER OF CYCLES)) = ceil( log2(2*8e6))

Capture Registers: High-Precision Event Timestamps

Consider this scenario: You are building a circuit to store the time an event occurred (indicated by a rising edge on a wire). How would you make the circuit?

Now, consider how you would do the same with a microcontroller (indicated by a rising edge on an external pin)?

Based on what you have learned these might be the approaches:

Compare Match Registers

Timer Behaviors

A timer can be set up to regularly do one or more of the following:

  1. trigger an interrupt,
  2. cause the hardware to make some pin value change,
  3. reset the timer itself

Upon one or more of the following events

  1. Overflow
  2. Compare-match events

Event Counters

Though we have mostly discussed the use of the counters as timers, the hardware
counters are also useful for counting rapid events, like tracking the number of
rotations of a high RPM motor.
Number of button presses

AVR Timer/Counter Modes

AVR Counter Hardware

Normal Mode

Clear Timer on Compare Match (CTC) Mode

CTC Mode with Output Toggle (frequency modulation)

• Use CTC mode to toggle output pin on compare match
• Vary counter compare register to achieve different frequency

Pulse width modulation

Waveforms with unequal low and high times (DutyCycle!=50%\rm Duty\, Cycle\,!= 50\%) are desired.

Duty Cycle: fraction of time in a period that a waveform is active (e.g. high)

Commonly expresses as percentage rather than a simple ratio:

D=PWT×100%D=\frac{PW}{T} \times 100 \%

https://en.wikipedia.org/wiki/Duty_cycle:

Pulse Width Modulation and Frequency Modulation

In-class

Drawings

Fast PWM

Phase Correct PWM Mode

AVR 16-Bit Timers

Timer Counter 0 and 1

• Share the same prescaler but have separate selection

Timer/Counter 1 Modes:

Counter Modes Summary

AVR timers has a few important modes of operation:

Hardware Buffering for Multi-word Software-Hardware Read and Writes

Only write to lower triggers write to peripheral registers, upper byte is buffered to a buffer register and should be written first

Reading lower byte triggers upper-byte to be copied to buffer registers. Second read of upper byte is actually from the buffer register.

Hybird Hardare-Software Timers

The functionality of software and hardware can be combined for increased utility from timers

Extending Timer Through Software Using Overflow Bit

The effecter timer/counter range may be extended using software.
Timer/counters typically have overflow bits.

Example: count 10.5 *max counter

(Thanks Alex Nelson for providing overflow Example)

System Tick and Software Timers

Multiple Hybrid Hardware/Software timers with dynamic updates for next event

ACHIEVABLE hardware timing

Getting the frequency you want can require some thought.

Example:

Base derivative calculations on the Actual/Achieved Frequency, not the Desired Frequency

#define F_CPU = (1000000.0)
#define F_DESIRED = 3000
#define PRESCALER = 2
 //typically a power of two
#define F_PRESCALE = (F_CPU/PRESCALER)
 // gives value 500000
#define TIMER_COUNT = ceil((F_PRESCALE)/F_DESIRED)
 // find integer count: ceil -> slightly faster error
 // floor -> slightly slower error

#define TIMER_MAX_COUNT_TO_SET = (TIMER_COUNT-1) // define max count to set
// Find the achieved freq. on the next line 2994.012 Hz
#define F_ACTUAL = F_CPU/(PRESCALER *(TIMER_COUNT)) 
// Key point here: use F_ACTUAL not F_DESIRED
# define TEN_SECONDS = F_ACTUAL * 10 

"Leap Cycles for Leap Ticks" Slip Adjustment to Compensate for Error Accumulation