Download Embedded C Tutorial - (8051)
Transcript
8322 Chapter 3 p35-56
21/2/02
9:55 am
Page 51
Hello, Embedded World
51
void Longer_Loop_Delay(void)
{
unsigned int x;
unsigned int y;
for (x=0; x <= 65535; x++)
{
for (y=0; y <= 65535; y++);
}
}
Listing 3.6 shows how the 1-second delay required in this application is achieved
using this approach.
Listing 3.6
Part of the ‘Hello, Embedded World’ example code
/*------------------------------------------------------------*DELAY_LOOP_Wait()
Delay duration varies with parameter.
Parameter is, *ROUGHLY*, the delay, in milliseconds,
on 12MHz 8051 (12 osc cycles).
You need to adjust the timing for your application!
-*------------------------------------------------------------*/
void DELAY_LOOP_Wait(const unsigned int DELAY)
{
unsigned int x, y;
for (x = 0; x <= DELAY; x++)
{
for (y = 0; y <= 120; y++);
}
}
Note: there is nothing miraculous about the values used in this listing: they were
determined by trial and error. If you use this (or similar) code in a real application,
the delays will vary with compiler optimization settings and other factors. They
must always be checked carefully as part of the final pre-release tests.11
11. Please note that some more robust techniques for generating delays are considered in Chapter 6.