[go: up one dir, main page]

0% found this document useful (0 votes)
6 views27 pages

Timers

Uploaded by

Nguyễn Trung
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views27 pages

Timers

Uploaded by

Nguyễn Trung
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Lecture 4’s sequence

4.1 Interrupt programming in C for ATmega16

4.2 Timers in ATmega16

Review of C Programming

4.3 Timer applications


Timers in ATmega16

▪ Many computer applications require accurate timing

▪ Examples include:
❑ recording the time when an event occurs

❑ calculating the time difference between events

❑ performing tasks at specific or periodic times

❑ creating accurate time delays

❑ generating waveforms of certain shape, period, or duty cycle

▪ Lectures 4 and 5 focus on using timers to perform time-related tasks


Timer terminology

▪ Input Capture:
❑ Input signal is connected to a pin, called input capture, of the timer

❑ When an event (rising edge, falling edge, or change) occurs on this pin, the current
timer value is automatically stored in a register

▪ Output Compare:
❑ A timer typically has a pin, called output compare

❑ When the timer reaches a preset value, the output compare pin can be
automatically changed to logic 0 or logic 1
Overview of Timers in ATmega16

▪ ATmega16 has three timers: Timer 0, Timer 1 and Timer 2

▪ Each timer is associated with a counter and a clock signal

▪ The counter is incremented by 1 in every clock cycle of the timer

▪ The clock signal of a timer can come from:


❑ the internal system clock, or

❑ an external clock source


Overview of Timers in ATmega16

▪ When the internal system clock is used, a pre-scaler can be applied to make
the timer count at a slower rate

▪ Example:
❑ Consider a system clock of 1Mhz (i.e., 1μs per cycle)

❑ Suppose that a timer pre-scaler of 64 is used

❑ Then, timer will increment every 64μs


Overview of Timers in ATmega16

Timer 0 Timer 1 Timer 2


- 8-bit counter - 16-bit counter - 8-bit counter
Overall
- 10-bit pre-scaler - 10-bit pre-scaler - 10-bit pre-scaler
- PWM - PWM - PWM
- Frequency generation - Frequency generation - Frequency generation
Functions - Event counter - Event counter - Event counter
- Output compare - Output compare channels: 2 - Output compare
- Input capture
- Normal mode - Normal mode - Normal mode
Operation - Clear timer on compare match - Clear timer on compare match - Clear timer on compare match
modes - Fast PWM - Fast PWM - Fast PWM
- Phase correct PWM - Phase correct PWM - Phase correct PWM

▪ Timer 1 has the most capability among the three timers


Study plan

▪ In Lecture 4, we focus on:


❑ operations of Timer 1
❑ using Timer 1 overflow interrupt
❑ using Timer 1 input capture interrupt
❑ measuring time, creating time delay
❑ measuring period/duty cycle of a signal
❑ information required for Lab 3

▪ In Lecture 5, we will learn:


❑ using Timer 1 output compare interrupt
❑ generating PWM signals
❑ information required for Lab 4
Timer 1: An overview

▪ 16-bit counter

▪ 10-bit pre-scaler: 8, 64, 256, and 1024

▪ can trigger a timer overflow interrupt when counter reaches MAX

▪ can trigger an input capture interrupt when an event occurs on the input capture pin:
❑ timer value is stored automatically in a register

❑ input capture pin for Timer 1 is ICP1 (D.6)

▪ can trigger an output compare match interrupt when timer reaches a preset value:
❑ There are two independent output compare channels A and B
Timer 1: Block diagram

External clock pin

Current timer/counter value

Output Compare pins


Output Compare registers

Input Capture register

Input Capture pin

Not shown here: TIMSK and TIFR registers


Timer/Counter
Control registers
Timer 1 ─ Relevant pins

used in ATmega16 chip


this lecture
Timer 1 ─ Five groups of registers

1) Timer/Counter 1
❑ TCNT1
❑ 16-bit register that stores the current value of the timer

2) Timer/Counter 1 Control Registers


❑ TCCR1A and TCCR1B
❑ to configure the operations of Timer 1

3) Input Capture Register


❑ ICR1
❑ to store timer value when an event occurs on input capture pin

4) Interrupt registers
❑ TIMSK to enable timer interrupts
❑ TIFR to monitor status of timer interrupts

