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

The Microcontroller Idea Book (Jan Axelson, 1994)

.pdf
Скачиваний:
256
Добавлен:
12.08.2013
Размер:
2.85 Mб
Скачать

Sources

Siemens Components

8052 chips, data books

2191 Laurelwood Rd.

 

Santa Clara, CA 95054

 

408-980-4500

 

Siemens Components

optoelectronic components

Optoelectronics Division

 

19000 Homestead Rd.

 

Cupertino, CA 95014

 

408-257-7910

 

Sil-Walker

keypad kits

880 Calle Plano, Unit #N

 

Camarillo, CA 93012

 

805-389-8100

 

FAX: 805-484-3311

 

Suncoast Technologies

8052-BASIC board

PO Box 5835

 

Spring Hill, FL 34606

 

Voice/FAX: 352-596-7599

 

Systronix

BASIC compiler, BASIC-52 programming

555 South 300 East

manual

Salt Lake City, UT 84111

 

801-534-1017

 

FAX: 801-534-1019

 

BBS 801-487-2778

 

TAB-McGraw Hill

book publisher

P.O. Box 0850

 

Blue Ridge Summit, PA 17294-0850

 

1-800-262-4729

 

Timeline, Inc.

surplus LCD modules

1490 W. Artesia Blvd.

 

Gardena, CA 90247

 

1-800-872-8878

 

Unicorn Electronics

8052-BASIC chip, electronic components

10010 Canoga Ave. Unit B-8

 

Chatsworth, CA 91311

 

1-800-824-3432

 

The Microcontroller Idea Book

261

Appendix A

Universal Cross Assemblers

8051 assembler

P.O. Box 6158

 

Saint John, NB E2L 4R6

 

Canada

 

506-847-0681

 

262

The Microcontroller Idea Book

Programs for Loading Files

Appendix B

Programs for Loading Files

This appendix contains the BASIC-52 programs HEX2RAM.BAS and HEXLOAD.BAS described in Chapter 13. Use HEX2RAM.BAS to load an Intel Hex file from your host computer to RAM, including battery-backed (NV) RAM. The HEXLOAD.BAS programdoes the same, but also allows you to load the file into EEPROM or EPROM. HEX2RAM uses XBY, while HEXLOAD uses PGM, and is slower.

The Microcontroller Idea Book

263

Appendix B

Listing B-1 (page 1 of 2). HEX2RAM.BAS loads Intel Hex files from a host computer to memory in the 8052-BASIC system. Reprinted with permission from

Systronix.

10 STRING 82, 80 : DIM HI (70) : DIM LOW (70)

15FOR I=48 TO 57 : HI(I) = (I - 48) * 16 : LOW(I) = I - 48 : NEXT

25 FOR I=65 TO 70 : HI(I) = (I - 55) * 16 : LOW(I) = I - 55 : NEXT

35 PRINT TAB(19),"INTEL HEX FILE TO RAM LOADING PROGRAM V1.1" 45 PRINT TAB(14), “Copyright 1991 Systronix Inc. All rights

reserved.” : PRINT

55PRINT TAB(8),"This program accepts as input an Intel hex file and stores"

65PRINT TAB(8),"it in external RAM at the addresses specified in the HEX file.":PRINT

75PRINT TAB(8),"Ready to receive the input file one line at a time. Set your"

85 PRINT TAB(8),"communication software to send a line when when it receives"

95 PRINT TAB(8),"the ’>’ prompt." : PRINT

100LINE=LINE+1 : INPUT “>”,$(0) : PRINT"Validating and storing...",

105 C = ASC($(0),1) : IF C<>58 THEN GOTO 500

REM Get the byte count and save it in the variable COUNT 115 I = 2 : GOSUB 700 : COUNT = CH

REM Get the starting address for this record’s data

125FOR I = 4 TO 7 STEP 2 : GOSUB 700 : ADDR = (ADDR * 256) + CH : NEXT

REM Get the record type (we only understand types 0 and 1)

135I = 8 : GOSUB 700 : IF (CH <>0 .AND. CH <>1) THEN GOTO 510

145 RECORD = CH

REM The initial checksum calculation

155 CHECK=(ADDR/256)+(ADDR.AND.0FFH)+COUNT+RECORD

REM Get the individual bytes, accumulate them in the check sum and store

REM them in memory at the appropriate destination

165 FOR I = 10 TO 10 + (COUNT * 2) STEP 2 : GOSUB 700

264

The Microcontroller Idea Book

Programs for Loading Files

Listing B-1 (page 2 of 2).

REM Here we deal with the data bytes (not executed when RECORD is type 1)

