0% found this document useful (0 votes)
314 views41 pages

Timer/counter: The AVR Microcontroller and Embedded Systems Using Assembly and C

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

Timer/counter: The AVR Microcontroller and Embedded Systems Using Assembly and C

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

Timer/counter

The AVR microcontroller


and embedded
systems
using assembly and c

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
A counter register

UP Counter Register Load


COUT

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
A simple design (counting people)
First design

Sensor UP Counter Register Load


COUT 4

Comparator
=

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
A simple design (counting people)
Second design

Sensor UP Counter Register Load


COUT

$FC

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
A simple design (making delay)

UP Counter Register Load


COUT

$FC

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
A generic timer/counter
 Delay generating
 Counting
 Wave-form generating
 Input Capturing (to calculate the freq of an input
signal)

Oscillator 0
Counter register
COUT
External 1
source
Flag
Counter/Timer

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Timers in AVR
 1 to 6 timers
 3 timers in ATmega32
 8-bit and 16-bit timers
 two 8-bit timers and one 16-bit timer in ATmega32

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Timer in AVR
Oscillator
 TCNTn (Timer/Counter register) External
Counter register

source
 TOVn (Timer Overflow flag) Counter/Timer
Flag

 TCCRn (Timer Counter control register)


 OCRn (output compare register) TCCRn
 OCFn (output compare match flag)
TCNTn

TOVn
= OCFn
Comment:
Comment:
All
Allofofthe
thetimer
timerregisters
registersare
are
byte-addressable
byte-addressableI/O I/Oregisters
registers OCRn

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Timer 0 (an 8-bit timer)

The AVR microcontroller


and embedded
systems
using assembly and c

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Timer 0

TCCR0

TCNT0

TOV0
= OCF0

OCR0

OCF2 TOV2 ICF1 OCF1A OCF1B TOV1 OCF0 TOV0 TIFR

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
FOC0 WGM00 COM01 COM00 WGM01 CS02 CS01 CS00 TCCR0

Clock Mode
Timer Selector
(WGM)
(CS)

CS02 CS01 CS00 Comment


0 0 0 No clock source (Timer/Counter stopped)
WGM00 WGM01 Comment
0 0 1 clk (No Prescaling)PSR10
0 0 Normal
0 1 0 clk / 8
0 1 CTC (Clear Timer on Compare Match)Clear
0 1 1 clk / 64 clkIO 10-bit T/C Prescaler
1 0 PWM, phase correct
1 0 0 clk / 256
1 1 Fast PWM

clk/64
clk/8

clk/256

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

CS00 0 1 2 3 4 5 6 7
CS01
CS02

Timer/Counter0 clock
source
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Normal mode
1. Load the TCNT with 256-N:
TCNT0 = 256-N TCNT0
2. Start the timer – by loading a
value into TCCR : TCCR0 = X
0xFF
3. Monitor the TOV flag
TOV TOV TOV
1. Once the TOV becomes high, it time
indicates the timer has finished 0

FF
FE TOV0: 0
1

2
1 TOV0 = 1
0
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Example 1: Write a program that waits 14 machine cycles in
Normal mode to generate a square wave.

UP Counter Register Load


14 = $0E COUT

$100 8
256-14 =242
-$0E $F2
242,243,244,….,255, 0
$F2

FOC0 WGM00 COM01 COM00 WGM01 CS02 CS01 CS00 TCCR0

0 0 0 0 1
PSR10

WGM00 WGM01 Comment clkIO


Clear
10-bit T/C Prescaler

0 0 Normal

clk/64
clk/8

clk/256

clk/1024
0 1 CTC T0
0
1 0 PWM, phase correct
1 1 Fast PWM CS00 0 1 2 3 4 5 6 7
CS01
CS02

Timer/Counter0 clock
source
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Square wave on pin 5 of port B

14 cycles 14 cycles

TCNT0 = 256-14 = 242 = $F2


TCCR0 = $01
SBIC/SBIS – for IO registers

SBRS/SBRC – for GPRs

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Example 1: write a program that waits 14 machine cycles in
Normal mode.
$100 FOC0 WGM00 COM01 COM00 WGM01 CS02 CS01 CS00 TCCR0

