您的当前位置:首页正文

单片机音乐盒程序

2020-09-21 来源:易榕旅网
RW BIT P2.1 E BIT P2.2 ;******************** L50MS EQU 60H L1MS EQU 61H L250MS EQU 62H SEC EQU 65H MIN EQU 64H HOU EQU 63H

;********************* ORG 0000H LJMP MAIN ORG 000BH LJMP TT0 ORG 001BH LJMP T1INT ORG 1000H MAIN: 液晶初始化 MOV SP,#70H MOV P0,#01H ;清屏 CALL ENABLE

MOV P0,#38H ;8位,2行显示 LCALL ENABLE

MOV P0,#0FH ;屏显ON,光标ON,闪烁ON LCALL ENABLE

MOV P0,#06H ;计数地址加1,显示幕ON

LCALL ENABLE 内存初始化 LCALL INITIL KEY1: LCALL KEY

LCALL MODD

LJMP KEY1键盘扫描 KEY: NOP NOP LCALL KS

JNZ K1 有按键转到K1 LCALL KAIJI LCALL SOP XN: LJMP KEY K1:

LCALL MODD LCALL MODD LCALL KS JNZ K2 LJMP KEY K2:

MOV R2,#0FEH ;读键盘 MOV R4,#00H K3: MOV A,R2 MOV P1,A MOV A,P1

JB ACC.4,L1为1跳转,第一行无按键 MOV A,#00H LJMP LK L1:

JB ACC.5,L2 MOV A,#04H LJMP LK

L2:

JB ACC.6,L3 MOV A,#08H LJMP LK L3:

JB ACC.7,NEXT1 MOV A,#0CH LK: ADD A,R4 PUSH ACC K4:

LCALL DELAY1 ;若同时有其他按键,则等待 LCALL KS JNZ K4

MOV R3,#07H CLR A MOV R0,#30H MOV R1,#31H MM1: MOV A,@R1 MOV @R0,A INC R0 INC R1

DJNZ R3,MM1 POP ACC MOV @R0,A RET NEXT1:

INC R4 ;列扫描 MOV A,R2

JNB ACC.3,N2 LJMP N1 N2: LJMP KEY N1: RL A MOV R2,A LJMP K3 KS:

MOV A,#0F0H ;判断P1口 MOV P1,A NOP NOP MOV A,P1 CPL A ANL A,#0F0H RET DELAY1: SETB RS0 DL:

MOV R5,#0AH DL2: MOV R6,#63 DL3: NOP NOP DJNZ R6,DL3 DJNZ R5,DL2 CLR RS0 RET 30H-37H初值为零

INITIL: MOV R3,#08H MOV R0,#30H M1:

MOV @R0,#00H INC R0

DJNZ R3,M1 RET MODD: MOV P0,#8EH

LCALL ENABLE MOV A,37H

MOV DPTR,#TABLE3 MOVC A,@A+DPTR LCALL WRITE2 RET开机提示字 KAIJI: MOV A,37H CJNE A,#0FH,DFF

MOV P0,#80H ;设光标地址 LCALL ENABLE

MOV DPTR,#TABLE1;写数据 LCALL WRITE1 DFF:

RET按键值播放歌曲 SOP: MOV A,37H CJNE A,#01H,A11 LCALL MODD

MOV 52H,#HIGH TABLE10

MOV 53H,#LOW TABLE10 LCALL INITILE2 LCALL MAIN0 A11:

CJNE A,#02H,A22 LCALL MODD

MOV 52H,#HIGH TABLE20 MOV 53H,#LOW TABLE20 LCALL INITILE2 LCALL MAIN0 A22:

CJNE A,#03H,A33 LCALL MODD

MOV 52H,#HIGH TABLE30 MOV 53H,#LOW TABLE30 LCALL INITILE2 LCALL MAIN0 A33:

CJNE A,#04H,A44 LCALL MODD

MOV 52H,#HIGH TABLE40 MOV 53H,#LOW TABLE40 LCALL INITILE2 LCALL MAIN0 A44:

CJNE A,#05H,A55

LCALL MODD

MOV 52H,#HIGH TABLE50 MOV 53H,#LOW TABLE50 LCALL INITILE2

LCALL MAIN0 A55:

CJNE A,#06H,A66 LCALL MODD

MOV 52H,#HIGH TABLE60 MOV 53H,#LOW TABLE60 LCALL INITILE2 LCALL MAIN0 A66:

CJNE A,#07H,A77 LCALL MODD

MOV 52H,#HIGH TABLE70 MOV 53H,#LOW TABLE70 LCALL INITILE2 LCALL MAIN0 A77:

CJNE A,#08H,A88 LCALL MODD

MOV 52H,#HIGH TABLE80 MOV 53H,#LOW TABLE80 LCALL INITILE2 LCALL MAIN0 A88:

CJNE A,#09H,A99 LCALL MODD

MOV 52H,#HIGH TABLE90 MOV 53H,#LOW TABLE90

LCALL INITILE2 LCALL MAIN0 A99:

CJNE A,#0AH,AAA LCALL MODD