175 CHECK=CHECK+CH

185 IF RECORD = 1 THEN GOTO 210

REM Store the data in RAM 195 XBY(ADDR)=CH

205if CBY (ADDR) <>CH THEN GOTO 550 ELSE ADDR = ADDR + 1 : NEXT

210 CHECK=CHECK.AND.0FFH : IF CHECK<>0 THEN GOTO 520

215 IF RECORD = 1 THEN GOTO 640

REM Reset and continue

225 ADDR = 0 : PRINT CR : GOTO 100

REM Error messages and program termination

500PRINT CR, “Line ”, LINE, “: Character”, I, “unexpected” : GOTO 600

510PRINT CR, “Line ”, LINE, “: Unknown record type”, CH : GOTO 600

520 PRINT CR, “Line ”, LINE, “: Checksum failure. Expected”, 525 PH0. ABS (CHECK-CH) : GOTO 600

550PRINT CR, “Line”, LINE, “: Verify error at address”, : PH0. ADDR

555 PH0. “The byte at”, addr, “ is”, CBY (ADDR) 565 PH0. “The byte should be”, CH : GOTO 600

600PRINT “Stop sending input file. Type a CONTROL+C to quit.”

605 GOTO 605

REM Normal program exit point

640 PRINT CR, “Received an End record in line”,LINE : END

REM Convert the ASCII text to numbers

700 C = ASC($(0),I) : CH = HI(C) : C = ASC($(0),I + 1) : CH = CH + LOW (C)

705 RETURN

The Microcontroller Idea Book

265

Appendix B

Listing B-2 (page 1 of 4). HEXLOAD.BAS copies an Intel Hex file into RAM, EEPROM, or EPROM in an 8052-BASIC system.

1 STRING 82, 80

2 PRINT TAB(22),"HEX FILE LOADING PROGRAM 1.2"

3 PRINT TAB(14), “(C) 1990,1991 Systronix Inc. All rights reserved.” : PRINT

4 PRINT “This program accepts as input, an Intel format hex file and stores”

5 PRINT “it in RAM, EPROM, or EEPROM at the addresses specified.” : PRINT

6 PRINT : PRINT “Select the destination device type:” : PRINT

7 PRINT TAB(10), “[1] RAM” : PRINT TAB(10), “[2] Timed EEPROM”

8 PRINT TAB(10), “[3] EPROM (Intelligent algorithm)”

9 PRINT TAB(10), “[4] EPROM (50mS algorithm)”

10 PRINT TAB(10), “[5] EEPROM (RDY/BUSY type)”

11 INPUT “Select Device Type (1, 2, 3, 4, 5) >> ”, TYPE

12 IF (TYPE < 1) .OR. (TYPE 5) THEN PRINT CHR(7) : GOTO 11 20 TYPE=TYPE-1 : IF TYPE = 0 THEN GOTO 1300

21 if type = 1 then goto 22 else goto 30

22 INPUT “Delay between writes >> ”, delay : goto 1300 30 ON (TYPE) GOSUB 2100, 2100, 2110, 2120, 2100

40 IF TYPE = 2 THEN DBY(38) = DBY(38) .OR. 8 ELSE DBY(38) = DBY(38) .AND. 0f7H

rem Calculate and store pulse width

50 WAIT = 65536-WAIT*XTAL/12 : DBY(40H) = WAIT/256 : DBY(41H) = WAIT.AND.0ffH

1300 PRINT “Ready to receive the input file one line at a time. Set your”

1310 PRINT “communication software to send a line when when it receives”

1320 PRINT “the ’>’ prompt.” : PRINT

1330 SOURCE = MTOP - FREE + 1

1340 LINE = LINE + 1 : INPUT">", $(0) : PRINT “Validating input buffer ...”,

1350 I = 1 : C = ASC($(0),1) : IF C<>58 THEN GOTO 3000 : REM if not “:”

266

The Microcontroller Idea Book

Programs for Loading Files

Listing B-2 (page 2 of 4).

rem Get the byte count and save it in the variable COUNT 1360 I = 2 : GOSUB 2000 : COUNT = CH

rem Get the starting address for this record’s data 1370 FOR I = 4 TO 7 STEP 2 : GOSUB 2000 : ADDR = (ADDR *

256) + CH : NEXT

rem Get the record type (we only understand types 0 and 1) 1380 I = 8 : GOSUB 2000 : IF (CH <>0 .AND. CH <>1) THEN

GOTO 3010

1385 IF TYPE <>0 .AND. ADDR <8000H .AND. CH = 0 THEN GOTO 3040

1390 RECORD = CH

