100% found this document useful (2 votes)
115 views55 pages

Topic 12 - ATMega32 Timer in C (ISMAIL - FKEUTM 2017)

This document discusses timers in microprocessors. It begins by introducing timers and their objectives of counting events and generating time delays. It describes the basic registers associated with timers like TCNT, TOV, TCCR, OCR, and OCF. It focuses on the programming of Timer0 in ATmega32 microcontrollers. It explains the functions of bits in the TCNT0 and TCCR0 registers. It provides examples of initializing Timer0 in normal mode to generate time delays and calculating the appropriate values to load into the TCNT0 register. It also discusses using the prescaler to increase the size of time delays that can be generated.

Uploaded by

Aya Amir
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
100% found this document useful (2 votes)
115 views55 pages

Topic 12 - ATMega32 Timer in C (ISMAIL - FKEUTM 2017)

This document discusses timers in microprocessors. It begins by introducing timers and their objectives of counting events and generating time delays. It describes the basic registers associated with timers like TCNT, TOV, TCCR, OCR, and OCF. It focuses on the programming of Timer0 in ATmega32 microcontrollers. It explains the functions of bits in the TCNT0 and TCCR0 registers. It provides examples of initializing Timer0 in normal mode to generate time delays and calculating the appropriate values to load into the TCNT0 register. It also discusses using the prescaler to increase the size of time delays that can be generated.

Uploaded by

Aya Amir
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/ 55

Topic 11: Timer

ISMAIL ARIFFIN
FKE UTM SKUDAI JOHOR
Introduction
• Timer’s objective

• Timer features

• Timer Registers
- Understand function of each bit

• Initialization
Introduction
o In micro-p, we use counter registers in order to count an event or
generate time delays.
o To count an event: connect the external event source to the clock pin of
the register. When an event occurs externally, the content of the counter
is incremented.

o To generate time delays: connect the oscillator to the clock pin of


counter. When oscillator ticks, the content of the counter is
incremented.
o The contents of counter register represents how may ticks have occurred
from the time we have cleared the counter.
Introduction
o Thus, to generate time delay (1st method): need to clear the counter at
the start time and wait until the counter reaches a certain number.
o Example: A micro-p speed is 1MHz, and the content of the counter
register increments one per microsecond. Thus, if we want a time delay
of 100μs, we should clear the counter and wait until it become 100.

o In micro-p, there is a flag for each of the counters.


o The flag is set when counter overflows.
o to generate time delay (2nd method): load the counter register and wait
until the counter overflow and the flag is set.
o Example: A micro-p speed is 1MHz, with an 8-bit counter register, if we
want a time delay of 3μs, load the counter register with $FD and wait
until the flag is set after 3 ticks.
First tick – content increment to $FE.
Second tick - content increment to $FF.
Third tick – it overflows (contents become $00) and the flag is set.
Introduction
o Every timer needs a clock pulse to tick. The clock source can be internal
or external:
o Internal clock source – the frequency of oscillator is fed into the timer as
time delay generation.
o External clock source – fee pulses through one of the AVRs pins as a counter.
o In ATmega32, there are three timer:
o Timer0: 8-bit
o Timer1: 16-bit
o Timer2: 8-bit
o These timers can be used as:
o Timers to generate time delay.
o Counters to count events happening outside the micro-p.
Programming Timers 0,1 and 2
Introduction
o Every timer needs a clock pulse to tick. The clock source can be internal
or external:
o Internal clock source – the frequency of oscillator is fed into the timer as
time delay generation.
o External clock source – fee pulses through one of the AVRs pins as a counter.
o In ATmega32, there are three timer:
o Timer0: 8-bit (short count/time limit)
o Timer1: 16-bit (long count/time limit)
o Timer2: 8-bit (short count/time limit)
o These timers can be used as:
o Timers to generate time delay.
o Counters to count events happening outside the micro-p.
Basic Registers of Timers
o In ATmega32, a total of five basic
registers that are associated with
timers.
o TCNTn (timer/counter) register.
o TOVn (Timer overflow) flag.
o TCCRn (timer/counter control)
register.
o OCRn (Output compare) register.
o OCFn (output compare) flag
o The TCNTn register is actually a
counter.
o Upon reset, TCNTn contains zero.
o Its counts up with each pulse.
o The contents of timers/counters can
be accessed by loading a value to
TCNTn or read its value.
Basic Registers of Timers
o The TOVn flag will be set (1),
whenever the timer overflows.
o The TCCRn register is used for
setting modes operation.
o E.g. we can specify Timer0 to work
as a timer or a counter by loading
proper value into TCRR0.
o The OCRn register is used to
compare the content of OCRn with
the content of TCNTn.
o When OCRn=TCNTn, the OCFn will
be set (1).
o These timer registers are located in
the I/O memory. Thus, we can read
or write using IN / OUT instructions.
Timer0 Programming

