51单片机万用表仿真,原理图如下:
仿真及完整程序的下载地址:
实用万用表.rar
(33.37 KB, 下载次数: 64)
c语言版本的单片机万用表51黑下载:http://www.51hei.com/bbs/dpj-50069-1.html
万用表的程序源码如下:
- org 0000h ;单片机复位地址
- ajmp main ;转移到主程序处
-
- org 0100h ;main被定位在0x0100处
- main: mov sp,#80h ;初始化堆栈指针
- mov DPTR,#0E000h
- mov A,#00h
- movx @DPTR,A
- setb C
- mov P3.0,C
- call DELAY_5ms
- JUDGE: mov A,P1 ;判断测量种类
- anl A,#0Fh
- cjne A,#00h,NON_CAP
- call CAP_MEA
- NON_CAP: cjne A,#01h,NON_V
- call V_MEA
- NON_V: cjne A,#02h,NON_A
- call A_MEA
- NON_A: call R_MEA
-
- CAP_MEA: mov A,#00h ;测量电容
- clr C
- mov P3.0,C
-
- DELAY_1S: mov R5,#2Eh ;设置R5初始计数值
- DELAY_1S1: mov R6,#58h ;设置R6初始计数值
- DELAY_1S2: mov R7,#7Ah ;设置R7初始计数值
- DELAY_1S3: djnz R7,DELAY_1S3 ;延时时间为 Time=( ( (R7*2)+2+1 )*R6+2+1 )*R5+1
- djnz R6,DELAY_1S2 ;
- djnz R5,DELAY_1S1 ;
- setb C
- nop
- nop
- mov P3.0,C
- READC: mov P2,#00h
- setb C
- mov P3.1,C
- nop
- clr C
- mov P3.1,C
- nop
- call DELAY_5ms
- mov DPTR,#0000h
- movx A,@DPTR
- mov B,A ;防止数据溢出
- mov A,#0E6h
- subb A,B
- jc LARGERC
- mov B,A
- subb A,#31h
- jc MIDDLEC
- LESSC: mov A,#30h
- ajmp CALCULATEC
- LARGERC: mov A,#00h
- ajmp CALCULATEC
- MIDDLEC: mov A,B
- CALCULATEC: mov R1,#40h
- mov @R1,#00h
- inc r1
- mov @R1,#0BFh
- inc r1
- mov DPTR,#TABLE1 ;查找表格
- movc A,@A+DPTR
- mov B,A
- mov @R1,#0BFh
- anl A,#0F0h
- swap A
- mov DPTR,#SEGMENT7
- movc A,@A+DPTR
- mov @r1,A
- inc r1
- mov A,B
- anl A,#0Fh
- mov DPTR,#SEGMENT7
- movc A,@A+DPTR
- mov @r1,A
- LOOPC: call DISPLAY
- ajmp LOOPC
- ret
- V_MEA: mov A,#00h ;测量电压
- READV: mov P2,#20h
- setb C
- mov P3.1,C
- nop
- clr C
- mov P3.1,C
- nop
- call DELAY_5ms
- mov DPTR,#2000h
- movx A,@DPTR
- 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
- LOOPV: call DISPLAY
- nop
- ajmp LOOPV
- ret
-
- A_MEA: mov A,#00h ;测量电流
- READA: mov P2,#40h ;采样
- setb C
- mov P3.1,C
- nop
- clr C
- mov P3.1,C
- nop
- call DELAY_5ms
- mov DPTR,#4000h
- movx A,@DPTR ;读取采样值
- 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
- LOOPA: call DISPLAY
- nop
- ajmp LOOPA
- ret
-
- R_MEA: mov A,#00h ;测量电阻
- READR: mov P2,#60h ;采样
- setb C
- mov P3.1,C
- nop
- clr C
- mov P3.1,C
- nop
- call DELAY_5ms
- mov DPTR,#6000h
- movx A,@DPTR ;读取采样值
- 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
- LOOPR: call DISPLAY
- nop
- ajmp LOOPR
-
- 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
- TABLE1: DB 10h,11h,12h,14h,15h,16h,17h,18h ;电容量表格0.1uF---1uF
- DB 20h,22h,24h,25h,27h,28h,30h,31h
- DB 33h,34h,35h,36h,38h,40h,41h,43h
- DB 45h,47h,50h,52h,55h,57h,59h,60h
- DB 61h,63h,65h,68h,70h,72h,75h,77h
- DB 80h,82h,85h,88h,90h,93h,95h,97h
- DB 99h
-
- SEGMENT7: DB 3Fh ;7段数码管字符0的abcdefg的值,a在最低位,最高位始终保留为0
- DB 06h ;7段数码管字符1的abcdefg的值
- DB 5Bh ;7段数码管字符2的abcdefg的值
- DB 4Fh ;7段数码管字符3的abcdefg的值
- DB 66h ;7段数码管字符4的abcdefg的值
- DB 6Dh ;7段数码管字符5的abcdefg的值
- DB 7Dh ;7段数码管字符6的abcdefg的值
- DB 07h ;7段数码管字符7的abcdefg的值
- DB 7Fh ;7段数码管字符8的abcdefg的值
- DB 6Fh ;7段数码管字符9的abcdefg的值
-
-
- ; 3Fh======0 0 1 1 1 1 1 1
- ; h g f e d c b a
- ; a
- ; ■■■■■
- ; ■ ■
- ; f■ ■b
- ; ■ g ■
- ; ■■■■■
- ; ■ ■
- ; e■ ■c
- ; ■ ■
- ; ■■■■■
- ; d
- DISPLAY: mov DPTR,#0E000h ;写第一位数码管
- mov r1,#40h
- mov A,@r1
- movx @DPTR,A
- setb C
- mov P3.2,C
- clr C
- mov P3.2,C
- call DELAY_5ms
- setb C
- mov P3.2,C
- inc r1
-
- mov A,@r1 ;写第二位数码管
- movx @DPTR,A
- setb C
- mov P3.3,C
- clr C
- mov P3.3,C
- call DELAY_5ms
- setb C
- mov P3.3,C
- inc r1
-
- mov A,@r1 ;写第三位数码管
- movx @DPTR,A
- setb C
- mov P3.4,C
- clr C
- mov P3.4,C
- call DELAY_5ms
- setb C
- mov P3.4,C
- inc r1
-
- mov A,@r1 ;写第四位数码管
- movx @DPTR,A
- setb C
- mov P3.5,C
- clr C
- mov P3.5,C
- call DELAY_5ms
- setb C
- mov P3.5,C
- ret
- DEBUG_OUT: mov DPTR,#0E000h ;调试输出
- movx @DPTR,A
- D_LOOP: setb C
- mov P3.5,C
- clr C
- mov P3.5,C
- ajmp D_LOOP
- 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
复制代码
|