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 in c

delay in c

|

Microcontroller › 8051 › delay in c

  • This topic has 8 replies, 6 voices, and was last updated 12 years, 11 months ago by Amrith.
Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • May 25, 2012 at 6:42 am #1816
    subhajit
    Participant

    i want a delay of 1sec.

    my osc freq 11.0592Mhz.

     

    when i write

     

    void delay(unsigned int itime)

    {

    int x,y;

     

    for(x=0;x<itime;x++)

    for(y=0;x<1275;y++);

    }

     

     

    it is not giving exact isec delay.

     

    wat should be the modification?

    May 25, 2012 at 7:28 am #7901
    Sameer K.
    Participant

     

    In C programs you cannot be sure of delay, cause  it depends on compiler how it optimize the loops as soon as you make changes in the options the delay changes.

    you will have to use Timers for making exact delays….. it does not matters whether u need delays of ms , us or even seconds…

    below is a function of 1 second using timers…..

    // the idea is to make a 50ms delay and run it 20 times as 20x50ms= 1000ms= 1sec

    delay_1s() // timer of 1 sec
    {
    int d;
    for(d=0;d<=20;d++)
    {
    TMOD=0×01;
    TL0=0xFD;
    TH0=0x04B;
    TR0=1; // start timer.
    while(TF0==0); // run until TF turns to 1
    TR0=0; // stop timer
    TF0=0; // reset the flag
    }
    }

     

    May 25, 2012 at 8:02 am #7902
    subhajit
    Participant

    ur ans is for !2Mhz but m using csc 11.0592Mhz

    May 25, 2012 at 5:09 pm #7903
    Anonymous
    Guest

     

    You won’t get exact timing delays unless you use timers. Your for delay is handy only for getting delay in the range of micro seconds. As the delay value increases the variation from expected value also increase. When using in the seconds range the deviation from expected value become significantly high.

    May 25, 2012 at 5:46 pm #7905
    AJISH ALFRED
    Participant

     

    You can’t predict how many instructions will be there for your for loop, after you compile the code. Normally we assume 1us for a single loop, but it won’t help you when you long delays, like in seconds. Use timers instead.

    May 26, 2012 at 4:11 am #7908
    subhajit
    Participant

    BUT WEN M USING TIMER ALSO M NOT GETTING EXACT DELAY.ACTUALLY M MAKING A WATCH SO ITS SHOWING EITHER MORE OR LESS

    May 26, 2012 at 4:47 pm #7911
    AJISH ALFRED
    Participant

     

    Once properly initialised, the timers then depend only on one thing; Crystal oscillator. Check if there is anything wrong with your crystal and its coupling capacitors.

    Also in your code make sure there should not be a delay in your timer’s ISR which is more than the timer’s firing period itself.

     

    If you are trying to make a watch, its better to use RTC chip.

    June 14, 2012 at 10:28 am #8038
    midhun
    Participant

    while making a watch better choose a RTC like DS1307, which gives accurate output. and if you need 1 sec pulse, that option is also available from the same IC. the only thing is that you need to study I2C protocol for interfacing it, and it is possible with 8051/89c51.

    June 14, 2012 at 1:16 pm #8047
    Amrith
    Participant

    for accurate time you need a good crystal oscillator on DS1307.

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

RSS Recent Posts

  • Electronic Components May 13, 2025
  • Need Help Figuring Out the Schematics Of Circuit Board May 13, 2025
  • Wish to buy Battery, Charger and Buck converter for 12V , 2A router May 12, 2025
  • applying solder paste from a jar May 12, 2025
  • Question i-nears headphones magnetic drivers May 12, 2025

Stay Up To Date

Newsletter Signup
EngineersGarage

Copyright © 2025 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