ВУЗ: Не указан
Категория: Не указан
Дисциплина: Не указана
Добавлен: 15.06.2025
Просмотров: 2081
Скачиваний: 0
Chapter 13
Listing 13-6. This assembly-language routine is similar to Listing 13-1, except that external interrupt 1 causes the routine to run.
;On external interrupt 1, Port 1, bit 0 is complemented
org |
4013h |
;vector for external |
;interrupt 1 |
||
cpl |
p1.0 |
;complement Port 1, bit 0 |
;(pin 1) |
||
pop |
psw |
;push psw was automatic on |
;interrupt, |
||
;but pop psw must be added |
reti end
and add this to the end, before returning to BASIC-52:
pop psw |
;restore program status word |
Your routine can then write to registers R0-R7 without worrying about conflicting with BASIC.
Interrupts
BASIC-52 also includes a way of adding assembly-language routines that respond to interrupts. Normally, the 8052 stores its interrupt vectors (the locations where the program jumps on interrupts) from 03h to 2Bh in code memory. Since these locations are in ROM in the 8052-BASIC, your programs can’t change their contents. But built into BASIC-52 is the ability to place alternate interrupt routines from 4003h to 402Bh.
To illustrate, Listing 13-6 is an assembly-language interrupt routine. The routine’s origin is 4013h, which is BASIC-52’s alternate vector for external interrupt 1. The interrupt routine has the same function as Listing 13-1. It toggles bit 0 of Port 1, then returns to BASIC-52.
These are a few things of note about Listing 13-6:
•You must have code memory at 4013h, since BASIC-52 specifies that this location must contain either the interrupt routine or a jump to a longer routine. If the routine is longer than 8 bytes, use a jump instruction, such as sjmp 4033h, to prevent overwriting any interrupt vectors that follow.
232 |
The Microcontroller Idea Book |
Calling Assembly-language Routines
•BASIC-52’s ON EX1 instruction will also respond to external interrupt 1, but the response time will be much slower. ON EX1 has priority, however, and Listing 13-6 will not execute if an ON EX1 statement has executed in BASIC-52.
•When BASIC-52 jumps to an assembly-language interrupt routine, it automatically pushes psw on the stack. But popping psw on returning from the interrupt routine is not automatic, so the interrupt routine must include an instruction to do so.
•Unlike other subroutines, which end with ret, interrupt routines must end with reti.
To test Listing 13-6, upload it to RAM at 4013h, and execute these two lines of BASIC-52 code, to ensure that the interrupt is enabled:
IE=IE.OR.84h
TCON=TCON.OR.04h
Now, each time pin 13 goes low, pin 1 should toggle, as it did with Listing 13-1.
Adding Custom Commands and Instructions
Another feature of BASIC-52 is the ability to add up to 16 custom keywords representing commands or instructions that you define with assembly-language routines. Listing 13-7 is an example program for doing so.
To add custom keywords, you must have code memory from 2000h to 2071h, because BASIC-52 looks for special information at several addresses in this area.
On bootup, BASIC-52 examines the data at address 2048h. If bit 5 is set, BASIC-52 assumes that you have added custom keywords, and it looks for additional information in a token table and vector table.
At 2078h, BASIC-52 expects to find the starting address of a token table, which lists your new keywords. At 2070h, it expects the starting address of a vector table, which lists the assembly-language routines that the new keywords execute.
You may upload both tables to any free area of combined code/data memory. If you’re using an external EPROM programmer, you can use code-only memory in the 8052-BASIC system.
In the token table, each new keyword is assigned a number, in sequence from 10h to 1Fh. The name of the keyword is surrounded by quotation marks, and a 0 indicates the end of the token. The final keyword in the list ends in 0FFh, to signify the end of the list of tokens. Listing 13-7 adds three keywords, which toggle, set, and clear bit 0 of Port 1.
The Microcontroller Idea Book |
233 |
Chapter 13
Listing 13-7. (page 1 of 2) Creates instructions to set, clear, and toggle Port 1, bit 0 in BASIC-52.
;example program for creating custom commands and instructions in BASIC-52
;system must contain code memory from 2000h-2079h
tokentable equ |
2100h |
;start address, token table |
|
vectortable equ |
2200h |
;start address, vector table |
|
org |
2002h |
;5Ah at 2002h tells BASIC-52 |
|
db |
5ah |
;to call 2048h (see below) |
|
org |
2048h |
;Set bit 45 to tell BASIC-52 |
|
;that custom commands or |
|||
setb |
45 |
;instructions have been |
|
ret |
;added |
||
org |
2078h |
;stores starting address of |
|
mov |
dptr,#tokentable |
token table |
|
ret |
|||
org |
2070h |
;stores starting address of |
|
mov |
dptr,#vectortable |
vector table |
|
ret |
|||
org |
tokentable |
;token table start address |
|
db |
10h |
;first user-defined token |
|
db |
“TGGP10" |
;command or instruction name |
|
;(must use all capital |
|||
;letters, beginning |
|||
;combination of letters |
|||
;must be unique) |
|||
db |
0 |
;end of token indicator |
|
db |
11h |
;2nd user-defined token |
|
db |
“SETP10" |
;command or instruction name |
|
db |
0 |
;end of token indicator |
|
db |
12h |
;final user-defined token |
|
db |
“CLRP10" |
;command or instruction name |
|
db |
0ffh |
;end of list indicator |
|
234 |
The Microcontroller Idea Book |
Calling Assembly-language Routines |
|||
Listing |
13-7. (page 2 of 2) |
||
;can add up to 1Fh tokens |
|||
;final token must end with 0ffh |
|||
org |
vectortable |
;vector table address |
|
dw |
tggp10 |
;label to branch to on |
|
;TGGP10 command |
|||
dw |
setp10 |
;label to branch to on |
|
;SETP10 command |
|||
dw |
clrp10 |
;label to branch to on |
|
;CLRP10 command |
|||
tggp10 |
org 3000h |
;use any available address |
|
cpl |
p1.0 |
;complement Port 1, bit 0 |
|
;(pin 1) |
|||
ret |
;return to BASIC-52 |
||
setp10 |
org 3010h |
;use any available address |
|
setb |
p1.0 |
;set Port 1, bit 0 (pin 1) |
|
ret |
;return to BASIC-52 |
||
clrp10 |
org 3020h |
;use any available address |
|
clr |
p1.0 |
;clear Port 1, bit 0 (pin 1) |
|
ret |
;return to BASIC-52 |
||
end
The vector table consists of a list of labels corresponding to the beginning of each assembly-language routine.
In addition to the tables, you must store the assembly-language routines themselves, again using any free code/data or code memory.
To use Listing 13-7, you must assemble it and upload the resulting Intel Hex file into NV memory that will be preserved on powering down or rebooting. Reboot, and you can use the new keywords TGGP10, SETP10, and CLRP10 to control bit 0 of Port 1. (Notice that the “bit-toggle”keyword is TGGP10, rather than TOGP10, which contains the keyword TO and so won’t work.)
The Microcontroller Idea Book |
235 |
Chapter 13
Listing 13-8. (page 1 of 2) Copies data from external memory into EPROM, EEPROM, or NV RAM.
10 |
PRINT “enter device type: ” |
|
20 |
PRINT “EPROM 50-msec |
1" |
30 |
PRINT “EPROM Intelligent |
2" |
40 |
PRINT “EEPROM or NV RAM |
3" |
50 |
PRINT “quit |
4" |
60 |
INPUT T |
|
70 |
REM set pulse width for device type |
|
80 |
REM W = pulse width in milliseconds |
|
90 |
IF T=1 THEN W=.05 |
|
100 |
IF T=2 THEN W=.001 |
|
110 |
IF T=3 THEN W=.0005 |
|
120 |
IF T=4 THEN GOTO 470 |
|
130 |
REM calculate and store pulse width |
|
140 |
B=(65536-(W*XTAL/12)) : GOSUB 500 |
|
150 |
DBY(40H)=BH : DBY(41H)=BL |
|
160 |
REM set up for intelligent programming or not |
|
170 |
I=DBY(26H) |
|
180 |
IF W=.001 THEN DBY(26H)=I.OR.8 ELSE DBY(26H)=I.AND.0F7H |
|
190 |
INPUT “starting address of data to copy (source)? ”,S |
|
200 |
IF S<200H OR S>0FFFFH THEN |
GOTO 190 |
210 |
INPUT “ending address of data to copy (source)? ”,E |
|
220 |
IF E<S OR E>0FFFFH THEN GOTO 210 |
|
230 |
INPUT “starting address to program (destination)? ”,P |
|
240 |
IF P<MTOP OR P>0FFFFH THEN |
GOTO 230 |
250 |
REM calculate and store number of bytes to program |
|
260 |
B=(E-S)+1 : GOSUB 500 : DBY(1FH)=BH : DBY(1EH)=BL |
|
270 |
REM store starting address of destination-1 |
|
280 |
B=P-1 : GOSUB 500 : DBY(1AH)=BH : DBY(18H)=BL |
|
290 |
PH0. “eprom low = ”,BL |
|
300 |
PH0. “eprom high = ”,BH |
|
236 |
The Microcontroller Idea Book |
Calling Assembly-language Routines
Listing 13-8. (page 2 of 2)
310 REM store starting address of source
320 B=S : GOSUB 500 : DBY(1BH)=BH : DBY(19H)=BL
330 PH0. “ram low = ”,BL
340 PH0. “ram high = ”,BH
350 PRINT “press ENTER to begin programming”
360 X=GET : IF X<>0DH THEN 360
370 REM program the EPROM
380 PRINT “programming in progress...”
390 PGM
400 REM check for errors
410 IF (DBY(1EH).OR.DBY(1FH))=0 THEN PRINT “programming OK”
:GOTO 470
420 |
REM on error, calculate address that failed to program |
||
430 |
DC=DBY(19H)+256*DBY(1BH)-1 |
||
440 |
PH0. “ERROR: Source address |
”,DC," = “,XBY(DC) |
|
450 |
DP=DBY(18H)+256*DBY(1AH) |
||
460 |
PH0. “ |
Destination address ”,DP," = “,XBY(DP) |
|
470 |
END |
||
500 REM separate B into high (BH) and low (BL) bytes 510 BL=(B.AND.0FFH)
520 BH=INT(B/256)
530 RETURN
A General-purpose EPROM Programmer
With Listing B-2, you can use an 8052-BASIC system as a general-purpose programmer for EPROM, EEPROM, or NV RAM. The program will read any file in Intel Hex format, and store it at the addresses specified in the file.
For example, you can add a socket for an 8K EPROM, EEPROM, or NV RAM addressed at A000h-BFFFh in combined code/data memory. For EEPROM or NVRAM, wire the socket exactly like U8 in Figure 4-3, except wire pin 1 of U9 to chip-select A000h (pin 10 of U6 in Figure 3-1) instead of to 8000h. For EPROM programming, also connect Figure 4-5’s circuits to pins 1 and 28 of the EPROM, for the programming voltages.
The Microcontroller Idea Book |
237 |
Chapter 13
With these added components and Listing B-2, you can program a DS1225 NV RAM, a 28(C)64 EEPROM, or a 27(C)64 EPROM with an Intel Hex file.
One use would be to program an EPROM for a non-BASIC-52 system, where EA is tied low and on bootup, the 8052 runs a program beginning at 0 in external code memory, instead of running the BASIC-52 interpreter in internal ROM. For this use, you must add A000h to the values given in all ORG directives. For example, you would change ORG 0 to ORG A000h, and change ORG 200h to ORG A200h. You can then use Listing B-2 to copy the program into the EPROM at A000h, remove the EPROM and install it at 0 in code memory in your non-BASIC-52 system. On bootup, the 8052 will run the program in EPROM.
Another Way to Program EPROMs
Listing 13-8 is another program that you can use to copy information from external data memory into an EPROM, EEPROM, or NVRAM. To use this program, you must specify the locations to copy (the source), the locations to copy to (the destination), and the device type to copy to. The program does the rest. With this program, you can copy information directly from RAM or other memory to another device, without uploading or translating to Intel Hex format.
The program prompts you for and stores information about the programming algorithm and addresses to program and copy from. BASIC-52’s PGM instruction then uses this information to program the selected locations.
238 |
The Microcontroller Idea Book |