MSP430 - C Essentials

I had never intended for this to be a tutorial on programming in the C language. There are many excellent tutorials available online. I cannot endorse any particular site but will only provide an example:

However, I will make a few comments along the way if it helps the newcomer to C programming.

Comment Lines

There are two styles of comment statements.

// The double slash creates a comment on a single line.

This is an example
of a block of lines
creating a comment block

This is another example of
a block of comment lines

Comment blocks cannot be nested, i.e. you cannot surround a comment block with another comment block.

I prefer to use the double slash for comments and reserve the comment block in order to disable blocks of code while debugging the program.


There is a set of instructions that are used by the compiler to add functionality to the compilation process. These statements do not actually create any computer code but are used to direct the compiler in telling it how to process the code. These instructions are called preprocessor directives.

Here are two common preprocessor directives:

#include "thisfile.h"

The #include statement is used to chain any number of related files into a single project.

When your project becomes very large this is an essential mechanism for dividing the project into separate modules. It is also an important mechanism for creating your own library of common functions which you can reuse from project to project. This avoids the need to cut and paste code snippets into one huge file.

---------- Advanced Topic ----------
If you are a beginner, you may skip this section.

Typically, you will put your library functions that you create into a file with a name such as "mylib.c"

You will create an additional header file called "mylib.h" which will contain the definitions and prototypes to all constants, structures, and functions to be used by the library.

The #define preprocessor directive is used to declare constants to be used in the program. This is always the preferred way of using constants because you can define all your constants in one place or even in a header file "constant.h".

#define ON 1
#define OFF 0
#define LED1 P1OUT_bit.P0

These would allow you to create more simplified statements such as

LED1 = ON;

which the compiler will translate into

P1OUT_bit.P0 = 1;

Think of #define statements as word or text substitutions. Wherever the word is used in your code it will be replaced with its definition.

It is common practice to declare all constants using all caps.

Note that C is case sensitive.

Note also that preprocessor statements do not end with semicolon.

C Statements

A single C statement ends with a semicolon.
You can create a compound statement which is a block of single statements enclosed with a pair of curly braces. Compound statements can be nested, for example,

{ // example of compound statement


   while (1)
      { // while loop
         LED1 = ON;
         LED1 = OFF;
      } // end of while loop

} // end of example

Literal Constants

P1OUT = 16;
P1OUT = 0x10;

(P1OUT = 0b00010000; is not recognized by IAR compiler).

These are examples of literal constants.
These statements result in exactly the same code being generated.

Where would you choose one style over the other?

Rule of thumb:

Use decimal notation when you are doing a decimal operation, e.g.

seconds = 60*minutes;

Use hexadecimal (or binary if allowed) when you are referring to a bit position, pattern or binary mask operation.

P1OUT = 0x15;

This instruction sets bits 4, 2 and 0 of PORT1.

Blog entry information

Last update

More entries in General

More entries from MrChips

Share this entry