- 源程序代码:(弹奏部分代码暂时去掉,见谅,需要的留言,转载请注明出处,谢谢!)
- DUAN EQU 0c000H WEI EQU 0a000H LED0 EQU 8000H ORG 0000H ;复位地址 LJMP MAIN ;跳转到主程序 ORG 0003H ;外部中断0入口地址 LJMP INT00 ;跳转到外部中断0服务程序 ORG 000BH ;T0中断入口 LJMP TIM0 ;跳转到T0中断服务程序 ORG 0030H ;主程序入口地址 MAIN:
- MOV DPTR,#LED0MOV A,#00HMOVX @DPTR,A MOV TMOD,#01H ;写入T0控制字, 16位定时方式 SETB IT0 ;外部中断为下降沿触发 MOV IE,#83H ;开中断 MOV 30H,#00H ;取简谱码指针 MOV R6,#0FFH ;选择音乐节奏灯循环次数 CLR 23H ;功能选择标志 00:键盘 01:音乐
- NEXT: CLR TR0 JNB 23H,KEYPLAY ;若标志位是0则转到键盘程序 SETB P2.2 MOV A,30H ;简谱码指针装入A JNB P0.1,START1 ;否则查询播放哪一首歌曲 JNB P0.2,START2 JNB P0.3,START3 LJMP NEXT
- ;确定不同歌曲的基地址START1:
- MOV DPTR,#TABLE ;从TABLE处取简谱码 MOV 60H,DPH MOV 61H,DPL MOV A,#00H LJMP NEXT1START2:
- MOV DPTR,#TABLE2 ;从TABLE2处取简谱码 MOV 60H,DPH MOV 61H,DPL MOV A,#01H LJMP NEXT1START3:
- MOV DPTR,#TABLE3 ;从TABLE处取简谱码 MOV 60H,DPH MOV 61H,DPL MOV A,#02H LJMP NEXT1
- ;键盘弹奏程序入口KEYPLAY: ;键盘程序入口 SETB P2.1 CLR P2.2 JNB P0.4,LOWTONE ;查询是高音还是其他的音 JNB P0.5,MIDDLETONE JNB P0.6,HIGHTONE LJMP NEXT ;没有选择高中低音则重新查询
- ;确定高中低音基地址LOWTONE: MOV DPTR,#LOWTABLE ;赋基地址值 MOV 50H,DPH MOV 51H,DPL LJMP KEY ;跳转到按键查询程序
- MIDDLETONE: MOV DPTR,#MIDDLETABLE MOV 50H,DPH MOV 51H,DPL LJMP KEY
- HIGHTONE: MOV DPTR,#HIGHTABLE MOV 50H,DPH MOV 51H,DPL LJMP KEY
- ;歌曲播放程序NEXT1: MOV DPTR,#SEG7 MOVC A,@A+DPTR MOV DPTR,#DUAN MOVX @DPTR,A MOV DPTR,#WEI MOV A,#000BH MOVX @DPTR,A MOV DPH,60H MOV DPL,61H MOV A,30H MOVC A,@A+DPTR MOV R2,A ;取得的简谱码暂存于R2 JZ END0 ;是否取到结束码00H ANL A,#0FH ;不是,则取节拍码 MOV R5,A ;节拍码存于R5 MOV A,R2 ;将简谱码装入A SWAP A ;高、低4位交换 ANL A,#0FH ;取音符码 JNZ SING ;取得的音符码是否为0? CLR TR0 ;是则不发音 lJMP D1 ;跳转到D1
- SING: DEC A ;取得的音符码减1(不含0) MOV 22H,A ;存入22H单元 RL A ;乘2 MOV DPTR,#TABLE1 ;到TABLE1中取相对的高位字节值 MOVC A,@A+DPTR MOV TH0,A ;取得的高位字节装入TH0和21H单元 MOV 21H,A MOV A,22H ;再装入取得的音符码 Rl A ;乘2 INC A ;加1 MOVC A,@A+DPTR ;到TABLE1中取相对的低位字节值 MOV TL0,A ;取得的高位字节装入TL0和20H单元 MOV 20H,A SETB TR0 ;启动T0D1: LCALL DELAY ;基本单位时间1/4拍,延时187毫秒 INC 30H ;取简谱码指针加1 LJMP NEXT ;取下一个码END0: CLR TR0 ;停止T0 CLR 30H ;清零简朴指针 LJMP NEXT ;重复循环
- ;按键查询程序KEY: ;判断是哪个音的按键按下 JNB P1.0,M0 ;分别是DO,RE,ME.... JNB P1.1,M1 JNB P1.2,M2 JNB P1.3,M3 JNB P1.4,M4 JNB P1.5,M5 JNB P1.6,M6 LJMP NEXT ;没有按键按下则跳转到循环查询
- ;按键软件防抖动程序M0: LCALL DELAY1 ;延时一段时间判断按键是否继续按下,是则跳转到确定偏移量程序 JNB P1.0,M00 SJMP KEYM1: LCALL DELAY1 JNB P1.1,M11 SJMP KEYM2: LCALL DELAY1 JNB P1.2,M22 SJMP KEYM3: LCALL DELAY1 JNB P1.3,M33 SJMP KEYM4: LCALL DELAY1 JNB P1.4,M44 SJMP KEYM5: LCALL DELAY1 JNB P1.5,M55 SJMP KEYM6: LCALL DELAY1 JNB P1.6,M66 SJMP KEY
- ;确定按键的偏移地址程序M00: MOV A,#00H LJMP PLAYM11: MOV A,#01H LJMP PLAYM22: MOV A,#02H LJMP PLAYM33: MOV A,#03H LJMP PLAYM44: MOV A,#04H LJMP PLAY M55: MOV A,#05H LJMP PLAYM66: MOV A,#06H LJMP PLAY
- ;按键音弹奏程序
- ;音符对应的定时器初值表: TABLE1:DW 64580,64684,64777,64820DW 64898,64968,65030,64260DW 64400,64524,65058,63835,64021
- LOWTABLE:DW 63628,63835,64021,64103,64260,64400,64524MIDDLETABLE:DW 64580 ,64684,64777,64820,64898,64968,65030HIGHTABLE:DW 65058,65110,65157,65178,65217,65252,65283SEG7:DB 06H, 5BH, 4FH,66H, 6DH, 7DH,07H
- ;音符节拍表: TABLE: DB 02H,82HDB 16H,32H,54H,02H,52HDB 0A6H,32H,54H,02H,52HDB 64H,74H,0B6H,64HDB 52H,5CH,32H,22HDB 16H,12H,14H,32H,22HDB 16H,12H,14H,22H,32HDB 26H,12H,94H,22H,32HDB 2CHDB 32H,52HDB 36H,22H,14H,54HDB 0ACH,92H,0A2HDB 96H,0A2H,96H,82HDB 3CH,54HDB 36H,22H,14H,54HDB 0ACH,92H,0A2HDB 16H,12H,14H,22H,32HDB 2CH,02H,82HDB 16H,32H,56H,12HDB 0A6H,32H,56H,52HDB 66H,72H,0B6H,62HDB 62H,52H,58H,32H,22HDB 16H,12H,14H,32H,22HDB 16H,12H,14H,22H,32HDB 26H,92H,0A4H,12H,22HDB 1CHDB 00TABLE2:DB 88h,14h,24h,36H,42H,34H,14H,28H,14H,94HDB 1fH,88H,14H,24H,34H,32H,42H,54H,14HDB 46H,32H,54H,22H,32H,32H,22H,2CH,38H,54H,74HDB 76H,62H,68H,54H,52H,62H,74H,62H,52H,3fHDB 46H,42H,54H,64H,54H,42H,32H,28H,0a4H,0a2HDB 92H,84H,94H,1fH,0b8H,68H,46H,52H,68HDB 74H,72H,72H,74H,62H,52H,3fH,0b8H,68HDB 46H,52H,68H,64H,62H,62H,64H,42H,32H,2fHDB 58H,14H,24H,36H,42H,34H,12H,12H,26H,22H,24H,22H,12HDB 94H,9cH,0a8H,0a6H,92H,84H,64H,54H,22H,22H,46H,42H,44H,32H,22H,5fHDB 00TABLE3:DB 82H,92H,12H,14H,12H,14H,92H,82H,12H,14H,22H,14H,92H,12H,22H,24HDB 22H,24H,12H,92H,22H,24H,32H,24H,32H,52H,62H,54H,52H,64H,52H,32HDB 22H,32H,22H,12H,94H,82H,92H,12H,14H,12H,14H,94H,1cHDB 82H,92H,12H,14H,12H,14H,92H,82H,12H,14H,22H,14H,92H,12H,22H,24HDB 22H,24H,12H,92H,22H,24H,32H,24H,32H,52H,62H,54H,52H,64H,52H,32HDB 22H,32H,22H,12H,94H,82H,92H,12H,14H,12H,14H,22H,12H,1cH,32H,52HDB 62H,64H,62H,64H,0b2H,62H,52H,54H,62H,54H,32H,22HDB 12H,14H,12H,14H,24H,3cH,32H,22H,12H,14H,12H,14H,0b2H,62H,52H,54H,62H,54H,32H,52HDB 66H,0b2H,0b4H,62H,52H,5cH,32H,52H,62H,64H,62H,64H,0b2H,62H,52H,54HDB 62H,54H,32H,22H,12H,14H,12H,14H,54H,3cH,32H,22H,12H,14H,12H,14H,22H,32HDB 52H,54H,32H,54H,32H,52H,6fH,04H,02H,92H,32H,24H,12H,1fHDB 00
- END
复制代码
|