Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[51] Generating 1 second delay using timers in 8051

Status
Not open for further replies.

phrbob93

Member level 1
Member level 1
Joined
Mar 28, 2014
Messages
38
Helped
0
Reputation
0
Reaction score
0
Trophy points
6
Location
Jalandhar, punjab
Activity points
343
i am Generating 1 second delay using timers in 8051
my concept is like this
iam using 11.0592 crystal frequency

time required for 1 machine cycle is1.085 micro second

if timer is set to 0000h then time required for timer to run once is 65536*1.085= 7.11 ms

now if 7.11*142(times the loop runs) = 1000ms=1sec

so here is my code

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include<reg51.h>
void delay(void);
void main()
{
  while(1)
  {
    P2=0x00;                // led off
 
    delay();                // delay for 1sec
    P2=0xFF;                // led on
 
    delay();                // delay for  1 sec
  }
}
 
 
 void delay(void)
 {
  int i;
  TMOD=0x01;          // timer 0 in mode 1
           
for(i=0;i<14;i++)
  {
     TL0=0x00; // starting value from 0 
     TH0=0x00;
     TR0=1;              // sart timer
     while(TF0==0);       // polling TF flag for high
     TR0=0;               // stop timer
     TF0=0;                // clear flag TF0
 
    }
 }



im not getting delay of 1 second, instead i am geting delay of mili seconds.. plz help me where iam wrong
 
Last edited:

First, its not 7.11ms. Its 71.1ms. So number of loops will be 14. 11.0592MHz crystals are good for baud rates, not timings.
Second, you didn't specified your question.
 

First, its not 7.11ms. Its 71.1ms. So number of loops will be 14. 11.0592MHz crystals are good for baud rates, not timings.
Second, you didn't specified your question.


m not getting delay of 1 second, instead i am geting delay of mili seconds.. plz help me where iam wrong
 

Can't say about the C language, but in ASM, it can be written like this. I guess your C compiler will support assembly with 'ASM' initial in every line.

Code:
MOV R1, 0EH

LOOP:
MOV P2, #00H

CALL DELAY
MOV P2, #0FFH

CALL DELAY
JMP LOOP

DELAY:
MOV TMOD, #01H
MOV TH0, #00H
MOV TL0, #00H
SETB TR0
JNB TF0, $
CLR TF0
DJNZ R1, DELAY
RET
 

first of all see your target settings that you have correctly entered the crystal frequency as:11.0592mhz
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top