330 |
Chapter 8 / The BASIC Stamp 2p Prototype |
|
|
Figure 8-39
Try disconnecting the port. You will notice that unlike the earlier version that went into wait mode, the BS2 continues to send data. Once again, if you wish to shut the data off you will need to toggle the DTR line manually or use a terminal program that allows you to configure the state of the DTR line.
Before we leave the USB part of this chapter, remember that the two USB interfaces are powered differently. The DLPUSB232BM is powered by our power supply. The USB2SER board is powered by the host USB controller or hub. This means that disconnecting the USB2SER board will have no effect on the host plug-and-play system, whereas unplugging the DLP design board will remove the device because power has been lost.
Chapter 8 / The BASIC Stamp 2p Prototype |
331 |
|
|
The Maxim/Dallas Hardware Interfaces
The 1-Wire Interface Hardware
Now it’s time to connect our 1-Wire interface. Since we covered testing the 1-Wire in Chapter 7, we will now attach it to the BS2p and modify the test program to test our connections. Remember that the 1-Wire uses a data line, power, and ground line as shown in Figure 8-40.
Figure 8-40
We added a three-pin connector and a 2.2 K resistor, and attached a 2.2 K between the data and +5 volt line. This circuit uses one additional BS2p pin, which is pin 4. Listing 8-10 is a modified version of the test program from Chapter 7 designed to work with this schematic.
332 |
Chapter 8 / The BASIC Stamp 2p Prototype |
|
|
Listing 8-10
//{$STAMP BS2p}
//{$PBASIC 2.5}
//Test Program for Dallas 1-Wire Thermometer
//for Embedded Systems Desktop Integration
//Copyright 2004 - Oliver H. Bailey
//
// This program uses Pin 4 for both Input and Output
// Pin 4 - Transmit/Receive 1-Wire Data |
<=> |
// |
|
Temp VAR Word |
// Temperature Storage Variable |
TempLo VAR Temp.LOWBYTE |
// Temperature Low Order Byte |
TempHi VAR Temp.HIGHBYTE |
// Temperature High Order Byte |
TempSign VAR temp.BIT11 |
// Temperature Sign Bit |
signBit VAR Bit |
|
tempCel VAR Word |
// Centigrade |
tempFar VAR Word |
// Fahrenheit |
ChkLoop VAR Byte |
// Three attempts |
//This is a very simple program for testing a DS1822 Temp sensor. Rather than using
//the traditional Read ROM command we simply make an attempt to read the temperature.
//If a device is present then the temperature is returned; otherwise it is assumed
//no DS1822 is attached.
ChkLoop = 0 |
// Set to Zero Tries |
Main: |
|
GOSUB ChkDev |
// Check for DS1822 |
// Reaching this line requires a response from the DS1822 |
tmpLoop: |
// Read temperature loop start |
GOSUB Read_Temp |
// Gosub read temperature |
DEBUG HOME, SDEC tempCel," C", CR |
// Display Centigrade |
DEBUG SDEC tempFar," F", CR |
// Display Fahrenheit |
PAUSE 500 |
// Wait |
GOTO tmpLoop |
// Start again |
// If the code reaches here, it was by mistake. |
|
GOTO forever |
// Loop forever |
// Check Device Subroutine
334 |
Chapter 8 / The BASIC Stamp 2p Prototype |
|
|
Since we used the BS2p in Chapter 7 to test our 1-Wire design, we have only modified the existing code to be certain it works with the BS2p I/O pin 4.
The 3-Wire Hardware Interface
The Dallas 3-Wire interface is all that remains to be implemented for directions from the BS2p. Again, we will add this to the existing circuit and make only the necessary changes required to test this part of the circuit. We covered this interface in Chapter 7 but did not provide a schematic since it is a common SPI interface that is made up of only three wires — reset, clock, and data. We will implement this interface on pins 6, 7, and 8 as shown in Figure 8-41.
336 |
Chapter 8 / The BASIC Stamp 2p Prototype |
|
|
ByteTwo VAR Byte
SA1 = %00000000
SA2 = %00000000
Len = %00000000
ByteOne = %00000000
ByteTwo = %00000000
//Misc. Variables
//CurBit VAR Byte TA=$0000
PAUSE 1000
Main:
ChkLoop = 0
GOSUB ChkDev
// DEBUG DEC ChkLoop, CR
//Test Byte 2
//SA1 = 0
//SA2 = 0
//Len = 0
//ByteOne = 0
//ByteTwo = 0
//For Bit Bang Mode
//Target Address is Page 0, Byte 0
//Wait for Debug
//Set to zero again
//Check for DS2404
//Debug Display
//Reaching this line requires a response from the DS2404
//DEBUG HOME, "DS2404 Device Found..",CR
//Write to RAM Locations 0026H and 0027H. Write binary 01010101 and 10101010 to
//check RAM locations
GOSUB Write_SP_Mem |
// Write to scratchpad memory |
FOR index = 0 TO 3 |
// Loop through variables and |
tempVar(index) = $95 |
// write asterisks * to data area |
NEXT |
|
PAUSE 100 |
// Wait for 3 ms. |
GOSUB Read_SP_Mem |
// Read scratchpad memory |
// PAUSE 1000 |
// Wait |
// GOTO Main |
// Start all over |
// If the code reaches here, it was by mistake. |
|
GOTO forever |
// Loop forever |
//Check Device Subroutine - This subroutine looks for a presence pulse to determine
//if a 1-Wire device is available. There are a couple of things to be aware of here.
//First, the normal state of the line is high since a pull-up resistor is used. This
//means that with ChkLoop being 0 upon entry, at least one state transition is needed
//to determine a 1-Wire device is available. Without at least one state transition
//there is no way to determine if the line was pulled low after the inquiry was done. ChkDev:
OWOUT 14, 1, [$CC] |
// Read Memory Page |
NotDone: |
|
OWIN 14, 0, [TmpByte] |
// Check Status |
ChkLoop = ChkLoop+1 |
// Increment Loop Counter |
338 |
Chapter 8 / The BASIC Stamp 2p Prototype |
|
|
interfaces in hardware, let’s write a short example program to access the DS2404 via the 3-Wire interface. Listing 8-12 contains a simple time and memory access routine using the 3-Wire interface. You will note that the amount of code needed is much less than the same functions using the 1-Wire protocol. This is simply due to the fact that we no longer have to look for a device type or serial number since this interface has a dedicated data line to the BS2.
Listing 8-12
//Test Program for DS2404 3-Wire Interface
//for Embedded Systems Desktop Integration Chapter 8
//Copyright 2004 - Oliver H. Bailey
//
//{$STAMP BS2p}
//{$PBASIC 2.5}
Reset |
CON |
5 |
// Reset/Chip Select Line |
Dio |
CON |
6 |
// Data IO Line |
Clk |
CON |
7 |
// Clock Line |
Wrsp |
CON |
$0F |
// Write Scratchpad command |
Rscp |
CON |
$AA |
// Read Scratchpad command |
Cscp |
CON |
$55 |
// Copy Scratchpad command |
Rmem |
CON |
$F0 |
// Read Memory command |
TA1 |
VAR |
Byte |
// Target Address 1 |
TA2 |
VAR |
Byte |
// Target Address 2 |
EA |
VAR |
Byte |
// Ending Address |
DLen |
VAR |
Byte |
// Length of Data Read/Written |
SMem |
VAR |
Byte |
// Start Memory Location in DS2404 |
EMem |
VAR |
Byte |
// End Memory Location in DS2404 |
DByte VAR Byte |
// Data Byte from the DS2404 |
LoopCtr VAR Byte |
// Loop Counter for Reading Data |
Ctr1 |
VAR Word |
// Start Counter |
Ctr2 |
VAR Word |
// Current Counter |