MOV 52H,#HIGH TABLE100 MOV 53H,#LOW TABLE100 LCALL INITILE2 LCALL MAIN0 AAA:RET INITILE2: MOV L50MS,#20 MOV L1MS,#00H MOV L250MS,#00H MOV SEC,#00H MOV MIN,#00H MOV HOU,#00H MOV A,HOU MOV B,#10 DIV AB

ADD A,#30H ;将BCD吗转化为ASIIC码 MOV P0,#0C8H ;显示小时十位 CALL ENABLE CALL WRITE2 MOV A,B ADD A,#30H

MOV P0,#0C9H ;显示小时个位 CALL ENABLE CALL WRITE2 MOV A,#3AH

MOV P0,#0DH ;显示冒号 CALL ENABLE CALL WRITE2

MOV A,MIN MOV B,#10 DIV AB ADD A,#30H

MOV P0,#0CBH ;显示分十位 CALL ENABLE CALL WRITE2 MOV A,B ADD A,#30H

MOV P0,#0CCH CALL ENABLE CALL WRITE2 MOV A,#3AH

MOV P0,#0CDH CALL ENABLE CALL WRITE2 MOV A,SEC MOV B,#10 DIV AB ADD A,#30H

MOV P0,#0CEH CALL ENABLE CALL WRITE2 MOV A,B ADD A,#30H

MOV P0,#0CFH CALL ENABLE CALL WRITE2

MOV P0,#0C0H CALL ENABLE

;显示分个位 ;显示冒号 ;显示秒十位 ;显示秒个位 ;设光标地址 MOV DPTR,#TABLE2;写数据 CALL WRITE1 RET写指令 ENABLE: CLR RS CLR RW CLR E

LCALL DELAY SETB E RET写数据 WRITE1: MOV R1,#00H A1: MOV A,R1

MOVC A,@A+DPTR CALL WRITE2 INC R1

CJNE A,#0FEH,A1 ;未到字符串末尾继续 RET WRITE2: MOV P0,A SETB RS CLR RW CLR E CALL DELAY SETB E RET DELAY:

MOV R4,#05 D1:

MOV R5,#0FFH DJNZ R5,$ DJNZ R4,D1 RET TAB:

db 0c0h,0f9h,0a4h,0b0h,99h,92h,82h,0f8h db 80h,90h,88h,83h,0c6h,0a1h,86h,8eh TAB1:db 89h,86h,0c7h,0c7h,0c0h TAB2:db 0c6h,88h,0c0h

TABLE1:DB'WELCOM HERE',0FEH TABLE2:DB'DFF WB ',0FEH TABLE4:DB 'CAU',0FEH

TABLE3:DB 30H,31H,32H,33h DB 34H,35H,36H,37H DB 38h,39H,41H,42H

DB 43H,44H,45H,46H播放歌曲程序 MAIN0:

MOV TMOD,#11H MOV IE,#8aH MOV TH1,#3cH MOV TL1,#0b0H lcall ks

jnz TTM12 MOV 40H,#00H NEXT20: MOV A,40H MOV DPH,52H MOV DPL,53H

MOVC A,@A+DPTR MOV R2,A

JZ STOP ANL A,#0FH MOV R1,A MOV A,R2 SWAP A ANL A,#0FH JNZ SING CLR TR0 JMP W1 SING: DEC A MOV 22H,A RL A

MOV DPTR,#TABLE00 MOVC A,@A+DPTR MOV TH0,A MOV 21H,A MOV A,22H RL A INC A

MOVC A,@A+DPTR MOV TL0,A MOV 20H,A SETB TR0 SETB TR1 W1:

LCALL DELAY30 INC 40H

LCALL KS ;有键跳出

JNZ STOP

LJMP NEXT20 STOP: CLR TR0 CLR TR1 LJMP MAIN0 TTM12: LCALL K2 MOV A,37H

CJNE A,#0EH,TTM13 CLR TR1 LCALL DELAY1 TTM120: LCALL KS LCALL DELAY1 JZ TTM120 LCALL K2 MOV A,37H

CJNE A,#0EH,TTM120 SETB TR1 LJMP NEXT20 TTM13:

CJNE A,#0CH,TTM14 MOV A,52H INC A MOV 52H,A MOV A,53H INC A MOV 53H,A MOV A,36H INC A

MOV 37H,A LJMP TTM2 TTM14:

CJNE A,#0DH,TTM2 MOV A,52H DEC A MOV 52H,A MOV A,53H DEC A MOV 53H,A MOV A,36H DEC A MOV 37H,A TTM2:

RET定时器中断子程序0 TT0: PUSH ACC PUSH PSW MOV TL0,20H MOV TH0,21H CPL P3.7 POP PSW POP ACC

RETI定时器中断子程序1 T1INT: MOV TH1, #3cH MOV TL1, #0b0H DJNZ L50MS,X4

MOV L50MS,#20每秒种时钟刷新秒显示一次 SECSET:

MOV A,SEC MOV B,#10 DIV AB ADD A,#30H

MOV P0,#0CEH ;显示秒十位 lCALL ENABLE lCALL WRITE2 MOV A,B ADD A,#30H

