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

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

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

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.

Chapter 8

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

 

Chapter 8 / The BASIC Stamp 2p Prototype

333

 

 

 

 

 

 

 

 

ChkDev:

 

 

 

OWOUT 15, 1, [$CC, $44]

// Check for temperature

 

NotDone:

 

 

 

PAUSE 25

// Wait for response

 

OWIN 15, 4, [Temp]

// Check Status

 

ChkLoop = ChkLoop+1

// Increment Loop Counter

 

IF ChkLoop = 100 THEN no_Dev

// Max Tries then Stop

 

IF Temp = 0 THEN

// No Response

 

GOTO NotDone

// Otherwise Try Again

 

ENDIF

 

 

 

RETURN

// Return if Found

 

// Read and Convert Temperature

 

 

 

Read_Temp:

 

 

 

OWOUT 15, 1, [$CC, $44]

// Check for temperature

 

notReady:

// Start completion loop

 

PAUSE 25

// Wait for response

 

OWIN 15, 4, [Temp]

// Check results

 

IF Temp = 0 THEN notReady

// Not done, start again

 

OWOUT 15, 1, [$CC, $BE]

// Read Scratchpad Temp

 

OWIN 15, 2, [TempLo, TempHi]

// Get Temp Bytes

 

signBit = TempSign

// Get sign bit (0 = +, 1 = -)

 

tempCel = Temp

// Store to variable

 

tempCel = tempCel >> 4

// Store Temp to variable

 

IF (signBit = 0) THEN PosCel

// Shift to get actual temperature

 

tempCel = tempCel | $FF00

// Check for sub-zero

 

 

// Adjust if below zero

 

PosCel:

 

 

 

tempFar = tempCel */ $01CD

// Positive Temperature

 

IF signBit = 0 THEN PosFar

// Adjust for display

 

tempFar = tempFar | $FF00

// Check for sub-zero

 

 

// Adjust Fahrenheit scale

 

PosFar:

 

 

 

tempFar = tempFar + 32

// Not sub-zero

 

RETURN

// Add 32 for Fahrenheit display

 

 

// All Done, return

 

no_Dev:

 

 

 

DEBUG "No DS1822 Device Found", CR

// No response at startup so display

 

 

// message

 

forever:

//

 

 

GOTO forever

// and loop forever

 

Chapter 8

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.

Figure 8-41

Chapter 8 / The BASIC Stamp 2p Prototype

335

 

 

With both the 1- and 3-Wire interfaces implemented, we can access the DS2404 using either interface. Listing 8-11 has been modified from Chapter 7 to use the 1-Wire interface.

Listing 8-11

//{$STAMP BS2p}

//{$PBASIC 2.5}

//Test Program for Dallas 1-Wire Time/Date Chip DS2404

//for Embedded Systems Desktop Integration

//Copyright 2004 - Oliver H. Bailey

//

//This program uses Pin 5 for all I/O. It should be noted that all

//communications with

//the DS2404 memory functions on the 1-Wire bus are done using bit I/O

//This is a very simple program for testing a DS2404 Time chip.

//ChkDev Variables

ChkLoop VAR Byte

// Loop Counter for Reset

TmpByte VAR Byte

// Presence Pulse Indicator

 

// Write_SP_Mem Variables

CTA CON $0077

 

TA VAR Word

 

TA1 VAR TA.LOWBYTE

// Target Address 1

TA2 VAR TA.HIGHBYTE

// Target Address 2

EADDR VAR Byte

 

tempVar VAR Byte(8)

 

index VAR Nib

 

TestByteOne VAR Byte

// First Test RAM Location Byte

TestByteTwo VAR Byte

// Second Test RAM Location Byte

TestByteOne = %01010101

// Byte One is 01010101

TestByteTwo = %10101010

// Byte Two is 10101010

TA1 = $26

// Hex 26

TA2 = 0

// Hex 00

// Read_SP_Mem Variables

 

SA1 VAR Byte

// Storage Address 1

SA2 VAR Byte

// Storage Address 2

LEN VAR Byte

// Data or Offset Length

ByteOne VAR Byte

// Test Byte 1

Chapter 8

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

 

Chapter 8 / The BASIC Stamp 2p Prototype

337

 

 

 

 

IF ChkLoop = 100 THEN no_Dev

