Generating Time Delays in 8051 Code Using C

  • Thread starter Thread starter soul
  • Start date Start date
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
7 replies · 11K views
soul
Messages
61
Reaction score
0
Hi everyone,
As I understand from my textbook, to generate a time delay by using C programming, we should use timers or use a for loop. My question is this: every C compiler creates their own assembly codes. Then, it means that the same code can generate different time delays in each compiler. How can we overcome with this problem? Or is it a problem?
 
Engineering news on Phys.org
soul said:
Hi everyone,
As I understand from my textbook, to generate a time delay by using C programming, we should use timers or use a for loop. My question is this: every C compiler creates their own assembly codes. Then, it means that the same code can generate different time delays in each compiler. How can we overcome with this problem? Or is it a problem?

I don't believe there are real-time functions in ANSI C (I could be wrong). You would need a real-time operating system (RTOS) with extensions to ANSI C that expose timers or interrupts or some other timing mechanism on the 8051 hardware. Which 8051 variant are you using, and what compiler tool chain are you using?
 
Well, I am using DS89C420 and to compile my code I am using ProView 32.Also, could you extend what you said about the real-time functions and operating systems?
 
soul said:
Well, I am using DS89C420 and to compile my code I am using ProView 32.Also, could you extend what you said about the real-time functions and operating systems?

I'm not very familiar with other uCs, but the uCs that we make for my company's products use a variation of ANSI C called "Neuron C". It has extensions for real-time functions like timers, including millisecond timers and second timers. They are described in Chapter 2 of the Programmer's Guide:

http://www.echelon.com/support/documentation/manuals/devtools/078-0002-02G.pdf

There should either be a variation of ANSI C for your uC to do real-time functions, or else you can write the real-time stuff in assembly, and combine that with your main C code. Your assembly could start a timer and enable an interrupt for when the timer expires, for example.
 
BTW, I just googled DS89C420 real time, and got some good hits. This is the first one, which lists some RTOS options for that uC. BTW, it also says that the 420 has been obsoleted, and the 430 should be used for new designs:

http://www.keil.com/dd/chip/3219.htm
 
The solution is to never use for loops to create precision time delays. They're also a bad idea in many portable/embedded situations, because they use up CPU cycles and consume power. Use a timer/counter or some more sophisticated resource for precision time delays.

- Warren
 
soul, I found this using Google, which is an embedded assembly function in C, also this completely ignores chroot's remark about power consumption, so if you're trying to find a power optimized solution just ignore it.
 

Attachments

berkeman said:
... or else you can write the real-time stuff in assembly, and combine that with your main C code. Your assembly could start a timer and enable an interrupt for when the timer expires, for example.
How can I combine them? Can anyone show some examples?

Abdelrahman said:
soul, I found this using Google, which is an embedded assembly function in C, also this completely ignores chroot's remark about power consumption, so if you're trying to find a power optimized solution just ignore it.
Is this working? because my compiler displayed many syntax error and though I tried to use some other methods that I found, I failed again.