单片机脉冲计数器 1、 设计内容
用单片机实现对一路脉冲计数和显示的功能。硬件包括单片机最小系统、LED显示、控制按钮;软件实现检测到显示 2、 要求
计数范围0~2000;
脉冲输入有光电隔离整形, 有清零按钮 程序如下:
ORG 0000H LJMP MAIN ORG 0003H LJMP 0100H ORG 0013H LJMP 0150H ORG 0050H MAIN: CLR A
MOV 30H , A ;初始化缓存区 MOV 31H , A MOV 32H , A MOV 33H , A MOV R6 , A MOV R7 , A SETB EA SETB EX0 SETB EX1 SETB IT0 SETB IT1 SETB PX1
NEXT1: ACALL HEXTOBCDD ;调用数制转换子程序 ACALL DISPLAY ;调用显示子程序 LJMP NEXT1
ORG 0100H ;中断0服务程序 MOV A , R7 ADD A , #1 MOV R7, A MOV A , R6 ADDC A , #0 MOV R6 , A
CJNE R6 , #07H , NEXT CLR A
MOV R6 , A MOV R7 , A NEXT: RETI
ORG 0150H ;中断1服务程序 CLR A MOV R6 , A MOV R7 , A RETI
ORG 0200H
HEXTOBCDD:MOV A , R6 ;由十六进制转化为十进制 PUSH ACC MOV A , R7 PUSH ACC MOV A , R2 PUSH ACC CLR A
MOV R3 , A MOV R4 , A MOV R5 , A MOV R2 , #10H
HB3: MOV A , R7 ;将十六进制中最高位移入进位位中 RLC A MOV R7 , A MOV A , R6 RLC A
MOV R6 , A
MOV A , R5 ;每位数加上本身相当于将这个数乘以2 ADDC A , R5 DA A
MOV R5 , A MOV A , R4 ADDC A , R4
DA A ;十进制调整 MOV R4 , A MOV A , R3 ADDC A , R3 DJNZ R2 , HB3 POP ACC MOV R2 , A POP ACC MOV R7 , A POP ACC MOV R6 , A RET
ORG 0250H
DISPLAY: MOV R0 , #30H MOV A , R5
ANL A , #0FH MOV @R0 , A MOV A , R5 SWAP A
ANL A , #0FH INC R0
MOV @R0 , A MOV A , R4 ANL A , #0FH INC R0
MOV @R0 , A MOV A , R4 SWAP A
ANL A , #0FH INC R0
MOV @R0 , A MOV R0 , #30H
MOV R2 , #11111110B AGAIN: MOV A , R2 MOV P2 , A MOV A , @R0 MOV DPTR , #TAB MOVC A , @A+DPTR MOV P0 , A ACALL DELAY INC R0
MOV A , R2 RL A
MOV R2 , A
JB ACC.4 , AGAIN RET
TAB: DB 03FH , 06H , 5BH , 4FH , 66H , 6DH , 7DH , 07H , 7FH , 6FH DELAY: MOV TMOD , #01H ;0.05s延时子程序 MOV TL0 , #0B0H MOV TH0 , #3CH SETB TR0
WAIT: JNB TF0 , WAIT CLR TF0 CLR TR0 RET END
;七段码表 单片机的T1口计数,T0口定时,P1口输出段码,P2口位选,三位数码管显示 ORG 0000H AJMP MAIN ORG 000BH AJMP COUNT ORG 0100H MAIN: MOV TMOD,#51H MOV TH1,#00H MOV TL1,#00H MOV TL0,#0B0H MOV TH0,#3CH SETB PT0 SETB ET0 SETB EA SETB TR0 SETB TR1 WAIT: AJMP WAIT COUNT: MOV 30H,TL1 MOV 31H,TH1 LCALL BCD LCALL WF RETI
BCD: MOV R1,30H MOV A,R1 MOV B,#100 DIV AB MOV 33H,A MOV A,B MOV B,#10 DIV AB MOV 34H,A MOV 35H,B PLAY: MOV A,33H MOV DPTR,#TAB MOVC A,@A+DPTR MOV P1,A SETB P2.2 LCALL DELAY CLR P2.2 MOV A,34H MOVC A,@A+DPTR
MOV P1,A SETB P2.1 LCALL DELAY CLR P2.1 MOV A,35H MOVC A,@A+DPTR MOV P1,A SETB 2.0 LCALL DELAY CLR P2.0 RET
TAB: DB 3FH,06H,5BH,4FH,66F, 6DH,7DH,07H,7FH,6FH DELAY: MOV R5,#10H DE1: MOV R7,#5 DE2: MOV R6,#20 DE3: DJNZ R6,DE3 DJNZ R7,DE2 DJNZ R5,DE1 RET
WF: MOV TH1,#00H MOV TL1,#00H MOV TH0,#0B0H MOV TL0,#3CH SETB TR1 SETB TR0 RET END
COUNT: CLR TR1
CLR TR0 MOV 30H,TL1 MOV 31H,TH1 LCALL BCD LCALL WF RET
ORG 0000H AJMP MAIN ORG 000BH AJMP COUNT ORG 0100H MAIN: MOV TMOD,#51H MOV TH1,#00H MOV TL1,#00H MOV TL0,#0B0H
MOV TH0,#3CH SETB PT0 SETB ET0 SETB EA SETB TR0 SETB TR1 WAIT: AJMP WAIT COUNT: MOV 30H,TL1 MOV 31H,TH1 LCALL BCD LCALL WF RETI
BCD: MOV R1,30H MOV A,R1 MOV B,#100 DIV AB MOV 33H,A MOV A,B MOV B,#10 DIV AB MOV 34H,A MOV 35H,B PLAY: MOV A,33H MOV DPTR,#TAB MOVC A,@A+DPTR MOV P1,A SETB P2.2 LCALL DELAY CLR P2.2 MOV A,34H
MOVC A,@A+DPTR MOV P1,A SETB P2.1 LCALL DELAY CLR P2.1 MOV A,35H
MOVC A,@A+DPTR MOV P1,A SETB 2.0
LCALL DELAY CLR P2.0 RET
TAB: DB 3FH,06H,5BH,4FH,66F, 6DH,7DH,07H,7FH,6FH
DELAY: MOV R5,#10H DE1: MOV R7,#5 DE2: MOV R6,#20 DE3: DJNZ R6,DE3 DJNZ R7,DE2 DJNZ R5,DE1 RET
WF: MOV TH1,#00H MOV TL1,#00H MOV TH0,#0B0H MOV TL0,#3CH SETB TR1 SETB TR0 RET END
COUNT: CLR TR1 CLR TR0
MOV 30H,TL1 MOV 31H,TH1 LCALL BCD LCALL WF RET