How to create inbuilt delay function in robotics programming?

This entry is part 5 of 7 in the series 8051 MCU Projects

This is 8051 code to create inbuilt “delay” function.

In Keil, there is no inbuilt function for delay. So we have to use the “for” loop to create delay, every time.

So study this program so that you will understand how to create UDF only once in your program, and just write delay(1000); or something like this wherever you want to insert delay.

Try this, its really interesting!

Even our students of Basic Robotics Course (Level-1) can try the code given in red color in the following program.

This will avoid writing


or something like that every time in your program.

This code is equally applicable to ATMega8/16 programming. Its the inbuilt coding style of “C” or “C++”.

Remember, you have to use this code, before the main() function of your program.

If you want to change the word “delay” to create delay, in your program, simply change it in the following line of the code….

void delay(unsigned int time)
#include <reg52.h>
#define LEDs P1 // assigning the PORT1 to the 4 LEDs
void delay(unsigned int time) // user defined “delay” function
int a,b;
main() // main function of the program
while(1) // infinite loop
LEDs=0x0F; // all LEDs are ON, because P1^3 to P1^0=1111
delay(1000); // user defined “delay” function is used here

LEDs=0x00; // all LEDs are OFF, because P1^3 to P1^0=0000
delay(1000); // user defined “delay” function is used here

} // while closed   
} // main function closed