EXERCISE 1: Decisions and Macros

Decisions, Macros, and String Variables Microcontroller System Development EXERCISE 1: Decisions and Macros EXERCISE OBJECTIVE When you have complet...
Author: Aleesha Wheeler
1 downloads 2 Views 459KB Size
Decisions, Macros, and String Variables

Microcontroller System Development

EXERCISE 1: Decisions and Macros EXERCISE OBJECTIVE When you have completed this exercise, you will be able to describe the use of conditional operators and macros. You will use Flowcode and the Microcontroller System Development FACET board to simulate a car's directional signals. EXERCISE DISCUSSION One of the most important features of any software program is the ability to make decisions. How does a software program make decisions? The program evaluates whether a condition is true or false. If the condition is True, one sequence of steps will be followed. If the condition is False, another, different sequence of steps will be followed. That condition is actually a Boolean expression. Conditional operators compare two variable values to determine if they are less than, greater than, or equal to each other. The result will always be True or False (Yes or No). These conditional statements are often referred to as decision structures. Frequently, when programming decisions, the software code is identical except for the "response". This is a good example of an opportunity to use a macro. When the programming code gets to a certain point, the appropriate macro is referenced. The code in the macro is then followed by the program. If the program response does not require a particular macro, it is "bypassed" by the program. Macros are embedded in the source code. Macros are referenced by the main program when the parameters for that macro are met. This is accomplished by using referencing labels in the macro. Other Macro features As you work with the FLASH macro in this exercise, you will note that the dialogue window contains options additional to those you will be using. Local variables All of the variables you have used so far are 'global' variables. The word 'global' here refers to the fact that these variables are available at all points in your program. With small programs like the ones in this course this is not a problem – there are nearly 400 separate bytes of RAM we can use in the 16F 877A and it is unlikely that we will run out of variable space. However with larger programs - particularly those using strings - there is a chance that we will run out of variable space. Because of this, Flowcode has a feature that allows you to declare a number of variables that are only available while the macro is running. These are called 'local' variables. Flowcode will create these variables in RAM at the start of the Macro and it will delete them at the end of the macro, freeing up RAM space for other parts of the program. You should not have any real problems using local variables so we will not look at an example here.

166

Student Manual FACET by Lab-Volt

Microcontroller System Development

Decisions, Macros, and String Variables

Return values Macros have another useful feature that makes your program neater and which saves you allocating a further variable - the return value. If you have a macro that is carrying out mathematical operations, then it is often useful to have the macro return a value directly to the point in the program that called the macro. You could implement this by having a global variable assigning the calculated value to the global variable, and then picking up the result in your main program. However, returning the value in the macro is a neater option and it means that, again, you save RAM space. EXERCISE QUESTIONS Software programs make decisions using a. Boolean expressions b. conditional operators c. Both a and b are correct Conditional operators compare a. two variables. b. all string statements. c. all pre-programmed parameters. Macros are a. part of the programmed variable. b. created outside the main program. c. imbedded in source code. EXERCISE PROCEDURE Set up the equipment The following is the equipment you will need for this exercise: Multiprogrammer Hardware: Setting: PICmicro Device 16F877A Toggle Switch (Fast/Slow) N/A Toggle Switch (RC/XTAL) XTAL XTAL 19,660,800 Hz PORTA Switch Block PORTB LED Block PORTC N/A PORTD N/A PORTE N/A

Student Manual FACET by Lab-Volt

167

Decisions, Macros, and String Variables

Microcontroller System Development

Left or right? More and more PICmicro microcontrollers are finding their way into cars. Examples are engine management, anti-lock brakes, cruise-control, and air-conditioning. This task is more modest. We are going to program a PICmicro chip to control the car indicators or directional signals. Ideally when we press one switch, the left indicator will flash on and off. Pressing the other switch will flash the right indicator.

Set up the flowchart Launch Flowcode and start a new flowchart. Select the 16F877A, when the 'Choose a target' dialogue box appears. to check whether an indicator switch has been You are going to use the Decision icon pressed. If neither switch is pressed, the program follows the 'No' route and loops back to the beginning, When a switch is pressed, the program follows the 'Yes' route through the flowchart, causing the indicator to light for a short time, and then turn off for a short time. These times are set by the delays. As long as the switch is pressed, this process repeats, causing the indicator to flash on and off.

168

