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

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

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

230

Chapter 7 / Hardware Development

 

 

Listing 7-3

//{$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 14 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

 

TA 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

ByteTwo VAR Byte

// Test Byte 2

SA1 = %00000000

// SA1 = 0

SA2 = %00000000

// SA2 = 0

Len = %00000000

// Len = 0

ByteOne = %00000000

// ByteOne = 0

 

Chapter 7 / Hardware Development

231

 

 

 

 

ByteTwo = %00000000

// ByteTwo = 0

 

// Misc. Variables

// For Bit Bang Mode

 

// CurBit VAR Byte

//

 

 

TA=$0000

// Target Address is Page 0, Byte 0

 

PAUSE 1000

// Wait for Debug

 

Main:

 

ChkLoop = 0

// Set to zero again

GOSUB ChkDev

// Check for DS2404

// DEBUG DEC ChkLoop, CR

// 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 3ms.

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 a 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

IF ChkLoop = 100 THEN no_Dev

// Max Tries then Stop

IF TmpByte = 0 THEN

// If no Response

GOTO NotDone

// Otherwise Try Again

Chapter 7

232

 

Chapter 7 / Hardware Development

 

 

 

 

 

 

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, & 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] // Setup Address & Length

RETURN

// Return to Caller

PAUSE 5000

 

// Read Scratchpad Memory

 

Read_SP_Mem:

 

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

// Read Scratchpad Function

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

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

DEBUG "End Address:", HEX2 EADDR, CR

// Send End Address to Debug Terminal

DEBUG "DATA Received:", STR tempVar\4, CR

// Send Received Data Bytes to Debug Terminal

RETURN

 

no_Dev:

 

DEBUG "No DS2404 Device Found", CR

// No response at startup so display message

forever:

//

GOTO forever

// and loop forever

This program will write the numbers 0123 to scratchpad memory, write asterisks (*) to the local variable, and then read the scratchpad memory locations back and display them. While it’s a simple program, it tests the network and memory functions. Later we will implement the time, date, and cycle counter functions.

Chapter 7 / Hardware Development

233

 

 

From Solderless Breadboard to Circuit Board

Now that we’ve gotten our design working on a solderless breadboard, let’s make a real soldered version. To make this board we will use a grid-style PC board from Radio Shack (Part 276-150). If you look at this board you will notice two lines of white markings that run the entire length of the board as shown in Figure 7-25.

Chapter 7

Figure 7-25

Those will be used as our power strips, just like our last board. The first step in building this circuit is to solder two terminal connectors to each end. One will supply power to our board and the other will feed power to another board. We will solder these to each end. Next, we will solder a 16-pin socket at the top left of the board. This will be for the DS2404 chip. Below the socket we will solder our battery holder. This will hold the CR2032 battery for backing up our RAM if we lose power. At the bottom we will solder one 3-Wire connector for our 3-Wire interface, and two 3-Wire connectors for our 1-Wire interface. By using two connectors we can continue to extend our 1-Wire network without additional components. At the bottom on the right side we can mount our DS1822 thermometer chip. I added an LED at the top left just to be certain that power is not a problem. If you decide to add an LED, a 1N914 diode should be used to prevent draining the battery if the power goes off. This allows the LED to double as a visual indicator that external power is being supplied.

Figure 7-26

234

Chapter 7 / Hardware Development

 

 

In Figure 7-26 the LED is at the upper left. Going clockwise is the DS2404, battery holder and backup circuit, 3-Wire interface, 1-Wire interface, and DS1822 thermometer chip at the lower left. The thermometer has been mounted away from the other components to avoid false readings due to heat. There is plenty of room left on this board for a 1-Wire switch or pot and relay for fan control. Power is supplied to the center rows and all 1- and 3-Wire inter-

faces are located at the lower right of the photo. Note the power connectors attached to the center rows of the board.

The Keyboard, Alarm, and Power

Supply Board

The third and final board contains the keypad, alarm buzzer, alarm LED, power LED, and power supply. We’ve decided to use the Matrix Orbital LK202-25 LCD display. This display is an intelligent device that has support for six general-purpose outputs, a 5 row by 5 column keypad, and text and graphics display capabilities. It can be addressed via the RS-232 port or through the I2C protocol. Using this board requires only two processor

Chapter 7 / Hardware Development

235

 

 

data pins, a +5 volt supply, and a ground. The LK202-25 offers the following functions:

A 2 line by 20 character backlit LCD display

Bidirectional serial or I2C interface

Programmable baud rates (serial interface only)

Keyboard inputs for a 5x5 keypad matrix

Six general-purpose I/O lines

Programmable startup screen

Programmable serial number

Software controlled LCD backlight

