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.

Delay in 89c51 using C in keil

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
Activity points
0
I am using 89c51 controller and crystal frequency of 11.0592mhz and using c language to create delay loop. I wanted to know that delay_us(), delay_ms() and delay_sec() are by default function of this controller or I have to create it on my own way.

if its by default then ow could I use it and if not then please verify bellowed one.. is that right one.
and can I use this loop delay for using I2C communication for DS1307 RTC.

HTML:
/*It genarates a approximate delay of 10us for each count,
if 5000 is passed as the argument then it generates a delay of apprx 50ms.*/
void delay_us(unsigned int us_count)
{
	while(us_count!=0)
	{
		us_count--;
	}
}

/*It genarates a approximate delay of 1ms for each count, 
if 1000 is passed as the argument then it generates delay of apprx 1000ms(1sec)*/
void delay_ms(unsigned int ms_count)
{
	while(ms_count!=0)
	{
		delay_us(112);   //delay_us is called to generate 1ms delay
		ms_count--;
	}	
}

/*It genarates a approximate delay of 1sec for each count,
if 10 is passed as the argument then it generates delay of apprx 10sec
note: A max of 255 sec delay can be generated using this function.*/
void delay_sec(unsigned char sec_count)
{
	while(sec_count!=0)
	{
		delay_ms(1000); //delay_ms is called to generate 1sec delay 
		sec_count--;
	}
}
 

several way to create delay function . but best way is using _nop_() instruction and count cycle using stop watch to get better result .
 

several way to create delay function . but best way is using _nop_() instruction and count cycle using stop watch to get better result .

as per your mean below code right?
will that give 1us delay in keil?

void delay_us(unsigned int us_count)
{
while(us_count!=0)
{
_nop_();
us_count--;
}
}
 

in my 89c51rd2 and crystal 11.0592 delay for 1 microsecond is below
Code:
void DelayMicro (int us)
{
      for( ; us > 0 ; us--)
      {
             _nop_();
             _nop_();
             _nop_();
             _nop_();
             
      }
}

and 1 mSec

Code:
void DelayMs (int  mS)
{
      unsigned int a ;
      unsigned char b ;
      for(a = 0 ; a < mS ; a++)
      {
          for(b = 0 ; b < 190 ; b++)
          {
                 _nop_();
                 _nop_();
          }
      }
}
 
in my 89c51rd2 and crystal 11.0592 delay for 1 microsecond is below
Code:
void DelayMicro (int us)
{
      for( ; us > 0 ; us--)
      {
             _nop_();
             _nop_();
             _nop_();
             _nop_();
             
      }
}

and 1 mSec

Code:
void DelayMs (int  mS)
{
      unsigned int a ;
      unsigned char b ;
      for(a = 0 ; a < mS ; a++)
      {
          for(b = 0 ; b < 190 ; b++)
          {
                 _nop_();
                 _nop_();
          }
      }
}


how do you calculate this?
can you give me for 1 sec for 11.0592MHz...
 

put DelayMs(1000) in my function bro!!
 
several way to create delay function . but best way is using _nop_() instruction and count cycle using stop watch to get better result .

For small delays, such as around few micro seconds, no problem.
However, for somewhat on magnitude of mili seconds and higher, this implementation waste excessive computer processing of the microcontroler.

On that circumstances, is better suitable employ time slot approach, interrupt driven, in order to release overall program processing.


+++
 

For small delays, such as around few micro seconds, no problem.
However, for somewhat on magnitude of mili seconds and higher, this implementation waste excessive computer processing of the microcontroler.

On that circumstances, is better suitable employ time slot approach, interrupt driven, in order to release overall program processing.


+++

I got you. my project is also same that I have to keep delay more then an hour also or for few hours as per user requirement. (I mean by interrupt I have to take data from user for to which pin to automate of controller)
can you help me to get idea to complete this.
I am using AT89C51.
 

For small delays, such as around few micro seconds, no problem.
However, for somewhat on magnitude of mili seconds and higher, this implementation waste excessive computer processing of the microcontroler.

On that circumstances, is better suitable employ time slot approach, interrupt driven, in order to release overall program processing.


+++

ya bro u are absolutely right and i agree with u.
 

in my 89c51rd2 and crystal 11.0592 delay for 1 microsecond is below
Code:
void DelayMicro (int us)
{
      for( ; us > 0 ; us--)
      {
             _nop_();
             _nop_();
             _nop_();
             _nop_();
             
      }
}

and 1 mSec

Code:
void DelayMs (int  mS)
{
      unsigned int a ;
      unsigned char b ;
      for(a = 0 ; a < mS ; a++)
      {
          for(b = 0 ; b < 190 ; b++)
          {
                 _nop_();
                 _nop_();
          }
      }
}

I have cheked this code for DelayMS(1000) but its not working for 1 sec for 11.0592MHz.
 

ok then now try to some calculation clear . you have to calculate the time taken for the cycle execution if the frequency is 11.0592MHz. 8051 uses 1/12 of oscilator frequency, So now frequency is 921.6kHz

Now Cycle execution time becomes 1/f = 1/921.6kHz = 1.085uS

now your Total Cycles * 1.085uS = your desired time.

- - - Updated - - -

and also using timer read thread may be helpful
 
but i think if you are using assembly then you can multiply no of cycle with 1.085 to find desired delay.
but in terms of C it is not true. bcoz compiler convert the code to the assembly so we cannot judge one for loop for 1 instruction.

for loop get convert it into assembly with big code.
 

...I have to keep delay more then an hour also or for few hours...can you help me to get idea to complete this...

I don´t think that generating timing without applying an external RTC on project would work properly.
If you achieve for instance 1s through internal Timer, is associated some error, which would be multiplied when calculating some hours.

Take a look on some examples bellow :



+++
 

Can anyone help me to understand the 1 sec delay using external timer in assembly language only?
 

I already did that on other core with DS1307 RTC using C language, and it is extremely easy, due this device can be configured to generate a precise 1s at output pin which can be read from microcontroler through I/O interrupt routine.

Check if this helps you: https://www.edaboard.com/threads/225009/#post959855








+++
 

I already did that on other core with DS1307 RTC using C language, and it is extremely easy, due this device can be configured to generate a precise 1s at output pin which can be read from microcontroler through I/O interrupt routine.

Check if this helps you: https://www.edaboard.com/threads/225009/#post959855








+++


I seen the link given by you but that I have seen before also but there I am getting many errors.
as you have said you have work on DS1307 with 89c51 then please see below link and help me if you could solve the mistakes.

https://www.edaboard.com/threads/305721/
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top