- This topic has 8 replies, 6 voices, and was last updated 10 years, 9 months ago by .
Viewing 9 posts - 1 through 9 (of 9 total)
Viewing 9 posts - 1 through 9 (of 9 total)
- You must be logged in to reply to this topic.
|
Microcontroller › 8051 › delay in c
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?
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
}
}
ur ans is for !2Mhz but m using csc 11.0592Mhz
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.
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.
BUT WEN M USING TIMER ALSO M NOT GETTING EXACT DELAY.ACTUALLY M MAKING A WATCH SO ITS SHOWING EITHER MORE OR LESS
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.
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.
for accurate time you need a good crystal oscillator on DS1307.