// Max Tries then Stop

 

IF TmpByte = 0 THEN

// If no Response

 

GOTO NotDone

// Otherwise Try Again

 

ENDIF

 

 

 

IF (ChkLoop <> 1) THEN no_Dev

// State Change was detected

 

RETURN

// Return if Found

 

// WRITE Scratchpad Memory

 

 

 

Write_SP_Mem:

 

 

 

DEBUG CLS, "***** Sent Data Info *****", CR

 

 

 

FOR index = 0 TO 3

// Loop through 4 Bytes

 

tempVar(index) = index + $30

// Write 0, 1, 2, and 3

 

NEXT

// Write next number

 

DEBUG "Data Sent: ", STR tempVar\4, CR

// Send debug string to terminal

 

DEBUG "TA: ", HEX2 TA2, HEX2 TA1, CR

// Print Settings to Debug Terminal

 

OWOUT 14, 1, [$CC, $0F, TA1, TA2, STR tempVar\4] // Set up Address and Length

 

RETURN

// Return to Caller

 

PAUSE 5000

 

 

 

// Read Scratchpad Memory

 

 

 

Read_SP_Mem:

 

 

 

OWOUT 14, 1, [$CC, $AA]

// Read Scratchpad Function

 

//Read Address, Length, and Memory Contents OWIN 14, 2, [TA1, TA2, EADDR, STR tempVar\4] DEBUG "***** Received DATA Info *****", CR

//Send Target Address to Debug Terminal

DEBUG "Target Address :", HEX2 TA2, HEX2 TA1, CR // Send End Address to Debug Terminal

DEBUG "End Address :", HEX2 EADDR, CR

// Send Received Data Bytes to Debug Terminal DEBUG "DATA Received:", STR tempVar\4, CR

RETURN

no_Dev:

 

DEBUG "No DS2404 Device Found", CR

// No response at startup so display message

forever:

//

GOTO forever

// and loop forever

So how do we interface the DS2404 using the 3-Wire interface, you ask. Since we have already implemented the 1- and 3-Wire

Chapter 8

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

Initialize:

Chapter 8 / The BASIC Stamp 2p Prototype

339

 

 

LOW Reset

// Pull Rest Line Low

LOW Dio

// Set Data Line Low

LOW Clk

// Set Clock Line Low

PAUSE 1000

// Wait for things to settle

Main:

 

TA1 = $E0

// Start Memory Location

TA2 = $01

// End Memory Location

GOSUB WriteSP

// Write to Scratchpad

TA1=255

// Assign new values to TA1

TA2=255

// and TA2

GOSUB ReadSP

// Read Scratchpad

// Print Start Address, End Address, Length

DEBUG DEC3 SMem, " ", DEC3 EMem, " ", DEC3 DLen, CR

GOSUB WriteMem

// Write Scratchpad to Memory

GOSUB ReadRAM

// Read RAM Contents

END

 

//DS2404 3-Wire Subroutines - Copyright 2004, Oliver H. Bailey

//For the book Embedded Systems, Desktop Integration

//

 

 

WriteSP:

// Write Data to Scratchpad

HIGH Reset

// Bring Reset High

SHIFTOUT Dio, Clk, LSBFIRST, [Wrsp]

// Output control byte

SHIFTOUT Dio, Clk, LSBFIRST, [TA1, TA2]

// Output start and end address

SHIFTOUT Dio, Clk, LSBFIRST, ["This is the DS2404"]

// Write message to scratchpad

DLen = 18

// Set Datalen to new number

LOW Reset

// Bring Reset line Low

RETURN

 

 

ReadSP:

// Read Data from Scratchpad

HIGH Reset

// Bring Reset High

SHIFTOUT Dio, Clk, LSBFIRST, [Rscp]

// Output control Byte

SHIFTIN Dio, Clk, LSBPRE, [SMem, EMem]

// Read Start & End memory Addresses

Dlen = 0

// Set Data Length to 0

SHIFTIN Dio, Clk, LSBPRE, [DLen]

// Read Data Length

FOR LoopCtr = 0 TO Dlen

// Loop and

SHIFTIN Dio, Clk, LSBPRE, [Dbyte]

// Read scratchpad Data

// WRITE DLen, DByte

// Store to EEPROM

Chapter 8

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