-$0E OCF2 TOV2 ICF1 OCF1A OCF1B TOV1 OCF0 TOV0 TIFR

$F2
.INCLUDE "M32DEF.INC"

LDI R16,0x20 ;0b00100000 DDRB = 1<<5;


SBI DDRB,5 ;PB5 as an output PORTB &= ~(1<<5); //PB5=0
CBI PORTB,5
BEGIN: LDI R20,256-14 ; 242 = $F2 while (1)
OUT TCNT0,R20 ;load timer0 {
LDI R20,0x01
OUT TCCR0,R20 ;Timer0,Normal mode,int clk TCNT0 = 0xF2;
AGAIN: IN R20,TIFR Question:
Question:
;read TIFRHow
Howto tocalculate
calculatethe
TCCR0the
delay
delayininseconds
= 0x01; seconds
SBRS R20,0 ;if TOV0 generated
is set skipby
generated the
next
by timer?
inst.
the timer?while((TIFR&(1<<TOV0))==0);
RJMP AGAIN
LDI R20,0x0 TCCR0 = 0;
OUT TCCR0,R20 Answer:
Answer:
;stop Timer0
LDI ;R20 = 0x01how much aTIFR
R20,(1<<TOV0) 1) Calculate = (1<<TOV0);
machine clock lasts.
OUT TIFR,R20 1);clear
Calculate
TOV0 how
flag much a machine clock lasts.
PORTB = PORTB^(1<<5);
1<<TOV0 = 1<<0 = 0b00000001 T=
T=1/f
1/f
EOR R17,R16 2) Calculate
;toggle D5 how
of R17many }machine clocks it waits.
OUT PORTB,R17
2);toggle
Calculate
PB5
how many machine clocks it waits.
RJMP BEGIN 3)
3)Delay
Delay==TT**number
numberof ofmachine
machinecycles
cycles
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
256-14 =242 TCNT0 = 256-78
TCCR0 = 00000011

Square wave on pin 5 of port B T = 6.4 uS


F = 1/T = 156.25 kHz

0.5 ms

0.5 ms

Generate a square wave of 1 ms time period. CLK = 10 MHz. Use 8 bit timer.
Td = 1 ms /2 = 0.5 ms

Clock cycles, N = Td/Tclk = 0.5 ms / 0.1 us = 500 us/0.1 us = 5000 cycles


Clk/8: N = 5000/8 = 625
Clk/64: N = 5000/64 = 78.13 ~ 78 = 4992 cc instead of 5000
Clk/256: N = 5000/256 = 19.53 = ~20 = 5120 cc
Clk/1024: N = 5000/1024 = 4.88 ~ 5 = 5120

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Write a program using a timer to generate a square wave of 200 kHz.
CLK = 10 MHz, Tclk = 0.1 uS

T = 1/200k = 5 us
Ton = 2.5 us, Toff = 2.5 us

N = Td/Tclk = desired time period / clk time period

N = 2.5 us/0.1 us = 25

TCNT0 = 256-N = 231


TCCR0 = $01

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
In example 1 calculate the delay. Imagine XTAL = 10 MHz.

Solution 1 (inaccurate): .INCLUDE "M32DEF.INC"

1) Calculating T: LDI R16,0x20


