01c. Timer

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 66

Timer/Counter

rif T Rahmawanto

Definisi Timer & Counter

Timer ?
Counter?

Counter clock dan lebar bit conter


2

Definisi Timer & Counter (cont)

Timer dan counter pada AVR

Penghitung naik, bila sudah mencapai nilai maksimal,


akan roll over, saat roll over akan ada status (flag)
Nilai maksimal tergantung lebar bit

Beda Counter dan Timer pada AVR/MCU


Counter Sumber clock dari eksternal
Timer Sumber clock dari internal

Timer/Counter pada
AtMega8535

Ada 3 timer/counter

Timer 0, 8 Bit; Timer 1, 16 Bit; Timer 2, 8 Bit

Mode timer pada 8585

Normal
CTC (Clear Timer if Compare match)
PWM (Pulse Width Modulation)
Fast PWM

Sumber Clock (ada 8 pilihan, 2 eksternal, 5


internal, 1 tidak terhubung)

Timers: Why we need


them
Provide accurately timed delays or actions independent of code
execution time
How are Timers used?
Accurate delay
Read the timer, store value as K. Loop until timer reaches
K+100.
Schedule important events
Setup an Output Compare to trigger an interrupt at a precise
time
Port B pin3, PB3, when used as output port,
OC0 (Timer/Counter0 Output Compare Match Output)
(p.57 of Atmeg16 manual)
Measure time between events
When event#1 happens, store timer value as K
When event#2 happens, read timer value and subtract K
The difference is the time elapsed between the two events

Counter mode and Timer mode

There are counter registers in microcontrollers to


count an event or generate time delays.
When we connect the external event source to the
clock pin of the counter register. This is counter
mode.
When we connect the oscillator to the clock pin of
the counter. This is timer mode
one way to generate a time delay is to clear the
counter at the start time and wait until the counter
reaches a certain number.

Counter mode and Timer mode

In the microcontrollers, there is a flag for


each of the counters. The flag is set when
the counter overflows, and is cleared by
software.
The second method to generate a time delay is
to load the counter register and wait until
the counter overflows and the flag is set.
In ATmega32,ATmega16, there are three timers:
Timer0, Timer1, and Timer2. TimerO and Timer2
are 8-bit, while Timer1 is 16-bit.
In AVR, for each of the timers, there is a
TCNTn (timer/counter) register. In ATmega32
we have TCNTO, TCNT1, and TCNT2.
The TCNTn register is a counter. Upon reset,
the TCNTn contains zero. It counts up with
each pulse. The contents of the timers/
counters can be accessed using the TCNTn. You
can load/read a value into the TCNTn register

Basic Registers of Timers

Each timer has a TOVn


(Timer Overflow) flag,
When a timer overflows,
its TOVn flag is set.
Each timer also has the
TCCRn (timer/counter
control register)
register for setting
modes(timer or counter)
of operation.
Each timer also has an
OCRn (Output Compare
Register). The content of
the OCRn is compared with
the content of the TCNTn.
When they are equal the
OCFn (Output Compare
Flag) flag is set.

Timer0 Programming

Timer0 is 8-bit in ATmega32; thus, TCNT0 (timer/


counter register) is 8-bit.

TCCRO (Timer/Counter Control Register) register


is an 8-bit register used for control of Timer0.
CS02:CS00 (Timer0 clock source)
These bits in the TCCRO register are used to
choose the clock source. If CS02:CS00 = 000, then
the counter is stopped. If CS02:CS00 have values
between 001 and 101, the oscillator is used as
clock source and the timer/counter acts as a
timer. In this case, the timers are often used
for time delay generation. If CS02:CS00 are 110
or 111, the external clock source is used and it
acts as a counter

WGM01:WGM00
Timer0 can work in four
phase correct PWM, CTC,
and WGMOO bits are used
We will discuss the PWM

different modes: Normal,


and Fast PWM. The WGM01
to choose one of them.
options in Chapter 16.

