Hi everyone, can somebody guide me to produce very small delays for controlling a servo motor using PWM.
I need to generate 3 different pulse widths of 1.2ms , 1.5ms and 1.8ms.
I am using Keil compiler, and 8051 microcontroller. I am a good C coder, but dont understand much embedded C.
It is very urgent, and I need the code ready by tomorrow. I tried a lot of ways, but I can never generate an extremely small delay. I have even tried changing the tick interval.

Thanks a lot for any help.

1.2ms , 1.5ms and 1.8ms.

That's an awfully small delay. The best I could get is a perfect 10ms delay. Sometimes about 5ms(rare). Don't know if this small delay is possible or not. You can try calling a function(read: Any custom function) to get you a delay this small. But it's just a wild guess. :)

thank you, i shall try that, but i am open to more suggestions and replies. thanks !

If however you can use POSIX, nanosleep() might do the trick.

thanks for your reply. sorry but i have no clue how to use it!! i tried but to no avail!

You need to look at your hardware for such small timing intervals, not software.

Are you using a 555 timer IC chip? What timer features does your PIC chip offer?

That's where you need to look - C that uses your hardware, rather than your OS.

Agreed with Adak if your hardware cannot support an interval this small, then there's no use looking further.

i am using an 8051, not a PIC chip. i've been told that hardware delays are more complex than software delays, and cannot generate extremely small delays

I tried this code for servo. but did not work.

#include <REGX51.H>
sbit pulse = P2^0; // for give pulse to survo
void delay()//10us delay
void main()
	int i;
	while(1)  //20ms pulse width
		for(i=0;i<100;i++) //1ms high
		for(i=0;i<1900;i++)	//19ms low

If you get any better solution let us know.

Right now, i have changed the tick interval. And I am trying. I have managed to use

if (++count=10) // suppose 10

for the count of 10. i get a pulse width of 2.4ms , and for lower count it is lower, but it still isnt accurate enough. thanks for your post though i will try your method and let you know

By PIC chip, I don't mean PIC cpu, I mean a Programmable Interrupt Controller chip. That's what takes the clock sweeps from a raw source, and divides them up. In a PC, it gives 1/3rd of the sweeps to the RAM, one third to the speaker, and one third to the clock() function of the cpu.

You may have it integrated into your 8051 chip, I'm not sure where that function is located, but that's what you need to find - something on the hardware level, that handles low level timing.

Have you Googled for that, or asked about this on the forums/newsgroups that are dedicated to this kind of interest? Any manufacturer or the 8051 should be able to give you the info you are looking for.

You are SO lucky to have the internet so widely available. Good luck! :D

oh god what is that, there is no way i can understand that code!

You're losing time for code delays.
All 3 numbers have a common denomiator. How about a simple delay function of 100ms and then merely call it your 12, 15 or 18 times? 100ms is much easier to track then 10ms and thus no need for fine precision. If those numbers are fixed then how about a true common denominator...

4 x 3 x 100
5 x 3 x 100
6 x 3 x 100
so {4, 5, 6} loops in a 300ms delay! But don't use an outer loop.
void Delay300()
  Delay approx. 298ms, the call will take 300ms.

void Delay1200()
   Delay300(); Delay300(); Delay300(); Delay300();


i need 1.2 milliseconds, and not 1200 milliseconds. !!! any idea how i can do it?

OOPS. So what is the clock speed of the processor?

i have made the code finally using a 12mhz oscillator and hardware delays.
im at home now, code is at uni, so unable to post it. if any1 ever needs help with it reply here and i'll get back with you. thanks to all of you on this website for all your efforts. cheers.