Bailey O.H.Embedded systems.Desktop integration.2005
.pdf
260 |
Chapter 8 / The BASIC Stamp 2p Prototype |
|
|
Figure 8-2
Power Sources
In Chapter 7 we used the built-in voltage regulator on the BASIC Stamp itself. That works for applications that have low current requirements, but since we are adding Ethernet we need a little more current than the Stamp can provide. If you made your own circuit boards using the artwork in Chapter 7, then you need only the 7805 voltage regulator and power connector. If you haven’t made those boards, then the following instructions will provide you with the information necessary to build a +5 volt supply.
Building a +5 volt supply requires only four components. They are:
Power supply 7.5 V (Radio Shack 273-1696)
Power supply connector (Jameco 216451)
Voltage regulator (Radio Shack 276-1770)
Capacitor 220 F (Radio Shack 272-1017)
Chapter 8 / The BASIC Stamp 2p Prototype |
261 |
|
|
This is a very simple circuit. Figure 8-3 is a diagram of how the circuit is built. The LM7805 is on the left and the capacitor is on the right. The capacitor is optional. The ground and +5 out connections are attached to the center rows on the prototype board we are building.
Figure 8-3
Changing the power source to the BASIC Stamp requires no modification because the same 5-volt regulator output on the Stamp can also be used for +5 regulated input. This isolates our other power circuit, and we can power everything by simply connecting the output from the above circuit to the +5 and ground connector on the keyboard.
Connecting the Reset and Host Communications
We need to be able to reset the Stamp so we will solder a normally open push button to pin 21 on one side and ground on the other. When the switch is pressed, the momentary grounding of pin 22 will reset the BASIC stamp. A normally open boardmounted push button switch (Radio Shack 275-1571) can be used as shown.
Note:
Be sure you use a normally open push button (NO). If you use a normally closed button, your stamp will always be in reset mode and never execute a program.
Now we need to connect our programming and debugging lines to be able to start using the Stamp. Figure 8-4 shows how our regulated power in, reset, and programming/debugging lines are
Chapter 8
262 |
Chapter 8 / The BASIC Stamp 2p Prototype |
|
|
attached. Pins 1 through 4 are attached to data in, data out, attention, and ground.
Figure 8-4
Now insert a BASIC Stamp 2p into the socket with pin 1 inserted into the side with the notch in the middle. Your board should look similar to Figure 8-5.
Figure 8-5
Chapter 8 / The BASIC Stamp 2p Prototype |
263 |
|
|
Notice the RS-232 programming interface is the four wires on the left. The reset button is at the upper left, and the power is at the lower portion of the board, out of view.
Remaining I/O Pin Requirements
Before we begin connecting the remaining I/O devices and pins, let’s see what our needs are if we choose not to share I/O pins among devices.
I2C — Two I/O lines, +5 volt supply, ground
Dallas 1-Wire interface — One I/O line, +5 volts, ground
Parallax USB — Three I/O lines, ground
Ethernet — Four I/O lines, ground
Dallas 3-Wire interface — Two I/O lines, +5 volts, ground
DLP 232BM — Four I/O lines, ground
Non-Host RS-232 — Four I/O lines, ground
This list is in order of importance. We need I2C for display output, keyboard input, alarm function, and fan control. The Dallas 1-Wire interface is required to gather temperature and temperature alarms. We need at least one USB and Ethernet interface.
We also need one RS-232 interface for the finished product. If we look at just the I/O pins required in this list, we have a total of 20 I/O lines. Since we have 16 I/O lines available, we will have to drop some devices or find creative ways to make this work. The good news is that we have plenty of I/O pins available to meet our minimum requirements. With this in mind, we will begin implementing our final functionality in order of importance and see how we can modify our design to allow the extra I/O devices to be supported.
Chapter 8
264 |
Chapter 8 / The BASIC Stamp 2p Prototype |
|
|
The LCD Display
The I2C Communications Interface
The I2C interface was developed by Phillips Electronics to allow multiple slave devices to communicate with a master device via a single serial interface. This form of serial communications has similarities to RS-232, RS485, and USB; however, it is designed for use only over short distances (although there are ways to obtain greater distances). It has two wires for communications just like RS-232, RS485, and USB, but it does not use differential signaling like USB and it has only one data line, unlike RS-232. The other line is used for the data clock to synchronize the data bits. Like USB, all communications are initiated by the master, which in this case is the BASIC Stamp, and the master always provides the data clock signal since it initiates communications. Each device has a unique ID that it responds to. In the case of the LCD display, the default device ID is the decimal number 80 (or 50 hex).
BASIC Stamp I2C Communications Support
Of all the BASIC stamp products, only the BASIC Stamp 2p series has built-in support for I2C communications, and those communications are limited to only two sets of I/O pins. Those are pins 0 and 1, and 8 and 9 with the even-numbered pin carrying the data and the odd-numbered (or higher) pin carrying the clock signal. Before we begin writing code to support our LCD we should attach the device to our BASIC Stamp board. We will use pins 8 and 9 for our prototype. These two pins are located at the very bottom of the right-hand side of the Stamp with pin 8 at the bottom and pin 9 the next pin up. We will solder a four-pin header to the top right side of our Stamp prototype board. The top pin of the header will be attached to +5 volts. This will provide power to the LCD. The next pin will be attached to Stamp
Chapter 8 / The BASIC Stamp 2p Prototype |
265 |
|
|
pin 9 for the clock signal. The third pin will be attached to pin 8, the data line, while the fourth and lowest pin will be attached to ground. The following schematic shows our current circuit.
Chapter 8
Figure 8-6
There are only two commands in PBASIC to manage I2C: I2COUT and I2CIN. The slave device address is used to send data to the proper device. To receive data we must add 1 to the slave device address as illustrated in the following code snippet.
I2COUT 8, 80, [“Temp”] |
' |
Send |
the |
string “Temp” to the LCD |
I2CIN 8, 81, [KeyChar] |
' |
Read |
Key |
Character |
The first command outputs the string “Temp” to the LCD display after the last cursor position. No formatting commands were used in this case so the text will be output as typed. The second command reads a keyboard character from the LCD and stores it to the variable KeyChar, which is defined as a byte. If no key has been pressed, a zero will be returned. Using a keypad with the LK202-25 LCD gives us flexiblility. We can program auto repeat and duration, and even transmit the characters without being polled. For this prototype we will use the default values that come standard from the factory. I’ve explained how to send display data and receive keystrokes, but one question remains: How
266 |
Chapter 8 / The BASIC Stamp 2p Prototype |
|
|
do we toggle the I/O lines available on the LCD? There are two commands that deal with general-purpose outputs. Both are shown below.
I2COUT |
8, |
80, |
[254, |
86, |
1] |
' |
Turn |
off Output #1 |
I2COUT |
8, |
80, |
[254, |
87, |
1] |
' |
Turn |
on Output #1 |
The state of an output cannot be read, so it’s up to the developer to keep track of outputs in the software or to always set outputs to a known state based on program inputs. We have covered all the information needed to use the LCD display, key scan, and outputs, so we can now begin developing our prototype software.
LCD Command Software
The LCD command functions we need to implement for our project are:
Clear Screen
Display Time
Display Date
Display Temperature
Display Menu
Display Error
Turn Backlight On
Turn Backlight Off
Read Keyboard
Turn Alarm Buzzer On
Turn Alarm Buzzer Off
Turn Alarm Light On
Turn Alarm Light Off
These 13 functions cover our needs for finishing the prototype. None of these functions alone are complicated, but combining some of these functions may prove tricky or create unexpected results. The LK202-25 uses control codes mixed with command
Chapter 8 / The BASIC Stamp 2p Prototype |
267 |
|
|
codes to alter the appearance of the display. Some of the functions we need are simple command codes sent to the LCD, but others require data or text to execute. Of the above functions, the following can be handled by simply sending control codes to the LCD display. The PBASIC command is shown to the right.
|
Clear Screen |
I2COUT 8, 80, [254, 88] |
|
Turn Backlight On |
I2COUT 8, 80, [254, 66, Delay] |
|
Turn Backlight Off |
I2COUT 8, 80, [254, 70] |
|
Read Keyboard |
I2CIN 8, 81, [Key] |
Turn Alarm Buzzer On |
I2COUT 8, 80, [254, 87, 1] |
|
Turn Alarm Buzzer Off |
I2COUT 8, 80, [254, 86, 1] |
|
Turn Alarm Light On |
I2COUT 8, 80, [254, 87, 2] |
|
Turn Alarm Light Off |
I2COUT 8, 80, [254, 86, 2] |
|
When turning on the backlight we can turn it on indefinitely by setting Delay = 0, or we can set a time delay in minutes. We will set Delay to 3 so the backlight will turn off after three minutes. This will give the user enough time to make changes and save them.
We could use a single I/O pin on the LCD for both the buzzer and LED, but to give us flexibility we will use two different I/O pins. The alarm buzzer will use I/O pin 0 and the LED will use I/O pin 1 on the LCD display.
The above eight functions have completed almost 75 percent of our work for the LCD. The remaining functions are:
Display Time
Display Date
Display Temperature
Display Menu
Display Error
These remaining functions require formatted data output. We have several options available to us here, as we can clear the display and repaint both lines each time an update is needed or we
Chapter 8
268 |
Chapter 8 / The BASIC Stamp 2p Prototype |
|
|
can position the cursor and update only those areas that need to be changed.
We have 20 characters on each display line. If we display the time in 24-hour format we will use eight characters for the time, a blank space, and eight characters for the date, which is a total of 17 characters. If we use AM/PM indicators, our time field is expanded to use 11 characters, bringing the total characters for line 1 to 20, the exact number available. This leaves us 20 characters on line 2 for menu items and the temperature display. We want to remain as flexible as possible so we will allow Fahrenheit and Celsius to be selected from the menu. Our temperature display will show the minus sign, three digits, and the Fahrenheit or Celsius character for a total of five characters. This leaves us 15 characters for displaying navigation and error messages for the first-level menus. On the far right of line 2 we will display an asterisk if a host system is connected and communicating with the thermostat. That will leave us 14 characters for our first-level menu or error message display.
Creating Formatted LCD Output
If we store all the text in memory for a full line display, it will take 40 characters. This is a lot of memory for the BASIC Stamp. If we use cursor positioning, we won’t have to redraw the display each time it needs to be updated. Based on our calculations above, our display routine would look something like this:
I2COUT 8, 80, [254, 71, 1, 1, Time] ‘ Display time @ column 1, row 1 I2COUT 8, 80, [254, 71, 13, 1, Date] ‘ Display Date @ column 13, row 1 I2COUT 8, 80, [254, 71, 1, 2, Temp] ‘ Display Temperature @ column 1, row 2
I2COUT 8, 80, [254, 71, 6, 2, Menu] ‘ Display Current Menu @ column 6, row 2 I2COUT 8, 80, [254, 71, 20, 2, Connected] ' Host Connection @ column 20, row 2
This method requires that we have five preformatted variables that each contain display information. These commands can be written as a subroutine that simply displays the information from each stored variable.
Chapter 8 / The BASIC Stamp 2p Prototype |
269 |
|
|
Alarm, LED, and Fan Control Outputs
As we’ve seen earlier, using the outputs on the LK202-25 is not difficult from a software point of view. We do need to review the electrical current requirements of our devices to be sure we don’t overload the outputs. This could result in, at the very least, destroying an output, or, at the very worst, destroying the LCD completely.
The I/O pins on the LK202-25 have current-limiting resistors of 240 ohms attached. They can sink 20 mA of current. Our LEDs require 2.6 volts and the buzzer operates on as little as 3 volts, so neither of these will be a problem. The alarm buzzer uses about 4 milliamps, which leaves us 16 milliamps for the LED; this is more than enough.
Fan control is another story. We will isolate the fan circuitry by using a low-voltage relay with a diode and capacitor to prevent voltage spikes from damaging our I/O pins on the LCD. The voltage that controls our fan is then physically isolated from our control circuit. We can use 110 or 220 relays, allowing us to control fans of various sizes depending on the need. This is a very simple method of isolation and requires only three components. They are:
One 1N4001 diode (Radio Shack 276-1101)
One 50 F electrolytic capacitor (Radio Shack 272-1018)
One 5-volt relay (Radio Shack 275-232)
The use of this circuit requires a separate power supply for the fan. Figure 8-7 illustrates how the circuit is constructed. The output pins on the LCD are shown at the top of the image.
This circuit prevents damage to the LCD output pin by creating an electrical shock absorber. Current surges are typical when relay contacts
change positions. These surges can destroy elec-
Figure 8-7
trical equipment. The capacitor absorbs voltage spikes and discharges them slowly through the
Chapter 8
