Microcontroller › 8051 › Delay function in C › Hi Osama,It is due to the
May 16, 2014 at 4:36 am
#11736
AJISH ALFRED
Participant
Hi 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++);
}