In order to provide longer delays without wasting too much
inline code space, the delay_50usec()
and delay_msec() functions
are used. These 2 functions represent a variable number of
delay periods based on 50 microseconds and 1 millisecond
respectively.
Notice that we have now introduced parameter passing as
arguments to functions. Both of these functions contain the unsigned
char delay declaration within the parentheses of
the function declaration. This means that when we call them
from another function we need to provide a number as an
argument which in both cases represent the number of time
delays.
Both function internally declare a variable x as an
unsigned char. Remember that an unsigned char is an 8 bit
number ranging from 0 to 255 (or 0x00 to 0xff in hex or
0b00000000 to 0b11111111 in binary). Since the variable is
declared within the function it's scope is limited to the
function only and cannot be seen by other functions.
The for loop
is now executed a number of times as specified in the delay
parameter passed to them. In the for loop, x is first
initialized to 0 and the for
loop is executed as long as x is less than the
value in delay. The last statement in the for loop increments
to variable x at the end of each loop.
The function delay_50usec()
uses a variety of #define's at the top of the file to cause
each execution of it's for
loop to delay 50 microseconds.
The function delay_msec()
calls the delay_50usec() with an argument of 22 to
provide a 1 millisecond delay each time it's for
loop is executed.