Important key to timer0 usage:


- Understand timer0 register bit function
TCNT0 Register
o Timer0 is 8-bit in ATmega32, thus TCNT0 is 8-bit wide also.

o TCNT0 is used as a counter for Timer0 to hold timer start value in


producing the required timer delay time for AVR application.
TCCR0 Register
o TCCR0 is an 8-bit register used for control of Timer0.
TCCR0 Registers
o CS02:CS00 (Timer0 clock source).
o These bits are used to choose the clock source.
o If CS02:CS00 = 000, the counter is stopped.
o If CS02:CS00 have value between 001 and 101, the oscillator is used as a
clock source and the Timer0 could acts as a timer – used as time delay
generation.
o If CS02:CS00 are 110 or 111, the external source clock is used and acts
as a counter.
o WGM01:WGM00
o These bits are use to determine the Timer0 modes: normal, phase
correct PWM, CTC and fast PWM.
TCCR0 Registers: Examples
TIFR Registers
o The TIFR (Timer/counter Interrupt Flag register) register contains the flag
of different timer.

o TOV0 (Timer0 Overflow).


o The flag is set when counter overflows. i.e. going from $FF to $00.
o When time rolls over from $FF to $00, TOV0 flag is set to 1 and it
remains set until the software clears it.
Normal Mode
o In Normal Mode – the content of the timer/counter increments with
each clock.
o It will counts up until it reaches its max of $FF.
o When it rolls over from $FF to $00, it sets high TOV0.
Steps to Program Timer0 in Normal Mode
1. Load TCNT0 register with the initial count value.
2. Load the value into TCCR0 register, determine which mode to be used
and the prescale option.
3. Monitor TOV0 to see if it raised. Get out from loop when TOV0 is high.
4. Stop timer by disconnecting the clock source, using following
instructions:

TCCR0 = 0x00 ;// timer stopped, normal mode

5. Clear TOV0 for the next round.


6. Go back to step 1 to load TCNT0 again.
Algorithm to program Timer Operation
in normal mode
• Set Starting count in TCNTx
• Start Timer which by setting prescaler
• Wait for TOVx bit (overflow bit) is set (while
TCNTx counts up in background).
• Stop Timer
• When TOVx bit is set, Clear TOVxbit by writing
1 to TOVx bit.
The code in C/C++ Language
• The function name is Delay250msUsingTimer0

void Delay250msUsingTimer0(void)
{
// Refer Section A.3(k)
TCNT0 = 255-243; //start TCNT0 with (255-243) i.e. 12
TCCR0 = (1<<CS02) | (1<<CS00); //Set Prescaler =1024
volatile uint8_t TIFRdata;
do //Wait
{
TIFRdata=TIFR&(1<<TOV0); //Read status of TOV0 bit in TIFR

} while (TIFRdata==0); //until TOV0==1


TCCR0= 0x00; // Stop Timer0
TIFR=TIFR|TIFRdata; //Clear TOV0 bit;
}
The code in C/C++ Language
• Calculation of TCNT0 ?
CPU Frequency= 1000000 Hz
Prescaler = 1024
Required delay time= 250ms

