简易电子琴设计(单片机课程设计).doc
Four short words sum up what has lifted most successful individuals above the crowd: a little bit more.-author-date简易电子琴设计(单片机课程设计)简易电子琴设计简易电子琴设计用单片机控制扬声器,弹奏出一段音乐。一、预备知识:乐曲由不同音符组成,各音符都有对应的频率,频率不同的方波加在扬声器上,就产生一个持续的音符。利用不同的音符、音高、音长组合,就可产生想要的音乐。对于单片机来说,利用其定时器产生不同频率的方波非常方便,以 11.0592、22.1184MHz 晶振(实验箱)为例,列出高中低音符与单片机定时器初值如下表所示注意:1、一个音段常见由7 个标准音组成(1do、2ri、3mi、4fa、5sol、6la、7si),但实际乐曲中还有一些介于2 个标准音之间的音,即半度音,有1.5、2.5、4.5、5.5、6.5 共5 个,所以完整的1 个音段有12 个音。2、选择简谱时,尽量选用只含7 个标准音的乐曲,而且要选C 大调的(简谱左上角标着1=C X/4),否则各音符的频率要做相应的变化。10二、设计目的了解扬声器的发声原理,进一步熟悉定时器、键盘扫描、LED 显示电路的工作原理及编程方法。三、设计内容1、设计键盘、LED 显示、扬声器驱动电路原理图;2、设计程序流程图;3、编程调试,利用按键作为琴键,按下不同的键发出相应的音调,同时在LED最右位显示对应键号值。四、参考接线扬声器的 SPK+端接VCC,SPK 负端连接到某一I/O 口。五、设计步骤程序: KEYBUF EQU 30H STH0 EQU 31H STL0 EQU 32H TEMP EQU 33H ORG 0000 LJMP MAIN ORG 000BH LJMP INT_T0MAIN: MOV TMOD,#01H SETB ET0 SETB EAWAIT: MOV P3,#0FFH CLR P3.4 MOV A,P3 ANL A,#0FH XRL A,#0FH JZ NOKEY1 LCALL DELAY MOV A,P3 ANL A,#0FH XRL A,#0FH JZ NOKEY1 MOV A,P3 ANL A,#0FH CJNE A,#0EH,NK1 MOV KEYBUF,#0 LJMP DK1NK1: CJNE A,#0DH,NK2 MOV KEYBUF,#1 LJMP DK1NK2: CJNE A,#0BH,NK3 MOV KEYBUF,#2 LJMP DK1NK3: CJNE A,#07H,NK4 MOV KEYBUF,#3 LJMP DK1NK4: NOPDK1: MOV A,KEYBUF MOV B,#2 MUL AB MOV TEMP,A MOV DPTR,#TAB MOVC A,A+DPTR MOV STH0,A MOV TH0,A INC TEMP MOV A,TEMP MOVC A,A+DPTR MOV STL0,A MOV TL0,A SETB TR0DK1A: MOV A,P3 ANL A,#0FH XRL A,#0FH JNZ DK1A CLR TR0NOKEY1: MOV P3,#0FFH CLR P3.5 MOV A,P3 ANL A,#0FH XRL A,#0FH JZ NOKEY2 LCALL DELAY MOV A,P3 ANL A,#0FH XRL A,#0FH JZ NOKEY2 MOV A,P3 ANL A,#0FH CJNE A,#0EH,NK5 MOV KEYBUF,#4 LJMP DK2NK5: CJNE A,#0DH,NK6 MOV KEYBUF,#5 LJMP DK2NK6: CJNE A,#0BH,NK7 MOV KEYBUF,#6 LJMP DK2NK7: CJNE A,#07H,NK8 MOV KEYBUF,#7 LJMP DK2NK8: NOPDK2: MOV A,KEYBUF MOV B,#2 MUL AB MOV TEMP,A MOV DPTR,#TAB MOVC A,A+DPTR MOV STH0,A MOV TH0,A INC TEMP MOV A,TEMP MOVC A,A+DPTR MOV STL0,A MOV TL0,A SETB TR0DK2A: MOV A,P3 ANL A,#0FH XRL A,#0FH JNZ DK1A CLR TR0NOKEY2: MOV P3,#0FFH CLR P3.6 MOV A,P3 ANL A,#0FH XRL A,#0FH JZ NOKEY3 LCALL DELAY MOV A,P3 ANL A,#0FH XRL A,#0FH JZ NOKEY3 MOV A,P3 ANL A,#0FH CJNE A,#0EH,NK9 MOV KEYBUF,#8 LJMP DK1NK9: CJNE A,#0DH,NK10 MOV KEYBUF,#9 LJMP DK1NK10: CJNE A,#0BH,NK11 MOV KEYBUF,#10 LJMP DK1NK11: CJNE A,#07H,NK12 MOV KEYBUF,#11 LJMP DK3NK12: NOPDK3: MOV A,KEYBUF MOV B,#2 MUL AB MOV TEMP,A MOV DPTR,#TAB MOVC A,A+DPTR MOV STH0,A MOV TH0,A INC TEMP MOV A,TEMP MOVC A,A+DPTR MOV STL0,A MOV TL0,A SETB TR0DK3A: MOV A,P3 ANL A,#0FH XRL A,#0FH JNZ DK3A CLR TR0NOKEY3: MOV P3,#0FFH CLR P3.7 MOV A,P3 ANL A,#0FH XRL A,#0FH JZ NOKEY4 LCALL DELAY MOV A,P3 ANL A,#0FH XRL A,#0FH JZ NOKEY4 MOV A,P3 ANL A,#0FH CJNE A,#0EH,NK13 MOV KEYBUF,#12 LJMP DK4NK13: CJNE A,#0DH,NK14 MOV KEYBUF,#13 LJMP DK4NK14: CJNE A,#0BH,NK15 MOV KEYBUF,#14 LJMP DK4NK15: CJNE A,#07H,NK16 MOV KEYBUF,#15 LJMP DK4NK16: NOPDK4: MOV A,KEYBUF MOV B,#2 MUL AB MOV TEMP,A MOV DPTR,#TAB MOVC A,A+DPTR MOV STH0,A MOV TH0,A INC TEMP MOV A,TEMP MOVC A,A+DPTR MOV STL0,A MOV TL0,A SETB TR0DK4A: MOV A,P3 ANL A,#0FH XRL A,#0FH JNZ DK4A CLR TR0NOKEY4: MOV DPTR,#LED_TAB MOV A,KEYBUF MOVC A,A+DPTR MOV P1,A LJMP MAINDELAY: MOV R6,#10D1: MOV R7,#248 DJNZ R7,$ DJNZ R6,D1 RET INT_T0: MOV TH0,STH0 MOV TL0,STL0 CPL P2.0 RETITAB: DW 62018,62401,62743,62895 ;低1,2,3,4 DW 63185,63441,63670,63774 ;低5,6,7,中1DW 63966,64138,64216,64360 ;中2,3,4,5DW 64489,64603,64655,64752 ;中6,7,高1,2LED_TAB: DB 3FH,06H,5BH,4FH,66H,6DH,7DH,07H,7FH,6FH ;0至9共阴 DB 77H, 7CH, 39H, 5EH, 79H, 71H;A至F END原理图:-