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

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

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

340

Chapter 8 / The BASIC Stamp 2p Prototype

 

 

 

 

 

 

DEBUG Dbyte

// Display to Debug Console

NEXT

 

// Until LoopCtr = DLen

DEBUG CR

 

// Send CR to debug Console

LOW Reset

 

// Bring Reset Low

RETURN

 

 

WriteMem:

 

// Write Scratchpad to RAM

HIGH Reset

// Bring Reset High

SHIFTOUT Dio, Clk, LSBFIRST, [Cscp]

// Output Control Byte

SHIFTOUT Dio, Clk, LSBFIRST, [SMem, EMem, DLen]// Output RAM Start, End, and Length

DEBUG DEC3 TA1, DEC3 TA2, DEC3 DLen, CR

// Check Values to debug Screen

DO

 

// Do

PAUSE 1

 

// Wait 1 ms

LOOP UNTIL (Dio <> 0)

// Until Data Line is Low

LOW Reset

 

// Bring Reset Low

RETURN

 

 

ReadRAM:

 

// Read from DS2404 RAM

HIGH Reset

// Bring Reset High

SHIFTOUT Dio, Clk, LSBFIRST, [RMem]

// Output Control Byte

SHIFTOUT Dio, Clk, LSBFIRST, [SMem, EMem]

// Output Start & End RAM Locations

FOR LoopCtr = 0 TO Dlen

// Loop from 0 to DLen

SHIFTIN Dio, Clk, LSBPRE, [Dbyte]

// Read RAM Location Byte

// WRITE DLen, DByte

// Write to EEPROM

DEBUG Dbyte

// Send to Debug Terminal

NEXT

 

// Increment LoopCtr

DEBUG CR

 

// Send CR to Debug Console

LOW Reset

 

// Bring Reset Low

RETURN

 

 

Listing 8-12 is much smaller than Listing 8-11. Running this listing on either of the 3-Wire boards from Chapter 7 will produce the following output to the BS2 Debug terminal.

Chapter 8 / The BASIC Stamp 2p Prototype

341

 

 

Figure 8-42

The first line of output is echoed from the read scratchpad subroutine. The second line is the decimal display $E0 $01 $1F, which is the starting address, ending address, and data length. The last line is from the read RAM subroutine, which validates the data we wrote to the scratchpad was properly copied to RAM and read back.

Adding RS-232 Communications

I waited to add the RS-232 host interface to see how many I/O pins remain unused on the BS2. We have six unused pins so we will add the RS-232 interface on I/O pins 12 to 15, which are pins 17 to 20 in Figure 8-43.

Since the I/O pin assignment remains unchanged from Chapter 7, the following program listing (Listing 8-13) should work without modification. It contains the test program modified to reflect the pin assignments we are using for the BS2p.

Chapter 8

342

Chapter 8 / The BASIC Stamp 2p Prototype

 

 

Figure 8-43

Listing 8-13

//Test Program for RS-232 output using MAX232 with RTS/CTS Handshaking

//for Embedded Systems Desktop Integration Chapter 8

//Copyright 2004 - Oliver H. Bailey

//

// Pins are assigned in the following order:

// Pin 12 -

Transmit Data

(TX or TD) =>

// Pin 14

-

Receive Data

(RX or RD) <=

// Pin 13

-

Clear To Send

(CTS)

=>

// Pin 15

- Request To Send (RTS)

<=

//

 

 

 

 

//This program uses the FPin variable on SerIn and SerOut commands. Example follows below

//Serout 12\15, 110, "Mary had a little lamb" CR, LF

InData VAR Byte

OutData VAR Byte

 

Chapter 8 / The BASIC Stamp 2p Prototype

343

 

 

 

 

 

 

LOW 14

// Set RD Low

 

LOW 15

// Set RTS Low

 

Main:

 

 

SERIN 14\13, 110, [InData]

// 14=RD,13=CTS,110=9600,N,8,1

 

OutData = InData

// Assign to output variable

 

IF OutData = 13 THEN

// Turn CR into CRLF

 

SEROUT 12\15, 110, [CR, LF]

// Echo Back to Terminal

 

DEBUG CR, LF

// Echo to Debug port

 

ENDIF

 

 

SEROUT 12\15, 110, [OutData]

// Just print the RAW data

 

DEBUG OutData

// Echo to Debug Port

 

GOTO Main:

// Start Again

 

Finishing Touches for Our BS2p Prototype

We have finally reached the end of the prototype development cycle for our embedded system using the Basic Stamp BS2p24. By sharing the same I/O pins for the Ethernet and USB interfaces we have two I/O pins left over for future use. Remember that this is a prototype so we will simply plug and unplug the USB and Ethernet interfaces for test purposes. I mounted the boards all together on a poster board backing that cost about $3. I used #6 screws and nuts to make the boards stand off the backer board for test purposes. Figure 8-44 shows the finished prototype using the boards we developed in Chapter 7.

Chapter 8

344

Chapter 8 / The BASIC Stamp 2p Prototype

 

 

Figure 8-44

From the upper left going clockwise, are the Dallas temperature/clock/relay board, Matrix Orbital LK202-25 display, keyboard/alarm board, BS2p board, DLP Designs USB232BM board, and the NetBurner SB72 board. Not shown are the RS-232 host interface, the Parallax USB2SER board, and 7805 power supply.

Chapter Summary

