Generating Time Delays in 8051 Code Using C

  • Thread starter Thread starter soul
  • Start date Start date
Click For Summary
Generating precise time delays in 8051 code using C can be challenging due to variations in how different compilers translate code into assembly, potentially leading to inconsistent timing. To achieve accurate delays, it is recommended to utilize hardware timers or interrupts instead of relying on for loops, which can waste CPU cycles and power. The discussion highlights the importance of using real-time operating systems (RTOS) or specific ANSI C variations that support real-time functions for the 8051 microcontroller. Users are encouraged to explore assembly language for implementing timing functions and to consider alternatives like the DS89C430 for newer designs. Overall, leveraging timers and interrupts is essential for achieving reliable timing in embedded applications.
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.
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 14 ·
Replies
14
Views
2K
  • · Replies 15 ·
Replies
15
Views
4K
  • · Replies 10 ·
Replies
10
Views
4K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
6
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 29 ·
Replies
29
Views
3K
Replies
11
Views
3K