课程设计(论文)基于单片机的电子琴设计2_第1页
课程设计(论文)基于单片机的电子琴设计2_第2页
课程设计(论文)基于单片机的电子琴设计2_第3页
课程设计(论文)基于单片机的电子琴设计2_第4页
课程设计(论文)基于单片机的电子琴设计2_第5页
已阅读5页,还剩11页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、郑州航空工业管理学院单片机课程设计说明书 届 电子信息工程 专业 班级题 目 基于单片机的电子琴设计 学 号 姓 名 指导教师 二一年月日1、 电子琴的基本原理1、本次设计的内容是:具有7个基本音符的按键,能够弹奏简单乐曲;能够播放存贮在内存中的3首乐曲;每次按下一个8键可以播放一首歌。2、整体工作原理: 采用stc89c52单片机作为主控芯片,设置了键盘、蜂鸣器等外围器件,另外还用到一些简单器件如:四位数码管和pnp型三极管及电阻等。利用按键实现音符和音调的输入;四位的数码管进行被操作的按键显示;用pnp型三极管实现低音频功率放大;最后用蜂鸣器发音。主控芯片采用stc89c52单片机,它是大

2、规模集成电路技术发展的产物,具有高性能、高速度、体积小、价格低廉、稳定可靠、应用广泛的特点,同时具有强大的控制功能和灵活的编程实现特性。 3、发音原理:若要产生音频脉冲,只要算出某一音频的周期(频率),再将此周期除以2,即为半周期的时间。利用定时器计时半周期时间,每当计时终止后就将p1.7反相,然后重复计时再反相。就可在p1.7引脚上得到此频率的脉冲。利用stc89c52的内部定时器使其工作计数器模式(mode1)下,改变计数值th0及tl0以产生不同频率的方法产生不同音阶。例如,频率为523hz,其周期t1/5231912s,因此只要令计数器计时956s/1s956,每计数956次时将i/o

3、反相,就可得到中音do(523hz)。计数脉冲值与频率的关系式是:nfi2fr,式中,n是计数值;fi是机器频率(晶体振荡器为12mhz时,其频率为1mhz);fr是想要产生的频率。其计数初值t的求法如下:t65536n65536fi2fr例如:设k65536,fi1mhz,求中音do(261hz)。t65536n65536fi2fr6553610000002fr65536500000/fr,中音do的t65536500000/52364580单片机12mhz晶振,中音符与计数t0相关的计数值如表所示: 音符 频率(hz)计数值(t值)音符频率(hz)计数值(t值)低1do 26263628#

4、4fa#74064860#1do# 27763737中5so78464898低2re29463835#5so#83194934#2re#31163928中6la88064948低3mi33064021#中6la#93264494低fa34964103中7si96865030#4fa#37064185低1do104665058低so39264260#1do#110965085#5so#41564331高2re117565110低6la44064400#2re#124565134#6la#46664463高3mi131865157低7si49464524高4fa139765178中1do52364

5、580#4fa#149065198#1do#55464633高5so156865217中2re58764633#5so#166165235#2re#62264884高6la176065252中3mi65964732#6la#186565268中4fa69864820高7si196765283 用单片机播放音乐,或者弹奏电子琴,实际上是按照特定的频率,输出一连串的方波。为了输出合适的方波,首先应该知道音符与频率的关系。 2、 电子时钟的硬件设计1、硬件原理图: 本系统有主控芯片stc89c52、发音单元、显示模块、按键模块组成。硬件电路要以单片机作为主控芯片,实现按键输入音符和音调,两位数码管的

6、显示以及低音频功率放大和蜂鸣器发音。针对本设计的功能和用途,采用stc89c52单片机更好.2、时钟电路 单片机内部具有一个高增益反相放大器,用于构成振荡器。通常在引脚xl和x2跨接石英晶体和两个补偿电容构成自激振荡器,结构图2中x1、c1、c2。可以根据情况选择6mhz、12mhz或24mhz频率的石英晶体,补偿电容通常选择30pf左右的瓷片电容。图-时钟电路 3、键盘设计 键盘可以分为2类:独立连接式键盘和矩阵式键盘。本设计采用矩阵式按键,矩阵式键盘由行线和列线组成,按键位于行、列线的交叉点上。矩阵式键盘中,行、列线分别连接到按键开关的两端,行线通过上拉电阻接到+5v上当无键按下时,行线处

