0% found this document useful (0 votes)
16 views48 pages

2 Timers Counters

The document discusses assembler directives used in programming, particularly for AVR microcontrollers, highlighting directives like .EQU, .ORG, .SET, .DEF, and .INCLUDE that enhance code readability and functionality. It also explains the importance of timers in generating delays and counting events, detailing how to program Timer0 in normal mode and the calculations required for setting timer values. Additionally, the document covers the impact of crystal frequency and AVR design on timing accuracy.

Uploaded by

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

2 Timers Counters

The document discusses assembler directives used in programming, particularly for AVR microcontrollers, highlighting directives like .EQU, .ORG, .SET, .DEF, and .INCLUDE that enhance code readability and functionality. It also explains the importance of timers in generating delays and counting events, detailing how to program Timer0 in normal mode and the calculations required for setting timer values. Additionally, the document covers the impact of crystal frequency and AVR design on timing accuracy.

Uploaded by

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

Assembler Directives &

AVR Time Delay


Assembler Directives

• Directives – Directions to Assembler

• Also called as pseudo instructions

• Make Programming easier

• Improves readability of code

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

• Value once assigned


cannot be reassigned

4
Assembler Directives (.EQU)
Using .EQU for fixed data assignment

.EQU DATA1 = 0x39 ;hex


.EQU DATA2 = $39 ;hex
.EQU DATA3 = 0B0011 1001 ; binary
.EQU DATA4 = 39 ; decimal
.EQU DATA5 = ’2’ ;ASCII

5
Assembler Directives (.EQU)
Using .EQU for SFR address assignment

Ex. .EQU COUNTER = 0x00 ;counter value is 00


.EQU PORTB = 0x18 ; SFR Port B address
LDI R16, COUNTER ; R16 = 0x00
OUT PORTB, R16 ; Port B is now 0x00

6
Assembler Directives (.EQU)
Using .EQU for RAM address assignment

Ex. .EQU SUM = 0x120


LDI R20, 5
LDI R21, 2
ADD R20, R21
ADD R20, R21
STS SUM, R20

7
Assembler Directives (.ORG)

• .ORG (origin)

• Used to indicate the beginning of the address.

• Used for both code and data.

• 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

• Includes a file during translation phase

• Similar to #include directive in C language


Example (For using Atmega32)
.INCLUDE “M32DEF.INC”

11
Delay calculation for the AVR

In creating a time delay using Assembly language


instructions, one must be mindful of two factors that
can affect the accuracy of the delay:
1. The crystal frequency
2. The AVR design

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.

2. The AVR design:


• Advances in both IC technology and CPU design in the 1980s
and 1990s have made the single instruction cycle a common
feature of many microcontrollers.
• One way to increase performance without losing code
compatibility with the older generation of a given family is to
reduce the number of instruction cycles it takes to execute an
instruction.
13
Instruction Cycle time for AVR
• Instruction execution by CPU takes a certain amount
of time. This time is referred to as machine cycle.
• Simple instructions 1 or 2 machine cycles.
• Complex instructions 2 to 4 machine cycles.
• 1 machine cycle = 1 oscillator period (1/fosc) .

14
15
Timers/Counters

Basic registers of timers


Timers
• The timer is an important application in
Embedded systems.
• It maintains the timing of an operation in
sync with a system clock or an external
clock.
• The timer has so many applications such
as measure time generating delays,
generating baud rates to mention just a
few.
17
• A normal delay function can be created by a
loop iterating for a few 1000 cycles.
• But these types of delays need not be accurate
and fundamentally it is not a good
programming practice.
• So, TIMER/COUNTER can be used to generate
required delays or to count the time interval
between events.
• It counts the cycle of the peripheral clock or an
externally supplied clock.
• The AVR microcontroller ATmega32 has 3
Timers: Timer 0, Timer 1 and Timer 2
18
• Every timer needs a clock pulse to tick.
• The clock source can be internal or external.
• If we use the internal clock source, then the
frequency of the crystal oscillator is fed into
the timer.
• Therefore, it is used for time delay generation
and consequently is called a timer.
• By choosing the external clock option, we
feed pulses through one of the AVR’s pins.
• This is called a counter.

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

n takes the value 0, 1 or 2.


OCRn
Basic registers of timers
• In AVR, for each of the timers, there is a
TCNTn (timer/counter) register.
• That means in ATmega32 we have TCNT0,
TCNT 1, 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 a value into the TCNTn register or read
its value. 22
Normal Mode (Timer 0)
TCNT0
FF
FE 0xFF
TOV TOV TOV
0 time
2
1
0 TOV0 = 1

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

Timer0 is 8-bit in ATmega32; thus, TCNT0


is 8-bit as shown in Figure

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

a)16MHz/64=250kHz due to 1:64


prescaler and T = 1/250 kHz = 4µs

b)10 MHz /64 = 156.2 kHz due to 1:64


prescaler and T = 1/156.2 kHz = 6.4µ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

Required Delay= Count * T ---------------(1)

Count= 256 – XX

Required Delay= (256-XX) * T

XX= 256-Required Delay/T 35


1. Calculate the period of the timer clock using the
following formula:

Tclock = 1/FTimer

where FTimer frequency of the clock used for the timer.

ex: in no prescaler mode, FTimer = Foscillator.

Tclock  gives the period at which the timer


increments.
36
2. Divide the desired time delay by Tclock. This says
how many clocks we need.

3. Perform 256 - n, where n is the decimal value


we got in Step 2.

4. Convert the result of Step 3 to hex, where xx is


the initial hex value to be loaded into the timer’s
register.

5. Set TCNT0 = xx.


37
Assuming that XTAL = 16KHz, what is the hex
value to be loaded into the TCNT0 register to
generate a delay of 1ms?
Solution:
(a) T = 1 / F = 1 / 16 kHz = 62.5 µS the period of
the square wave.
(b) 1ms/ 62.5 µS = 16
(c) 256-16 = 240 = F0 H
(d) TCNT0 = 0xF0

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

To generate a time delay using Timer0 in Normal


mode, the following steps are taken:
1. Load the TCNT0 register with the initial count
value.
2. 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 starts to count, and each tick
causes the content of the timer to increment by
1.
41
1. Keep monitoring the timer overflow flag
(TOV0) to see if it is raised. Get out of the
loop when TOV0 becomes high.
2. Stop the timer by disconnecting the clock
source, using the following instruct ions:
LDI R20, 0x00
OUT TCCRO,R20 ;timer stopped, mode =Normal
3. Clear the TOVO flag for the next round.
4. Go back to Step 1 to load TCNT0 again.

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy