Delay Calculation using C for AT89C51 Controller with 11.0592MHz crystal frequency

Status
Not open for further replies.

jay_3189

Banned
Joined
Sep 19, 2013
Messages
104
Helped
0
Reputation
0
Reaction score
0
Trophy points
16
Location
Ahmedabad
Visit site
Activity points
0
Hello Everyone

Can any one suggest me code in embedded C or IDEA that how to calculate delay for AT89c51 with 11.0592MHz Crystal frequency in Keil.
If it possible with for loop than please say in that. because I found somewhat hard with timer as I am new to this.

And Here I want to take delay from user in future (it mean delay is not fixed one) so also suggest that how could I take values from user for delay.

Thanks in Advance.
 

try this with all reference book
Code:
void MSDelay(unsigned int value)
  {
    unsigned int x, y;
    for(x=0;x<1275;x++)
      for(y=0;y<value;y++);
  }
 

Hi there are lot of threads discussing the same delay in 8051 so search it and get it..
To get the delay value from user, what is your application??
 

To get the delay value from user, what is your application??

my app. is to automate 6 valves flow by programming and there is not any fix timing for 1 valve. I mean a code is such that we can able to change the delay easily of valve on/off duration. and at one time there will be more than 1 valve can also be running.

- - - Updated - - -

try this with all reference book
Code:
void MSDelay(unsigned int value)
  {
    unsigned int x, y;
    for(x=0;x<1275;x++)
      for(y=0;y<value;y++);
  }


Ok I will try this but I am not getting that why you have add 1275 in one loop.
and what value I have to enter at the place of value. (Is that time in milli seconds at place of value in loop?)
 

the best way to produce delay using _nop_() instruction and simulate using your keil ide . and also make sure post your some code to understand about your application .
 

try this with all reference book
Code:
void MSDelay(unsigned int value)
  {
    unsigned int x, y;
    for(x=0;x<1275;x++)
      for(y=0;y<value;y++);
  }

I have tried this logic and written 5000 at the place of value. but I am only getting 3 second duration. and about my code means in starting I am just doing for simple on/off program.
 

ok then try this
Code:
void TimeDelay (int mS)
{
     unsigned int a ;
     unsigned char b ;
     
     for(a = 0 ; a < mS ; a++)
     {
         for (b = 0 ; b < 190 ; b++)     /* for crystal 11.0592 Mhz delay is 1 mSec.  */
         {
                 _nop_();
                 _nop_();
         }
     }
}

and reply me .
 

What is the possible range of the delay durations do you require? Minutes, hours, days, weeks, etc?

Utilizing C loop structures, like the MSDelay() routine, to generate a delay is typically the least desirable of methods.


BigDog
 

Use a for loop which loops once. Set breakpoints at the beginning and end of loop. Start debugger then run to first breakpoint and note down the time in the sec variable shown in the registers window. Now run again it will stop after the loop. Now again note down time in sec variable. Subtract t2 - t1. It will give time to execute the loop once. Now make a value x which will give a delay of 1 us and 1 ms. Create two functions one for us delay and another for ms delay. You can then use i = 0 to i = x * required time, step.
 


I am not getting accurate time delay even in this..
Now I am trying to change this loop value and trying to do this..

- - - Updated - - -

What is the possible range of the delay durations do you require? Minutes, hours, days, weeks, etc?

Utilizing C loop structures, like the MSDelay() routine, to generate a delay is typically the least desirable of methods.


BigDog


I want its in minute and also for sometime its in hour.
 

try to post your full code with port setting . and if post #7 u apply then what's the result ?
 

Try this one
time is in milliseconds
Code:
void delay(unsigned int time) // SUB FUNCTIONS
{
    unsigned char pause, i = 0; //DECLARE PAUSE AS UNSIGNED CHAR
    short int temp = 0;

    temp = time;
    for (i = 0; i < 4; i++) {
        time = temp;
        while (time > 0) //LOOP UNTIL TIME IS GREATER THAN ZERO
        {
            pause = 112; //INITIALIZE PAUSE TO 255
            while (pause--); //DECREMENT PAUSE UNTIL IT BECOMES ZERO
            time--; //DECREMENT TIME AND LOOP BACK UNTIL IT BECOMES ZERO
        }
    }

}
 

I want its in minute and also for sometime its in hour.

Typically, software generated delays should be avoid at all costs, there are several reasons for avoid this coding practice.

Not only are they highly compiler dependent, they are highly version dependent.

Routines such as the following can in fact be optimized out-of-existence, as no variable value or SFR is effected outside the scope of the routine:

Code:
void MSDelay(unsigned int value)
  {
    unsigned int x, y;
    for(x=0;x<1275;x++)
      for(y=0;y<value;y++);
  }

Software generated delays also put the microcontroller in an unresponsive state, essentially locking it up for the duration of the delay.

One of the few acceptable occasions to use a software generated delay is at system start up to allow peripherals to initialize properly, even then the delays should only be limited to the microsecond or millisecond range.

The delay routines offered by C compilers are typically write in assembler in a C callable routine as this is the only way to obtain any reasonable amount of precision of the duration regardless of compiler version.



To generate delays within normal operation of the device with a delay duration of minutes to hours, a timer in conjunction with its interrupt and its service routine (ISR) should be utilized.

A variable can act as a secondary counter to keep track of multiple timer overflows to generate the required delay range.

There are numerous tutorials available on the topic of timers and timer interrupts, a few are listed below:

**broken link removed**

8051 Timer 0 Mode 1 Example Program








BigDog
 
try to post your full code with port setting . and if post #7 u apply then what's the result ?

I have tried this and it will work for 25 and 10 seconds but if when I am increasing seconds than im not getting right delay.. (I have tried for 60 seconds.. )

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
#include<reg52.h> 
#include<stdio.h> 
sbit VALVE_pin = P2^0;     //Defining VALVE PIN
 
void MSDelay(unsigned int); //Function prototype declaration
void main (void) 
{
 
   VALVE_pin = 0;    //Making VALVE pin output
 
   while(1)     //infinite loop 
   {
     VALVE_pin = 1; //VALVE ON
     MSDelay(25000); //Delay
     VALVE_pin = 0; //VALVE OFF 
     MSDelay(10000);
   }
}
 
void MSDelay(unsigned int k)
{
    unsigned int i,j;
    for(i=0; i<k; i++)
       for(j=0; j<2556; j++)
                 {
                     _nop_();
                 }
}

 
Last edited by a moderator:

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…