7、于高电平状态;当有键按下时,行、列线将导通,此时,行线电平将由与此行线相连的列线电平决定。这是识别按键是否按下的关键。图-矩阵键盘4、元件清单 原件名称数量焊接位置电源部分usb座1usbusb线1双排针(2)1conn1跳帽2104电容0.1uf6c2,5,6,7,8,9,11led灯1d1电阻2k1r2最小系统部分40针座1u1stc89c521晶振12mhz1y1电容20pf2c3,c4复位键1sw_rst电阻10k1r1蜂鸣器1ls1+三极管9015(密码锁,电子琴)5q1q4, q9电阻3301r3键盘显示、通信部分16针座1u2max2321单排针(5)1uart按键(密码锁,电子

8、琴)8key1key8单排针(3)1switch1跳帽14位led (密码锁,电子琴)1digiled1电阻4.7k8r14r21电阻3308r6r13 5 、发音部分设计 如下图所示,发音电路是由蜂鸣器、三极管、上拉电阻构成。由三极管来驱动扬声器发音的,同时加上拉电阻增强驱动电流,提高驱动能力。图4发音电路6、硬件调试 硬件调试主要是针对单片机部分进行的调试。在上电之前,先确保电路中不存在断路或短路情况,这一工作是整个调试工作的第一步,也是非常重要的一个步骤。在这部分调试中主要使用的工具是万用表,用来完成检测电路中是否存在断路或者短路情况的任务。注意焊点之间,确保焊点没有短接在一起,同时注意

9、焊点的美观,确保没有开路以及短路的现象出现在确保硬件电路正常且无异常情况(断路或短路)的情况下方可上电调试,上电调试的目的是检验电路是否接错,同时还要检验原理是否正确,在本次设计中,上电调试主要是检测单片机控制部分、数码管点亮部分、和音频转换电路硬件调试,3、 电子时钟的软件设计1、键盘扫描程序: 检测是否有按键按下,有按键按下则记录按下键的键值,并跳转至功能转移程序;无按键按下,则返回键盘扫描程序继续检测。 2、功能转移程序: 对检测到的按键值进行判断,是琴键则跳转至琴键处理程序,是功能键则跳转至相应的功能程序,我们设计的功能程序有两种,即音色调节功能和自动播放乐曲的功能。 3、琴键处理程序

10、:根据检测到的按键值,查询音调表,给计时器赋值,使发出相应频率的声音。 4、自动播放歌曲程序:检测到按键按下的是自动播放歌曲功能键后执行该程序,电子琴会自动播放事先已经存放的歌曲,歌曲播放完毕之后自动返回至键盘扫描程序,继续等待是否有按键按下。 开 始 5、程序流程图t0初始化并开中断允许t0中断按键是否成功按下 识别按键功能 根据按键功能,转入音符t到t0 启动t0工作按键释放成功否 停止t0工作 图5 流程图 6、设计源程序: /*说明* 曲谱存贮格式 unsigned char code musicname音高,音长,音高,音长., 0,0;末尾:0,0 表示结束(important)音

11、高由三位数字组成:个位是表示 17 这七个音符 ;十位是表示音符所在的音区:1-低音,2-中音,3-高音;百位表示这个音符是否要升半音: 0-不升,1-升半音。音长最多由三位数字组成: 个位表示音符的时值,其对应关系是: |数值(n): |0 |1 |2 |3 | 4 | 5 | 6 |几分音符: |1 |2 |4 |8 |16 |32 |64 音符=2n 十位表示音符的演奏效果(0-2): 0-普通,1-连音,2-顿音 百位是符点位: 0-无符点,1-有符点 调用演奏子程序的格式 play(乐曲名,调号,升降八度,演奏速度);|乐曲名 : 要播放的乐曲指针,结尾以(0,0)结束;|调号(0-

12、11) :是指乐曲升多少个半音演奏;|升降八度(1-3) : 1:降八度, 2:不升不降, 3:升八度;|演奏速度(1-12000):值越大速度越快;*/#define _soundplay_h_revision_first_#include /*#define system_osc 12000000/定义晶振频率12000000hz#define sound_space 4/5 /定义普通音符演奏的长度分率,/每4分音符间隔sbit beepio = p17;/定义输出管脚unsigned int code fretab12 = 262,277,294,311,330,349,369,392

