O'Reilly logo

Microcontroller Programming and Interfacing Texas Instruments MSP430 by Daniel J. Pack, Steven F. Barrett

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

366 A. PROGRAMMING
comment. A multi-line comment can be constructed using a /at the beginning of the comment
and a / at the end of the comment.
At the beginning of the program, comments may be extensive. Comments may include some
of the following information:
file name
program author
revision history or a listing of the key changes made to the program
compiler setting information
hardware connection description to microcontroller pins
program description
A.3.2 INCLUDE FILES
Often you need to add extra files to your project besides the main program. For example, most
compilers require a “personality file on the specific microcontroller that you are using. This file is
provided with the compiler and provides the name of each register used within the microcontroller.
It also provides the link between a specific register’s name within software and the actual register
location within hardware.These files are typically called header files and their name ends with a “.h”.
Within the C compiler, there will also be other header files to include in your program such as the
math.h” file when programming with advanced math functions.
To include header files within a program, the following syntax is used:
//include files
#include<file_name1.h>
#include<file_name2.h>
A.3.3 FUNCTIONS
In the top down design, bottom up implementation approach to a microcontroller based project
including both hardware and software, the project is partitioned into systems, subsystems, etc. The
idea is to take a complex project and break it into doable pieces with a defined action.
We use the same approach when writing computer programs. At the highest level is the main
program which calls functions that have a defined action. When a function is called, program control
is released from the main program to the function. Once the function is complete, program control
reverts back to the main program.
A.3. ANATOMY OF A PROGRAM 367
Functions may in turn call other functions as shown in Figure A.2.This approach results in a
collection of functions that may be reused over and over again in various projects. Most importantly
the program is now subdivided into doable pieces, each with a defined action. This makes writing
the program easier but also makes it much easier to modify the program since every action is in a
known location.
void main(void)
{
:
function1( );
:
}
void function1(void)
{
:
function2( );
:
}
void function2(void)
{
:
}
Figure A.2: Function calling.
There are three different pieces of code required to properly configure and call the function:
the function prototype,
the function call, and
the function body.
Function prototypes are provided early in the program as previously shown in the program
template. The function prototype provides the name of the function and any variables required by
the function and any variable returned by the function.
The function prototype follows this format:
return_variable function_name(required_variable1, required_variable2);
If the function does not require variables or sends back a variable the word void” is placed in
the variable’s position.
The function call is the code statement used within a program to execute the function. The
function call consists of the function name and the actual arguments required by the function. If the

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required