Continue to Site

Welcome to

Welcome to our site! is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

How to write simple routines for creating delay?

Not open for further replies.


Junior Member level 3
Jan 13, 2006
Reaction score
Trophy points
Activity points
how to write code for creating delay using timers

Hi everyone.

I have recently got the job in embedded field.
i am very much confused about the programming microcontroller.
Please mail me some example code for creating the delay of 20msec.
If say delay is 20ms, can i use for loop to get delays in msec, if yes how should i write it?
which is the simple method to write the delay loops.

Anybody worked with CC1010 Chip?
In that how to configure the timer2 in PWM mode, how to set the duty cycle.
I need your suggestions and help.
Please forward me some example code for creating delay. suggest me some website where i can get example programs for 8051compatible microcontroller.

With Best Regards,

Making delay in a microcontroller is relatively simple.

I suggest do not used for delay loops in creating delay because your MCU has nothing else to do execept for the delay loop.

I suggest use the timer module - it is simple to use real - time and scalable. the timer is simply a counter that will count from 0,1 ..255 in case of a 8-bit timer or 0,1, ...65535 in case for a 16 - bit counter. you can always scale the time increment for every count and this scale is a multiple of your machine cycle period.

For example if the MPU machine cycle period is 1microsec, you can scale the timer to 256 so that each count will increment every 256microseconds. You divide 20milliseconds/256microseconds = 78.125 counts. So when the timer is already D'77' then you can start processing what should be done after that 20ms delay. or you can load the count your timer should start at timer = D'255' - D'78' and generate the service routine on the timer overflow. This assumes that your timer is 8 - bit.

I have done various TMR0 counting, delay and other applications using PIC microcontroller but not the microcontroller that you mentioned. But I suspect your microcontroller has a timer feature and its operation should be the same of that of PIC.

I know this is not what you want to get but I hope its helping you a bit,

If you need a delay that is "at least" a certain time, use a software counter loop as neoaspilet proposed, if you need exact time, start a hardware timer and have it generate an interrupt on overflow or underflow.
For example, your timer increments every 1 usec and it is an 8-bit timer, the overflow will occur every 256 usec if you never stop it. For a delay you want to load the timer with a specific value and start it when the delay begins. The interrupt will tell you that the delay is passed. While you are waiting for the delay signal to come, your CPU can do useful things like tranmitting something through a serial interface, doing some calculations...


Please see attached delay routines for 8051 and Keil C51.
It is necessary to put your value of Crystal frequency in line
"#define DELAY_XTAL 18432000" of file delay.h

Not open for further replies.

Part and Inventory Search

Welcome to