基于51单片机的简易音乐播放器.docx_第1页
基于51单片机的简易音乐播放器.docx_第2页
基于51单片机的简易音乐播放器.docx_第3页
基于51单片机的简易音乐播放器.docx_第4页
基于51单片机的简易音乐播放器.docx_第5页
已阅读5页,还剩11页未读 继续免费阅读

下载本文档

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

文档简介

基于单片机的电子琴课程设计一:课程目的以及要求用51单片机作为主控电路设计一个电子琴,实现能通过切换不同的键盘按键来切换歌曲。二:课程设计背景:电子琴是现代电子科技与音乐结合的产物,是一种新型的键盘乐器。它在现代音乐扮演着重要的角色,单片机具有强大的控制功能和灵活的编程实现特性,它已经溶入现代人们的生活中,成为不可替代的一部分。本文的主要内容是用STC89C52单片机为核心控制元件,设计一个电子琴。以单片机作为主控核心,与键盘、扬声器等模块组成核心主控制模块。本文主要对使用单片机设计简易电子琴进行了分析,并介绍了基于单片机电子琴统硬件组成。利用单片机产生不同频率来获得我们要求的音阶,最终可随意弹奏想要表达的音乐。并且本文分别从原理图,主要芯片,各模块原理及各模块的程序的调试来详细阐述。三:系统硬件设计3.1系统框图51单片机AT89C51按键键盘音频输出3.2 51单片机产生音符原理MCS-5l单片机有两个定时器计数器。系统使用定时器计数器0产生方波。定时器计数器l刖用于记录音符的时长,即按键持续的时问。定时器计数器0的工作方式是l,即为16位定时器计数器。16位定时器计数器被赋予一个初值,并开始记数。当记数溢出,产生中断后,中断服务程序将与蜂鸣器连接的引脚电平置反,并且对定时器计数器再赋初值,重新开始记效。之后产生中断,引脚电平再被置反,如此周而复始,在引脚上就能产生一个频率与定时初值有关的方波信号。而定时初值与音符频率有关,相应频率的确定,可参照12平均律,相邻两个半音A和B的频率之间的关系为:A=B1221。定时器计数器l的作用为记录音符的时长,工作方式为l,即16位定时器计数器。在12MHZ的晶体振荡器F,单片机的机器周期为l“S。本系统中,定时器计数器l以10ms的时长为l定时单位,记录音符持续了多少个定时单位,如此,使用一个unmgnedchar型变量既可以记录2.55s的时长,这在用于记录一般弹奏的音符时是足够的。而若一个音符的时长超过2.55S,则程序将在数组该位记满时强制跳转,并复制此音符信息至教组下一位来记录该音符剩余的时长,实现一种。无缝连接”。如此以来,在存储空间允许的情况下,系统允许记录任意时长的音符。音乐是由音符和时长组成,单片机将一连串音符和时长的时间序列以两组对应的敬组的形式保存起来,也就是保存了一段音乐2。四 系统软件设计 4.1 单片机产生音符过程:一首音乐是许多不同的音阶组成的,而每个音阶对应着不同的频率,这样我们就可以利用不同的频率的组合,即可构成我们所想要的音乐了,当然对于单片机来产生不同的频率非常方便,我们可以利用单片机的定时/计数器T0来产生这样方波频率信号,因此,我们只要把一首歌曲的音阶对应频率关系正确即可。若要产生音频脉冲,只要算出某一音频的周期(1/频率),再将此周期除以2,即为半周期的时间。利用定时器计时半周期时间,每当计时终止后就将P1.0反相,然后重复计时再反相。就可在P1.0引脚上得到此频率的脉冲。 利用STC89C52的内部定时器使其工作计数器模式(MODE1)下,改变计数值TH0及TL0以产生不同频率的方法产生不同音阶,例如,频率为523Hz,其周期T1/5231912s,因此只要令计数器计时956s/1s956,每计数956次时将I/O反相,就可得到中音DO(523Hz)。计数脉冲值与频率的关系式(如式2-1所示)是:N=fi2fr式中,N是计数值;fi是机器频率(晶体振荡器为12MHz时,其频率为1MHz);fr是想要产生的频率。其计数初值T的求法如下:T65536N65536fi2fr例如:设K65536,fi1MHz,求低音DO(261Hz)、中音DO(523Hz)、高音DO(1046Hz)的计数值。T65536N65536fi2fr6553610000002fr65536500000/fr 低音DO的T65536500000/26263627 中音DO的T65536500000/52364580 高音DO的T65536500000/1046650594.2 程序编写流程图开始P1.0=0键盘扫描是否有键被按下?并检测确定按键执行输出电路,读相应程序进行放歌五:调试过程5.1 软件仿真调试软件仿真调试主要是针对单片机部分进行调试。在软件运行前,先确保电路中连线正确,这一工作是整个调试工作的第一步,也是非常重要的一个步骤。在这部分调试中主要通过目测,用来完成检测电路中是否存在断路或者短路情况等。在确保软件仿真电路正常,无异常情况(断路或短路)方可进行软件运行,在本次设计中,主要检测单片机的程序功能是否正确在理论上编写的程序是否可以实现。确保自己的电路板在理论上是可实现的。5.2实物调试过程:1.先确保电路板是否每条铜线支路都是导通的才能进行下一步调试2.在检测好每条通路没有断路的的情况下还要排除短路的情况,因为在焊接时会有可能将焊锡丝掉到两根铜线之间。3.将下好程序的单片机安装好后上电检测单片机的主要管脚电压值以确定单片机是否正常工作。4.在所有上诉步骤都正确的情况下,要是功能为实现就要考虑软件编写是否正确。5.3 本次出现的一些问题以及解决方法1.电路断线情况比较多,是由于腐蚀和转印的过程出现了一些问题2.开始设计的时候在每个按键的之路上添加一个红色二极管作为指示功能,但未考虑到编写的程序是按键扫描过程是检测单片机引脚是否为低电平,当加了一个二极管的时候会产生一个管压降,导致引脚不能产生低电平。所以该指示灯不能直接加到引脚处。六:体会:通过从理论到实际实物的制作能够让我们结合自己所学去应用到实际的电路设计中去。让我们能够发现一些课堂中学习的不足然后结合实践巩固一些单片机的控制以及内部的一些功能使用;比如对T0定时器以及外部中断的程序控制。我们应该多通过实践来学习和巩固自己一些知识点。七:附录7.1系统原理图:7.2程序:#include #define uchar unsigned charsbit beepIO=P37;sbit key_1=P10;sbit key_2=P11;sbit key_3=P12;sbit key_4=P13;sbit key_5=P14;sbit key_6=P15;sbit key_7=P16;sbit key_8=P17;uchar m,n; uchar code T492=0,0,0xF8,0x8B,0xF8,0xF2,0xF9,0x5B,0xF9,0xB7,0xFA,0x14,0xFA,0x66,0xFA,0xB9,0xFB,0x03,0xFB,0x4A,0xFB,0x8F,0xFB,0xCF,0xFC,0x0B,0xFC,0x43,0xFC,0x78,0xFC,0xAB,0xFC,0xDB,0xFD,0x08,0xFD,0x33,0xFD,0x5B,0xFD,0x81,0xFD,0xA5,0xFD,0xC7,0xFD,0xE7,0xFE,0x05,0xFE,0x21,0xFE,0x3C,0xFE,0x55,0xFE,0x6D,0xFE,0x84,0xFE,0x99,0xFE,0xAD,0xFE,0xC0,0xFE,0x02,0xFE,0xE3,0xFE,0xF3,0xFF,0x02,0xFF,0x10,0xFF,0x1D,0xFF,0x2A,0xFF,0x36,0xFF,0x42,0xFF,0x4C,0xFF,0x56,0xFF,0x60,0xFF,0x69,0xFF,0x71,0xFF,0x79,0xFF,0x81;uchar code music12=0,4,27,4,19,4,19,4,27,4,26,4,26,2,27,2,24,8,22,4,17,4,24,4,22,4,19,16,15,4,12,4,12,4,15,4,17,8,19,8,26,6,26,2,26,4,24,4,24,4,19,4,19,8,27,4,19,4,19,4,27,4,26,4,26,2,27,2,24,6,24,2,22,4,17,4,24,4,22,4,19,16,15,4,12,4,12,4,15,4,17,8,19,8,19,6,19,2,19,4,17,4,14,4,14,4,14,4,7,4,12,24,0xFF,0xFF;uchar code music22=0,4,23,4,21,4,23,16,23,4,21,4,23,4,21,4,19,16,16,4,19,4,21,8,21,4,23,4,21,4,19,4,16,4,19,4,14,24,23,4,21,4,23,16,23,4,21,4,23,4,21,4,19,24,16,4,19,4,21,8,21,4,23,4,21,4,19,4,16,4,19,4,21,24,23,4,21,4,23,16,23,4,21,4,23,4,21,4,19,16,16,4,19,4,21,8,21,4,23,4,21,4,19,4,16,4,19,4,14,24,23,4,26,4,26,16,26,4,28,4,26,4,23,24,21,4,23,4,21,8,21,4,23,4,21,4,19,4,16,4,16,2,19,2,19,24,0,20,26,4,26,4,28,4,31,4,30,4,30,4,28,4,23,4,21,4,21,4,23,16,0,4,23,4,23,4,26,4,28,8,28,12,16,4,23,4,21,4,21,24,23,4,26,4,26,4,23,4,26,8,0,4,31,8,30,4,28,4,30,4,23,8,0,4,28,4,28,4,30,4,28,4,26,4,23,4,21,8,23,4,21,4,23,4,26,16,0xFF,0xFF; uchar code music32=0,4,16,12,23,16,21,4,23,4,21,4,19,12,0,8,19,4,19,4,19,4,18,4,16,8,14,4,16,16,0,16,16,12,28,16,26,4,28,4,26,4,23,8,0,16,21,4,21,4,21,4,21,4,19,8,16,4,18,16,0,12,16,12,23,16,21,4,23,4,21,4,19,8,0,16,19,4,19,4,19,4,18,4,16,8,14,4,16,16,0,12,16,12,28,16,26,4,28,4,26,4,23,16,0,12,28,4,28,4,28,4,30,4,28,8,26,4,28,24,0,12,28,4,28,2,26,2,23,4,23,4,28,4,28,2,26,2,28,4,31,4,26,6,23,2,26,4,28,4,26,16,21,4,21,2,19,2,16,4,16,4,21,4,21,2,19,2,21,4,23,4,19,6,16,2,19,4,16,4,14,16,28,4,28,2,26,2,23,4,23,4,28,4,28,2,26,2,28,4,28,4,31,4,31,2,28,2,26,4,26,4,31,4,31,2,26,2,31,4,31,4,0,4,31,4,31,4,31,4,30,4,28,8,26,4,28,24,28,4,28,2,26,2,23,4,23,4,28,4,28,2,26,2,28,4,31,4,26,6,23,2,26,4,28,4,26,16,21,4,21,2,19,2,16,4,16,4,21,4,21,2,19,2,21,4,23,4,19,6,16,2,19,4,16,4,14,16,28,4,28,2,26,2,23,4,23,4,28,4,28,2,26,2,28,4,28,4,31,4,31,2,28,2,26,4,26,4,31,4,31,2,26,2,31,4,31,4,0,4, 31,4,31,4,31,4,30,4,28,8,26,4,28,20, 0,4,31,4,31,4,31,4,30,4,28,8,26,4,28,24,0xFF,0xFF;uchar code music42=0,4,14,4,17,4,21,4,24,4,21,8,21,4,19,4,21,8,21,4,19,4,21,8,14,4,16,4,17,4,21,4,19,4,17,4,14,8,12,8,9,24,14,4,17,4,21,4,24,4,21,8,21,4,19,4,21,8,21,4,19,4,21,8,14,4,16,4,17,4,21,4,19,4,17,4,14,8,12,8,14,24,14,4,16,4,17,4,19,4,21,4,22,4,24,8,22,4,21,4,19,8,19,4,21,4,19,16,14,4,16,4,17,4,19,4,21,8,16,4,14,4,16,24,14,4,16,4,17,4,19,4,21,4,22,4,24,8,22,4,21,4,19,8,19,4,21,4,19,8,14,4,16,4,17,4,21,4,19,4,17,4,14,8,12,8,14,24,21,8,26,16,24,8,21,4,24,4,26,16,24,8,21,4,19,4,17,4,14,4,17,4,19,4,21,8,21,4,19,4,21,24,21,8,26,16,24,8,21,4,24,4,26,24,24,8,21,4,19,4,17,4,14,4,17,4,19,4,21,8,26,4,24,4,26,24,0xFF,0xFF;uchar code music52=0,4,19,4,19,4,19,8,14,8,23,4,23,4,23,8,19,8,19,4,23,4,26,8,26,8,24,4,23,4,21,16,21,4,23,4,24,8,24,8,23,4,21,4,23,8,19,8,19,4,23,4,21,8,14,8,18,4,21,4,19,16,0xFF,0xFF;uchar code music62=0,4,15,4,27,4,26,8,24,4,24,4,22,8,17,2,19,8,17,2,19,2,15,8,15,2,0,2,15,4,27,4,26,8,24,4,24,4,22,8,17,4,19,14,0,2,15,4,17,4,19,4,22,4,27,4,26,8,24,4,24,4,22,8,17,2,19,8,17,2,19,2,15,8,12,4,15,4,17,8,19,8,12,4,15,4,17,4,19,4,17,8,15,14,0,2,0,8,0,8,17,4,29,4,28,8,26,4,26,4,24,8,19,2,21,8,19,2,21,2,17,8,17,2,0,2,17,4,29,4,28,8,26,4,26,4,24,8,19,4,21,14,0,2,17,4,19,4,21,4,24,4,29,4,28,8,26,4,26,4,24,8,19,2,21,8,19,2,21,2,17,8,14,4,17,4,19,8,21,8,14,4,17,4,19,4,21,4,19,8,17,14,0,2,17,4,29,4,28,8,26,4,26,4,24,8,19,2,21,8,19,2,21,2,17,8,17,2,0,2,17,4,29,4,28,8,26,4,26,4,24,8,19,4,21,14,0,2,17,4,19,4,21,4,24,4,29,4,28,8,26,4,26,4,24,8,19,2,21,8,19,2,21,2,17,8,14,4,17,4,19,8,21,8,14,4,17,4,19,4,21,4,19,8,17,14,0,2,17,4,29,4,28,8,26,4,26,4,24,8,19,2,21,8,19,2,21,2,17,8,17,2,0,2,17,4,29,4,28,8,26,4,26,4,24,8,19,4,21,14,0,2,17,4,19,4,21,4,24,4,29,4,28,8,26,4,26,4,24,8,19,2,21,8,19,2,21,2,17,8,14,4,17,4,19,8,21,8,14,4,17,4,19,4,21,4,19,8,17,14,0,2,0,8,0xFF,0xFF;uchar code music72=0,4,18,2,18,2,18,2,18,2,18,6,18,2,18,4,18,4,13,4,14,2,16,2,14,16,0,8,0,4,11,4,16,2,16,2,16,2,16,2,16,6,16,2,16,4,18,4,20,4,20,4,13,16,0,8,0,4,13,4,18,2,18,2,18,2,18,2,18,6,18,2,25,4,25,4,23,16,0,8,0,8,0,4,20,4,20,4,20,6,20,2,20,6,20,6,13,2,21,4,20,2,21,2,20,16,0,8,0,4,13,4,18,2,18,2,18,2,18,2,18,6,18,2,18,4,18,4,13,4,14,2,16,2,14,16,0,8,0,4,11,4,16,2,16,2,16,2,16,2,16,6,16,2,16,4,18,4,20,4,20,4,13,16,0,8,0,4,13,4,18,2,18,2,18,2,18,2,18,6,18,2,18,6,18,2,25,4,25,4,23,16,0,8,0,8,0,4,20,2,20,2,20,6,20,2,20,6,20,2,20,4,18,2,20,2,18,16,0,8,0,4,25,4,30,2,25,2,25,2,25,2,25,6,21,2,18,6,13,2,13,4,25,4,23,16,0,8,0,4,13,4,28,2,28,2,28,2,28,2,28,6,28,2,28,6,30,2,32,2,32,6,25,16,0,8,0,4,25,4,30,2,25,2,25,2,25,2,25,6,21,2,18,6,13,2,13,4,25,4,23,16,0,8,0,8,0,4,23,2,23,2,23,6,23,2,23,6,13,2,20,6,20,2,18,16,13,4,0xFF,0xFF;uchar code music82=0,4,18,2,18,2,18,2,18,2,18,6,18,2,18,4,18,4,13,4,14,2,16,2,14,16,0,8,0,4,11,4,16,2,16,2,16,2,16,2,16,6,16,2,16,4,18,4,20,4,20,4,13,16,0,8,0,4,13,4,18,2,18,2,18,2,18,2,18,6,18,2,25,4,25,4,23,16,0,8,0,8,0,4,20,4,20,4,20,6,20,2,20,6,20,6,13,2,21,4,20,2,21,2,20,16,0,8,0,4,13,4,18,2,18,2,18,2,18,2,18,6,18,2,18,4,18,4,13,4,14,2,16,2,14,16,0,8,0,4,11,4,16,2,16,2,16,2,16,2,16,6,16,2,16,4,18,4,20,4,20,4,13,16,0,8,0,4,13,4,18,2,18,2,18,2,18,2,18,6,18,2,18,6,18,2,25,4,25,4,23,16,0,8,0,8,0,4,20,2,20,2,20,6,20,2,20,6,20,2,20,4,18,2,20,2,18,16,0,8,0,4,25,4,30,2,25,2,25,2,25,2,25,6,21,2,18,6,13,2,13,4,25,4,23,16,0,8,0,4,13,4,28,2,28,2,28,2,28,2,28,6,28,2,28,6,30,2,32,2,32,6,25,16,0,8,0,4,25,4,30,2,25,2,25,2,25,2,25,6,21,2,18,6,13,2,13,4,25,4,23,16,0,8,0,8,0,4,23,2,23,2,23,6,23,2,23,6,13,2,20,6,20,2,18,16,13,4,0xFF,0xFF;void delay(uchar p) uchar i,j; for(;p0;p-) for(i=181;i0;i-) for(j=181;j0;j-);void pause() uchar i,j; for(i=150;i0;i-) for(j=150;j0;j-);void T0_int() interrupt 1 beepIO=!beepIO; TH0=Tm0; TL0=Tm1;void main() uchar i=0; while(1)P1=0xff; while(!key_1)TMOD=0x01; EA=1; ET0=1; while(1) m=music2i0;n=music2i1; if(m=0x00) TR0=0;delay(n);i+; else if(m=0xFF) TR0=0;delay(30);i=0; else if(m=music2i+10) TR0=1;delay(n);TR0=0;pause();i+; else TR0=1;delay(n);i+;while(!(key_2) TMOD=0x01; EA=1; ET0=1; while(1) m=music1i0;n=music1i1; if(m=0x00) TR0=0;delay(n);i+; else if(m=0xFF) TR0=0;delay(30);i=0; else if(m=music1i+10) TR0=1;delay(n);TR0=0;pause();i+; else TR0=1;delay(n);i+; while(!key_3)TMOD=0x01; EA=1; ET0=1; while(1) m=music3i0;n=music3i1; if(m=0x00) TR0=0;delay(n);i+; else if(m=0xFF) TR0=0;delay(30);i=0; else if(m=music3i+10) TR0=1;delay(n);TR0=0;pause();i+; else TR0=1;delay(n);i+;while(!key_4)TMOD=0x01; EA=1; ET0=1; while(1) m=music4i0;n=music4i1; if(m=0x00) TR0=0;delay(n);i+; els

温馨提示

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

最新文档

评论

0/150

提交评论