Timer0 count = (Delay time/ Timer Period


= Delay time/ (1 / (CPU_frequency/1024))
= (250 x10-3/ (1/ (1000000/1024))
= 12 (round-up)

TCNT0 = 255 – 243 = 12


Steps to Program Timer0 in Normal Mode

#define F_CPU 1000000


#include <avr/io.h>
void delay_t0(ungsigned char delayms))
void delay_timer0 (unsigned char delayms) { Int main() {
unsigned char TIFRdata; // PORTB.5 as output
unsigned char delaycount ; DDRB=DDRB | 1 << PB5 ;
delay count=round ((delayms/((1000/F_CPU)*1024))-1) // clear PORTB.5
TCNT0 = 255-delaycount ; // PORTB= 0b0000000 ;
// TIMER0, Normal mode, int clk, prescale 1024,start while (1) {
TCCR0 = 0x01 ; delay_timer0(250) ;//delay
do // toggle PORTB.5
TiFRdata =TIFR ; PORTB= PORTB ^ (1<<PC5) ;
while (TiFRdata==0) { }
TCCR0 = 0x00;// Stop Timer; return 1 ;
TIFR = TIFR | TIFRdata ; // clear TOVO }
}
}
Steps to Program Timer0 in Normal Mode
Steps to Program Timer0 in Normal Mode
Calculate Timer0 StartValue
Finding Values to be Loaded in The Timer
o Assuming the amount of timer delay needed is known. Find the values
needed for the TCNT0 register.
o The following steps can be used to calculate the values to be loaded into
TCNT0:
Finding Values to be Loaded in The Timer

void delaymsUsingTimer0(void)
{
TCNT0 = 256-50; //start TCNT0 with (256-50) i.e. 200
TCCR0 = (1<<CS00); //No prescaler
volatile uint8_t TIFRdata;
do //Wait
{
TIFRdata=TIFR&(1<<TOV0); //Read status of TOV0 bit in TIFR
} while (TIFRdata==0); //until TOV0==1
TCCR0= 0x00; // Stop Timer0
TIFR=TIFR|TIFRdata; //Clear TOV0 bit;
}
Prescaler and Generating a Large Time Delay
o Previously, the size of time delays depends on two factors:
a) The crystal frequency
b) The timer’s 8-bit register
o In which, the largest time delay only achieved by making TCNT0 zero.
What if that is not enough?
o Answer: use the prescaler option in TCCR0 to increase the delay by
reducing the period.
o The prescaler option allows us to divide the instruction clock by factor of
8 to 1024. (see Figure 9-5)
Prescaler and Generating a Large Time Delay
o Previously, with no prescaler enabled, the crystal oscillator freqequency
is directly fed into Timer0.
o If prescaler bit is enable, we can divide the clock before it is fed into
Timer0.
o The lower 3 bits of TCCR0 give the options of the number we can divide
by. i.e. 8, 64, 256 and 1024.
Prescaler and Generating a Large Time Delay
Prescaler and Generating a Large Time Delay
Prescaler and Generating a Large Time Delay
Prescaler and Generating a Large Time Delay
#include <avr/io.h>

void delay_timer0 () { int main() {


unsigned char TIFRdata; DDRB=DDRB | 1 << PB3 ;// PB3 as output
TCNT0 = 0x83 ; // PORTB= 0b0000000 ;// clear PORTB
// TIMER0, Normal mode, int clk while (1) {
// prescale 256,start PORTB= PORTB ^ (1<<PC5) ;// toggle
TCCR0 = 0x04 ; delay_timer0;
do }
TIFRdata=TIFR; return 1 ;
while (TIFRdata == 1<<TOV0); }
TCCR0 = 0x0 ; // Stop TIMER0
// clear TOVO
TIFR = TIFR | TiFRdata ;
}
Clear Timer0 on Compare Match (CTC) Mode
o The OCR0 register is used with CTC mode.

o As in the Normal mode, in the CTC mode, the timer is incremented with
a clock.
o But it counts up until the content of TNCT0 = OCR0 (compare match
occurs); then the timer will be cleared and OCF0 flag will be set.
o This OCF0 flag is located in the TIFR register.
Clear Timer0 on Compare Match (CTC) Mode

#include <avr/io.h>
void delay_t0 () { Int main() {
unsigned char TIFRdata; DDRB=DDRB | 1 << PB3 ;// PB3 as output
TCNT0 = 0x00 ; // PORTB= 0b0000000 ;// clear PORTB
OCR0 =0x09; // load OCR0 while (1) {
// TIMER0, CTC mode, int clk PORTB= PORTB ^ (1<<PC5) ;// toggle
//prescale 256,start delay_t0;
TCCR0 = 0x09 ; }
do ( return 1 ;
}
TIFRdata=TIFR;
} while (TIFRdata != 1<<OCF0) ;
TCCR0 = 0x0 ;// Stop TIMER0
// clear TOVO
TIFR = TIFR | TIFRdata;
}
Clear Timer0 on Compare Match (CTC) Mode
Clear Timer0 on Compare Match (CTC) Mode
Timer2 Programming
Timer2 Programming
o Timer2 is an 8-bit in ATmega32, therefore it works same way as Timer0.
o But there are two differences between Timer0 and Timer2:
o Timer2 can be used as a real time counter.
o In Timer2 more prescale option can be found.
Timer1 Programming
Timer1 Programming
o Timer1 is an 16-bit timer and has lots of capabilities.
o Since Timer1 is a 16-bit timer its could split into two bytes:
o TCNT1L – Timer1 Lower Byte.
o TCNT1H – Timer1 High byte

