📄 ex10-1.asm
字号:
org 0000h ;单片机复位地址
ajmp main ;转移到主程序处
org 0100h ;main被定位在0x0100处
main: mov sp,#80h ;初始化堆栈指针
jnb P1.0,cr
jnb P1.1,cv
jnb P1.2,ca
cr: mov R7,#00h
lcall adc
LCALL RDAT
lcall DISPLAY
sjmp main
CV: MOV R7,#01H
LCALL ADC
LCALL VDAT
LCALL DISPLAY
SJMP MAIN
CA: MOV R7,#02H
LCALL ADC
LCALL ADAT
LCALL DISPLAY
SJMP MAIN
ADC: MOV A,R7 ;0808 A/D转换子程序
MOV DPTR,#7FFFH
MOVX @DPTR,A
JB P3.3,$
MOVX A,@DPTR ;输入转换结果
RET
vdat:
mov R2,#00h
mov R3,A
mov R6,#01h
mov R7,#0F4h
call MULD2 ;乘以500
clr C
mov A,r5
add A,#60h ;加96修正
mov r5,A
mov A,r4
addc A,#00h
mov r4,A
mov A,r3
addc A,#00h
mov r3,A
mov A,r2
addc A,#00h
mov r2,A
mov r0,#30h
mov r1,#34h
mov A,R2
mov @r1,A
inc r1
mov A,R3
mov @r1,A
inc r1
mov A,R4
mov @r1,A
inc r1
mov A,R5
mov @r1,A
inc r1
mov @r1,#00h
inc r1
mov @r1,#00h
inc r1
mov @r1,#00h
inc r1
mov @r1,#0FFh
call DIVD4 ;除以255
mov r1,#38h
mov @r1,#00h
inc r1
mov @r1,#00h
inc r1
mov @r1,#00h
inc r1
mov @r1,#0Ah
call DIVD4
mov 43h,33h
call DIVD4
mov 42h,33h
call DIVD4
mov 41h,33h
mov r0,#40h
mov @r0,#00h
inc r0
mov A,41h
mov DPTR,#SEGMENT7
movc A,@A+DPTR
orl A,#80h
mov @r0,A
inc r0
mov A,42h
mov DPTR,#SEGMENT7
movc A,@A+DPTR
mov @r0,A
inc r0
mov A,43h
mov DPTR,#SEGMENT7
movc A,@A+DPTR
mov @r0,A
ret
ADAT:
mov B,A
mov A,#0B6h
clr C ;以下根据范围设置数值以防溢出
subb A,B
jc LARGERA
mov A,B
subb A,#16h
jc LESSA
ajmp MIDDLEA
LARGERA: mov A,#0B6h
ajmp CALCULATEA
LESSA: mov A,#16h
ajmp CALCULATEA
MIDDLEA: mov A,B
CALCULATEA: mov r2,#0C3h
mov r3,#50h
mov r6,#00h
mov r7,A
call MULD2 ;乘以50000
clr C
mov A,r5
subb A,#70h ;以下减去102000
mov r5,A
mov 37h,A
mov A,r4
subb A,#8Eh
mov r4,A
mov 36h,A
mov A,r3
subb A,#01h
mov r3,A
mov 35h,A
mov A,r2
subb A,#00h
mov r2,A
mov 34h,A
mov r0,#30h
mov r1,#38h
mov @r1,#00h
inc r1
mov @r1,#01h
inc r1
mov @r1,#5Eh
inc r1
mov @r1,#0A0h
call DIVD4 ;除以89760
mov r1,#38h
mov @r1,#00h
inc r1
mov @r1,#00h
inc r1
mov @r1,#00h
inc r1
mov @r1,#0Ah
mov DPTR,#SEGMENT7
call DIVD4
mov A,33h
movc A,@A+DPTR
mov 43h,A
call DIVD4
mov A,33h
movc A,@A+DPTR
mov 42h,A
call DIVD4
mov A,33h
movc A,@A+DPTR
cjne A,#3Fh,NOTEQU
mov A,#00h
NOTEQU: mov 41h,A
mov 40h,#00h
RET
rdat:
mov R2,#00h
mov R3,A
mov R6,#03h
mov R7,#0E8h
call MULD2 ;乘以1000
mov r0,#30h
mov r1,#34h
mov A,R2
mov @r1,A
inc r1
mov A,R3
mov @r1,A
inc r1
mov A,R4
mov @r1,A
inc r1
mov A,R5
mov @r1,A
inc r1
mov @r1,#00h
inc r1
mov @r1,#00h
inc r1
mov @r1,#00h
inc r1
mov @r1,#0FFh
call DIVD4 ;除以255
mov r1,#38h
mov @r1,#00h
inc r1
mov @r1,#00h
inc r1
mov @r1,#00h
inc r1
mov @r1,#0Ah
mov DPTR,#SEGMENT7
call DIVD4 ;连续进行4次除以10的操作
mov A,33h ;取得10进制值
movc A,@A+DPTR
mov 43h,A
call DIVD4
mov A,33h
movc A,@A+DPTR
mov 42h,A
call DIVD4
mov A,33h
movc A,@A+DPTR
mov 41h,A
call DIVD4
mov A,33h
movc A,@A+DPTR
cjne A,#3Fh,NONZERO
mov A,#00h
NONZERO: mov 40h,A
ret
DELAY_5ms: mov R5,#01h ;设置R5初始计数值
DELAY_5ms1: mov R6,#16h ;设置R6初始计数值
DELAY_5ms2: mov R7,#70h ;设置R7初始计数值
DELAY_5ms3: djnz R7,DELAY_5ms3 ;延时时间为 Time=( ( (R7*2)+2+1 )*R6+2+1 )*R5+1
djnz R6,DELAY_5ms2 ;
djnz R5,DELAY_5ms1 ;
ret
SEGMENT7: DB 3Fh ;7段数码管字符0的abcdefg的值,a在最低位,最高位始终保留为0
DB 06h
DB 5Bh
DB 4Fh
DB 66h
DB 6Dh
DB 7Dh
DB 07h
DB 7Fh
DB 6Fh
DISPLAY: mov DPTR,#0fEffh ;写第一位数码管
mov r1,#40h
mov A,@r1
movx @DPTR,A
setb C
mov P1.4,C
clr C
mov P1.4,C
call DELAY_5ms
setb C
mov P1.4,C
inc r1
mov A,@r1 ;写第二位数码管
movx @DPTR,A
setb C
mov P1.5,C
clr C
mov P1.5,C
call DELAY_5ms
setb C
mov P1.5,C
inc r1
mov A,@r1 ;写第三位数码管
movx @DPTR,A
setb C
mov P1.6,C
clr C
mov P1.6,C
call DELAY_5ms
setb C
mov P1.6,C
inc r1
mov A,@r1 ;写第四位数码管
movx @DPTR,A
setb C
mov P1.7,C
clr C
mov P1.7,C
call DELAY_5ms
setb C
mov P1.7,C
ret
;双字节二进制无符号数乘法
;被乘数在R2(高位)、R3(低位)中,乘数在R6(高位)、R7(低位)中。
;乘积在R2(高位)、R3、R4、R5(低位)中。
;用到累加器A,B,PSW,R2~R7。
;永远不会产生进位。
;在出口时总是清除C。
;若结果超出2个字节范围则OV=1。
MULD2:
MOV A,R3
MOV B,R7
MUL AB
MOV R4,B
MOV R5,A
MOV A,R3
MOV B,R6
MUL AB
ADD A,R4
MOV R4,A
CLR A
ADDC A,B
MOV R3,A
MOV A,R2
MOV B,R7
MUL AB
ADD A,R4
MOV R4,A
MOV A,R3
ADDC A,B
MOV R3,A
CLR A
RLC A
XCH A,R2
MOV B,R6
MUL AB
ADD A,R3
MOV R3,A
MOV A,R2
ADDC A,B
MOV R2,A
ORL A,R3
JZ MULD21
SETB OV
RET
MULD21:
CLR OV
RET
;四字节无符号数除法
;R0存放被除数,除数,商数的地址。
;从R0开始的连续四个字节为结果的余数,入口时可以为任意,但在出口时发生变化。
;其后的连续4个字节在入口时是被除数,出口时是商数。
;再其后的连续四个字节在入口时是除数,出口时保持不变。
;用到累加器A,B,PSW,R0~R7。
;如果除数为零,则置OV=1标志,否则清零。
;在出口时总是清除C。
DIVD4:
MOV A,R0
MOV B,A
ADD A,#08h
MOV R1,A
MOV A,#00h
ORL A,@R1
INC R1
ORL A,@R1
INC R1
ORL A,@R1
INC R1
ORL A,@R1
JZ DIVD45
MOV R1,B
MOV R2,#04h
DIVD41:
MOV @R1,#00h
INC R1
DJNZ R2,DIVD41
MOV R3,#20h
DIVD42:
MOV R2,#08h
MOV A,B
MOV R0,A
ADD A,#07h
MOV R1,A
CLR C
DIVD43:
MOV A,@R1
RLC A
MOV @R1,A
DEC R1
DJNZ R2,DIVD43
MOV A,R0
ADD A,#03h
MOV R1,A
MOV A,R0
ADD A,#0Bh
MOV R0,A
MOV A,@R1
SUBB A,@R0
MOV R4,A
DEC R1
DEC R0
MOV A,@R1
SUBB A,@R0
MOV R5,A
DEC R1
DEC R0
MOV A,@R1
SUBB A,@R0
MOV R6,A
DEC R1
DEC R0
MOV A,@R1
SUBB A,@R0
MOV R7,A
JC DIVD44
MOV A,B
MOV R0,A
ADD A,#03h
MOV R1,A
MOV A,R4
MOV @R1,A
DEC R1
MOV A,R5
MOV @R1,A
DEC R1
MOV A,R6
MOV @R1,A
DEC R1
MOV A,R7
MOV @R1,A
MOV A,R0
ADD A,#07h
MOV R1,A
INC @R1
DIVD44:
DJNZ R3,DIVD42
MOV R0,B
CLR OV
CLR C
RET
DIVD45:
SETB OV
CLR C
RET
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -