8051 timer ; TImer 0 ; Mode 1 (16 bit) didn't produce exact delay.
Discussion in "8051 Discussion Forum" started by Khadka_Bishal Mar 16, 2018.
Fri Mar 16 2018, 05:04 pm
I want to have a delay of 15 microsecond in 8051 using a crystal oscillator of 11.0592 MHz. .
To test the delay, i toggle port pin 1.0 for every execution of delay.
The code and its calculations are shown in code's comment section below
====================================================================
ORG 00h
MOV TMOD,#01 ;TIMER0 -> MODE1
;No. of machine cycle = 480uS/1.0850693us = 14
; 2^16 - 14 = 65522
; ( 65522 ) dec = ( FFF2 )hex
; The required value to generate 15us is (FFF2)16
; BUT, there is extra delay of 17 us due to execution of
;other codes except the delay loop
; To Compensate this,
; i add (17)dec =(11)hex to FFF2 = 1 0003 which exceed
; the max value FFFF.
; but i loaded the maximum value i.e. FFFF to timer reg
; and still i got a delay of 17 uS
LOOP:
CPL P1.0
ACALL DELAY15
SJMP LOOP
DELAY15:
MOV TH0,#0FFH
MOV TL0,#0FFH
SETB TR0
HERE: JNB TF0,HERE
CLR TR0
CLR TF0
RET
END
===================================================================
Even assigning the timer register its maximum value i am not getting delay of 15 uS (microsecond) . I have checked for the code optimization as well but i didn't see ta single step that could be either eliminated or optimized.
Please help on it !
To test the delay, i toggle port pin 1.0 for every execution of delay.
The code and its calculations are shown in code's comment section below
====================================================================
ORG 00h
MOV TMOD,#01 ;TIMER0 -> MODE1
;No. of machine cycle = 480uS/1.0850693us = 14
; 2^16 - 14 = 65522
; ( 65522 ) dec = ( FFF2 )hex
; The required value to generate 15us is (FFF2)16
; BUT, there is extra delay of 17 us due to execution of
;other codes except the delay loop
; To Compensate this,
; i add (17)dec =(11)hex to FFF2 = 1 0003 which exceed
; the max value FFFF.
; but i loaded the maximum value i.e. FFFF to timer reg
; and still i got a delay of 17 uS
LOOP:
CPL P1.0
ACALL DELAY15
SJMP LOOP
DELAY15:
MOV TH0,#0FFH
MOV TL0,#0FFH
SETB TR0
HERE: JNB TF0,HERE
CLR TR0
CLR TF0
RET
END
===================================================================
Even assigning the timer register its maximum value i am not getting delay of 15 uS (microsecond) . I have checked for the code optimization as well but i didn't see ta single step that could be either eliminated or optimized.
Please help on it !
Sun Mar 18 2018, 02:56 am
CPL P1.0 3
ACALL DELAY15 6
SJMP LOOP 3
DELAY15:
MOV TH0,#0FFH 2
MOV TL0,#0FFH 2
SETB TR0 3
HERE: JNB TF0,HERE 4
CLR TR0 3
CLR TF0 3
RET 4
total 33 = 17.9 uS
Instructions need different numbers of machine cycles.
I have noted the number needed for each instruction in your loop.
I get a total of 33.
You must be using a version of the 8051 that has
6 clock cycles per machine cycle.
Older types use 12 clock cycles per machine cycle.
33 machine cycles take around 17 uS as you found.
One obvious way to speed up the loop is to
not use a subroutine.
Put the delay "inline".
This saves the ACALL and RET.
You don't need Timer0 at all, you could just
just use a software loop.
LOOP:
CPL P1.0 3
// DELAY15:
MOV TH0,#0FFH 2
MOV TL0,#0FFH 2
SETB TR0 3
HERE: JNB TF0,HERE 4
CLR TR0 3
CLR TF0 3
SJMP LOOP 3
total 23 = 12.4 uS
ACALL DELAY15 6
SJMP LOOP 3
DELAY15:
MOV TH0,#0FFH 2
MOV TL0,#0FFH 2
SETB TR0 3
HERE: JNB TF0,HERE 4
CLR TR0 3
CLR TF0 3
RET 4
total 33 = 17.9 uS
Instructions need different numbers of machine cycles.
I have noted the number needed for each instruction in your loop.
I get a total of 33.
You must be using a version of the 8051 that has
6 clock cycles per machine cycle.
Older types use 12 clock cycles per machine cycle.
33 machine cycles take around 17 uS as you found.
One obvious way to speed up the loop is to
not use a subroutine.
Put the delay "inline".
This saves the ACALL and RET.
You don't need Timer0 at all, you could just
just use a software loop.
LOOP:
CPL P1.0 3
// DELAY15:
MOV TH0,#0FFH 2
MOV TL0,#0FFH 2
SETB TR0 3
HERE: JNB TF0,HERE 4
CLR TR0 3
CLR TF0 3
SJMP LOOP 3
total 23 = 12.4 uS
Powered by e107 Forum System