SBI DDRB,5 ;PB5 as an output
T = 1/f = 1/10M = 0.1µs LDI R17,0
OUT PORTB,R17
2) Calculating num of BEGIN: LDI R20,256-14
OUT TCNT0,R20 ;load timer0
machine cycles: LDI R20,0x01
OUT TCCR0,R20 ;Timer0,Normal mode,int clk
$100 AGAIN: IN R20,TIFR ;read TIFR
SBRS R20,0 ;if TOV0 is set skip next inst.
-$F2
RJMP AGAIN
$0E = 14 LDI R20,0x0
OUT TCCR0,R20 ;stop Timer0
3) Calculating delay LDI R20,0x01
OUT TIFR,R20 ;clear TOV0 flag
14 * 0.1µs = 1.40 µs
EOR R17,R16 ;toggle D5 of R17
OUT PORTB,R17 ;toggle PB5
RJMP BEGIN
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Accurate calculation
Other than timer, executing the instructions consumes time; so if we
want to calculate the accurate delay a program causes we should add
the delay caused by instructions to the delay caused by the timer
LDI R16,0x20
SBI DDRB,5
LDI R17,0
OUT PORTB,R17
BEGIN: LDI R20,0xF2 1
OUT TCNT0,R20 1
LDI R20,0x01 1
OUT TCCR0,R20 1
AGAIN: IN R20,TIFR 1
SBRS R20,0 1/2
RJMP AGAIN 2
LDI R20,0x0 1
OUT TCCR0,R20 1
LDI R20,0x01 1
OUT TIFR,R20 1
EOR R17,R16 1
OUT PORTB,R17 1
RJMP BEGIN 2
18
Delay caused by timer = 14 * 0.1µs = 1.4 µs Delay caused by instructions = 18 * 0.1µs = 1.8
Total delay = 3.2 µs  wave period = 2*3.2 µs = 6.4 µs  wave frequency = 156.25 KHz

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Finding values to be loaded into the timer
1. Calculate the period of clock source.
 Period = 1 / Frequency
 E.g. For XTAL = 8 MHz  T = 1/8MHz = 125 ns
2. Divide the desired time delay by period of
clock, N = Td/Tclk
3. If N > 255, use a pre-scaled clock
4. Perform 256 - N, where N is the decimal
value we got in Step 2.
5. Set TCNT0 = 256 - N
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Example 2: Assuming that XTAL = 10 MHz, write a program to generate a
square wave with a period of 10 us (= 100 kHz) on pin PORTB.3.
 For a square wave with T = 10 µs we must have a time delay of 5 µs. Because
XTAL = 10 MHz, the counter counts up every 0.1 µs. This means that we need 5
µs / 0.1 µs = 50 clocks. 256 - 50 = 206.
.INCLUDE "M32DEF.INC" DDRB = 1<<3;
PORTB &= ~ (1<<3);
LDI R16,0x08
SBI DDRB,3 ;PB3 as an output while (1)
LDI R17,0
{
OUT PORTB,R17
BEGIN: LDI R20,206 TCNT0 = 206;
OUT TCNT0,R20 ;load timer0
LDI R20,0x01 TCCR0 = 0x01;
OUT TCCR0,R20 ;Timer0,Normal mode,int clk while((TIFR&0x01) == 0);
AGAIN: IN R20,TIFR ;read TIFR
SBRS R20,TOV0 ;if TOV0 is set skip next TCCR0 = 0;
RJMP AGAIN TIFR = 1<<TOV0;
LDI R20,0x0
OUT TCCR0,R20 ;stop Timer0 PORTB = PORTB ^ (1<<3);
LDI R20,0x01 }
OUT TIFR,R20 ;clear TOV0 flag
EOR R17,R16 ;toggle D3 of R17
OUT ;toggle PB3
RJMP BEPORTB,R17GIN

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Example 3: Modify TCNT0 in Example 2 to get the largest time delay possible with no prescaler.
Find the delay in µs. In your calculation, do not include the overhead due to instructions.

 To get the largest delay we make TCNT0 zero. This will


count up from 00 to 0xFF and then roll over to zero.
.INCLUDE "M32DEF.INC"
DDRB = 1 << 3;
LDI R16,1<<3
SBI DDRB,3 ;PB3 as an output PORTB &= ~(1<<3);
LDI R17,0
OUT PORTB,R17 while (1)
BEGIN: LDI R20,0x0 {
OUT TCNT0,R20 ;load Timer0
LDI R20,0x01 TCNT0 = 0x0;
OUT TCCR0,R20 ;Timer0,Normal mode,int clk TCCR0 = 0x01;
AGAIN: IN Solution
R20,TIFR ;read TIFR
SBRS R20,TOV0 ;if TOV0 is set skip next
while((TIFR&(1<<TOV0))==0);
RJMP 1) Calculating
AGAIN T:
LDI R20,0x0 TCCR0 = 0;
OUT T = TCCR0,R20
1/f = 1/10MHz = 0.1µs
;stop Timer0
LDI R20,0x01 TIFR = 0x01;
OUT 2) Calculating
TIFR,R20 delay;clear TOV0 flag PORTB = PORTB^(1<<3);
EOR R17,R16 ;toggle D3 of R17
OUT 256 * 0.1µs = 25.6µs
PORTB,R17 ;toggle PB3 }
RJMP BEGIN

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
For an 8-bit timer, what is the maximum delay that can be generated for
CLK = 10MHz?