TIFR (Timer/counter Interrupt Flag


Register)

The TIFR contains the flags of different timers.

Timer0 Programming
TOV0 (Timer0 Overflow)
The flag is set when the timer rolls over from
FF to 00. The TOV0 flag is set to 1 and it
remains set until the software clears it.
The strange thing about this flag is that in
order to clear it we need to write 1 to it.
Indeed this rule applies to all flags of the AVR
chip. In AVR, when we want to clear a given flag
of a register we write 1 to it and 0 to the
other bits.
Normal mode
In this mode, the content of the timer/counter
increments with each clock. It counts up until
it reaches its max of FF. When it rolls over
from FF to 00, it sets high a flag bit called
TOV0 (Timer Overflow).

Steps to program Timer0 in


Normal mode
1. Load
2.

3.
4.
5.
6.

the TCNT0 register with the initial count value


(TCNT = 100H xxH, banyaknya counter).
Load the value into the TCCR0 register, indicating
which mode (8-bit or 16-bit) is to be used and the
prescaler option. When you select the clock source,
the timer/counter starts to count, and each tick
causes the content of the timer/counter to increment
by 1.
Keep monitoring the timer overflow flag (TOVO) to
see if it is raised. Get out of the loop when TOVO
becomes high.
Stop the timer by disconnecting the clock source,
using the following instructions:
Clear the TOVO flag for the next round.
Go back to Step 1 to load TCNT0 again.

Timer0 Programming
// toggle bits of PORTB continuously with some delay.
#include <avr/io.h>
void T0Delay ();
int main (){
DDRB = 0xFF;
// PORTB output port
PORTB = 0x55;
while(1){//repeat forever
PORTB = ~PORTB;
T0Delay();
//delay size unknown
}
}
void T0Delay(){
TCNT0 = 0x20;
// load TCNT0 (RUN AGAIN FOR TCCR0=2)
TCCR0 = 0x01;
// Run Timer0, Normal mode, no prescaler
while ((TIFR & 0x01)==0); // wait for TF0 to roll over
TCCR0 = 0;
// Stop Timer
TIFR = 1;
//clear TF0
}

Timer0 Programming
Calculating Delay Length of Previous Program Assume XTAL
= 8 MHz.
Sol: XTAL = 8MHz Clock Period = 1/8 MHz
= 0.125 s
Timer Counts = (1+0xFF- 0x20) = 0xE0 = 224
Delay = 224 0.125 s = 28 s
Note: There will also be an Overhead Due to Instructions

Timer0 Programming

Timer0 Programming
Calculating Delay Length Using Timers
Q

Write a C program to toggle only the


PORTB.4 bit continuously every 70 s. Use
TimerO, Normal mode, and 1:8 prescaler to
create the delay. Assume XTAL = 8 MHz.
Solution:
XTAL = 8MHz Tmachine Cycle = 1/8 MHz
Prescaler = 1:8 TClock = 81/8MHz = 1s
70s/1s = 70 clocks
(1 + 0xFF) 70
= 256 - 70 = 186 = 0xBA = -70

Timer0 Programming
// toggle bits of PORTB continuously with some delay.
#include <avr/io.h>
void T0Delay ();
int main (){
DDRB = 0xFF;
// PORTB output port
while(1){//repeat forever
PORTB = 0x55;
T0Delay();
//delay size unknown
PORTB = 0xAA;
T0Delay();
}
}
void T0Delay(){
TCNT0 = -70;
// load TCNT0
TCCR0 = 0x02;
// Run Timer0, Normal mode,1:8 prescaler
while ((TIFR & 0x01)==0); // wait for TF0 to roll over
TCCR0 = 0;
// Stop Timer
TIFR = 1;
//clear TF0
}

Timer0 Programming

