2 Timers Counters
2 Timers Counters
2
Assembler Directives
• Few Directives
– .EQU
– .DEF
– .ORG
– .INCLUDE
– .SET
3
Assembler Directives (.EQU)
• .EQU (equate)
• Used to define a constant value or a fixed address.
• Does not set aside storage for a data item, but associates a constant
number with a data or an address label.
• Label is substituted by value, when label appears in the program
Example
.EQU COUNT = 0x25
-
-
LDI R21, COUNT ; R21 = 0x25
4
Assembler Directives (.EQU)
Using .EQU for fixed data assignment
5
Assembler Directives (.EQU)
Using .EQU for SFR address assignment
6
Assembler Directives (.EQU)
Using .EQU for RAM address assignment
7
Assembler Directives (.ORG)
• .ORG (origin)
• Example
.ORG 0X0100
LDI R31,$43
8
Assembler Directives (.SET)
• .SET
• Used to define a constant or fixed address
• Similar to .EQU ?
• But value assigned .SET can be reassigned
later in the code
9
Assembler Directives (.DEF)
• .DEF
• Example
.DEF num1=R31
.DEF num2=R30
LDI num1,$45
LDI num2,$54
ADD num1,num2
10
Assembler Directives (.INCLUDE)
• .INCLUDE
11
Delay calculation for the AVR
12
1. The crystal frequency
• The frequency of the crystal oscillator connected to the
XTAL 1 and XTAL2 input pins is one factor in the time delay
calculation.
• The duration of the clock period for the instruction cycle is
a function of this crystal frequency.
14
15
Timers/Counters
19
Timers in Atmega32
Timer Registers and Flags
TCNTn (Timer/CouNTer Register)
TOVn (Timer OVerflow Flag)
TCCRn (Timer/Counter Control Register)
OCRn (Output Compare Register)
TCCRn
OCFn (Output Compare Match Flag)
TIFRn (Timer Interrupt Flag Register)
TCNTn
TOVn
= OCFn
TOV0: 0
1
• Each timer has a TOVn (Timer Overflow)
flag.
• When a timer overflows, its TOVn flag will
be set.
• Each timer also has the TCCRn
(timerIçounter control register) register for
setting modes of operation.
• For example, you can specify Timer0 to work
as a timer or a counter by loading proper
values into the TCCR0.
24
• Each timer also has an OCRn (Output
Compare Register) register.
• The content of the OCRn is compared
with the content of the TCNTn.
• When they are equal the OCFn (Output
Compare Flag) flag will be set.
25
• The timer registers are located in the I/O
register memory.
• Therefore, you can read or write from
timer registers using IN and OUT
instructions, like the other I/O registers.
LDI R20, 25 ;R20 25
OUT TCNT0, R20 ;TCNT0 R20
IN R19, TCNT2 ; R19 TCNT2
26
Timer0 programming
27
TCCR0 (Timer/control Register)
28
TCCR0 (Timer/control Register)
29
TIFR(Timer/counter Interrupt Flag
Register
The timers can be operated in any of 4
modes:
Normal
CTC (Clear Timer on Compare
Match),
PWM, phase correct,
Fast PWM.
Now we discuss how to program Timer
0 in normal mode.
31
Find the timer’s clock frequency and its
period for various AVR-based systems, with
the following crystal frequencies. Assume
that a prescaler of 1:64 is used. (other
values are no prescalar, 8, 256 and 1024)
(a) 8 MHz (b) 16 MHz (c) 10 MHz
32
a) 8 MHz /64= 125 kHz due to 1:64
prescaler and T = 1/125 kHz = 8 µs
33
• If the Timer has to create a required
delay then the first step is to load a
suitable Hex value into the 8bit TCNT0
register.
• Assuming that we know the amount of
timer delay we need, the question is how
to find the values needed for the TCNT0
register.
• To calculate the values to be loaded into
the TCNT0 registers, we can use the
following steps: 34
Calculation of XX to be loaded into TCNT0
Count= 256 – XX
Tclock = 1/FTimer
38
Assuming that XTAL = 16KHz, what is the hex
value to be loaded into the TCNT0 register
create a square wave of frequency 1KHz
Solution:
(a) T = 1 / F = 1 / 16 kHz = 62.5 µS the period of
the square wave.
(b) 0.5ms/ 62.5 µS = 8
(c) 256-8 = 248 = F8 H
(d) TCNTO = 0xF8
39
• Find the value for TCCRO if we want to
program Timer0 in Normal mode with a
prescaler of 64 using internal clock for the
clock source.
40
Steps to program Timer0 in Normal mode
42
COUNTERS
• The AVR timer can also be used to count, detect, and
measure the time of events happening outside the
AVR.
• The use of the timer as an event counter is discussed.
• When the timer is used as a timer, the AVR’s crystal is
used as the source of the frequency.
• When it is used as a counter, however, it is a pulse
outside the AVR that increments the TCNTx register.
• Notice that, in counter mode, registers such as TCCRn,
OCRn, and TCNTn are the same as for the timer
discussed in the previous session
• They even have the same names
• We know that the CS bits (clock selector) in the TCCR0
register decide the source of the clock for the timer.
• If CS02:CS00 is between 1 and 5, the timer gets pulses
from the crystal oscillator.
• In contrast, when CS02:CS00 is 6 or 7, the timer is used as
a counter and gets its pulses from a source outside the
AVR chip.
• You can as well load a count value in TCNT0 and start the
timer from a specific count.
• Another interesting feature is that a value can be set in the
Output Compare Register (OCR0), and whenever TCNT0
reaches that value, the Output Compare Flag ( OCF0) flag is
Set.
• Therefore, when CS02:CS00 is 6 or 7, the
TCNT0 counter counts up as pulses are fed
from pin TO (Timer/Counter 0 External
Clock input).
• In ATmega32, T0 is the alternative
function of P0RTB.0.
• Similar steps are used to configure Timer
1 and Timer 2 in various modes as
timers or counters.
Ex 1:
Ex 2:
48