Tclk = 0.1 us

N = 256 => 25.6 us

Slowest clock = clk/1024 >>> Tclk = 0.1 us x 1024 = 102.4 us

0.1 x 1024 = 102.4 us for one clock cycle using a pre-scaled clock

Max. delay = 256 x 102.4 us ~ 250 ms

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Generating Large Delays
 Using loop
 Pre-scaled clock
 Bigger counters

16-bit timer, N = 65536

Tclk = 1/f x 1024 = largest clock time period with prescaling

Max. delay = N * Tclk_p

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Prescaler and generating a large time delay
PSR10

Clear
clkIO 10-bit T/C Prescaler

clk/8
clk/64
clk/256

clk/1024
T0
0

CS00 0 1 2 3 4 5 6 7
CS01
CS02

Timer/Counter0 clock
source
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
CTC (Clear Timer on Compare match) mode
TCNT0
0xFF
OCR0

OCF0 OCF0 OCF0


0 time

OCR0 = 99 0
xx TOV0:

2 0
1
OCF0:
1 TOV0 = no change
0
OCF0 = 1
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Timer in Normal Mode Timer in CTC Mode
1.Load the TCNT with 256-N: 1.Load the OCR with N-1: OCR0=N-

TCNT0 = 256-N or –N 1; 0 to N-1 = N cycles


LDI R16,-50 ; (256-50) 2.Start the timer – by loading a

OUT TCNT0,R16 value into TCCR : TCCR0 = X


2. Start the timer – by loading a WGM = 01
value into TCCR : TCCR0 = X Monitor the OCF0 flag
3.

WGM=00 Once the OCF0 becomes high, it


1.

indicates the timer has finished


3. Monitor the TOV0 flag
1. Once the TOV0 becomes high, it
indicates the timer has finished

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Rewrite
Example example
4: Rewrite 2 using
example CTC
2 using CTC
FOC0 WGM00 COM01 COM00 WGM01 CS02 CS01 CS00 TCCR0

OCF2 TOV2 ICF1 OCF1A OCF1B TOV1 OCF0 TOV0 TIFR


 For a square wave with T = 10 µs we must have a time delay of 5 µs. Because
XTAL = 10 MHz, the counter counts up every 0.1 µs. This means that we need 5
µs / 0.1 µs = 50 clocks. Therefore, we have OCR0= 49 (N-1).
.INCLUDE "M32DEF.INC" DDRB |= 1<<3;
LDI R16,0x08
PORTB &= ~(1<<3);
SBI DDRB,3 ;PB3 as an output
LDI R17,0 while (1)
OUT PORTB,R17
{
LDI R20,49
OUT OCR0,R20 ;load timer0 OCR1 = 49;
BEGIN: LDI R20,0x09
OUT TCCR0,R20 ;Timer0,CTC mode,int clk TCCR0 = 0x09;
AGAIN: IN R20,TIFR ;read TIFR
SBRS R20,OCF0 ;if OCF0 is set skip next while((TIFR&(1<<OCF0))==0);
RJMP AGAIN
LDI R20,0x0 TCCR0 = 0; //stop timer0
OUT TCCR0,R20 ;stop Timer0 TIFR = 0x02;
LDI R20,0x02
OUT TIFR,R20 ;clear TOV0 flag PORTB.3 = ~PORTB.3;
EOR R17,R16 ;toggle D3 of R17 }
OUT PORTB,R17 ;toggle PB3
RJMP BEGIN
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Timer2
 Timer0  Timer2

TCCR0 TCCR2

TCNT0 TCNT2

TOV0 TOV2
= OCF0 = OCF2

OCR0 OCR2

OCF2 TOV2 ICF1 OCF1A OCF1B TOV1 OCF0 TOV0 TIFR


AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
The difference between Timer0 and Timer2
 Timer0  Timer2

CS02 CS01 CS00 Comment CS22 CS21 CS20 Comment