Timer0 Programming
// toggle bits of PORTB continuously with some delay.
#include <avr/io.h>
void T0Delay ();
int main (){
DDRB = 0xFF;
// PORTB output port
while(1){ //repeat forever
PORTB = 0x55;
T0Delay(); //delay size unknown
PORTB = 0xAA;
T0Delay();
}
}
void T0Delay(){
TCNT0 = 0;
// timer starts from 00
OCR0 = 124; // Output Compare Register has value 124
TCCR0 = 0x0B; // Run Timer0,(CTC)Clear Timer on Compare Match,
// 1:64 Prescaler
while ((TIFR & 0x02)==0); // wait for OCF0 to roll over
TCCR0 = 0;
// Stop Timer
TIFR = 0x02;
//clear TF0
} // In Theory Delay = (124+1)*8 us = 1ms (for 8MHz Clock)

CTC

Timer2 Programming

Timer2 is an 8-bit timer. Therefore it


works the same way as Timer0. But there are
two differences between Timer0 and Timer2:
1.Timer2 can be used as a real time counter.
To do so, we should connect a crystal of
32.768 kHz to the TOSCl and TOSC2 pins of
AVR and set the AS2 bit.
2.In Timer0, when CS02-CS00 have values 110
or 111, Timer0 counts the external events.
But in Timer2, the multiplexer selects
between the different scales of the clock.
In other words, the same values of the CS
bits can have different meanings for Timer0
and Timer2.

Timer2 Programming

Timer2 Programming

Timer2 Programming
#include <avr/io.h>
void T2Delay ();
int main (){
DDRB = 0xFF;
// PORTB output port
while(1){ // repeat forever
PORTB = 0x55; T2Delay();
PORTB = 0xAA; T2Delay();
}
}
void T2Delay(){
TCNT2 = 0;
// timer starts from 00
OCR2 = 249;
// Output Compare Register has value 249
TCCR2 = 0x0E; //Run Timer2,Clear Timer on Compare Match,
// Prescaler mode 1:256
while ((TIFR & (1<<OCF2))==0); // wait for OCF2 flag
TCCR2 = 0;
// Stop Timer
TIFR = (1<<OCF2);
//clear OCF2 flag
}

Timer1 Programming
Timer1

is a 16-bit timer and it is split into two


bytes, TCNT1L (Timer1 low byte) and TCNT1H (Timer1
high byte).
Timer1 has two control registers named TCCR1A
(Timer/counter 1 control register) and TCCR1B.
The TOV1 (timer overflow) flag bit goes HIGH when
overflow occurs.
Timer1 also has the prescaler options.
There are two 16-bit OCR registers in Timerl:
OCR1A and OCR1B. There are two separate flags for
each of of Timerl OCR registers, which act
independently of each other.
Whenever TCNT1 equals OCR1A, the OCF1A flag will
be set on the next clock. When TCNT1 equals 0CR1B,
the OCF1B flag will be set on the next clock.

Timer1 Programming

Timer1 Programming
As

Timer1 is a 16-bit timer, the OCR registers


are 16-bit registers. OCRIA is made of OCR1AH
(OCRIA high byte) and OCR1AL (OCRIA low byte).
The TIFR register contains the TOV1, OCF1A, and
OCF1B flags.
There is also an auxiliary register named ICR1,
which is used in operations such as capturing.
ICR1 is a 16-bit register made of ICR1H and
ICR1L

Timer1 Programming
As

Timer1 is a 16-bit timer, the OCR registers


are 16-bit registers. OCRIA is made of OCR1AH
(OCRIA high byte) and OCR1AL (OCRIA low byte).
The TIFR register contains the TOV1, OCF1A, and
OCF1B flags.

Timer1 Programming

Timer1 Programming
WGM13:WGM10
The WGM13, WGM12, WGM11, and WGM10 bits define
the mode of Timerl, Timerl has 16 different
modes. One of them (mode 13) is reserved (not
implemented). we cover mode0 (Normal mode) and
mode4 (CTC mode). The other modes will be
covered in Chapters 15 and 16.
Timerl operation modes
Normal mode (WGM13:10 = 0000)
In this mode, the timer counts up until it
reaches 0xFFFF (which is the maximum value) and
then it rolls over from 0xFFFF to 0000.
When the timer rolls over from 0xFFFF to 0000,
the TOV1 flag will be set.

