Best Practices Programming Embedded Systems

Discussion in 'Embedded Systems and Microcontrollers' started by bobbylight, Apr 18, 2011.

  1. bobbylight

    Thread Starter New Member

    Apr 18, 2011
    4
    0
    I am programming a wireless fan controlled thermostat. Up until now I have only been writing code in assembly. I have written software code in C before but haven't yet tried writting embedded systems with C. I am wondering if C is used more commonly than Assembly in the field. Also, are there any common beginner mistakes associated with using C to write embedded systems? Does anyone know of some good links describing best practices and standards for writing embedded systems in C?
     
  2. mjhilger

    Member

    Feb 28, 2011
    119
    16
    I would say C is used quite heavily for embedded controllers. (There is an industry magazine "embedded systems" where many examples are in C.) Since many of the newer controllers are RISC machines, you should only break into assembly when you think the C is hindering your speed. That being said, you should get to know the controller family and understand its "stack" usage (usually limited stack) and registers. Also understanding the architecture can help you determine how to code a particular device interface efficiently. Just like in programming for a desktop, there are definitely optimal paths. The compilers today have very good optimization, but again, understand the inner workings can help as well.
    Just my $0.02
     
    bobbylight likes this.
  3. RiJoRI

    Well-Known Member

    Aug 15, 2007
    536
    26
    Pick up the book, "Code Complete" by Steve McConnell; Microsoft Press, ISBN 1-55615-484-4. This will cover a number of problems people come across in writing C code, which is applicable to micros as well.

    Google "Jack Ganssle". He has written articles about code nightmares.

    DESIGN BEFORE CODING.

    Use "Why I am doing this" comments instead of "What I am doing" comments. In six months a comment like:
    /*
    * Detect ringing phone line for 30 seconds
    */

    will be better than:

    Code ( (Unknown Language)):
    1.  
    2. [FONT="Courier New"]
    3. Areg = IPort & 1 /* load accumulator */    <- No, REALLY?
    4. if(Areg != LastTest){ /* Has the line changed? */ <- I'm NOT an IDIOT!!
    5. [/FONT]
    6. (My running comments after <-)
    7.  
    HTH,
    --Rich
     
    bobbylight likes this.
  4. bobbylight

    Thread Starter New Member

    Apr 18, 2011
    4
    0
    Thank you both very much. This was exactly what I was looking for. I just wanted to get an idea whether or not it would be worth investing the time to learn C, and avoid some pitfalls. I'll look into "code complete".
     
Loading...