rem Get the individual bytes, accumulate them in the check sum and store

rem them in memory

1400 INDEX = SOURCE : FOR I = 10 TO 10 + (COUNT * 2) STEP 2 : GOSUB 2000

rem Here we deal with the data bytes (not executed when RECORD is type 1)

1410 CHECK=CHECK+CH

1420 IF RECORD = 1 THEN GOTO 1440

1430 XBY(INDEX)=CH : INDEX = INDEX + 1 : NEXT

rem Calculate the checksum

1440 CHECK=(CHECK+(ADDR/256)+(ADDR.AND.0FFH)+COUNT+RECORD).AND.0FFH

1450 IF CHECK<>0 THEN GOTO 3020

1460 IF RECORD = 1 THEN GOTO 3030

1470 PRINT CR, “Storing a Data record of”, COUNT, “bytes at”, : PH0. ADDR

rem Retreive the data from memory and store them in the proper addresses

1480 on type gosub 1500, 1570, 1520, 1520, 1520

rem Restore our variables and continue 1490 ADDR = 0 : CHECK=0 : GOTO 1340

The Microcontroller Idea Book

267

Appendix B

Listing B-2 (page 3 of 4).

rem Store the data in RAM

1500 FOR I=SOURCE TO SOURCE + COUNT-1 : XBY(ADDR)=XBY(I)

rem Now verify that stored data is readable as code 1505 if XBY(I) <> CBY(ADDR) THEN GOTO 3130

1508 ADDR = ADDR+1 : NEXT

1510 RETURN

rem Store the data in EPROM or RDY/BUSY EEPROM rem First load the source address registers

1520 DBY (1BH) = SOURCE/256 : DBY (19H) = SOURCE .AND. 0FFH

rem Now load the destination address registers

1530 DBY (1AH) = (ADDR-1)/256 : DBY (18H) = (ADDR - 1)

.AND. 0FFH

rem Now load the number of bytes to program

1540 DBY (1Fh) = COUNT/256 : DBY (1Eh) = COUNT .AND. 0FFh

rem Program the data into the part

1550 PGM : IF DBY (1Fh) .OR. DBY (1Eh) <>0 THEN GOTO 3060 1560 RETURN

1570 FOR I=SOURCE TO SOURCE + COUNT-1 : XBY (ADDR) = XBY

(I)

1580 time = 0 : dby (47h) = 0 : clock1 : do : until time >= delay

1590 if xby (addr) <>xby (i) goto 3100

1600 addr = addr + 1: clock0 : next : clock0 : return

rem Convert the ASCII text to numbers

2000

C = ASC($(0),I)

: IF (C<=70 .AND. C>=65) THEN C = C -

 

55 ELSE C = C -

48

 

 

 

 

 

2020

CH = C * 16 : C

= ASC($(0),I + 1)

2040

IF (C<=70 .AND. C>=65) THEN C = C - 55 ELSE C = C - 48

2050

CH = CH + C : RETURN

rem Here when we are

going to load the data into EEPROM.

2100

WAIT = 0.0005 : RETURN

rem Here for Intelligent programming

2110

WAIT = 0.001 : RETURN

268

 

 

 

The Microcontroller Idea Book

Programs for Loading Files

Listing B-2 (page 4 of 4).

rem Error messages and program termination

3000 PRINT CR ,"Line “,LINE,” EXPECTED ’:’, FOUND ’",CHR(C),"’" : END

3010 PRINT CR, “Line ”, LINE, “: Unknown record type”, CH : END

3020 PRINT CR, “Line ”, LINE, “: Checksum failure. Expected”,

3025 PH0. ABS (CHECK - CH) : END rem Normal program exit point

3030 PRINT CR, “Received an End record in line”,LINE : END 3040 PRINT CR, “Line”, LINE, “: Illegal EPROM/EEPROM ad-

dress :”,

3050 PH0. ADDR : END

3060 PRINT CR, “Line”, LINE, “: PGM error at address”, 3070 ADDR = DBY (1AH) * 256 + DBY (18h) : PH0. ADDR 3080 PH0. “The byte at”, address, “ is”, XBY(ADDRESS) 3090 PH0. “The byte should be”,DBY (28) : END

3100 PH0. CR, “Line”, LINE, “: Error writing at address”, ADDR

3110 PH0. “The byte at”, addr, “ is”, XBY(ADDR) 3120 PH0. “The byte should be”, XBY (I) : END REM verify CBY() read in RAM failed:

3130 PH0. “The byte at”,addr," failed a verify with CBY" : END

The Microcontroller Idea Book

269

270

The Microcontroller Idea Book