0 0 0 Timer/Counter stopped 0 0 0 Timer/Counter stopped
0 0 1 clk (No Prescaling) 0 0 1 clk (No Prescaling)
0 1 0 clk / 8 0 1 0 clk / 8
0 1 1 clk / 64 0 1 1 clk / 32
1 0 0 clk / 256 1 0 0 clk / 64
1 0 1 clk / 1024 1 0 1 clk / 128
1 1 0 External clock (falling edge) 1 1 0 clk / 256
1 1 1 External clock (rising edge) 1 1 1 clk / 1024
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Timer 1
OCR1BH OCR1BL

= OCF1B

TCCR1A TCCR1B TCNT1H TCNT1L

TOV1 = OCF1A

OCR1AH OCR1AL

OCF2 TOV2 ICF1 OCF1A OCF1B TOV1 OCF0 TOV0 TIFR

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
COM1A1 COM1A0 COM1B1 COM1B0 FOC1A FOC1B WGM11 WGM10 TCCR1A

ICNC1 ICES1 - WGM13 WGM12 CS12 CS11 CS10 TCCR1B

Clock Selector (CS)

CS12 CS11 CS10 Comment PSR10


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

clk/8

clk/64
clk/256

clk/1024
0 1 1 clk / 64
1 0 0 clk / 256 T1
1 0 1 clk / 1024 0
1 1 0 External clock source on T0 pin. Clock on falling edge
1 1 1 External clock source 0on 1T0 pin. Clock on5 rising edge
CS10 2 3 4 6 7
CS11
CS12

Timer/Counter1 clock
source

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Assuming XTAL = 10 MHz write a program that toggles
PB5 once per millisecond, using Normal mode.
XTALLDI = 10 MHz  1/10
.INCLUDE "M32DEF.INC"
R16,HIGH(RAMEND) MHzpointer
;init stack = 0.1 µs
Num.LDI of
OUT
machine cycles = 1 ms / 0.1 µs = 10,000
SPH,R16
R16,LOW(RAMEND)

TCNT1
OUT
SBI = 65,536
SPL,R16
DDRB,5 – an10,000
;PB5 as output = 55,536 = $D8F0
BEGIN:SBI PORTB,5 ;PB5 = 1
RCALL DELAY_1ms
CBI PORTB,5 ;PB5 = 0
RCALL DELAY_1ms TCNT1H TCNT1L
RJMP BEGIN

DELAY_1ms:
LDI
LDI R20,0xD8
R20,HIGH(-10000) ; load -N
OUT
OUT TCNT1H,R20
TCNT1H,R20 ;TEMP = 0xD8
LDI
LDI R20,0xF0
R20,LOW(-10000)
OUT
OUT TCNT1L,R20
TCNT1L,R20 ;TCNT1L
;Timer1 =overflows
0xF0, TCNT1H
after= 10000
TEMP machine cycles
LDI R20,0x00
OUT TCCR1A,R20 ;WGM11:10=00
LDI R20,0x01
OUT TCCR1B,R20 ;WGM13:12=00,CS=CLK
AGAIN:IN R20,TIFR ;read TIFR
SBRS R20,TOV1 ;if OCF1A is set skip next instruction
RJMP AGAIN
LDI R20,1<<TOV1
OUT TIFR,R20 ;clear TOV1 flag
LDI R19,0
OUT TCCR1B,R19 ;stop timer
OUT TCCR1A,R19 ;
RET
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
LDI R16,LOW(-10000)
OUT TCNT1L,R16
LDI R16,HIGH(-10000)
OUT TCNT1H,R16

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
TCNT0 = 256 –N
TCNT0 = -N

TCNT1L = LOW(-N)
TCNT1H = HIGH(-N)

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
TEMP register
Data bus (8-bit)

TEMP (8-bit)

OCR1xH (8-bit) OCR1xL (8-bit) TCNT1H (8-bit) TCNT1L (8-bit)

OCR1x (16-bit Register) TCNT1 (16-bit Counter)

OCR1xH buf. OCR1xL buf.


Note: OCR1x is OCR1A or OCR1B
OCR1x buffer (16-bit Register)

