单片机电子琴系统程序设计 第5页
第三章 单片机系统程序设计
3.1 发出七个音符程序
源程序如下:
ORG 0000H
SJMP START
ORG 000BH
AJMP INT_0
ORG 0030H
START: MOV P1,#00H
SETB EA
SETB ET0 ;计数器0
MOV TMOD,#02H
MOV TH1,#09CH
MOV TL1,#09CH ;定时器初植
CLR TR0 ;定时器不允许
SCAN: MOV A,P2 ;键盘扫描
;判断键盘按下,跳到SCAN_1,若未按,继续扫描
CJNE A,#0FFH,SCAN_1
NOP
SJMP SCAN
SCAN_1:ACALL DELAY ;是不是真的要按下?如果是,则跳转SU_KEY查询是哪个按下?
MOV A,P2
CJNE A,#0FFH,SU_KEY
NOP
SJMP SCAN
SU_KEY:JNB P2.1,MU_1 ;判断是哪个键按下?
JNB P2.2,MU_2
JNB P2.3,MU_3
JNB P2.4,MU_4
JNB P2.5,MU_5
JNB P2.6,MU_6
JNB P2.7,MU_7
SJMP SCAN
;R1信号周期
MU_1: MOV R1,#19 ;如果第一个按键按下,则R1=19,同时程序转NEXT
SJMP NEXT
MU_2: MOV R1,#17
SJMP NEXT
MU_3: MOV R1,#15
SJMP NEXT
MU_4: MOV R1,#14
SJMP NEXT
MU_5: MOV R1,#13
SJMP NEXT
MU_6: MOV R1,#11
SJMP NEXT
MU_7: MOV R1,#10
SJMP NEXT
NEXT: MOV A,R1
MOV R0,A
SETB TR0 ;启动定时器0
NEXT_1:MOV A,P2 ;没键按下继续执行,有键按下,返回读引脚
CJNE A,#0FFH,NEXT_1
ACALL DELAY
MOV A,P2 ;真的没键按下?
CJNE A,#0FFH,NEXT_1
CLR TR0 ;不响
AJMP SCAN ;扫描键盘去
INT_0: ;中断程序
DJNZ R0,RE ;R0不等于0时,返回
CPL P1.0
MOV A,R1
MOV R0,A
RE: RETI
DELAY:MOV R7,#100 ;延长时间等待,键盘消抖
D1: MOV R6,#10
D2: DJNZ R6,D2
DJNZ R7,D1
RET
END
3.2 演奏“生日快乐歌”程序
源程序如下:
ORG 0000H
JMP MAIN
ORG 000BH
JMP TT0
MAIN: MOV TMOD,#01H ;设置定时器0工作方式
MOV IE,#82H ;设置中断
MAIN0:MOV 30H,#00H
NEXT: MOV A,30H
MOV DPTR,#TABLE ;查表
MOVC A,@A+DPTR
MOV R2,A
JZ STOP
ANL A,0FH
MOV R5,A
MOV A,R2
SWAP A
ANL A,#0FH
JNZ SING
www.751com.cn
MOV 22H,A
RL A
MOV DPTR,#TABLE1 ;查表
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
W1: CALL DELAY
INC 30H
JMP NEXT
STOP: CLR TR0
JMP MAIN0
TT0: PUSH ACC ;中断服务程序
PUSH PSW
MOV TL0,20H
MOV TH0,21H
CPL P1.0
POP PSW
POP ACC
RETI
DELAY:MOV R7,#2 ;延时187ms
W2: MOV R4,#187
W3: MOV R3,#248
DJNZ R3,$
DJNZ R4,W3
DJNZ R7,W2
DJNZ R5,DELAY
RET
TABLE1:DW 64260,64400,64524,64580 ;音符计数植
DW 64684,64777,64820,64898
DW 64968,65030,65058,65110
DW 65157,65178,65217
TABLE:DB 82H,01H,81H,94H,84H ;节拍表
DB 0B4H,0A4H,04H
DB 82H,01H,81H,94H,84H
DB 0C4H,0B4H,04H
DB 82H,01H,81H,0F4H,0D4H
DB 0B4H,0A4H,94H
DB 0E2H,01H,0E1H,0D4H,0B4H
DB 0C4H,0B4H,04H
DB 82H,01H,81H,94H,84H
上一页 [1] [2] [3] [4] [5] [6] 下一页