Timer1 Programming
CTC (Clear Timer on Compare Match) mode
(WGM13:10 = 0100)
In mode 4, the timer counts up until the content
of the TCNT1 register becomes equal to the
content of OCR1A (compare match occurs); then,
the timer will be cleared when the next clock
occurs. The OCF1A flag will be set as a result
of the compare match as well

Timer1 Programming

Timer1 Programming

Timer1 Programming
#include <avr/io.h>
void T1Delay();
int main (){
DDRB = 0xFF; // PORTB output port
while(1){
// repeat forever
PORTB = 0x55; T1Delay();
PORTB = 0xAA; T1Delay();
}
}
void T1Delay(){
TCNT1 = 0;
// timer starts from 00
OCR1A = 31250 - 1; // Output Compare Register A
TCCR1A = 0x00; TCCR1B = 0x0C; //Run Timer1, Clear
// Timer on Compare Match, Prescaler mode 1:256
while ((TIFR & (1<<OCF1A))==0);//wait for OCF1A TCCR1B =
0x00;
// Stop Timer
TIFR = (1<<OCF1A);
//clear OCF2 flag
}

Timer1 Programming

16-bit counter Programming


#include <avr/io.h>
int main (){
PORTB
= 0x01;
DDRC
= 0xFF;
DDRD
= 0xFF;
TCCR1A
TCCR1B

= 0x00;
= 0x06;

// activate pull-up of PB0


// PORTC as output
// PORTD as output
// 16-bit Normal mode
// at T1 Falling Edge Counter

TCNT1H = 0x00;
// Set initial Count to 0
TCNT1L = 0x00;
// Set initial Count to 0
while(1){
PORTD = TCNT1L;
PORTC = TCNT1H;
if((TIFR & (1<<TOV1))==1) // if it overflows
TIFR = 1<<TOV1;
// clear TOV1 flag
}
}

49

50

Timers: Why we need


them
Provide accurately timed delays or actions independent of code
execution time
How are Timers used?
Accurate delay
Read the timer, store value as K. Loop until timer reaches
K+100.
Schedule important events
Setup an Output Compare to trigger an interrupt at a precise
time
Port B pin3, PB3, when used as output port,
OC0 (Timer/Counter0 Output Compare Match Output)
(p.57 of Atmeg16 manual)
Measure time between events
When event#1 happens, store timer value as K
When event#2 happens, read timer value and subtract K
The difference is the time elapsed between the two events

51

AVR Timer/Counter 0
8 Bit
Wrap-Around
Up Counter
Interrupt on
overflow

52

AVR Timer/Counter 0
8 Bit Up Counter
counts from 0 to 255 (0xFF), then loops to 0
Internal or External Clock source
Prescaler
Output capture through OC0, i.e. PB3, pin 4
Interrupt on Overflow
Transition from 255 to 0 can trigger interrupt if
desired
53

AVR Timer/Counter 0

OC0, Output Compare Match output:


Whenever TCNT0 equals OCR0 (Output Compare

Register 0), the comparator signals a match


The PB3 pin can serve as an external output for
the Timer/Counter0 Compare Match. The PB3 pin
has to be configured as an output

54

AVR Timer/Counter 1

16 Bit
Dual Comparators A,B (output compares)
Up Counter
Interrupt on:
Overflow
Compare A/B
Input Capture of external event on ICP pin.
Can also act as an 8, 9 or 10 bit PWM UpDown Counter.
55

AVR Timer/Counter 1

The Input Capture unit of Timer/Counter captures

external events and gives them a time-stamp


indicating time of occurrence.
The external signal indicating an event, or multiple
events, can be applied via the ICP1 pin or
alternatively, via the Analog Comparator unit.
The time-stamps can then be used to calculate
frequency, duty-cycle, and other features of the signal
applied.
Alternatively the time-stamps can be used for creating
a log of the events.
56