5) Output Compare Registers


❑ OCR1A, OCR1B
will be covered
❑ to store the preset values for output compare in Lecture 5
Timer 1 ─ Five groups of registers

We now study the


important registers for Timer 1
Timer/Counter 1 Control Register A (TCCR1A)

7 6 5 4 3 2 1 0
COM1A1 COM1A0 COM1B1 COM1B0 FOC1A FOC1B WGM11 WGM10
Specify Waveform Generation Mode used with
WGM13, WGM12
WGM13 ... WGM10 = 0000 for normal mode.

1 to force output compare on channel B

1 to force output compare on channel A

Output compare mode for channel B

Output compare mode for channel A


Timer/Counter 1 Control Register B (TCCR1B)

7 6 5 4 3 2 1 0
ICNC1 ICES1 - WGM13 WGM12 CS12 CS11 CS10

clock select (next slide)

Specify Waveform Generation Mode used with


WGM13, WGM12
WGM13 ... WGM10 = 0000 for normal mode.

input capture edge select: 1 will select rising edge,


0 will select falling edge

1 to activate input capture noise canceller


Clock select

CS12 CS11 CS10 Description


0 0 0 No clock source (timer stopped)
0 0 1 CLKI/O/1 (no prescaling)
0 1 0 CLKI/O/8 (from prescaler)
0 1 1 CLKI/O/64 (from prescaler)
1 0 0 CLKI/O/256 (from prescaler)
1 0 1 CLKI/O/1024 (from prescaler)
1 1 0 External clock source on T1 pin, clock on falling edge
1 1 1 External clock source on T1 pin, clock on rising edge

▪ For ATmega16, the default internal clock is CLKI/O = 1MHz


▪ Timer 1 can use the internal or external clock
▪ If using the internal clock, we can set Timer 1 to run 8, 64, 256, or 1024 times slower than the
internal clock
Timer/Counter Interrupt Mask Register (TIMSK)

7 6 5 4 3 2 1 0
OCIE2 TOIE2 TICIE1 OCIE1A OCIE1B TOIE1 OCIE0 TOIE0

For Timer 0

Timer 1 Overflow Interrupt Enable

Timer 1 Output Compare B Match Interrupt Enable: 1 to enable

Timer 1 Output Compare A Match Interrupt Enable: 1 to enable

Timer 1 Input Capture Interrupt Enable: 1 to enable

For Timer 2
Timer/Counter Interrupt Flag Register (TIFR)

7 6 5 4 3 2 1 0
OCF2 TOV2 ICF1 OCF1A OCF1B TOV1 OCF0 TOV0

For Timer 0

Timer 1 Overflow Interrupt Flag: set to 1 when overflow

Timer 1 Output Compare B Match Flag: set to 1 when match

Timer 1 Output Compare A Match Flag: set to 1 when match

Timer 1 Input Capture Flag: set to 1 when capture event occurs

For Timer 2

▪ This register has flags that indicate when a timer interrupt occurs
▪ It is not often used in ATmega16 programs
Lecture 4’s sequence

4.1 Interrupt programming in C for ATmega16

4.2 Timers in ATmega16

4.3 Timer applications


Timer applications

▪ In this section, we consider three applications of Timer 1:

❑ Creating an accurate delay using timer overflow interrupt

❑ Measuring elapsed time between two events

❑ Measuring the period of a square signal using input capture interrupt


Example 1: Creating an accurate delay

Write a C program for ATmega16 to toggle PORTB every 2 seconds. It should use Timer 1
overflow interrupt to create delays of 2 seconds each.

▪ Analysis
❑ Internal system clock: 1MHz
❑ With no pre-scaler, Timer 1 increments every 1 μs
❑ Timer 1 is 16-bit counter, so it will overflow every 216 μs
❑ For a 2s delay, we need Timer 1 to overflow for 2s/216 μs = ~31 times

▪ Implementation
❑ Write code to enable Timer 1 overflow interrupt
❑ Use ISR to count the number of overflows
❑ When the number of overflows is 31, invert port B
Example 1: Creating an accurate delay