Student Manual FACET by Lab-Volt

Microcontroller System Development

Decisions, Macros, and String Variables

Create the following flowchart.

Student Manual FACET by Lab-Volt

169

Decisions, Macros, and String Variables

Microcontroller System Development

Single bit input 1. Double click on the upper Input icon. This opens the Input Properties dialogue box. 2. Type 'Check the left switch!' in the Display name: box. 3. Click on the 'Variables...' button to open up the Variable Manager dialogue box. 4. Click on the 'Add New Variable' button in the dialogue box. Create two variables, called LEFT, and RIGHT. The LEFT variable will record whether the left indicator switch is pressed. The variable RIGHT will record whether the right indicator switch is pressed.

5. Click on the LEFT variable to highlight it, and then click on the 'Use Variable' button. 6. This takes you back to the Input Properties box. You will use port A as the input port. Click on the 'Single Bit:' radio button, and leave the selection in the box next to it as 0. This means that only the value of bit 0 of port A will be transferred to the variable LEFT. This means that LEFT will always be either a 1 or a 0. 7. Click on the 'OK' button to shut down the Input dialogue window. 8. Now double click on the lower Input icon. Set up this Input Properties box in a similar way, to look like the example shown here. Setting up the decisions 1. Double click on the upper Decision icon to open the Properties dialogue box.

170

Student Manual FACET by Lab-Volt

Microcontroller System Development

Decisions, Macros, and String Variables

2. Type 'Left switch pressed?' in the Display name box. 3. The program makes a decision based on the value stored in a variable. We spell out what that decision is in the If box: Delete the '0' in the If box. Click on the 'Variables...' button. Click on LEFT, to select it, and then on the Use Variable button. Type '=1' after the word LEFT in the If box. 4. The Decisions properties box should now look like:

5. Ignore the Swap Yes and No option, which reverses the 'Yes' and 'No' routes on the flowchart. 6. Now click on the OK button, to close the dialogue box. 7. Double click on the lower Decision icon. Set up the properties as shown in the next box:

Set up the delays There are four delays on the flowchart. We will set up each one to do exactly the same thing cause a delay of 200 ms. 1. Double click on one of the Delay icons to open the Delay Properties box. 2. Change the value in the Delay value or variable: box to 200, giving a delay of 200 ms. The box should look like this:

Student Manual FACET by Lab-Volt

171

Decisions, Macros, and String Variables

Microcontroller System Development

3. Click on the OK button to close the dialogue box. 4. Do exactly the same for the other three delays. Set up the left output 1. Follow the Yes route from the Left switch pressed decision box. 2. Double click on the first (upper) Output icon, to open the Output Properties dialogue box. 3. Set it up to look like the one shown:

A word of explanation! This setup outputs the number 255 from the output port. Normally, this would light up all the LED's connected to the port. By clicking the Use Masking box, and selecting bit 0, we make all the LED's inactive apart from LED 0, the right hand one. This is one way to turn on only bit 0. Here are two alternative ways of doing the same thing! Outputting the value '1' (= 0000 0001 in binary) switches on only the LED attached to bit 0. But this command will also force all the other pins on port B to logic 0. The third option outputs value '255' (= 1111 1111 in binary,) and it puts all pins at logic 1. This should give you a hint of what the advantage of masking is. It allows you to alter the status of the lines on a port individually. Masking allows you to make a pin high or low without altering the status of the other pins on the same port.

172

Student Manual FACET by Lab-Volt

Microcontroller System Development

Decisions, Macros, and String Variables

4. Double click on the lower Output icon. Set up the properties as shown:

Outputting value '0' turns off all LED's connected to Port B.

Student Manual FACET by Lab-Volt

173

Decisions, Macros, and String Variables

Microcontroller System Development

Set up the right output 1. Now follow the Yes route from the Right switch pressed decision box. 2. Double click on the first Output icon you get to. Set up the properties for this output as shown in the next diagram:

Here is an alternative for this output:

As we want to light up the right hand LED, the output port needs to turn on bit 7. One way to do that is to output the value '128 ' (=1000 0000 in binary). The other way outputs 255 (= 1111 1111 in binary), but only activates a single output bit, bit 7 in this case.

174

Student Manual FACET by Lab-Volt

Microcontroller System Development

Decisions, Macros, and String Variables

3. Double click on the lower Output icon. Set up the properties as shown in the next graphic:

