Quick C : Looking at the default code for Lab 4B (prelim. version. Oct 2006)

Background:  Here we reproduce and vet (analyze) the code you are to modify for your Lab 4B, programming a rover to knock over a block.

We copy source code from the IAR screen as Courier font, but lose the font colors and indent, which we imperfectly restore... info after // are comments in the code, more comments in RED are in this file...

#include <msp430x12x2.h> // defines various MSP430 terms
#include handles declarations, as found in a dot-h header file, which in this case contains all the MSP430 register and pin definitions; it is a necessary line of code for variable names to come, such as WDTCTL, WDTPW, WDTHOLD seen in the watchdog timer line below.

Before reaching main{} as the official start of the C program, we have two functions, named and defined below: Notice that in function turn_delay msec is a declared integer variable to be the input. the function runs a for-loop 50*msec times and uses up msec worth of real time. the number 50 had to be determined by experimenting with code execution times. what goes on in the function is between curly brackets { }. The form ii++ means increment ii by 1.

void turn_delay(long int msec) // a function to be called
{
  long int tics, ii;
  tics = 50 * msec;
  for (ii=0; ii < tics; ii++)
  { }
}

"What goes on in the function stays in the function"
The variables declared in the functions are unknown to main{}: they are local variables, distinct from the "global" variables of main.

mask_op is needed to "bit flick" pins dedicated to input and output from the microcomputer chip. input orig is the name of a port (1, 2, or 3) which will be RETURNED at the end of the function, changed by the mask operations.

int mask_op(int orig, int mask_zone, int mask_pattern)
// bit flicking with mask operations. only bits masked are changed
// mask_zone blocks out mask with 0s in hex (AND)
// mask_pattern admits with 1's in pattern only (OR)

{
orig &= mask_zone; // orig <- orig & mask_zone;
This assignment statement takes as one input the "destination" orig and as the second input mask_zone. The ampersand & means that the individual bits of orig and mask_zone are AND'd together: if orig = 1111 1111 and mask_op = 0000 0011 then orig will become 0000 0011. The top six bits are "masked out".
orig |= mask_pattern; // orig <- orig OR mask_pattern;
// use of assignment statements
return orig;
// could use orig& ref ptr
}

Here is where the main program starts, again inside curly brackets.

main() // start of main program; single step starts here
{ // one of the curly brackets encompassing main{}
Next comes the declarations of variables to be used in main:
Possible data types: int, float, arrays... some of the variables are vestigal from a previous rev
and draw complaints from the complier, in the form of warnings...

unsigned long pp = 1; // noninfinite loop counter set to 1
unsigned long pp_max = 100;
int tstP1 = 0x33;
int tstP3 = 0x00;
int ii; //jj; // for future photoxsistor A-D signal
variable jj is not used in this version of the default code...
int cond[10], SwL, SwR, SwLR;
int mask_zone, mask_zone1, mask_zone3, mask_pattern, strt_flg;
int divi, state, tim_cnt;

Switch Left, Switch Right and Switch Left or Right are all set to zero in this initialization.
SwL = 0; SwR = 0; SwLR = 0;
strt_flg = 0; // ?

WDTCTL = WDTPW + WDTHOLD; // Stop Watchdog WDT

The statement above seems to be needed in most of the code we have... it supresses the Watchdog timer you can read more about in the MSP430 documentation.

 

Possible FTQ: Modify the distribution code so that the simulation automatically "turns left" for 1 sec 5 sec after it has started (a timing route). For this FTQ you should understand the form of a C if statement:
if (pp == 36) {
   P3OUT = mask_op(P3OUT, 0x3F, 0xC0);

}

The logic expression after if must be in parentheses; the actions to be taken if the logic expression is TRUE must be inside curly brackets. Note the use of logical equals "==" in the expression above, different from assignment =. .

Free Advice: Consult the yellow IAR User Guide for information about the features of the debugger, including single-stepping through code.

Additional notes: You will want to read the IAR software documentation (or see lab demo) on how to single-step through the code and verify how the LabVIEW VI exercises the MSP430 microcomputer code. Show how the program breaks out of the While loop that waits for the "red" start button from D6 of the green connector block to become active.

Modifying code to download to the rover.
Using what you learned in Lab4pre about modifying, rebuilding and debugging code, change whatever you like in your program, but (1) hit Rebuild often to make sure you have no typos and (2) leave the red button wireless start and stop feature in place so you can start and stop the rover from outside the track.

Download your modified, rebuilt, code directly into the MSP430F123(2) on the rover, using the Debug command. If you are not going to Single Step or Go To Cursor, then just hit Go and Stop to make sure your msp430 is running its infinite loop. Pull the debug cable off the board: the board on the rover is powered through an on-board 3.3v regulator.

Likely you will need to mount the rover up near the computer if you want to "debug" from. Have the rover "up on blocks" so you can watch the wheels rotate without the bot driving itself off a cliff.