Microcontroller › 8051 › Delay function in C
- This topic has 14 replies, 5 voices, and was last updated 10 years, 4 months ago by AJISH ALFRED.
-
AuthorPosts
-
May 6, 2014 at 7:53 pm #3077Osama MohammedParticipant
hello,
The delay function in c is not working with me and I don’t know why
this is the function that I am using :
void MSDelay(unsigned int itime){unsigned int i,j;for (i=0;i<itime;i++)for (j=0;j<1275;j++);}May 7, 2014 at 5:15 am #11695SHAH DISHANT H.ParticipantHi,
We can no see anything wrong in this function..!!
Have u declred it before using ??
How you concluded that it is not working?
May 7, 2014 at 10:10 am #11697Rupesh AnandParticipantIF YOU ARE USING IT ON PC
THEN SPEED OF PC IS VERY HIGH AND GIVE YOU VERY SMALL DELAY…
TRY WITH HIGHER VALUES…. VERY HIGH
OR TOU CAN USE INTERNAL DELAY FUNCTION ….
May 7, 2014 at 2:23 pm #11698Osama MohammedParticipantYeah I know that is not wrong ,so this is why I am suprised ,
I concluded that is not working because when I put 1 second delay (1000)
the code stuck in the delay function and doesn’t continue executing the code ,it is a 1 second delay though.
The problem is that I don’t know what’s the problem !! ,this caused many troubles for me , please could you suggest me a soultion
May 7, 2014 at 2:24 pm #11699Osama MohammedParticipantYes I declared it of course
May 9, 2014 at 4:34 am #11700SHAH DISHANT H.ParticipantSend me entire code..!
I will try to solve…This sounds an amazing issue..!!
Send the entire code pls..!!
May 9, 2014 at 8:42 am #11701Ashutosh BhattParticipantsee your function
if you are passing value 1000 to the function it will roll over for 1275000 times. ok?
so whats the problem? when it completes 1275000 cycles it will automatically come out of function and generate some delay. it may not be perfect 1 sec delay.
tell me how you run this delay function. step by step execution will take 1275000 steps. step over execution will take only 1 step
May 9, 2014 at 4:46 pm #11707Osama MohammedParticipantThis is the code of a setpper motorI am using ISIS Proteuswhen I run the code with that delay function the execution us stuck in the function !!when I remove this delay function the code is executed normally and continually without stopping !I tried to make many modfiications but nothing works#include <reg51.h>msDelay(unsigned int);#define stepperPort P2void main (void){stepperPort=0x03;msDelay(4000);stepperPort=0x06;msDelay(4000);stepperPort=0x0C;msDelay(4000);stepperPort=0x09;msDelay(4000);}msDelay(unsigned int tdelay){unsigned k,j;for(k=0;k<=tdelay;k++)for(j=0;j<=1275;j++);}May 10, 2014 at 3:46 am #11708SHAH DISHANT H.ParticipantHi,
What is the frequency of your crystal ?
1275 value will give 1 milisecond delay i crystal of 11.0592MHz is sued and the code is compiled in GCC not in KEIL.
Try to reduce two values.
1. Instead of 1275, use low value.
2. Instead of msDelay(4000) also give low value like msDelay(10).
I had faced same issue. What was happening was, the delay was a lot more than what was expected. So I also thought that it is stucked in that function and not coming out of the loop.
Try with this and revert back.
May 12, 2014 at 4:23 am #11720SHAH DISHANT H.ParticipantHi,
I have tried above function and tested on hardware…
Its working perfect AS EXPECTED…!!!
Issue seems in ur simulation only.,..!!
May 12, 2014 at 12:09 pm #11722Osama MohammedParticipantno my simulation is working good without the delay function ,when I use the delay function it stops working
anyway I tried to reduce the 1275 to 100 ,and it worked and it has worked well
but know how could I calibrate the function to return 1 second delay when I enter 1000 ms to the function arguement msDelay(1000)
May 12, 2014 at 1:16 pm #11723SHAH DISHANT H.ParticipantHi,
To calibrate in ur expected way, you have to go for try and error method…
Observe the delay, change the value and keep testing unless you have got he exact value…!!
May 14, 2014 at 9:23 am #11729Osama MohammedParticipantthanks you guys,
May 14, 2014 at 9:23 am #11730Osama MohammedParticipantThank you guys,
May 16, 2014 at 4:36 am #11736AJISH ALFREDParticipantHi Osama,
It is due to the compiler optimisation. It simply optimise out the loop, which is not executing any useful statements. One solution is to remove all the optimisation options, or reduce the level of optimisation to zero. Still not working, this is how people do it, declare the local variables as volatile.
void MSDelay(unsigned int itime){volatile unsigned int i,j;for (i=0;i<itime;i++)for (j=0;j<1275;j++);}Which compiler are you using. If you are using keil, this may also work.void MSDelay(unsigned int itime){xdata unsigned int i,j;for (i=0;i<itime;i++)for (j=0;j<1275;j++);} -
AuthorPosts
- You must be logged in to reply to this topic.