#include <avr/io.h>
#include <avr/interrupt.h> 1
volatile int overflow_count; // declare a global variable
ISR(TIMER1_OVF_vect) { // ISR for Timer1 overflow interrupt
overflow_count++; // increment overflow count
if (overflow_count >= 31) { // when 2s has passed
overflow_count = 0; // start new count 2
PORTB = ~PORTB; // invert port B
}
}
int main(void) {
DDRB = 0xFF; // set port B for output
PORTB = 0x00; // initial value of PORTB
overflow_count = 0; // initialize overflow count
TCNT1 = 0x00; // initial value of counter register
TCCR1A = 0b00000000; // normal mode 4
TCCR1B = 0b00000001; // no pre-scaler, internal clock
TIMSK = 0b00000100; // enable Timer 1 overflow interrupt 3
sei(); // enable interrupt subsystem globally 5
while (1){;} // infinite loop
return 0;
}
Example 2: Measuring elapsed time

▪ To measure time using Timer 1, we must keep track of both


❑ the number of times that Timer 1 has overflowed: n
❑ the current counter value: TCNT1
▪ If we reset n and TCNT1 at the beginning of the interval. The time elapse is (assuming no pre-
scaler, 1MHz clock):
t = n x 216 + TCNT1 (μs)

Timer Overflows
n=0 TCNT1
TCNT1 = 0

1 2 n
time interval t
start at time t1 stop at time t2
Example 2: Measuring elapsed time

Use Timer 1 to measure the execution time of some custom C code.

▪ Approach
❑ Clear Timer 1 when the custom code starts

❑ Record Timer 1 when the custom code finishes

❑ Use Timer 1 Overflow Interrupt to count the number of timer overflows


Example 2: Measuring elapsed time

#include <avr/io.h>
#include <avr/interrupt.h>
#include <inttypes.h>
volatile uint32_t n; // uint32_t is unsigned 32-bit integer data type
ISR(TIMER1_OVF_vect) { // handler for Timer1 overflow interrupt
n++; // increment overflow count
}
int main(void) {
int i, j;
uint32_t elapse_time; // uint32_t is unsigned 32-bit integer data type
TCCR1A = 0b00000000; // normal mode
TCCR1B = 0b00000001; // no pre-scaler, internal clock
TIMSK = 0b00000100; // enable Timer 1 overflow interrupt
n = 0; // reset n
TCNT1 = 0; // reset Timer 1
sei(); // enable interrupt subsystem globally

for (i = 0; i < 100; i++)


for (j = 0; j < 1000; j++){;} any random code
elapse_time = (n << 16) + (uint32_t) TCNT1;
cli(); // disable interrupt subsystem globally
return 0;
}
Example 3: Measuring period of a square signal

Use Timer 1 input capture interrupt to measure the period of a square wave.

▪ Analysis:
❑ The period of a square wave is equal the time difference between two consecutive rising edges

❑ Connect the square wave to Input Capture pin of Timer 1

❑ Configure input capture module to trigger on a rising edge

Input capture interrupt is triggered Input capture interrupt is triggered


Clear TCNT1 = 0 Read ICR1 register (= period)

period square waveform


Example 3: Measuring period of a square signal

▪ Assumption: The input signal has a high frequency; hence timer overflow can
be ignored

▪ Implementation:
❑ Select timer operations: normal, no pre-scaler, internal clock 1MHz, noise
canceller enabled, input capture for rising edges
TCCR1A = 0b00000000;
TCCR1B = 0b11000001;

❑ Enable input capture interrupt:


TIMSK = 0b00100000;
Example 3: Measuring period of a square signal

#include <avr/io.h>
#include <avr/interrupt.h>
#include <inttypes.h> // uint32_t is unsigned 32-bit integer data type
uint16_t period; // uint16_t is unsigned 16-bit integer

ISR(TIMER1_CAPT_vect){ // handler for Timer 1 input capture interrupt


period = ICR1; // period = value of Timer 1 stored in ICR1
TCNT1 = 0; // reset Timer 1
}

int main(void) {
DDRB = 0xFF; // set port B for output
TCCR1A = 0b00000000; // normal mode
TCCR1B = 0b11000001; // no pre-scaler, rising edge, noise canceller
TIMSK = 0b00100000; // enable Timer 1 input capture interrupt
sei(); // enable interrupt subsystem globally
while (1){ // infinite loop
PORTB = ~(period >> 8); // top 8-bit to PORT B: LED on=0, off=1
}
return 0;
}

You might also like