Wednesday, April 3, 2013

New Concepts, The if statement, and Aliasing

We're going to examine creating an alias for our inputs and outputs to help us better read our code.  Additionally, we're going to rewrite our first program a different way to examine the "if" statement.  We don't need to change anything about our hardware, so this lesson will only look at the software.  Let's get started.

Aliasing Pins

When your programs start getting long, and you're utilizing a large number of inputs and outputs, it becomes hard to keep track of what each pin is being used for.  Assigning a name, or alias to some of the pins can make writing and reading the code easier.  This is helpful only in the program-writing phase, and doesn't effect the program in any way.

Looking at our first project, we want to be able to reference GP3 as "button" and GP0 as "led".  To do that, we need to define those bits as our alias names.  That is done using the #define preprocessor directive.
#define button GPIObits.GP3
#define led GPIObits.GP0

We can now write:
while(button==1)
led=1;

The if Statement

The if statement is a way for a program to make a decision about whether or not to execute one or more instructions based on some condition.  In our case, the condition is whether or not the button is pressed.  Rather than saying "while the button is pressed, the LED is on" like we did in the first program, we're going to simply rephrase it and say "if the button is on, turn on the LED".

When using an if statement, we don't need to use braces if only one instruction follows the if.  If more than one instruction is to be executed when the if condition is true, we do need the braces.  This example only contains one instruction after the if.  The program in figure 1 will operate similarly to our first program.  It should be noted though, that the if statement is not a loop like while is.  The operator that follows if will be evaluated, and if it's true, the instruction will be executed.  The program will then continue on to the rest of the instructions regardless of whether or not the button is still pressed.  Since we have only one other instruction which turns off the LED, when we press and hold the button in this program the LED will flash on and off in rapid succession because each time we loop around the if is executed.  Because of the speed of instruction execution in a microcontroller, the LED flashes on and off about 500,000 times per second, making it appear as though the LED remains lit.  However, because of this rapid on/off cycle, the LED will appear to be dimmer than when we used the while loop.  When the button is not pressed, the if statement will not be executed at all, and the LED will remain off.  Compiling the program reveals that this is a smaller program than in example 1, using one less word of program memory space.

Figure 1
This program is intended only as an example of making decisions using an if statement.  More complex programs tend to process information on the condition of some variable, where using an if statement makes more sense than in this example.

No comments:

Post a Comment