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

A.4. FUNDAMENTAL PROGRAMMING CONCEPTS 371
for accurate and predictable results. The modifier unsigned indicates all bits will be used to specify
the magnitude of the argument. Signed variables will use the left most bit to indicate the polarity
(±) of the argument.
A global variable is declared using the following format provided below. The type of the
variable is specified, followed by its name, and an initial value if desired.
//global variables
unsigned int loop_iterations = 6;
A.3.6 MAIN PROGRAM
The main program is the hub of activity for the entire program.The main program typically consists
of program steps and function calls to initialize the processor, followed by program steps to collect
data from the environment external to the microcontroller, process the data and make decisions, and
provide external control signals back to the environment based on the data collected.
A.4 FUNDAMENTAL PROGRAMMING CONCEPTS
In the previous section, we covered many fundamental concepts. In this section, we discuss operators,
programming constructs, and decision processing constructs to complete our fundamental overview
of programming concepts.
A.4.1 OPERATORS
There are a wide variety of operators provided in the C language. An abbreviated list of common
operators are provided in Figures A.5 and A.6.The operators have been grouped by general category.
The symbol, precedence,and brief description of each operator are provided.The precedence column
indicates the priority of the operator in a program statement containing multiple operators. Only
the fundamental operators are provided. For more information on this topic, see Barrett and Pack
in the Reference section at the end of the chapter.
A.4.1.1 General operations
Within the general operations category are brackets, parenthesis, and the assignment operator. We
have seen in an earlier example how bracket pairs are used to indicate the beginning and end of the
main program or a function. They are also used to group statements in programming constructs and
decision processing constructs. This is discussed in the next several sections.
The parenthesis is used to boost the priority of an operator. For example, in the mathematical
expression 7 x 3 + 10, the multiplication operation is performed before the addition since it has
a higher precedence. Parenthesis may be used to boost the precedence of the addition operation. If
we contain the addition operation within parenthesis 7 x(3 + 10), the addition will be performed
before the multiplication operation and yield a different result from the earlier expression.
372 A. PROGRAMMING
Symbol
Precedence
Description
General
{ } 1
Brackets, used to group program statements
( ) 1
Parenthesis, used to establish precedence
=12
Assignment
Symbol
Precedence
Description
Arithmetic Operations
*3
Multiplication
/ 3 Division
+ 4 Addition
- 4 Subtraction
Symbol
Precedence
Description
Logical Operations
< 6 Less than
<= 6
Less than or equal to
> 6 Greater
>= 6
==
!=
&&
||
7
7
9
10
Greater than or equal to
Equal to
Not equal to
Logical AND
Logical OR
Figure A.5: C operators. (Adapted from [Barrett and Pack]).
The assignment operator (=) is used to assign the argument(s) on the right-hand side of an
equation to the left-hand side variable. It is important to insure that the left and the right-hand side
of the equation have the same type of arguments. If not, unpredictable results may occur.
A.4. FUNDAMENTAL PROGRAMMING CONCEPTS 373
Symbol
Precedence
Description
Bit Manipulation Operations
<< 5 Shift left
>> 5
Shift right
& 8 Bitwise AND
^ 8 Bitwise exclusive OR
| 8 Bitwise OR
Symbol
Precedence
Description
Unary Operations
! 2 Unary negative
~2
One’s complement (bit-by-bit inversion)
++ 2 Increment
-- 2
type(argument)
2
Decrement
Casting operator (data type conversion)
Figure A.6: C operators (continued). (Adapted from [Barrett and Pack]).
A.4.1.2 Arithmetic operations
The arithmetic operations provide for basic math operations using the various variables described
in the previous section. As described in the previous section, the assignment operator (=) is used to
assign the argument(s) on the right-hand side of an equation to the left-hand side variable.
Example: In this example, a function returns the sum of two unsigned int variables passed to the
function.
unsigned int sum_two(unsigned int variable1, unsigned int variable2)
{
unsigned int sum;
sum = variable1 + variable2;
return sum;
}

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