MCU 101 - C Programming For Embedded Systems

Document created by GUO XIAOLI Employee on Jul 12, 2012Last modified by johnmc on Sep 10, 2012
Version 6Show Document
  • View in full screen mode

Take some time to get yourself familiar with C programming before you continue on the programming tutorial.

 

Here is a list of topics that you should be comfortable with, and a couple of good tutorials below.

Topics included:

  • Program Structure
  • Commenting
  • Variables
  • Keywords
  • Data Types
  • Decimal, Binary and Hexadecimal Equivalents
  • ASCII Text/Number Conversion
  • Math Operators
  • Increment & Decrement
  • Shift
  • Logical Operators
  • Bitwise Operators
  • Loops
  • If Statement
  • Switch Statement
  • Functions
  • Recursion
  • Local Variables vs. Global
  • Arrays
  • Pointers
  • Typdef, struct and union
  • Preprocessor Directives
  • Static, const and Volatile Keywords

 

Tutorial 1: PSU Intro to C for Embedded Design

  From PSU Freescale Cup Senior Design Course

 

Tutorial 2: Learning Programming with C

This Freescale course consists of a collection of lessons that will introduce you to the fundamentals of programming using the C programming language.

 

Coding for Readability

Sometimes when a project has the ability to grow with new features, it is best to code in modules. This allows one to easily take a more modular approach to designing their program. Despite the fact that C does not support Classes like C++ does, you can create structures that can be addressed globally with little code, which is especially useful for microcontroller based projects.

An example of a structure which will be made global:

  • This goes in the globals.h file

typedef struct {
  unsigned char ServoPWM;
  char ServoAngle;
  unsigned char DrivePWM;
  int TimeOut;
  int Current;
  int Speed;
} sMotor;

extern volatile sMotor Motor;

  • This will go in any other file that we want our structure to be accessible from

#include <Globals.h>

volatile sMotor Motor;

  • This is how to address the variable in the structure

#include <Globals.h>
volatile sMotor Motor;

  • If you decided to have two distinguishable motors you could do this in the globals.h

extern volatile sMotor Motor1;

extern volatile sMotor Motor2;

  • Then do this in the other files

volatile sMotor Motor1;
volatile sMotor Motor2;

 

Helpful Hints

  • In developing an algorithm to detect the line position, we found two basic errors in the coding practice which caused catastrophic errors in line detection. Both of these tips are very basic coding practice. First, when using C code, it usually benefits the user to initialize all variables to some value, especially if computations are involved. Often times when the value wasn't initialized it would seem to acquire a wrong value seemingly from nowhere. Secondly, when doing calculations with arrays, make sure to do calculations with array indices that actually exist. Many times we would make the mistake in our loops of trying to use an index that wasn't assigned a value. Therefore it would acquire an unknown value from memory that caused errors in our calculations.

Attachments

Outcomes