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 375
For example, in the following code segment the value of the variable “i” is incremented. This
is a shorthand method of executing the operation “i = i + 1;”
unsigned int i;
i++;
Example: It is not uncommon in embedded system design projects to have every pin on a microcon-
troller employed. Furthermore, it is not uncommon to have multiple inputs and outputs assigned to
the same port but on different port input/output pins. Some compilers support specific pin reference.
Another technique that is not compiler specific is bit twiddling. Figure A.7 provides bit twiddling
examples on how individual bits may be manipulated without affecting other bits using bitwise and
unary operators [ImageCraft].
Syntax
Description
a | b
a & b
a ^ b
~a
bitwise or
bitwise and
bitwise exclusive or
bitwise complement
Example
P2OUT |= 0x80; // turn on bit 7 (msb)
if ((P2IN & 0x81) == 0) // check bit 7 and bit 0
P2OUT ^= 0x80; // flip bit 7
P2OUT &= ~0x80; // turn off bit 7
Figure A.7: Bit twiddling [ImageCraft].
A.4.2 PROGRAMMING CONSTRUCTS
In this section, we discuss several methods of looping through a piece of code. We will examine the
“for” and the while” looping constructs.
The for loop provides a mechanism for looping through the same portion of code a fixed
number of times. The for loop consists of three main parts:
loop initiation,
loop termination testing, and
the loop increment.
376 A. PROGRAMMING
In the following code fragment the for loop is executed ten times.
unsigned int loop_ctr;
for(loop_ctr = 0; loop_ctr < 10; loop_ctr++)
{
//loop body
}
The for loop begins with the variable loop_ctr” equal to 0. During the first pass through the
loop, the variable retains this value. During the next pass through the loop, the variable “loop_ctr
is incremented by one. This action continues until the “loop_ctr” variable reaches the value of ten.
Since the argument to continue the loop is no longer true, program execution continues after the
close bracket for the for loop.
In the previous example, the for loop counter was incremented at the beginning of each loop
pass. The loop_ctr variable can be updated by any amount. For example, in the following code
fragment the loop_ctr” variable is increased by three for every pass of the loop.
unsigned int loop_ctr;
for(loop_ctr = 0; loop_ctr < 10; loop_ctr=loop_ctr+3)
{
//loop body
}
The “loop_ctr variable may also be initialized at a high value and then decremented at the
beginning of each pass of the loop.
unsigned int loop_ctr;
for(loop_ctr = 10; loop_ctr > 0; loop_ctr--)
{
//loop body
}
As before, the loop_ctr” variable may be decreased by any numerical value as appropriate for
the application at hand.
The while loop is another programming construct that allows multiple passes through a
portion of code. The while loop will continue to execute the statements within the open and close
brackets while the condition at the beginning of the loop remains logically true. The code snapshot

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