LDI R20,0xF3
IN R20,TCNT1L
a = TCNT1L;
TCNT1H = 0xF3;
OUT TCNT1H,R20
IN R21,TCNT1H
b = TCNT1H;
TCNT1L = 0x53;
LDI R20,0x53
OUT TCNT1L,R20
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Assuming XTAL = 10 MHz write a program that toggles
PB5 once per millisecond, using CTC mode.
.INCLUDE "M32DEF.INC"
LDI R16,HIGH(RAMEND)
OUT SPH,R16
LDI R16,LOW(RAMEND)
OUT SPL,R16
SBI DDRB,5 ;PB5 as an output
BEGIN:SBI PORTB,5 ;PB5 = 1
RCALL DELAY_1ms
CBI PORTB,5 ;PB5 = 0
RCALL DELAY_1ms
RJMP BEGIN

DELAY_1ms:
LDI R20,0x00
OUT TCNT1H,R20 ;TEMP = 0
OUT TCNT1L,R20 ;TCNT1L = 0, TCNT1H = TEMP

LDI R20,0x27 ; load N-1 in OCR1AH:OCR1AL, 9999 = 0x27 0F


OUT OCR1AH,R20 ;TEMP = 0x27
LDI R20,0x0F
OUT OCR1AL,R20 ;OCR1AL = 0x0F, OCR1AH = TEMP

LDI R20,0x00
OUT TCCR1A,R20 ;WGM11:10=00
LDI R20,0x09
OUT TCCR1B,R20 ;WGM13:12=01,CS=CLK
AGAIN:
IN R20,TIFR ;read TIFR
SBRS R20,OCF1A ;if OCF1A is set skip next instruction
RJMP AGAIN
LDI R20,1<<OCF1A
OUT TIFR,R20 ;clear OCF1A flag
LDI R19,0
OUT TCCR1B,R19 ;stop timer
OUT TCCR1A,R19 ;
RET

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Counting

The AVR microcontroller


and embedded
systems
using assembly and c

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Counting
PSR10

Clear
clkIO 10-bit T/C Prescaler

clk/8

clk/64

clk/1024
clk/256
T0
0

CS00 0 1 2 3 4 5 6 7
CS01 T0
CS02

Timer/Counter0 clock
source

7
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Example Assuming that clock pulses are fed into pin T0, write a program for counter 0
in normal mode to count the pulses on falling edge and display the state of the TCNT0
count on PORTC.
.INCLUDE "M32DEF.INC"
CBI DDRB,0 ;make T0 (PB0) input PSR10

LDI R20,0xFF clkIO


Clear
10-bit T/C Prescaler
OUT DDRC,R20 ;make PORTC output

clk/64
clk/256
clk/8

clk/1024
LDI R20,0x06
T0
OUT TCCR0,R20 ;counter, falling edge
0
AGAIN:
IN R20,TCNT0
CS00 0 1 2 3 4 5 6 7
OUT PORTC,R20 ;PORTC = TCNT0 CS01
IN R16,TIFR CS02

SBRS R16,TOV0
RJMP AGAIN ;keep doing it Timer/Counter0 clock
source
LDI R16,1<<TOV0
OUT TIFR, R16
RJMP AGAIN ;keep doing it

FOC0 WGM00 COM01 COM00 WGM01 CS02 CS01 CS00 TCCR0

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Assuming that clock pulses are fed into pin T1. Write a program for counter 1 in CTC
mode to make PORTC.0 high every 100 pulses.
.INCLUDE "M32DEF.INC"

CBI DDRB,1 ;make T1 (PB1) input

SBI DDRC,0 ;PC0 as an output

LDI R20,0x0
OUT TCCR1A,R20
LDI R20,0x0E ; WGM = 0100 for CTC mode, clock = 110
OUT TCCR1B,R20 ;CTC, counter, falling edge
AGAIN:
LDI R20,0
OUT OCR1AH,R20 ;TEMP = 0
LDI R20,99
OUT OCR1AL,R20 ;ORC1L = R20, OCR1H = TEMP
L1: IN R20,TIFR
SBRS R20,OCF1A
RJMP L1 ;keep doing it
LDI R20,1<<OCF1A ;clear OCF1A flag
OUT TIFR, R20

SBI PORTC,0 ;PC0 = 1


CBI PORTC,0 ;PC0 = 0
RJMP AGAIN ;keep doing it

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights

You might also like