5. Reading Data on Input Lines
In this chapter a port is used to input data. It focuses on reading those data and making decisions based upon the data. Some basic switch configurations are provided which is used to stop and start the binary counting of the program. To implement the switch function, one component (switch), two icons (Input and Decision) and a new variable are added. It provides detailed information on working of the program. It also provides alternative ways to program a function. Use of Component Macros and Flowchart Macros are also described in the chapter.
Port; Data input; Data reading; Switch configurations; Binary counting; Input icons; Decision icons; Component Macros; Flowchart Macros
In Chapter 4 we focused on outputting data (binary numbers) to an 8-bit I/O Port B. We connected LEDs to Port B pins to display the data (binary numbers) being output on the microcontroller port. In this chapter we will use a port to input data. We will read those data and make decisions based upon the data.
As stated in previous chapters, microcontrollers see a binary “1” as approximately +5volts and a binary “0” as approximately 0volts (or ground). We want to supply binary “1”s and “0”s to an I/O line of the microcontroller and have the microcontroller read its value. Figure 5.1 shows two basic switch configurations that may be connected to an I/O pin of a microcontroller. The switch label SW1 outputs a normally high signal – binary “1” – to an I/O pin. When the momentary contact button is pressed, the output of the configuration switches to a binary low signal.
The switch labeled SW2 operates in an opposite manner to SW1. This information lets us add a switch to our LED display circuit. We will use the circuit to stop and start the binary counting of the program. Figure 5.2 shows the inclusion of a switch connected to pin 17, labeled RA0. This is a normally high switch configuration.
Figure 5.3 is the new flowchart to implement the switch function.
To implement this flowchart we need to add one component (switch), two icons (Input and Decision) and a new variable Y.
The Input Icon First
a. Select the Input icon from the left toolbar and drag it under the A: connection point (see Figure 5.4).
b. Double click on the Input icon to bring up its Properties window.
c. Change Input to “Single bit.”
d. Click on “Variables” to bring up the Variable Properties window.
e. In the Variable Properties window create an integer variable labeled “Y.”
f. Close the Variable Properties window. This drops you back down to the Input Properties window.
g. In the Input Properties window, select the variable “Y” in the Variable selection box (see Figure 5.5).
h. Hit OK.
Next We Add the Decision Icon
b. Double click on the Decision icon to bring up its Properties window.
c. In the If: text box, type in and replace the “0” with a “Y” (see Figure 5.6) and hit OK.
Next we need to move our icons into the Decision icon branch. Select the Output icon with your mouse. Keeping the left mouse button pressed, drag over the small arrow (which represents the Output icon) to the outside branch of the Decision icon (see Figure 5.7).
Then release the mouse button. The Output icon will have been moved to the outside branch of the Decision icon (see Figure 5.8).
Using the same procedure, select and drag the Delay icon and Calculation icon to the outside branch of the Decision icon. When you are finished your screen will look like Figure 5.9.
We are almost ready to run. To finish the program we need to add a switch. Go to the Common menu item on the top toolbar and select the Toggle switch (see Figure 5.10).
Hit the left mouse button with the Toggle switch highlighted; this places a Toggle switch in the bottom panel. The Toggle switch will be placed in the upper left corner; grab the switch and drag it to the center of the panel (see Figure 5.11).
With the Toggle switch selected you can look at the properties of the switch in the Properties window. Look at Ext Properties and Connections; you don’t need to change anything – leave them at their default values.
Running the Simulation
When you run the simulator, you can toggle the switch position by placing the mouse cursor on the Toggle switch and hitting a mouse button. The Toggle switch starts and stops the binary counting.
How the Program Works
We put the flowchart together without looking at the functions. The overview is that we are reading an input on one of the microcontroller’s pins and, depending upon the electrical value (or binary value presented on the pin), the program will either count in binary displaying the numbers on the LEDs, or stop counting.
The switch, as shown in Figure 5.1, can deliver either a binary “1” or “0” to the input pin. The Input icon allows the program to read the I/O line (or pin) on Port A. Since we are only reading a single pin, we set the input properties of this icon to “Single bit.” The value read at Port A is held in the variable “Y.”
Our next icon, the Decision icon, reads the value “Y” and depending upon the value, branches the execution of the program to one of two branches. If the value of “Y” = “Yes” the program goes to the outside branch and continues to count. The values of “Yes” and “True” are accepted to mean binary “1.” Consequently the values “No” and “False” are accepted to mean binary “0.”
The Toggle switch component allows us to change the binary value present on the I/O line. To understand how the switch plays into the program, we need to look at the Toggle switch’s properties. Select the switch and in the Properties window on the right select Ext Properties. The Edit Component Properties window opens as shown in Figure 5.12.
This gives basic information on our switch and its behavior. Close this window and open the Connections Properties window as shown in Figure 5.13.
In this property window we see the switch is connected to Port A Bit 0. We could change these connections but they are fine as they are. Close this window. All that’s missing is how the switch looks electrically inside the Flowcode developing environment. To see this, select “Help” in the Properties window. On the help page (see Figure 5.14) we have an electrical diagram of how the Toggle switch is represented electrically in the program.
While this switch circuit isn’t identical to the switch circuit we are using, both switch configurations can deliver binary “1” and “0,” depending upon the switch position, to the microcontroller I/O pin. If you haven’t already, compile the program to a Hex file and load it into your microcontroller or ECIO28 and take it for a spin in the real world.
There is more than one way to program a function; in fact there are dozens. The way we just programmed that switch may not be the most efficient way, but I thought it explained the program’s function and operation better. Let’s look at an alternative.
In this section we will use Component macros. If you are not familiar with computers, your first question might be, “What’s a macro?” A macro is a sequence of programmed instructions, a modular subprogram if you will, to be called upon from within a program when needed. The reason for macros is to make common programming tasks, like reading a switch that contains a number of program instructions, into an easy-to-use macro. So rather than writing the sequence of programming instructions to read a switch every time you wanted to read a switch, you would call a component macro instead. This makes programming components less tedious and less error-prone.
We can create additional flowcharts within our workspace and call these newly created flowchart programs from other flowchart programs. Flowcode titles these additional flowcharts macros, and calling the flowchart program is a “Macro call.” Readers who may have some programming experience will call these types of procedures “Subroutines.” We will use Macro calls in later chapters. For now we will remain focused on Component Macros.
We can use a Component Macro to read our switch component instead of the Input icon. Before we change this program, use the File > Save As function to save this program with a different name or add a V2 to the end of the current name. This will preserve our current program. Once you have saved the file highlight the Input icon and hit the Delete key (see Figure 5.15).
Next select the Component Macro icon (see Figure 5.16) and drag it above the Decision icon, which is where the Input icon was before we deleted it in the flowchart.
Double click on the Component Macro icon to bring up its Properties window (see Figure 5.17). Inside the Properties window under Components, the two components in use in this flowchart are listed.
Select the SWITCH(0) component. Once the SWITCH(0) is selected the macros available for the SWITCH(0) are shown in the Macro’s text box (see Figure 5.18).
In the Macro’s text box, select “ReadState” then use the pull-down on the variable text box to select the “Y” variable (see Figure 5.19).
Then hit “OK.” This writes the information in the “Call Component Macro” icon on the Flowchart (see Figure 5.20).
At this point you can run the simulation and use the Switch to control the counting. Once the simulation works you can have the Flowcode compiler generate a Hex file to program your microcontroller. Figure 5.21 shows the circuit on the solderless breadboard.