Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Bailey O.H.Embedded systems.Desktop integration.2005

.pdf
Скачиваний:
70
Добавлен:
23.08.2013
Размер:
9.53 Mб
Скачать

270

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.

Chapter 8

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.

Chapter 8 / The BASIC Stamp 2p Prototype

277

 

 

DS2404 Interrupt Registers

In addition to the alarm registers, there are interrupt enable flags for each of the three counters. If the associated interrupt bit is set, an interrupt is generated when the event occurs. It’s important to note two items: First the interrupts are only available on the 1-Wire interface and second, they only fire once. The good news is that even though we may miss the actual interrupt, the interrupt bit for the associated timer will remain set until it is manually cleared. This means that if we enable the interrupt for the Stamp, we will have to read the status register and test to see if the interrupt was triggered.

Timers and the BASIC Stamp 2p

There are two ways to test if a timer has expired. The first is to create an endless program loop that continuously polls for keyboard input, updates the display, checks for host communications, and reads the 1-Wire and 3-Wire networks. This is a classical way for a developer to handle what we call “the do forever loop” and it works just fine. The only drawback to this technique is that we will never reach power conservation mode. That’s not a big issue right now since are keeping the clock going with a battery backup circuit. In the lab, the CR2032 3.3-volt battery has kept the timer going for well over a week with no external power. Figure 8-11 illustrates our flowchart for how the BASIC Stamp code will manage our thermostat. The emphasis is on how we will process temperature and time.

Chapter 8

278

Chapter 8 / The BASIC Stamp 2p Prototype

 

 

 

Read DS2404

 

 

 

 

Cycle Timer

 

 

 

 

 

 

 

 

 

 

Calculate

 

 

 

 

 

 

 

 

 

 

 

 

 

Stored Time

 

 

 

 

 

 

YES

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Incremented

 

 

 

 

 

Elapsed Time

 

 

 

 

 

 

 

 

 

 

 

 

 

and Interrupts

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

NO

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Minute Timer

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

YES

 

 

 

 

 

 

Increment Minute

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Expired

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Minutes = 60

 

YES

 

 

Increment Hour

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

NO

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Reset Timer

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

NO

 

 

 

 

 

 

 

 

 

 

 

Hours = 24

 

YES

 

Increment Date

 

 

 

 

 

 

 

 

Interrupt

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Store to

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

DS2404 RAM

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Process

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Key Pressed

 

 

 

 

YES

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Keystroke

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

NO

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Pending

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

YES

 

 

 

 

 

 

Hi Temp

 

 

 

 

 

NO

 

 

 

 

Lo Temp

 

 

 

 

 

 

Temp Alarm

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

NO

 

 

 

 

 

 

 

 

 

 

 

 

 

YES

 

 

 

 

 

 

 

 

 

 

 

 

 

YES

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Get Temperature

 

 

 

 

 

 

 

 

 

 

 

Start Fan

 

 

 

 

 

 

 

 

 

 

Stop Fan

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Update Display

 

 

 

 

 

 

 

 

 

 

Turn on Cooling

 

 

 

 

 

 

 

 

 

Turn on Heat

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Host Attached

 

 

 

 

 

 

 

 

 

 

Reset Alarm

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Process

Host Input

Update Host

Output

Figure 8-11

First, we check the cycle counter to see if a power outage has occurred. If it has, we recalculate the elapsed time. The next thing we do in the loop is check to see if our minute timer has elapsed. If it has, we check and update minutes, hours, and date

Chapter 8 / The BASIC Stamp 2p Prototype

279

 

 

accordingly. You’ve probably noticed we are not checking seconds; the reason is simple: If the system gets busy, it is too easy to miss more than one second, thereby putting our second counter out of sync. We should be able to check the minute interrupt with ease and keep the display updated. Additionally, if we trigger on each second we have to add the logic to check if seconds equals 60. If we lose track, our minute counter may be off so we will stick with a minute display for now.

Static RAM vs. EEPROM

At this point we need to consider where our data is stored. You’ll notice the date, hours, and minutes are read and stored back to the DS2404 battery-backed RAM. This assures we always have a copy of the current date, hours, and minutes images as they were last updated. If we lose power, the counters in the DS2404 will continue to run but we have no reference counter to check it against. The solution to this is simple: We retain the cycle counter copy in the DS2404 RAM to compare against. If the cycle counter has incremented since it was last stored, we know that a power outage has occurred. In that case we will compare the counter values to the last counter values read and adjust our minutes, hours, and days accordingly.

We haven’t discussed using the static RAM area of the BASIC Stamp (EEPROM). Since we have battery backed-up RAM in the DS2404 and there is a limited number of times we can write to the EEPROM on the BASIC Stamp, we’ve avoided having to use the EEPROM. The EEPROM could be used but we would need to rotate locations to extend the life of the part. We would also need to check each write with a read to verify the data was written to a good location. Rather than add those routines, we’ve chosen instead to use the battery-backed RAM, which does not have those limitations. We can use the EEPROM for storing our menu strings, since it won’t need to be updated very frequently. Reading the EEPROM has no effect on its life; only writing to the EEPROM is limited.

Chapter 8

Соседние файлы в предмете Электротехника