Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

8051 codes by using C

  1. Jul 24, 2008 #1
    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?
     
  2. jcsd
  3. Jul 24, 2008 #2

    berkeman

    User Avatar

    Staff: Mentor

    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?
     
  4. Jul 25, 2008 #3
    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?
     
  5. Jul 25, 2008 #4

    berkeman

    User Avatar

    Staff: Mentor

    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.
     
  6. Jul 25, 2008 #5

    berkeman

    User Avatar

    Staff: Mentor

    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
     
  7. Jul 25, 2008 #6

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    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
     
  8. Jul 25, 2008 #7
    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.
     

    Attached Files:

  9. Jul 25, 2008 #8
    How can I combine them? Can anyone show some examples?

    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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: 8051 codes by using C
  1. 8051 microcontroller (Replies: 7)

  2. Interrupts on 8051 (Replies: 2)

Loading...