0% found this document useful (0 votes)
88 views27 pages

Time Delay

This document discusses methods for implementing time delays in microcontrollers using both software and hardware techniques. It provides examples of assembly and C code to generate delays using software loops and timer interrupts. It also describes the timer registers and control bits used to configure the timers. The document aims to help understand how to generate accurate time delays and use timers for applications like object counting.
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
0% found this document useful (0 votes)
88 views27 pages

Time Delay

This document discusses methods for implementing time delays in microcontrollers using both software and hardware techniques. It provides examples of assembly and C code to generate delays using software loops and timer interrupts. It also describes the timer registers and control bits used to configure the timers. The document aims to help understand how to generate accurate time delays and use timers for applications like object counting.
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/ 27

Microprocessor &

Microcontroller
Time Delay Methods
&
Calculations of Delay for Software
loop

Dr. C.H. Vithalani


Professor & Head of E.C. Department,
Government Engineering College, Rajkot
Timer/Counter in
ATMega32
• Timer/Counter 0
– 8 bit
• Timer/Counter 1
– 16 bit
• Timer/Counter 2
– 8 bit
Need of Timers …
• Generating Time delay

• Timing of events (internal or external)


• Scheduling Events
• Measuring the width of a pulse
• Speed control of motors (PWM)
• Generation of complex waveforms
• Frequency generation (sounds)
• Counting
Time-delay methods

Time Delay

Software Hardware
(Polling) (Timer interrupts)

Pure Software delay Checking Timer


(Loops) Overflow Flag
Pure Software Delay
• Load count value in registers
• Decrement registers until they become
zero.
Label Mnemonics Cycles Comments
DELAY: LDI R20,0x64 1 Load count 100 in R20
LOOP: DEC R20 1 Decrement R20 (count value)
BRNE LOOP 2/1 Continue to decrease until zero
RET 4 Return to main program

Total number of cycles CT = CO + CL + CR= 1 +


100
Execution time of one cycle = 1/ (8 MHz) = 0.125 µS (8 MHz crystal)

Time Delay = 304*0.125 s = 38 s

C.H. Vithalani Timer Programming


Exercise …
• Find out delay achieved by following
program if crystal frequency is 8 MHz.
Label Mnemonics Cycles Comments
DELAY: LDI R20,0xFF 1 Load count 255 in R20
LOOP2: LDI R21,0xFF 1
LOOP1: DEC R21 1
BRNE LOOP1 2/1 Decrement R20 (count value)
DEC R20 1 Decrement R20 (count value)
BRNE LOOP2 2/1 Continue to decrease until zero
RET 4 Return to main program

Total number of cycles CT = CO + CL + CR = 1+ 195584 + 4 =195589


CL = 255x[(255x3-1)+3]-1 = 195584

Time Delay = 195589*0.125 s = 24.448 mS


Software time delay
subroutine by polling of
Steps: timer overflow flag:
• Load count value in the timer registers
• Start timer
• Check for timer overflow flag
Registers:
TCNT0 for timer 0, TCNT1 for timer 1,
TCNT2 for timer 2
Timer overflow flags:
TOV0, TOV1 and TOV2 for Timer 0, 1 and
2
Registers
• TCNTn : Timer/Counter register (n=0,1,2)

• TCCRn: Timer/Counter Control register (n=0,1,2)

• OCRn: Output Compare Register (n=0,1,2)

• OCFn: Output Compare Flag (n=0,1,2)


When OCR and TCNT content are equal, OCF flag
set which can be used to generate interrupt
• The result of the compare can be used to generate
a PWM or variable frequency output on the Output
Compare Pin (OC0).
TCCR0 – Timer/Counter Control Register

• FOC0: Force compare match


(When writing a logical one to the FOC0 bit, an
immediate compare match is forced on the
Waveform Generation unit.)
• WGM00 and WGM01: Mode select bits
• COM01 and COM00: Compare output mode
• CS02:00 Timer Clock selector bits
WGM0[1:0]: Waveform Generation mode
select bits WGM00 WGM01 Mode
(D6) (D3)
0 0 Normal operation
0 1 Clear timer on compare match
1 0 PWM, Phase correct
1 1 Fast PWM

CS02:00 Timer clock selector bits


CS02(D2) CS01(D1) CS00(D0) Mode
0 0 0 No clock source (STOP)
0 0 1 Clock with no pre-scaling
0 1 0 CLK/8
0 1 1 CLK/64
1 0 0 CLK/256
1 0 1 CLK/1024
1 1 0 External clock (pulse) on T0
Timer Interrupt Registers …
• TIMSK – Timer/Counter Interrupt Mask
Register
• TIFR – Timer/Counter Interrupt Flag
Register
Enabling and Disabling Timer Interrupts…
TIMSK (Timer Interrupt Mask) Register