Timer 1 and Output


Compare
The AVR has two output compares (OCR1A/B)
OCR1A/B are 16-bit registers
When the value of OCR1A/OCR1B matches that of Timer1:
A user-defined action can take place on the OC1A/OC1B pin
(set/clear/inv) i.e.,PD5 /PD4 need to set as output
An interrupt can be triggered
Timer1 can be cleared to zero
Once set up, output compares operate continuously without
software intervention
Great for:
Precise recurring timing
Frequency/Tone generation (maybe sound effects)
All kinds of digital signal generation
Infrared communications
Software-driven serial ports
57

Timer 1 and PWM


Pulse-Width Modulation
Useful for using digital circuits to achieve analoglike control of motors, LEDs, etc
Timer 1 has two channels of PWM output on OCR1A
and OCR1B

58

Timer Control
Timer 0:
Control Register (TCCR0) for clock selection, external
clock or internal clock, prescaler etc.
Timer/Counter0 (TCNT0) holding counter value
Timer 1:
Control Register A & B (TCCR1A/B)
Input Capture Register (ICR1)
Timer/Counter1 Output Compare Register A and B
(OCR1A/B)
Timer/Counter1 (TCNT1)
Timer Interrupt Registers (Mask and Flag Registers) are
Common to Both Timers
59

AVR Timer/Counter
Sources

Shut Off
CPU frequency divided by 1,8,64,256,1024
At 8MHz thats: 1/8us, 1us, 8us, 32us, 128us
External Input (rising or falling).

60

61

Delay

Hardware delay

Menggunakan internal timer/counter

Software delay

Mengulang instruksi sampai beberapa kali


Ada instruksi yg tidak menghasilkan efek apapun tetapi
dikerjakan selama 1 siklus clock NOP
Perlu tahu setiap instruksi dikerjakan berapa clock
1 clock tergantung frekuensi yang digunakan
1 periode = 1/fxtal
Misal menggunakan xtal 4 MHz maka 1 clock periodenya
T = 1/(4.106) = . 10-6 = 0,25 us

PR.

Membuat program untuk menunda selama 0,1 detik


dengan menggunakan xtal 8 MHz

Ada hitungannnya (toleransi < 0,0001)


0,1 detik = 100ms =100 000us
12Mhz periode clock = 1/12MHz = 0,083us
Total perulanga 100 000/0,083 =

Frekuensi kristal 8 MHz

Periode = 1/8 Mhz = 1/8 us =0,125


Timer lebar 8 bit 28 = 256 0 255 = 00 FF
Tanpa prascalar maka waktu tunda maksimalnya =
256 x 0,125us = 32us
Prescalar 1/8 8 x 256 x 0,125us = 256 us
Prescalar 1/64 64 x 256 x 0,125us = 2024 us = 2,024ms
8ms = 8000 us
Tanpa preskalar jml cacahan = 8000/0,125 = 640000
Prescalar 1024 jml cacahan = 8000/0,125/1024 = 62,5
Cacahan = 62 waktu cacahan = 1024 x 62 x0,125 = 7936us
64

8ms menggunakan preskalar 1/1024


Cacahan 62 7936 kurang 64us
Timer 64 us
Preskalar 1/8 jml cacahan = 64
PR

Buatlah program tunda menggunakan timer 0 atau 2,


mode normal atau CTC. Besar tunda sesuai dengan
no mahasiswa (dalam us). fxtal = bebas
Dikumpulkan dalam bentuk softcopy (dapat dikirm ke
email [email protected]) paling lambat selasa 4
des 2012 jam 24:00. Selain program jelaskan
perhitungannya.
65

Pr

Bualah subroutine program untuk menunda selama


no mhs (udetik). Frekuensi kristal 8 Mhz.
Tunda menggunakan software delay
Ada perhtungannya.

66

You might also like