Best Practices Programming Embedded Systems

Thread Starter

bobbylight

Joined Apr 18, 2011
4
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?
 

mjhilger

Joined Feb 28, 2011
118
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
 

RiJoRI

Joined Aug 15, 2007
536
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?
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:

Rich (BB code):
Areg = IPort & 1 /* load accumulator */    <- No, REALLY?
if(Areg != LastTest){ /* Has the line changed? */ <- I'm NOT an IDIOT!!

(My running comments after <-)
HTH,
--Rich
 

Thread Starter

bobbylight

Joined Apr 18, 2011
4
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".
 
Top