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 377
below will implement a ten iteration loop. Note how the “loop_ctr” variable is initialized outside of
the loop and incremented within the body of the loop. As before, the variable may be initialized to
a greater value and then decremented within the loop body.
unsigned int loop_ctr;
loop_ctr = 0;
while(loop_ctr < 10)
{
//loop body
loop_ctr++;
}
Frequently, within a microcontroller application, the program begins with system initialization
actions. Once initialization activities are complete, the processor enters a continuous loop.This may
be accomplished using the following code fragment.
while(1)
{
}
A.4.3 DECISION PROCESSING
There are a variety of constructs that allow decision making. These include the following:
the if statement,
the if–else construct,
the if–else if–else construct, and the
switch statement.
The if statement will execute the code between an open and close bracket set should the
condition within the if statement be logically true.The basic if statement can be extended to include
and else alternative. If the if conditional is not true, the bracketed activities are executed.The if–else
construct can be further extended to included multiple if–else if–else conditionals.
Example: The Blinky 602A robot is equipped with three infrared (IR) sensors to determine the
location of maze walls and steer the robot through the maze. Separate analog-to-digital (ADC)
channels are used to measure the sensor voltage and hence determine the location of maze walls.
The sensor readings designated lt_sensor (left sensor), ct_sensor (center sensor), and rt_sensor (right
378 A. PROGRAMMING
sensor) are compared to a threshold to determine if a maze wall is present. The function “deter-
mine_robot_action” is used to determine the action required when different wall configurations are
sensed.
//***********************************************************************
void determine_robot_action(void)
{
//Implement robot action truth table
if((lt_sensor<threshold)&&(ct_sensor<threshold)&&(rt_sensor<threshold))
robot_action = straight_ahead;
else if((lt_sensor<threshold)&&(ct_sensor<threshold)
&&(rt_sensor>=threshold))
robot_action = straight_ahead;
else if((lt_sensor<threshold)&&(ct_sensor>=threshold)
&&(rt_sensor<threshold))
robot_action = turn_right;
else if((lt_sensor<threshold)&&(ct_sensor>=threshold)
&&(rt_sensor>=threshold))
robot_action = turn_left;
else if((lt_sensor>=threshold)&&(ct_sensor<threshold)
&&(rt_sensor<threshold ))
robot_action = straight_ahead;
else if((lt_sensor>=threshold)&&(ct_sensor<threshold)
&&(rt_sensor>=threshold))
robot_action = straight_ahead;
else if ((lt_sensor>=threshold)&&(ct_sensor>=threshold)
&&(rt_sensor<threshold))
robot_action = turn_right;
else if((lt_sensor>=threshold)&&(ct_sensor>=threshold)
&&(rt_sensor>=threshold ))
robot_action = reverse;
A.4. FUNDAMENTAL PROGRAMMING CONCEPTS 379
else
robot_action = straight_ahead;
}
The switch statement is used when multiple if-else conditions exist. Each possible condition is
specified by a case statement. When a match is found between the switch variable and a specific case
entry, the statements associated with the case are executed until a break statement is encountered.
Example: Suppose eight pushbutton switches are connected to P2IN. Each switch will implement a
different action. A switch statement may be used to process the multiple possible decisions as shown
in the following code fragment.
void read_new_input(void)
{
new_PORT2 = P2IN; //status change PORT2?
if(new_PORT2 != old_PORT2)
switch(new_PORT2)
{
//process change in PORT2 input
case 0x01: //P2IN[0]
//P2IN[0] related actions
break;
case 0x02: //P2IN[1]
//P2IN[1] related actions
break;
case 0x04: //P2IN[2]
//P2IN[2] related actions
break;
case 0x08: //P2IN[3]
//P2IN[3] related actions
break;
case 0x10: //P2IN[4]
//P2IN[4] related actions
break;
case 0x20: //P2IN[5]

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