01c. Timer
01c. Timer
01c. Timer
rif T Rahmawanto
Timer ?
Counter?
Timer/Counter pada
AtMega8535
Ada 3 timer/counter
Normal
CTC (Clear Timer if Compare match)
PWM (Pulse Width Modulation)
Fast PWM
Timer0 Programming
WGM01:WGM00
Timer0 can work in four
phase correct PWM, CTC,
and WGMOO bits are used
We will discuss the PWM
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).
3.
4.
5.
6.
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
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 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
Timer1 Programming
Timer1 Programming
As
Timer1 Programming
As
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
= 0x00;
= 0x06;
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
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
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
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
Software delay
PR.
Pr
66