We covered a lot of ground in this chapter and implemented several types of communications interfaces, two different Maxim/Dallas interfaces, and an I2C interface for the LCD. All of the remaining hardware chapters will use the same I/O boards and we will focus on the differences in the processors we use. The finished programs for the thermostat can be found at www.wordware.com/files/embsys or at www.time-lines.com.

Chapter 9

The PIC Prototype

In the previous chapter we implemented our prototype using the BS2p. I like using the BASIC Stamp for several reasons but the two that come to my mind first are fast prototyping and ease of programming. In short, I can take an idea and quickly see if it works using the BASIC Stamp. At the core of the BS2p is a microcontroller processor.

The microcontroller takes us a level closer to how the hardware actually works and provides features not available in the BASIC Stamp. Just about anyone can use a BASIC Stamp, and the BASIC Stamp is used widely in schools to educate students in electronics, robotics, and other areas. Using a microcontroller, on the other hand, requires a higher degree of knowledge to use but offers more flexibility to the professional engineer. Both devices have their place in the market. Let’s see first-hand how similar and dissimilar these devices really are.

Note:

Before continuing I want to say that I use both devices (and the others as well) all the time. All of these devices are unique and have their strengths in the commercial marketplace. We are not comparing these devices as acceptable or not acceptable but exploring which is best suited to this need based on cost, development effort, and functionality.

345

346

Chapter 9 / The PIC Prototype

 

 

This chapter has fewer photos and diagrams than the previous chapters. This is not an accident. The MPLAB IDE that is used in this chapter has its own documentation. I could never do that product justice by attempting to cover its features here so I have chosen to provide the logic and code snippets behind the implementation. This eliminates distracting you, the reader, from the main objective, which is developing the embedded device using the PIC microcontroller. I provide just enough code for you to test each feature and leave the main listings for you to download. Each listing is fully annotated as to what happens when and why it happens. This allows me to present the complete thought and implementation logic without again distracting you from the main development objective. Since the code is fully annotated it also allows you to use the code to build upon the design without having to have the book open. There is nothing I hate worse than having to open multiple documents or books at the same time to understand a subject.

Similarities between the BASIC Stamp and the PIC

The BASIC Stamp and the PIC share a similar BASIC language. This allows a quick prototype to be developed using the BASIC Stamp and then compiling the source using a third-party compiler to achieve the same functionality in the PIC. There are some implementation differences between the BS2p and the PIC. One example of this is POLLIN and POLLOUT commands on the BS2. The PIC instead supports interrupts — hardware features handled by software functions — so there is no need for these commands. An interrupt does not wait to be serviced as the POLL commands on the BASIC Stamp do. There is also no extra code needed between each BASIC command.

Chapter 9 / The PIC Prototype

347

 

 

Differences between the BASIC Stamp and the PIC

The PIC does not contain an interpreter but rather executes actual machine language. This increases the speed of program execution and provides the ability to support interrupts and other functions that may require faster processing. The most important difference is the availability of multiple langauges to develop with.

The BASIC Stamp has a built-in interpreter in EEPROM. This EPROM may be internal or external. Typically a language interpreter like PBASIC resides within the device for speed and security purposes. While both devices provide for variable storage and static data storage, the big difference is the amount of storage available to the developer. This brings us directly to the biggest difference — the sheer number of available parts and configurations. At publication time there were hundreds of different PIC parts and several product families to choose from. There are low-range, mid-range, and high-range parts defined as such by instruction length, which also determines the amount of addressable memory and execution speed. In just the DIP (dual inline package) parts range from six I/O pins to over 40 I/O pins. Clocks range from an internal 4 mHz clock to external clocks of 20 mHz and above. The PIC line also has a wide variety of built-in peripherals suce as USB and power management.

Chapter 9

348

Chapter 9 / The PIC Prototype

 

 

Types of PIC Microcontrollers and

Development Languages

There are literally hundreds of different PIC microcontroller configurations, but they can be broken down into different groups. They are:

8 bit

16 bit

Digital signal processing

Peripheral processing

Memory management

Power management

All of the PIC microcontrollers utilize the Harvard memory architecture, which uses a banked memory scheme instead of linear addressing.

Now let’s take a look at available compilers for the PIC processor line. The products range from free to about $1,000 depending on the source and type of compiler. Let’s take a look at the available languages for the PIC processor lines:

Assembler — Available free from Microchip and included with other products as well. Comes complete with an IDE.

The C language — Available from several vendors both in the U.S. and abroad. Prices range from free to about $1,000.

The C++ language — Available from at least one vendor, this product is priced below $100.

Pascal — Available from at least one vendor and also priced below $100.

BASIC — Available from several vendors and priced from $50 to $250 depending on compiler type and features.

Chapter 9 / The PIC Prototype

349

 

 

There are many compiler vendors. Following is a short list that was used to develop this book:

microEngineering Labs — PicBasic and PicBasic Pro

HI-TECH — ANSI C

Custom Computer Services — C compiler

SourceBoost — C, C++, and Pascal compilers

Microchip — Assembler and several C compilers

GNUPIC — Free C compiler

The Microchip Assembler (MPASM) provides an integrated workbench that all of the other compilers can be integrated into. All of the other compilers also provide a development interface of their own. If you want to have a consistent development interface, use the Microchip IDE. Each of the other IDEs have unique features, so you can experiment to see which suits your needs best.

The PIC Development Cycle

The PIC development cycle is different from BASIC Stamp development. Using the Stamp we wrote the code, downloaded it to the port, and used debug statements to output run-time information. We did all this from within the BASIC Stamp IDE. The PIC development cycle is much different, as shown in Figure 9-1.

Chapter 9

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