TOIE0: Timer 0 Overflow Interrupt Enable


OCIE0: Timer 0 Output compare match interrupt
enable
TICIE1: Timer 1 Input capture interrupt
enable
TIFR – Timer/Counter Interrupt
Flag Register
Assembly Language Program
.ORG 0X00
RJMP MAIN
.ORG 0X16 ;Vector location for timer 0 OVF interrupt
JMP SQR_WAVE
MAIN:
LDI R16, 0x08 ; Define Stack pointer
OUT SPH,R16
LDI R16, 0x5F
OUT SPL,R16
LDI R20,(1<<TOIE0) ;OR LDI R20,0x01
OUT TIMSK,R20 ; Enable timer 0 interrupt
SEI ; Set Global Interrupt Flag
LDI R20,0x80 ;Load Timer register
OUT TCNT0,R20
Assembly Language Program
LDI R20,0x01
OUT TCCR0,R20 ;Normal mode, no prescalar
LDI R16,0X80 ;Bit 7 of R16 set
LOOP:
RJMP LOOP

SQR_WAVE:
COM R16
OUT PORTB,R16
LDI R20,0x80 ;Load Timer register for next cycle
OUT TCNT0,R20
RETI
C Language Program

#include <avr/io.h>

#include <avr/interrupt.h>

ISR(TIMER0_OVF_vect)
{
TCNT0=0x80;
PORTB=PORTB^(1<<7);
}
C Language Program

int main(void)
{
TIMSK=TIMSK|(1<<TOIE0); //TIMSK=0x01;
sei(); //Enable global interrupt flag
TCNT0=0x80; //Load timer register
TCCR0=0x01; //Timer 0 normal mode, no prescalar
while(1);
}
Exercise
Modify Interrupt routine to get variable
frequency of square wave depending on
position of DIP Switch connected at PORTC
COM01:0: Compare Match Output Mode
COM01 (D3) COM00 (D4) Mode

0 0 Normal OC0 disconnected


0 1 Toggle OC0 when compare match
1 0 Clear OC0 when compare match
1 1 Set OC0 when compare match

• Alternate function of PB3 pin is OC0


Program to Toggle OC0 Pin
int main(void)
{
DDRB=DDRB|(1<<3); //Make OC0 pin Output
PORTB=PORTB|(1<<3); //Pull-up ON
OCR0=0x09;
TCNT0=0x00;
TCCR0=0x11; //Toggle OC0 pin on match, timer normal
while(1);
}
Object Counter Application
There are various object counter application where
counting is required in manufacturing plant, Malls,
Parking place etc.
We can use timer in counter mode for object counter
application
Object Counter Application
• Consider that photo transistor is connected
to T0 (PB0) pin of AVR microcontroller.
When one object passes between LED and
photo transistor, one pulse is applied at the
T0 pin. We want to count number of objects
passed and display count value on Seven
Segment Display connected at Port D.
Object Counter Application

U1
9 22
RESET PC0/SCL
23
PC1/SDA
13 24
XTAL1 PC2/TCK
12 25
XTAL2 PC3/TMS
26
PC4/TDO
40 27
PA0/ADC0 PC5/TDI
39 28
PA1/ADC1 PC6/TOSC1
38 29
PA2/ADC2 PC7/TOSC2
R1 37
PA3/ADC3
10k 36 14
PA4/ADC4 PD0/RXD
35 15
PA5/ADC5 PD1/TXD
34 16
PA6/ADC6 PD2/INT0
33 17
R2 U2 PA7/ADC7 PD3/INT1
18
PD4/OC1B
1 A C 4 1 19
PB0/T0/XCK PD5/OC1A
220 2 20
PB1/T1 PD6/ICP1
3 21
PB2/AIN0/INT2 PD7/OC2
2 3 4
PB3/AIN1/OC0
K E 5
PB4/SS
PC817 6
PB5/MOSI
7 32
PB6/MISO AREF
8 30
PB7/SCK AVCC
ATMEGA32
Object Counter Program …
CBI DDRB,0 ;Make PB0 (T0) pin input
LDI R20,0xFF ; Make Port D output
OUT DDRD,R20
LDI R20,0x06
OUT TCCR0,R20 ;Counter on falling edge
CHECK:
IN R20,TCNT0 ;Read count value from timer register
OUT PORTD,R20 ;Display Count value
IN R16,TIFR
SBRS R16,TOV0
RJMP CHECK
LDI R16,1<<TOV0
OUT TIFR,R16
RJMP CHECK
Exercise
• Convert Object Counter Program in C
Language …

• Modify program to display count value on


LCD Considering data lines of LCD
connected to Port D, RS with PC0, Enable
(EN) to PC1 and RW to ground.
Thank you …

You might also like