Generating Time Delays in 8051 Code Using C

  • Thread starter Thread starter soul
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around generating time delays in C programming for the 8051 microcontroller, focusing on the implications of different compilers and the use of timers versus loops for precision timing. Participants explore the use of real-time functions and operating systems in this context.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Exploratory

Main Points Raised

  • Some participants express concern that different C compilers may generate varying assembly code, potentially leading to inconsistent time delays across platforms.
  • One participant suggests that real-time functions may not be available in standard ANSI C, proposing that a real-time operating system (RTOS) might be necessary for precise timing on the 8051.
  • Another participant mentions using a specific microcontroller (DS89C420) and compiler (ProView 32), seeking clarification on real-time functions and operating systems.
  • Some participants advocate against using for loops for generating time delays, citing issues with CPU cycle consumption and power efficiency, recommending timers or counters instead.
  • There is mention of a variation of ANSI C called "Neuron C" that includes real-time function extensions, suggesting that similar options might exist for the DS89C420.
  • One participant shares a link to resources on RTOS options for the DS89C420, noting that the chip has been obsoleted in favor of a newer variant.
  • A participant requests examples of combining assembly with C code for real-time functionality, indicating challenges with syntax errors in their attempts.

Areas of Agreement / Disagreement

Participants express differing views on the best approach to generating time delays, with some advocating for timers and others discussing the potential use of for loops. There is no consensus on the best method or the necessity of real-time functions, and the discussion remains unresolved regarding the integration of assembly with C code.

Contextual Notes

Limitations include the dependence on specific compiler behaviors and the potential lack of real-time functions in standard ANSI C. The discussion also highlights the challenges of combining assembly and C code, particularly regarding syntax issues.

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
3K
  • · Replies 15 ·
Replies
15
Views
4K
  • · Replies 10 ·
Replies
10
Views
4K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
6
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 29 ·
Replies
29
Views
4K
Replies
11
Views
3K