Adding the switches You will use two push switches, one to operate the left indicator, the other the right indicator. 1. Click once on the switch icon, and drag it into a suitable spot on the work area. 2. Select the Properties option. Choose two switches in the Number of Switches box. Click on the Switch Labels tab, then Type "Left" in the "0" box and "Right" in the "1" box. Click OK to close this Dialog box.

3. Select the Component Connections option. Connect the switches to Port A, using the down arrow at the end of the Port box. Click on the Done button to finish the process. Student Manual FACET by Lab-Volt

175

Decisions, Macros, and String Variables

Microcontroller System Development

Adding the LED array 1. Click once on the LED icon. Place the LED array where you want it on the work area. 2. Select the component's Connections... option on the menu to open the Specify Component Pin Connections dialogue box. 3. Click on the down arrow at the end of the Port window, and click once on the PORT B option to connect the LEDs to the pins on Port B. Click on the Done button. 4. Change the color of the LEDs to yellow by clicking on the Properties option and selecting the color in the LED Colour: box. Change the number of LED's to two and the orientation to vertical. Select the LED Labels tab, then type "Left" in the "0" box and "Right" in the "1" box. Click OK to close this Dialog box. 5. Set up the clock for XTAL in Flowcode and with the Toggle switch on the Microcontroller System Development FACET Board. 6. Save the Program as Ex1 Decisions and Macros with your initials to your folder, then download it to the Microcontroller System Development FACET Board. The flowchart should now look like the one shown here:

176

Student Manual FACET by Lab-Volt

Microcontroller System Development

Decisions, Macros, and String Variables

Simulation 1. Now run the simulation by clicking on the Run button. 2. Close the Variables - Unavailable and Call Stack - Unavailable boxes. 3. Move the cursor over switch A0 and hold the switch closed (on). The left indicator, LED B0, flashes on and off until you release the switch. 4. Close the other switch and the right indicator flashes. 5. Set up the clock for XTAL in the Flowcode and with the Toggle switch on the Microcontroller System Development FACET Board. 6. Save the Program as Ex1 Decisions and Macros with your initials to your folder, then download it to the Microcontroller System Development FACET Board. PIC strikes again! The important thing is that the same PICmicro chip could be carrying out other functions in the car as well. We used only two of the inputs, and two outputs. The program could go on and control other devices in the car using the remaining inputs and outputs. Left or right? We are going to modify the flow chart that you developed for the car directional signals. A large part of the flow chart is taken up with causing either the left or the right directional signal to flash. The only difference between these two operations is the number that is sent to the output port. We are going to create a macro that outputs whatever number we give it, pauses, then outputs zero, and pauses again. Create new macros 1. From the menu select MACRO...NEW. In the 'name of new macro:' text box type in LEFT. This is the name of the new macro. Underneath enter the description as 'Left flash routine'. You should have a dialogue window that looks like the one shown here. Select 'OK'. Don't worry about all of the other text boxes on the macro dialogue window for now.

Student Manual FACET by Lab-Volt

177

Decisions, Macros, and String Variables

Microcontroller System Development

2. From the menu select MACRO...SHOW...LEFT. From the main flow chart cut the icons from the Yes branch of the first decision box and paste them into the LEFT macro. You should have a macro that looks like the one shown here.

3. Similarly set up another new macro called RIGHT with a description 'Right flash routine'. From the main flow chart cut the icons from the Yes branch of the second decision box and paste them into the RIGHT macro.

178

Student Manual FACET by Lab-Volt

Microcontroller System Development

Decisions, Macros, and String Variables

4. In the main flow chart insert a Macro icon into each Yes branch of each decision icon. Double click on each icon to set them to call the LEFT and RIGHT macros respectively. You should have a flowchart that looks like this:

5. Save the program as Ex1 Decisions and 2 Macros with your initials to your folder. More efficient macros 1. Run the program in the simulator. Did you find that it has much the same effect as before? However, now we have created two macros that we could use at other places in our program. However, it is possible to use another technique so that we can cut the number of macros down to just one that would also be even more flexible. 2. Create a new macro called FLASH. Click on the 'Edit Parameters' button and Add a variable (type Byte) called 'whichone' and select 'OK'. In the Description of new macros box, enter "Flashes a pin on the chip." You should have a Macro dialogue window that looks like the one shown here. Student Manual FACET by Lab-Volt