Because of the long feature list, the LK202-25 has several connectors on the back for different purposes. For our prototype we will use the I2C interface. I2C was developed by Phillips Electronics. It utilizes four pins: +5 volts, SDC (clock), SDA (data), and ground. To use this interface we need to locate the 4-pin connector at the bottom left side of the LK202-25 as the back is facing up.

Chapter 7

Figure 7-27

The connector shown in Figure 7-27 contains all the power and communications lines needed for using the LK202-25. In addition to the I2C interface, the LK202-25 has six general-purpose I/O lines. We will use two of these I/O lines, one for the alarm buzzer and the other for the alarm LED. The connections for these I/O

236

Chapter 7 / Hardware Development

 

 

lines are at the far left side of the board as the back is facing up. Figure 7-28 shows the I/O pin connections.

Figure 7-28

When connecting a device directly to the I/O pins, only devices with current requirements of 20 mA or less can be used unless the resistors (on the right) are bypassed. Both our alarm buzzer and alarm LED will work fine within the 20 mA current rating, so they will be directly attached to the top two posts (pins 1 and 2). The LK202-25 also supports standard RS-232 I/O. There is a DB9 connector on the back of the unit on the lower right side as shown in Figure 7-29. While we could use the RS-232 interface, I have chosen the I2C so you can gain experience with this method of communications. In addition, I2C is faster than standard RS-232.

Figure 7-29

Chapter 7 / Hardware Development

237

 

 

Our final connection is for the keyboard. This connector, located in the center at the bottom of the LK202-25, is shown in Figure 7-30.

Chapter 7

Figure 7-30

The connector has a total of ten pins. With the connector face up and looking from left to right, the first five pins are for connecting the keyboard columns 1 through 5. The next five pins are for connecting rows 1 through 5. The LK202-25 can handle a keyboard with five rows of five keys for a total of 25 keys. The keyboard interface is fairly simple to implement and is what we call a matrix type keyboard. Figure 7-31 illustrates exactly how we will implement this keyboard. Using this type of interface seems more confusing than it really is. When a key is pressed, two pins will sense the closure. Those two signals combined indicate which single key was pressed.

Row 1

 

 

 

 

 

 

 

Row 2

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Col 1 Col 2 Col 3 Col 4

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Figure 7-31

238

Chapter 7 / Hardware Development

 

 

Our keyboard matrix contains two rows and four columns for a maximum of eight keys. The first row has the direction keys on the left side of the unit with each key being part of a separate column. The three function keys on the right are row 2. Each of the function keys is also connected to columns 1 through 3 from row 1. We could add one more key to this matrix to fill it out, but for now we don’t need an additional key. Figure 7-32 shows the front of our I/O board. Clockwise starting on the left, we have the following connections:

+5 volts — Power connection

Ground — Common ground connection

I2C data line — Exchange data with embedded processor

I2C clock line — Synchronize data stream

Column 1 connection — Keyboard input

Column 2 connection — Keyboard input

Column 3 connection — Keyboard input

Column 4 connection — Keyboard input

Alarm LED — Visual alarm display

Power LED — Visual power display

Alarm buzzer — Audible alarm warning

Function keys — Store and clear parameters

Menu keys — Select menu items

Microcontroller I2C connections

Building the keyboard matrix requires only seven momentary push buttons (Jameco PN: BTS-1102B-2) and several lengths of 22 to 24 gauge stranded wire. The three pieces of unshielded wire just above the buttons are for test purposes. These allow each row and column to be tested for proper layout and continuity.

Chapter 7 / Hardware Development

239

 

 

Chapter 7

Figure 7-32

This board requires a little patience to build. With the exception of the power, ground, and I2C interface leads at the bottom of the figure, everything else connects to the LK202-25. That’s right — only two pins are used on the microcontroller. There are several different functions the I/O board performs. First, it contains the keyboard matrix that has already been explained. This board is also the connection point for the data and power connections to the LK202-25. Figure 7-33 illustrates the four-pin power and data communications connector. Since we have chosen I2C, the center two pins of that connector will be used for data and the data clock.

I2C Clock

 

 

 

To Pin 2

 

 

 

4.7 kOhms

 

+5 Volts

 

 

 

To Pin 1

 

 

 

 

 

 

To Microcontroller

 

 

4.7 kOhms

To LK202-25

 

 

 

I2C Data

 

 

 

To Pin 3

 

 

 

 

 

 

Ground

 

 

 

To Pin 4

 

 

 

Figure 7-33

Again, these are the only four wires required from the microcontroller. Following is the parts list for this portion of the circuit.

Two 4.7 kOhm resistors (Radio Shack 271-1124)

One four-pin male connector 0.1" centers (Jameco PN: 114930)

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