微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > 单片机电子琴电路图及源程序代码

单片机电子琴电路图及源程序代码

时间:11-25 来源:互联网 点击:

实验仿真图

源程序代码:(弹奏部分代码暂时去掉,见谅,需要的留言,转载请注明出处,谢谢!)

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,#LED0
MOV A,#00H
MOVX @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 NEXT1
START2:

MOV DPTR,#TABLE2 ;从TABLE2处取简谱码
MOV 60H,DPH
MOV 61H,DPL
MOV A,#01H
LJMP NEXT1
START3:

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 ;启动T0
D1: 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 KEY
M1:
LCALL DELAY1
JNB P1.1,M11
SJMP KEY
M2:
LCALL DELAY1
JNB P1.2,M22
SJMP KEY
M3:
LCALL DELAY1
JNB P1.3,M33
SJMP KEY
M4:
LCALL DELAY1
JNB P1.4,M44
SJMP KEY
M5:
LCALL DELAY1
JNB P1.5,M55
SJMP KEY
M6:
LCALL DELAY1
JNB P1.6,M66
SJMP KEY

;确定按键的偏移地址程序
M00:
MOV A,#00H
LJMP PLAY
M11:
MOV A,#01H
LJMP PLAY
M22:
MOV A,#02H
LJMP PLAY
M33:
MOV A,#03H
LJMP PLAY
M44:
MOV A,#04H
LJMP PLAY
M55:
MOV A,#05H
LJMP PLAY
M66:
MOV A,#06H
LJMP PLAY

;按键音弹奏程序

;音符对应的定时器初值表:
TABLE1:
DW 64580,64684,64777,64820
DW 64898,64968,65030,64260
DW 64400,64524,65058,63835,64021

LOWTABLE:
DW 63628,63835,64021,64103,64260,64400,64524
MIDDLETABLE:
DW 64580 ,64684,64777,64820,64898,64968,65030
HIGHTABLE:
DW 65058,65110,65157,65178,65217,65252,65283
SEG7:
DB 06H, 5BH, 4FH,66H, 6DH, 7DH,07H

;音符节拍表:
TABLE:
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
TABLE2:
DB 88h,14h,24h,36H,42H,34H,14H,28H,14H,94H
DB 1fH,88H,14H,24H,34H,32H,42H,54H,14H
DB 46H,32H,54H,22H,32H,32H,22H,2CH,38H,54H,74H
DB 76H,62H,68H,54H,52H,62H,74H,62H,52H,3fH
DB 46H,42H,54H,64H,54H,42H,32H,28H,0a4H,0a2H
DB 92H,84H,94H,1fH,0b8H,68H,46H,52H,68H
DB 74H,72H,72H,74H,62H,52H,3fH,0b8H,68H
DB 46H,52H,68H,64H,62H,62H,64H,42H,32H,2fH
DB 58H,14H,24H,36H,42H,34H,12H,12H,26H,22H,24H,22H,12H
DB 94H,9cH,0a8H,0a6H,92H,84H,64H,54H,22H,22H,46H,42H,44H,32H,22H,5fH
DB 00
TABLE3:
DB 82H,92H,12H,14H,12H,14H,92H,82H,12H,14H,22H,14H,92H,12H,22H,24H
DB 22H,24H,12H,92H,22H,24H,32H,24H,32H,52H,62H,54H,52H,64H,52H,32H
DB 22H,32H,22H,12H,94H,82H,92H,12H,14H,12H,14H,94H,1cH
DB 82H,92H,12H,14H,12H,14H,92H,82H,12H,14H,22H,14H,92H,12H,22H,24H
DB 22H,24H,12H,92H,22H,24H,32H,24H,32H,52H,62H,54H,52H,64H,52H,32H
DB 22H,32H,22H,12H,94H,82H,92H,12H,14H,12H,14H,22H,12H,1cH,32H,52H
DB 62H,64H,62H,64H,0b2H,62H,52H,54H,62H,54H,32H,22H
DB 12H,14H,12H,14H,24H,3cH,32H,22H,12H,14H,12H,14H,0b2H,62H,52H,54H,62H,54H,32H,52H
DB 66H,0b2H,0b4H,62H,52H,5cH,32H,52H,62H,64H,62H,64H,0b2H,62H,52H,54H
DB 62H,54H,32H,22H,12H,14H,12H,14H,54H,3cH,32H,22H,12H,14H,12H,14H,22H,32H
DB 52H,54H,32H,54H,32H,52H,6fH,04H,02H,92H,32H,24H,12H,1fH
DB 00

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top