o Timer1 has two control registers, TCCR1A and TCCR1B.


o TOV1 flag goes HIGH when overflow occurs.
o Timer1 has prescaler option of 1:1, 1:8, 1:64, 1:256 and 1:1024
Timer1 Programming
o There are two OCR register in Timer1:
OCR1A and OCR1B
o Whenever TNCT1 = OCR1A, OCF1A will
goes HIGH.
o Whenever TNCT1 = OCR1B, OCF1B will
goes HIGH.
TIFR Register
o The TIFR register that associated with TImer1 are: TOV1, OCF1A and
OCF1B flags.
COM1A1 COM1A0 COM1B1 COM1B0 FOC1A FOC1B WGM11 WGM10 TCCR1A
TCCR1A & TCCR1B Register
ICNC1 ICES1 - WGM13 WGM12 CS12 CS11 CS10 TCCR1B

Clock Selector (CS)

CS12 CS11 CS10 Comment


0 0 0 No clock source (Timer/Counter stopped)
PSR10
0 0 1 clk (No Prescaling)
0 1 0 clk / 8 Clear
clkIO 10-bit T/C Prescaler
0 1 1 clk / 64

clk/8
clk/64
clk/256

clk/1024
1 0 0 clk / 256
1 0 1 clk / 1024 T1
1 1 0 External clock source on T0 pin. Clock on falling edge
1 1 1 External clock source on T00pin. Clock on rising edge

CS10 0 1 2 3 4 5 6 7
CS11
CS12

Timer/Counter1 clock
source
Timer1: Normal Mode
o Normal Mode (WGM13:10 = 0000).
o In Normal mode, the timer counts up until it reaches $FFFF (max) and
then it rolls over from $FFFF to $0000.
o When rollover, TOV1 flag will be set.
Timer1: CTC Mode
o CTC Mode (WGM13:10 = 0100).
o In CTC mode, the timer counts up until the content of the TCNT1 =
OCR1A (compare match occurs), then timer will be cleared when the
next clock occurs.
o OCF1A flag will be set.
Timer1: Example1
Timer1: Example1
Timer1: Example2
Timer1: Example2
Timer1: Generating Large Time Delay Using
Presacaler
o Again, The size of time delays depends on two factors:
a) The crystal frequency
b) The timer’s 16-bit register
o Use the prescaler option in TCCR1B to increase the delay by reducing
the period.
o The prescaler option allows us to divide the instruction clock by factor
of 8, 64, 256 and 1024.
Counter
Introduction
o The AVR timer can also be used to count, detect and measure the time
events happening outside the AVR.
o When the timer is used as a timer, the AVR’s crystal is used as the source
of the frequency.
o When the timer is used as a counter, the pulse outside the AVR that
increment the TCNTn.
o In Counter mode, registers such TCCR, OCR and TCNT are the same as for
timer discussed previously.
CS02:00 bits in TCCR0
o Recall back the CS02:00 in the TCCRn register decide the source of the
clock and prescale used.
o When CS02:00 is between 001 and 101, timer get pulse from the crystal
oscillator.
o When CS02:00 is between 110 and 111, timer is used as counter and get
pulses from source outside AVR chip.
o When CS02:00 is 110 or 111: TCNT0 counter counts up as pulses fed from
pin T0 (Timer/counter 0 External Clock Input).
CS02:00 bits in TCCR0
o In ATmega32, T0 is alternative function of PORTB.0
o When CS02:00 is 110 or 111: pin T0 provides the clock pulse and counter
counts up after each clock pulse coming from T0 pin.

o Similarly, for Timer1, when CS12:10 is 110 or 111: the clock pulse coming
in from pin T1 (PORTB.1) makes TCNT1 counter count up.
o when CS12:10 is 110: the counter count up on negative (falling) edge.
o when CS12:10 is 111: the counter count up on positive (rising) edge.
Counter: Example

You might also like