Bailey O.H.Embedded systems.Desktop integration.2005
.pdf270 |
Chapter 8 / The BASIC Stamp 2p Prototype |
|
|
circuit. The diode prevents the electrical charge from reaching the voltage source. I added this relay circuit to the time and temperature board we built in Chapter 7. Figure 8-8 shows where the circuit was placed on the prototype board.
Figure 8-8
The two pins on the left edge of the board connect the relay to the output pins of the LCD. The coil we have chosen provides 250 ohms of resistance in the circuit. According to Matrix Orbital documentation if 240 ohms or more resistance are provided, then the 240 ohm resistor should be bypassed. See Figure 8-9.
Figure 8-9
Chapter 8 / The BASIC Stamp 2p Prototype |
271 |
|
|
The same power used for the LCD is also used to power the I/O pins on the LCD, so no additional connections are needed. The following PBASIC program will alternate the relay on and off every 5 seconds.
TestRelay: |
|
|
I2COUT 8, 80, [254, 87, 6] |
‘ Turn Fan Relay On |
|
PAUSE |
5000 |
|
I2COUT 8, 80, [254, 86, 6] |
‘ Turn Fan Relay Off |
|
PAUSE |
5000 |
|
GOTO TestRelay
Enter this program into the BASIC Stamp editor, connect the Stamp to the host via the RS-232 cable, apply power to the Stamp by attaching a 9-volt battery, and download and run this program. If you cannot feel the relay switching, set your multimeter to continuity or buzzer and attach the leads to the fan control terminals. You should get a tone or see the LED continuity light for 5 seconds and then go off for 5 seconds, then repeat the process. Once you’ve tested the circuit, disconnect the power as this will drain a 9-volt battery pretty quickly.
Note:
Earlier we defined fan control using a 1-Wire switch. Since we have chosen a smart LCD display that provides us with outputs that can drive a relay, this approach makes sense and saves the engineer the additional cost of a 1-Wire switch.
Wiring the Alarm LED
The alarm LED is connected to I/O pin 2 on the LCD display. Since we have a resistor already connected to the LED we need to bypass the current limiting resistor as we did on pin 6. Once this modification is completed, attach a wire between the + side of the I/O pin to the resistor for the alarm LED. To complete the circuit, add a second wire from the –I/O pin to the ground lead on the LED. Use the same software we developed for testing the fan
Chapter 8
272 |
Chapter 8 / The BASIC Stamp 2p Prototype |
|
|
control except change pin 6 to pin 2. The LED should flash on and off at 5-second intervals.
Wiring the Alarm Buzzer
We have worked our way back to I/O pin 1 on the LCD. The buzzer we installed in Chapter 7 will operate on voltages from 1.5 to 12 volts. Since we are outputting +5 volts, we are well within the ratings of the buzzer. As a result we can connect the + and – leads directly to the buzzer terminals.
Keyboard Wiring
The keyboard is the last item on the LCD to wire. The LK202-25 supports five rows of five keys for a total of 25 keys. We have seven keys, which is too many for one row or column. To make interfacing with the keys easier we will define two rows of keys by function. The first row will contain the four menu keys and the second row will contain the remaining three function keys.
Keyboard Codes
The following commands are useful when doing keyboard scanning or control. The following command will read the current key being pressed or zero if no key is pressed.
I2CIN 8, $51, [KeyChar] |
‘ |
Read Keyboard = 0 if no key pressed |
|
' |
or key code |
We covered this command earlier. Remember to add 1 to the base port number to read.
I2COUT 8, $50, [254, 126, 2] |
‘ |
Set Auto-Repeat mode to send |
|
' |
press/release codes |
This is the default mode for the I2C interface. When a key is pressed, no other codes are sent until the key has been released. The release code is the key code + 32 (20 hex).
|
Chapter 8 / The BASIC Stamp 2p Prototype |
273 |
|
|
|
I2COUT 8, $50, [254, 96] |
‘ Turn off Auto-Repeat mode |
|
Auto-Repeat is initiated by holding down the key just as with a PC keyboard.
I2COUT 8, $50, [254, 69] ‘ Flush Keyboard buffer.
This empties the keyboard buffer of any remaining keys that have not been received. This is useful when the time between fetching keyboard code is lengthy and multiple keyboard characters may be in the buffer.
We have not implemented all of the functions on the keyboard/alarm board we built in Chapter 7. The remaining fuctions are discussed next.
Menu Functionality
We have considerable flexibility in how we implement the menu details and menu navigation. Earlier we determined how our LCD display would be designed. Now, we will provide the details of how our menu should look. We have 12 characters to display our main menu. Following is a description of the needed menu items, the LCD display message, and the message length.
Table 8-1
Menu Item |
LCD Display |
Length |
Set Date |
Set Date |
8 |
|
|
|
Set Time |
Set Time |
8 |
|
|
|
Set Time Format |
Time Fmt |
8 |
|
|
|
Set Temperature Display (Celsius or Fahrenheit) |
Temp Fmt |
8 |
|
|
|
Set Ethernet Address |
Net Addr |
8 |
|
|
|
Set High Temperature Alarm |
High Temp |
9 |
|
|
|
Set Low Temperature Alarm |
Low Temp |
8 |
|
|
|
Set Fan Temperature |
OnFan On |
6 |
|
|
|
Set Fan Temperature Off |
Fan Off |
7 |
|
|
|
Reset Temperature Network |
Reset Temp |
10 |
|
|
|
Reset Unit |
Master Reset |
12 |
|
|
|
Chapter 8
274 |
Chapter 8 / The BASIC Stamp 2p Prototype |
|
|
The longest message length is 12 characters, the exact maximum we have. To add flexibility we will store the message strings in the EEPROM so they can be easily maintained. At this point our BASIC Stamp board is connected to the LCD and the alarm/keyboard is also attached. Your system should look similar to Figure 8-10.
Figure 8-10
Here we have the BASIC Stamp board, keyboard/alarm, power LEDs, and LCD mounted to a 0.25" backer board (you can purchase this at any art supply store for about $3). Now, let’s move on to integrating the time and temperature.
Chapter 8 / The BASIC Stamp 2p Prototype |
275 |
|
|
Using the DS2404 Time and Memory Chip
Now it’s time to integrate the Dallas 1- and 3-Wire components. Our design calls for a Dallas 1-Wire interface to be implemented. Earlier we built a board that has both the 1- and 3-Wire interfaces and here’s why. Accessing our temperature chip is fast since we are not sending or receiving large amounts of data. We have 512 bytes of RAM that is battery backed up. This is a great place to store nonvolatile information we want to retain. If we read and write large amounts of data using the 1-Wire interface, it will certainly slow things down. In addition, we can use the memory in the DS2404 as dual-port RAM, meaning it can be read from one interface and written to by the other. This has promise for future projects, so we will implement and test the 3-Wire interface even if we don’t use it in the final prototype right now.
The DS2404 Timers
Just as the DS1822 can tell us when a temperature threshold has been reached, the DS2404 can tell us when a timer has expired. The DS2404 time functions are simple free-running counters. We can set timed events; when they are reached, a bit in the corresponding status register is set. The DS2404 timers can trigger interrupts, but since the BASIC Stamp can’t process those interrupts, this feature is of little use to us right now. We can still set the interval timers and look for a change in the status register that indicates the event time has been reached. Let’s examine how the DS2404 timers work and how we can put them to effective use. First, the timing functions of the DS2404 don’t really store the time and date but are free-running elapsed counters. The DS2404 data sheet tells us there are three timers. They are:
Real-time clock — A 5-byte binary counter updated 256 times each second. The least significant byte contains a count of fractional seconds and the remaining four bytes holds the total seconds since the clock was started.
276Chapter 8 / The BASIC Stamp 2p Prototype
Interval timer — A 5-byte counter incremented 256 times each second. The least significant byte again holds fractional seconds while the remaining four bytes provide the interval counter in total seconds. This timer has two modes of operation: automatic and manual.
Cycle counter — A 4-bit binary counter. This counter keeps track of the number of times the unit has gone into backup battery mode.
If the interval timer is set to automatic mode, it is started by holding the data line for a predetermined time period. It will stop when the data line has been held low for that specified time period. Both time periods are controlled by the data select bit (DSel) in the control register. If the interval timer is set to manual mode, then it is started and stopped by the start/stop bit in the control register.
The cycle counter is incremented when the I/O line goes low if the proper timing requirements have been met. Again, timing is set by the DSel bit in the control register. The cycle counter is a very easy way to determine if a power outage has occurred and power has been restored. All the time functions mentioned here are available from either the 1- or 3-Wire interface.
The DS2404 Alarm Time Registers
In addition to the three timer registers, the DS2404 has an alarm register for each of these timers. To use the alarm registers, load the value of the alarm into the proper register. When the associated timer reaches that value, the appropriate flag bit is set in the status register. This feature is accessible for both the 1- and 3-Wire interfaces.