179

Decisions, Macros, and String Variables

Microcontroller System Development

3. Next insert an output icon that outputs the variable FLASH.whichone to port B. Flowcode knows that the new macro FLASH takes one parameter that is passed to it, and when you click on the Output icon 'Variables or value:' drop box within the FLASH macro Flowcode automatically gives you the choice of using FLASH.whichone.

4. Next insert a 200 ms delay, an Output icon which sends 0 to port B, and another 200 ms delay. You should end up with this:

180

Student Manual FACET by Lab-Volt

Microcontroller System Development

Decisions, Macros, and String Variables

5. On the main flowchart double click on the macro icon that calls the LEFT macro. In the 'Macro:' dialogue box select FLASH. below the 'Macro:' dialogue box you will see that Flowcode has recognized that this needs a parameter and the 'parameters:' dialogue box is enabled to allow you to enter the parameter. Enter the number '1':

6. Similarly for the RIGHT macro icon call the FLASH macro with a parameter of '2'. Your whole flowchart should now look like this:

Student Manual FACET by Lab-Volt

181

Decisions, Macros, and String Variables

Microcontroller System Development

You now have the same functionality as the previous program but you have only used one macro. That's not too important in a small program like this, but in later programs you will appreciate the efficiency of this method of writing programs. Simulation of "Call Macro" 1. Display the main program by minimizing the screen and restoring the main program using the buttons in the upper right corner of the screen. 2. Open the Switch Properties Dialog box and select Toggle from the Switch type drop down menu. This will allow you to latch the left or right switch in the "on" position while you step through the program. 3. Click on the left switch to latch it on. 4. Select Step Into from the Main Menu. You will see the Variables and Call Stack boxes open.

182

Student Manual FACET by Lab-Volt

Microcontroller System Development

Decisions, Macros, and String Variables

5. Watch the red outline go down the Flowcode each time you click on the Step Into button. When it reaches the Right switch input code in the Variables Box, you will see a 1 in the Left value field. 6. Because of this, the next two clicks of the Step Into button will take the Left Yes path to the macro call. 7. Click on Step Into again. The program jumps into the macros program and displays its name in the Variables and Macros Calls boxes. 8. Continue to Step Into. Observe how, once the Left LED turns on and off, the Step Into box jumps back into the main program at the point it jumped out. 9. Try the same steps for the Right Switch, but remember to open the toggle for the Left Switch first. 10. Next, try running this program on the Microcontroller System Development FACET Board. Set up the clock parameters, save the program as Ex1 Decisions and 1 Macro with your initials to your folder, then download the program.

Student Manual FACET by Lab-Volt

183

Decisions, Macros, and String Variables

Microcontroller System Development

EXERCISE QUESTIONS 1. The original program in this exercise was created a. using one macro - FLASH. b. using two macros - LEFT and RIGHT. c. without the use of macros. 2. The program was then modified to replace the Left and Right blinking sequences with a. using one macro - FLASH. b. using two macros - LEFT and RIGHT. 3. The final modification program a. used one macro - FLASH. b. used two macros - LEFT and RIGHT. c. did not use macros.

184

Student Manual FACET by Lab-Volt

Microcontroller System Development

Decisions, Macros, and String Variables

EXERCISE CONCLUSION • Software programs make decisions by evaluating whether a condition is true or false. • Conditional operators compare two variable values. These conditional statements are also referred to as decision structures. • Macros can be used when software code is identical except for the response. Macros are imbedded in the source code. • Local variables are called only while a macro is running. Global variables are available throughout an entire program. REVIEW QUESTIONS 1. A software program can make a decision by: a. evaluating whether a value is True or False. b. comparing two variable values to determine if they are less than, greater than, or equal to each other. c. Both of the above are correct. d. None of the above is correct. 2. Macros offer a distinct advantage when software programs have few repetitive steps. a. True b. False 3. Using _______________ in a macro identifies the parameters for use to the software program. a. referencing labels b. software tags c. programming labels d. numerical tags 4. Variables that are available at any point in the program are __________ variables. a. local b. global c. repetition structure d. counter 5. An easy way for the main program to pick up a calculated value from a macro is to use a(an): a. local variable b. global variable c. return value d. calculated value

CMS AVAILABLE FAULTS AVAILABLE

Student Manual FACET by Lab-Volt

185