13、,415,440,466,494 ; /原始频率表unsigned char code signtab7 = 0,2,4,5,7,9,11 ; /17在频率表中的位置unsigned char code lengthtab7= 1,2,4,8,16,32,64 ;unsigned char sound_temp_th0,sound_temp_tl0;/音符定时器初值暂存 unsigned char sound_temp_th1,sound_temp_tl1;/音长定时器初值暂存/*void initialsound(void)beepio = 1;sound_temp_th1 = (65535

14、-(1/1200)*system_osc)/256;/ 计算tl1应装入的初值 (10ms的初装值)sound_temp_tl1 = (65535-(1/1200)*system_osc)%256;/ 计算th1应装入的初值 th1 = sound_temp_th1;tl1 = sound_temp_tl1;tmod |= 0x11;et0 = 1;et1 = 0;tr0 = 0;tr1 = 0;ea = 1;void beeptimer0(void) interrupt 1/音符发生中断beepio = !beepio;th0 = sound_temp_th0; tl0 = sound_te

15、mp_tl0;/*void play(unsigned char *sound,unsigned char signature,unsigned octachord,unsigned int speed)unsigned int newfretab12;/新的频率表unsigned char i,j;unsigned int point,ldiv,ldiv0,ldiv1,ldiv2,ldiv4,currentfre,temp_t,soundlength;unsigned char tone,length,sl,sh,sm,slen,xg,fd;for(i=0;i 11)j = j-12;new

16、fretabi = fretabj*2;elsenewfretabi = fretabj;if(octachord = 1)newfretabi=2;else if(octachord = 3)newfretabi=2;soundlength = 0;while(soundsoundlength != 0x00)/计算歌曲长度soundlength+=2;point = 0;tone = soundpoint;length = soundpoint+1; / 读出第一个音符和它时时值ldiv0 = 12000/speed;/ 算出1分音符的长度(几个10ms) ldiv4 = ldiv0/4;

17、 / 算出4分音符的长度 ldiv4 = ldiv4-ldiv4*sound_space; / 普通音最长间隔标准 tr0 = 0;tr1 = 1;while(point = 2; /低音 if (sm=3) currentfre = 2; /高音temp_t = 65536-(50000/currentfre)*10/(12000000/system_osc);/计算计数器初值sound_temp_th0 = temp_t/256; sound_temp_tl0 = temp_t%256; th0 = sound_temp_th0; tl0 = sound_temp_tl0 + 12; /加

18、12是对中断延时的补偿 slen=lengthtablength%10; /算出是几分音符xg=length/10%10; /算出音符类型(0普通1连音2顿音) fd=length/100;ldiv=ldiv0/slen; /算出连音音符演奏的长度(多少个10ms)if (fd=1) ldiv=ldiv+ldiv/2;if(xg!=1)if(xg=0) /算出普通音符的演奏长度 if (slen0;i-) /发规定长度的音 while(tf1=0);th1 = sound_temp_th1;tl1 = sound_temp_tl1;tf1=0;if(ldiv2!=0)tr0=0; beepio

19、=1;for(i=ldiv2;i0;i-) /音符间的间隔while(tf1=0);th1 = sound_temp_th1;tl1 = sound_temp_tl1;tf1=0;point+=2; tone=soundpoint;length=soundpoint+1;beepio = 1;/*#endif#include #include soundplay.h/*music*unsigned char code sound1= 0x15,0x02, 0x00,0x00 ;/1unsigned char code sound2= 0x16,0x02, 0x00,0x00 ;/2unsign

20、ed char code sound3= 0x17,0x02, 0x00,0x00 ;/3unsigned char code sound4= 0x18,0x02, 0x00,0x00 ;/4unsigned char code sound5= 0x19,0x02, 0x00,0x00 ;/5unsigned char code sound6= 0x1a,0x02, 0x00,0x00 ;/6unsigned char code sound7= 0x1b,0x02, 0x00,0x00 ;/7unsigned char key;/挥着翅膀的女孩unsigned char code music_

21、girl= 0x17,0x02, 0x17,0x03, 0x18,0x03, 0x19,0x02, 0x15,0x03, 0x16,0x03, 0x17,0x03, 0x17,0x03, 0x17,0x03, 0x18,0x03, 0x19,0x02, 0x16,0x03, 0x17,0x03, 0x18,0x02, 0x18,0x03, 0x17,0x03, 0x15,0x02, 0x18,0x03, 0x17,0x03, 0x18,0x02, 0x10,0x03, 0x15,0x03, 0x16,0x02, 0x15,0x03, 0x16,0x03, 0x17,0x02, 0x17,0x0

22、3, 0x18,0x03, 0x19,0x02, 0x1a,0x03, 0x1b,0x03, 0x1f,0x03, 0x1f,0x03, 0x17,0x03, 0x18,0x03, 0x19,0x02, 0x16,0x03, 0x17,0x03, 0x18,0x03, 0x17,0x03, 0x18,0x03, 0x1f,0x03, 0x1f,0x02, 0x16,0x03, 0x17,0x03, 0x18,0x03, 0x17,0x03, 0x18,0x03, 0x20,0x03, 0x20,0x02, 0x1f,0x03, 0x1b,0x03, 0x1f,0x66, 0x20,0x03,

23、0x21,0x03, 0x20,0x03, 0x1f,0x03, 0x1b,0x03, 0x1f,0x66, 0x1f,0x03, 0x1b,0x03, 0x19,0x03, 0x19,0x03, 0x15,0x03, 0x1a,0x66, 0x1a,0x03, 0x19,0x03, 0x15,0x03, 0x15,0x03, 0x17,0x03, 0x16,0x66, 0x17,0x04, 0x18,0x04, 0x18,0x03, 0x19,0x03, 0x1f,0x03, 0x1b,0x03, 0x1f,0x66, 0x20,0x03, 0x21,0x03, 0x20,0x03, 0x1

24、f,0x03, 0x1b,0x03, 0x1f,0x66, 0x1f,0x03, 0x1b,0x03, 0x19,0x03, 0x19,0x03, 0x15,0x03, 0x1a,0x66, 0x1a,0x03, 0x19,0x03, 0x19,0x03, 0x1f,0x03, 0x1b,0x03, 0x1f,0x00, 0x1a,0x03, 0x1a,0x03, 0x1a,0x03, 0x1b,0x03, 0x1b,0x03, 0x1a,0x03, 0x19,0x03, 0x19,0x02, 0x17,0x03, 0x15,0x17, 0x15,0x03, 0x16,0x03, 0x17,0

25、x03, 0x18,0x03, 0x17,0x04, 0x18,0x0e, 0x18,0x03, 0x17,0x04, 0x18,0x0e, 0x18,0x66, 0x17,0x03, 0x18,0x03, 0x17,0x03, 0x18,0x03, 0x20,0x03, 0x20,0x02, 0x1f,0x03, 0x1b,0x03, 0x1f,0x66, 0x20,0x03, 0x21,0x03, 0x20,0x03, 0x1f,0x03, 0x1b,0x03, 0x1f,0x66, 0x1f,0x04, 0x1b,0x0e, 0x1b,0x03, 0x19,0x03, 0x19,0x03

26、, 0x15,0x03, 0x1a,0x66, 0x1a,0x03, 0x19,0x03, 0x15,0x03, 0x15,0x03, 0x17,0x03, 0x16,0x66, 0x17,0x04, 0x18,0x04, 0x18,0x03, 0x19,0x03, 0x1f,0x03, 0x1b,0x03, 0x1f,0x66, 0x20,0x03, 0x21,0x03, 0x20,0x03, 0x1f,0x03, 0x1b,0x03, 0x1f,0x66, 0x1f,0x03, 0x1b,0x03, 0x19,0x03, 0x19,0x03, 0x15,0x03, 0x1a,0x66, 0

27、x1a,0x03, 0x19,0x03, 0x19,0x03, 0x1f,0x03, 0x1b,0x03, 0x1f,0x00, 0x18,0x02, 0x18,0x03, 0x1a,0x03, 0x19,0x0d, 0x15,0x03, 0x15,0x02, 0x18,0x66, 0x16,0x02, 0x17,0x02, 0x15,0x00, 0x00,0x00;/同一首歌unsigned char code music_same= 0x0f,0x01, 0x15,0x02, 0x16,0x02, 0x17,0x66, 0x18,0x03, 0x17,0x02, 0x15,0x02, 0x

28、16,0x01, 0x15,0x02, 0x10,0x02, 0x15,0x00, 0x0f,0x01, 0x15,0x02, 0x16,0x02, 0x17,0x02, 0x17,0x03, 0x18,0x03, 0x19,0x02, 0x15,0x02, 0x18,0x66, 0x17,0x03, 0x19,0x02, 0x16,0x03, 0x17,0x03, 0x16,0x00, 0x17,0x01, 0x19,0x02, 0x1b,0x02, 0x1b,0x70, 0x1a,0x03, 0x1a,0x01, 0x19,0x02, 0x19,0x03, 0x1a,0x03, 0x1b,

29、0x02, 0x1a,0x0d, 0x19,0x03, 0x17,0x00, 0x18,0x66, 0x18,0x03, 0x19,0x02, 0x1a,0x02, 0x19,0x0c, 0x18,0x0d, 0x17,0x03, 0x16,0x01, 0x11,0x02, 0x11,0x03, 0x10,0x03, 0x0f,0x0c, 0x10,0x02, 0x15,0x00, 0x1f,0x01, 0x1a,0x01, 0x18,0x66, 0x19,0x03, 0x1a,0x01, 0x1b,0x02, 0x1b,0x03, 0x1b,0x03, 0x1b,0x0c, 0x1a,0x0

30、d, 0x19,0x03, 0x17,0x00, 0x1f,0x01, 0x1a,0x01, 0x18,0x66, 0x19,0x03, 0x1a,0x01, 0x10,0x02, 0x10,0x03, 0x10,0x03, 0x1a,0x0c, 0x18,0x0d, 0x17,0x03, 0x16,0x00, 0x0f,0x01, 0x15,0x02, 0x16,0x02, 0x17,0x70, 0x18,0x03, 0x17,0x02, 0x15,0x03, 0x15,0x03, 0x16,0x66, 0x16,0x03, 0x16,0x02, 0x16,0x03, 0x15,0x03,

31、0x10,0x02, 0x10,0x01, 0x11,0x01, 0x11,0x66, 0x10,0x03, 0x0f,0x0c, 0x1a,0x02, 0x19,0x02, 0x16,0x03, 0x16,0x03, 0x18,0x66, 0x18,0x03, 0x18,0x02, 0x17,0x03, 0x16,0x03, 0x19,0x00, 0x00,0x00 ;/两只蝴蝶 unsigned char code music_two = 0x17,0x03, 0x16,0x03, 0x17,0x01, 0x16,0x03, 0x17,0x03, 0x16,0x03, 0x15,0x01,

32、 0x10,0x03, 0x15,0x03, 0x16,0x02, 0x16,0x0d, 0x17,0x03, 0x16,0x03, 0x15,0x03, 0x10,0x03, 0x10,0x0e, 0x15,0x04, 0x0f,0x01, 0x17,0x03, 0x16,0x03, 0x17,0x01, 0x16,0x03, 0x17,0x03, 0x16,0x03, 0x15,0x01, 0x10,0x03, 0x15,0x03, 0x16,0x02, 0x16,0x0d, 0x17,0x03, 0x16,0x03, 0x15,0x03, 0x10,0x03, 0x15,0x03, 0x

33、16,0x01, 0x17,0x03, 0x16,0x03, 0x17,0x01, 0x16,0x03, 0x17,0x03, 0x16,0x03, 0x15,0x01, 0x10,0x03, 0x15,0x03, 0x16,0x02, 0x16,0x0d, 0x17,0x03, 0x16,0x03, 0x15,0x03, 0x10,0x03, 0x10,0x0e, 0x15,0x04, 0x0f,0x01, 0x17,0x03, 0x19,0x03, 0x19,0x01, 0x19,0x03, 0x1a,0x03, 0x19,0x03, 0x17,0x01, 0x16,0x03, 0x16,

34、0x03, 0x16,0x02, 0x16,0x0d, 0x17,0x03, 0x16,0x03, 0x15,0x03, 0x10,0x03, 0x10,0x0d, 0x15,0x00, 0x19,0x03, 0x19,0x03, 0x1a,0x03, 0x1f,0x03, 0x1b,0x03, 0x1b,0x03, 0x1a,0x03, 0x17,0x0d, 0x16,0x03, 0x16,0x03, 0x16,0x0d, 0x17,0x01, 0x17,0x03, 0x17,0x03, 0x19,0x03, 0x1a,0x02, 0x1a,0x02, 0x10,0x03, 0x17,0x0

35、d, 0x16,0x03, 0x16,0x01, 0x17,0x03, 0x19,0x03, 0x19,0x03, 0x17,0x03, 0x19,0x02, 0x1f,0x02, 0x1b,0x03, 0x1a,0x03, 0x1a,0x0e, 0x1b,0x04, 0x17,0x02, 0x1a,0x03, 0x1a,0x03, 0x1a,0x0e, 0x1b,0x04, 0x1a,0x03, 0x19,0x03, 0x17,0x03, 0x16,0x03, 0x17,0x0d, 0x16,0x03, 0x17,0x03, 0x19,0x01, 0x19,0x03, 0x19,0x03,

36、0x1a,0x03, 0x1f,0x03, 0x1b,0x03, 0x1b,0x03, 0x1a,0x03, 0x17,0x0d, 0x16,0x03, 0x16,0x03, 0x16,0x03, 0x17,0x01, 0x17,0x03, 0x17,0x03, 0x19,0x03, 0x1a,0x02, 0x1a,0x02, 0x10,0x03, 0x17,0x0d, 0x16,0x03, 0x16,0x01, 0x17,0x03, 0x19,0x03, 0x19,0x03, 0x17,0x03, 0x19,0x03, 0x1f,0x02, 0x1b,0x03, 0x1a,0x03, 0x1

37、a,0x0e, 0x1b,0x04, 0x17,0x02, 0x1a,0x03, 0x1a,0x03, 0x1a,0x0e, 0x1b,0x04, 0x17,0x16, 0x1a,0x03, 0x1a,0x03, 0x1a,0x0e, 0x1b,0x04, 0x1a,0x03, 0x19,0x03, 0x17,0x03, 0x16,0x03, 0x0f,0x02, 0x10,0x03, 0x15,0x00, 0x00,0x00 ;/*void delayms(unsigned int z)unsigned int x,y;for (x = z; x0; x-)for(y=110; y0; y- );void keyscan()/矩阵键盘扫描函数unsigned char x,y;p3=0xf0;if (p3!=0xf0) delayms(10); if (p3!=0xf0) x=p3&0xf0; p3=0x8f; y=p3&0x8f; key=x|y;

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论