EngineersGarage

  • Engineers Garage Main Site
  • Visit our active EE Forums
    • EDABoard.com
    • Electro-Tech-Online
  • Projects & Tutorials
    • Circuits
    • Electronic Projects
    • Tutorials
    • Components
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Advertise
You are here: Home / Topics / Delay function in C

Delay function in C

|

Microcontroller › 8051 › Delay function in C

  • This topic has 14 replies, 5 voices, and was last updated 11 years, 8 months ago by AJISH ALFRED.
Viewing 15 posts - 1 through 15 (of 15 total)
  • Author
    Posts
  • May 6, 2014 at 7:53 pm #3077
    Osama Mohammed
    Participant

    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 #11695
    SHAH DISHANT H.
    Participant

    Hi,

     

    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 #11697
    Rupesh Anand
    Participant

     

    IF 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 #11698
    Osama Mohammed
    Participant

    Yeah 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 #11699
    Osama Mohammed
    Participant

    Yes I declared it of course 

    May 9, 2014 at 4:34 am #11700
    SHAH DISHANT H.
    Participant

    Send 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 #11701
    Ashutosh Bhatt
    Participant

    see 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 #11707
    Osama Mohammed
    Participant
    This is the code of a setpper motor
    I am using ISIS Proteus 
    when 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 P2
     
    void 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 #11708
    SHAH DISHANT H.
    Participant

    Hi,

     

    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 #11720
    SHAH DISHANT H.
    Participant

    Hi,

     

    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 #11722
    Osama Mohammed
    Participant

    :( 

     

     

    no 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 #11723
    SHAH DISHANT H.
    Participant

    Hi,

     

    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 #11729
    Osama Mohammed
    Participant

    thanks you guys, 

    May 14, 2014 at 9:23 am #11730
    Osama Mohammed
    Participant

    Thank you guys,

    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++);
    }

     

  • Author
    Posts
Viewing 15 posts - 1 through 15 (of 15 total)
  • You must be logged in to reply to this topic.
Log In

RSS Recent Posts

  • factory device from 2017'ish with web ui - too old to function with Microsoft Edge ? January 14, 2026
  • flexible copper cable January 14, 2026
  • renewed interest in old project I call it WICKED 8 January 14, 2026
  • updating circuit with power on off switching January 14, 2026
  • Steering angle sensor question January 13, 2026

Stay Up To Date

Newsletter Signup
EngineersGarage

Copyright © 2026 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy | Advertising | About Us

Search Engineers Garage

  • Engineers Garage Main Site
  • Visit our active EE Forums
    • EDABoard.com
    • Electro-Tech-Online
  • Projects & Tutorials
    • Circuits
    • Electronic Projects
    • Tutorials
    • Components
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Advertise