MOV P0,#0CFH lCALL ENABLE lCALL WRITE2 MOV A,SEC INC A MOV SEC,A XRL A,#60 X4: JNZ OUT MINSET:

MOV SEC,#00H MOV A,MIN INC A MOV MIN,A MOV B,#10 DIV AB ADD A,#30H

MOV P0,#0CBH lCALL ENABLE lCALL WRITE2 MOV A,B ADD A,#30H

;显示秒个位 ;显示分十位

MOV P0,#0CCH ;显示分个位 lCALL ENABLE lCALL WRITE2 MOV A,MIN MOV B,#3 MUL AB MOV B,#100 DIV AB MOV R2,A MOV A,#10 XCH A,B DIV AB MOV A,MIN XRL A,#60 JNZ OUT HOUSET: MOV MIN,#00H MOV A,HOU MOV B,#10 DIV AB

ADD A,#30H MOV P0,#0C8H CALL ENABLE CALL WRITE2 MOV A,B ADD A,#30H

MOV P0,#0C9H CALL ENABLE CALL WRITE2 MOV A,HOU

;将BCD吗转化为ASIIC码;显示小时十位 ;显示小时个位 INC A MOV HOU,A XRL A,#10 JNZ OUT

SJMP OVERFLOW OUT: RETI OVERFLOW: MOV L50MS,#20 MOV L1MS,#00H MOV L250MS,#00H MOV SEC,#00H MOV MIN,#00H MOV HOU,#00H RETI DELAY30: MOV R7,#2 W2:

MOV R4,#125 W3:

MOV R3,#248 DJNZ R3,$ DJNZ R4,W3 DJNZ R7,W2 DJNZ R1,DELAY30 RET TABLE00:

DW 64580,64684,64777,64820 DW 64898,64968,65030,64260 DW 64400,64524,65058,63835,64021 第一首《精忠报国》

TABLE10:

DB 66H,52H,36H,22H,12H,12H,22H,32H,98H,84H,82H,92H,14H,12H,22H DB 32H,52H,22H,42H,38H,66H,52H,36H,22H,12H,22H,22H,32H,98H DB 24H,22H,32H,54H,52H,32H,92H,12H,92H,82H,98H,66H,52H,36H,22H DB 12H,12H,22H,32H,98H,84H,82H,92H,14H,12H,22H,32H,52H,22H,42H,38H DB 66H,52H,36H,22H,12H,22H,22H,32H,98H,24H,22H,32H,54H,52H,32H DB 22H,12H,92H,82H,98H,14H,24H,34H,44H,64H,0B4H,64H,32H,52H DB 62H,0B2H,62H,52H,38H,24H,22H,32H,54H,52H,62H,52H,32H,32H,22H,38H DB 64H,0B4H,64H,32H,52H,62H,0B2H,62H,52H,38H,22H,22H,22H,32H,52H,52H DB 52H,62H,7FH,74H,04H,58H,6FH DB 00

;第二首《月亮代表我的心》 TABLE20: DB 02H,82H

DB 16H,32H,54H,02H,52H DB 0A6H,32H,54H,02H,52H DB 64H,74H,0B6H,64H DB 52H,5CH,32H,22H DB 16H,12H,14H,32H,22H DB 16H,12H,14H,22H,32H DB 26H,12H,94H,22H,32H DB 2CH DB 32H,52H DB 36H,22H,14H,54H DB 0ACH,92H,0A2H DB 96H,0A2H,96H,82H DB 3CH,54H DB 36H,22H,14H,54H DB 0ACH,92H,0A2H

DB 16H,12H,14H,22H,32H DB 2CH,02H,82H DB 16H,32H,56H,12H DB 0A6H,32H,56H,52H DB 66H,72H,0B6H,62H DB 62H,52H,58H,32H,22H DB 16H,12H,14H,32H,22H DB 16H,12H,14H,22H,32H DB 26H,92H,0A4H,12H,22H DB 1CH DB 00

第三首《看我七十二变》 TABLE30:

DB 0d4H,84H,94H,82H,92H

DB 12H,22H,82H,92H,02H,92H,82H,0d2H

DB 82H,92H,14H,84H,02H,0d1H,0d1H DB 0c2H,0d2H,84H,0d2H,0d2H,82H,82H DB 84H,82H,82H,94H,82H,0d2H DB 82H,82H,82H,92H,02H,82H,82H,0d2H DB 0c4H,84H,0d4H,92H,0d2H DB 82H,0d2H,82H,92H,98H DB 04H,94H,88H DB 04H,94H,88H DB 04H,94H,88H DB 04H,94H,88H,04H

DB 32H,32H,22H,12H,02H,92H,12H,92H DB 32H,32H,22H,12H,02H,92H,12H,92H DB 32H,32H,22H,12H,02H,92H,12H,22H DB 54H,44H,34H,12H,22H DB 34H,22H,12H,02H,92H,12H,92H

DB 32H,32H,22H,12H,02H,92H,12H,92H DB 32H,32H,22H,12H,02H,92H,12H,22H DB 54H,44H,32H,22H,12H,92H DB 00 END

因篇幅问题不能全部显示,请点此查看更多更全内容