西北师范大学知行学院计算机与电子信息工程系课程设计报告书课程名称:单片机程序设计专业:电子信息工程课题名称:基于单片机的音乐播放器设计班级:12电本班学号:201292160124姓名:窦红莉指导教师:胡亚琦二○一四——二○一五学年第一学期基于单片机的音乐播放器设计一、实验题目基于单片机的音乐播放器设计二、实验内容本设计是一个基于AT89C51系列单片机的音乐播放器,依据单片机技术原理,通过硬件电路制作以及软件编译,设计制作出一个多功能多功能音乐播放器。该音乐播放器主要由按键电路、复位电路、时钟电路以及蜂鸣器组成。使用两个按键控制音乐播放器,一个用来切换歌曲,另一个用来切换8路LED的变化花样,本音乐盒共有两首歌曲,花样灯花样共计3种。播放歌曲时,蜂鸣器发出某个音调,与之对应的LED亮起。本设计利用KEIL编程软件对音乐播放器源程序进行编程并调试,配合PROTEUS仿真软件对硬件进行仿真调试,节约了设计时间。【关键字】音乐盒;AT89C51单片机;KEIL;PROTEUS;音调三、设计目的1、设计方案设计一个基于AT89C51系列单片机的音乐播放器,利用按键切换演奏出不同的乐曲。蜂鸣器发出某个音调,与之相对应的LED亮起。使用两个按键,一个用来切换歌曲,另一个切换八路LED的变化花样。2、研究内容1)电路有两种工作模式:演奏音乐模式和花样灯模式。演奏音乐模式:演奏完整的一首的歌曲,八路LED随着音乐变化。花样灯模式:八路LED变化出各种花样,蜂鸣器随着发出“嘀嘀”声2)按下按键1进入演奏音乐模式,再按切换歌曲,共两首歌曲。3)按下按键2进入花样灯模式,再按切换LED花样,共三种花样。此电路的程序只占用了1K左右,可编制更多的音乐和LED花样,使系统的功能更加强大。3、设计原理1)系统组成框图音乐播放器的系统结构以AT89C51单片机位控制核心,加上2个按键、时钟复位电路、蜂鸣器、LED模块组成。单片机负责接收按键的输入,根据输入控制音乐播放曲目和音乐花样灯的显示样式以及蜂鸣器发音。系统组成框图如图2.1所示。2)音乐盒的功能结构图音乐播放器的功能结构如图2.2所示。Key1负责切换播放歌曲,播放歌曲共2首,分别是千年之恋和寂寞沙洲冷。Key2负责切换LED显示花样,显示花样共3种,第一种顺序显示,第二种由两边向中间移动然后向两边移动,第三种循环显示。3)硬件设计四、总体设计框图五、调试5.1检查硬件连接在PROTUES检查各硬件管脚是否连接正确,线路逻辑是否正确,例如:晶振电路的连接,复位电路是否设计正确。5.2检查软件系统1.根据系统的原理结构检查各流程图是否正确,再根据流程图来检查程序是否也正确。2.将所有程序组织起来,在软件环境下运行,检查程序是否正确。通过对硬件和软件系统的认真检查,反复测试,如果没有出现问题即可把源程序编译成HEX文件装载到单片机中,对硬件进行仿真。5.3测试结果5.3.1.总体运行图5.3.2.花样灯3种花样图由于截图只能看到静态图,不能看到整体图样,下面画图说明:(1)第一种花样灯显示方式为:从D1移向D2,然后D1熄灭,再从D2移向D3,然后D2熄灭,以此类推,往复循环。(2)第二种花样灯显示方式为:从两边向中间移动,首先从D1移向D8,再从D8移向D2,以此类推,往复循环。具体如图5.3。(3)第三种花样灯显示方式为:从D1移向D2,然后D1熄灭,再从D2移向D3,然后D2熄灭,以此往复循环。与第一种方式的不同之处为:当D1移向D2时,D1不熄灭,再从D2移向D3时,D2也不熄灭,以此类推。六、实验程序#includereg52.h#defineucharunsignedchar#defineuintunsignedintsbitduan=P2^6;sbitkey1=P3^2;//按key1可切换花样sbitkey2=P3^3;//按key2可切换歌曲sbitfm=P0^7;//蜂鸣器连续的IO口sbitP34=P3^4;//矩阵键盘的一列ucharcodehuayang1[]={0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf};//花样1ucharcodehuayang2[]={0x7f,0xfe,0xbf,0xfd,0xdf,0xfb,0xef,0xf7,0xef,0xfb,0xdf,0xfd,0xbf,0xfe};//花样2ucharcodehuayang3[]={0x7f,0x3f,0x1f,0x0f,0x07,0x03,0x01,0x0,0x80,0xc0,0xe0,0xf0,0xf8,0xfc,0xfe,0xff};charcodehuayang4[]={0x55,0xaa,0xcc,0x33,0x99,0x66,0x0f,0xf0};ucharcount1;//花样标志ucharcount2;//歌曲标志uchartimeh,timel,i;//---------------------------简谱---------------------------------------//编程规则字节高位是简谱,低位是持续时间,//代表多少个十六分音符//1-7代表中央C调,8-E代表高八度,0代表停顿//最后的0是结束标志ucharcodeqnzl[]={//千年之恋0x12,0x22,0x34,0x84,0x74,0x54,0x38,0x42,0x32,0x22,0x42,0x34,0x84,0x72,0x82,0x94,0xA8,0x08,//前奏0x32,0x31,0x21,0x32,0x52,0x32,0x31,0x21,0x32,0x62,//竹林的灯火到过的沙漠0x32,0x31,0x21,0x32,0x82,0x71,0x81,0x71,0x51,0x32,0x22,//七色的国度不断飘逸风中0x32,0x31,0x21,0x32,0x52,0x32,0x31,0x21,0x32,0x62,//有一种神秘灰色的旋涡0x32,0x31,0x21,0x32,0x83,0x82,0x71,0x72,0x02,//将我卷入了迷雾中0x63,0xA1,0xA2,0x62,0x92,0x82,0x52,//看不清的双手0x31,0x51,0x63,0x51,0x63,0x51,0x63,0x51,0x62,0x82,0x7C,0x02,//一朵花传来谁经过的温柔0x61,0x71,0x82,0x71,0x62,0xA2,0x71,0x76,//穿越千年的伤痛0x61,0x71,0x82,0x71,0x62,0x52,0x31,0x36,//只为求一个结果0x61,0x71,0x82,0x71,0x62,0xA3,0x73,0x62,0x53,//你留下的轮廓指引我0x42,0x63,0x83,0x83,0x91,0x91,//黑夜中不寂寞0x61,0x71,0x82,0x71,0x62,0x0A2,0x71,0x76,//穿越千年的哀愁0x61,0x71,0x82,0x71,0x62,0x52,0x31,0x36,//是你在尽头等我0x61,0x71,0x82,0x71,0x62,0xA3,0x73,0x62,0x53,//最美丽的感动会值得0x42,0x82,0x88,0x02,0x74,0x93,0x89,0xff//结束标志//用一生守候};ucharcodejmszl[]={//寂寞沙洲冷0x12,0x12,0x22,0x32,0x31,0x22,0x21,0x22,//自你走后心憔悴0x21,0x31,0x51,0x52,0x31,0x52,0x61,0x15,0x14,//白色油桐风中纷飞0x51,0x52,0x31,0x52,0x62,0x13,0x11,0x13,0x32,0x28,0x08,0x28,//落花似人有情这个季节0x31,0x32,0x31,0x32,0x11,0x21,0x51,0x52,0x51,0x52,//河畔的风放肆拼命地吹0x51,0x51,0x31,0x32,0x31,0x32,0x81,0x72,0x63,//不断拨弄离人的眼泪0x62,0x71,0x81,0x72,0x61,0x61,0x52,0x31,0x21,0x32,0x51,0x54,//那样浓烈的爱再也无法给0x22,0x12,0x11,0x12,0x11,0x12,0x12,0x14,0x26,0x32,0x26,//伤感一夜一夜0x32,0x61,0x51,0x51,0x31,0x31,0x21,0x31,0x51,0x61,0x51,0x31,0x51,//当记忆的线缠绕过往支离破碎0x02,0x32,0x81,0x81,0x81,0x81,0x62,0x52,0x34,//是慌乱占据了心扉0x31,0x81,0x81,0x81,0x61,0x91,0x82,//有花儿伴着蝴蝶0x51,0x51,0x51,0x51,0x31,0x61,0x53,//孤雁可以双飞0x21,0x11,0x21,0x11,0x22,0x11,0x21,0x26,//夜深人静独徘徊0x32,0x61,0x51,0x51,0x31,0x31,0x21,0x31,0x51,0x61,0x51,0x31,0x51,0x52,//当幸福恋人寄来红色分享喜悦0x31,0x31,0x81,0x81,0x81,0x61,0x91,0x81,0x61,0x31,0x56,//闭上双眼难过头也不敢回0x32,0x32,0x81,0x81,0x81,0x81,0x91,0x81,0x61,0x81,0x61,0x51,0x31,0x51,0x34,//仍然捡尽寒枝不肯安歇微带着后悔0x21,0x31,0x51,0x31,0x21,0x11,0x61,0x21,0x16,//寂寞沙洲我该思念谁0xff};//----------------------------简谱音调对应的定时器初值---------------------------//适合11.0592M的晶振ucharcodecuzhi[]={0xff,0xff,//占位0xFC,0x8E,//中央C调1-70xFC,0xED,0xFD,0x43,0xFD,0x6A,0xFD,0xB3,0xFD,0xF3,0xFE,0x2D,0xFE,0x47,//高八度1-70xFE,0x76,0xFE,0xA1,0xFE,0xC7,0xFE,0xD9,0xFE,0xF9,0xFF,0x16};ucharyinyue[]={0xff,0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f,0x0,0x0};//将音调转化为对应的LED样式voiddelay1(uintz);//延时1MSvoiddelay(uintz);//延时165MS,即十六分音符voidsong();voidbeep();//蜂鸣器叫一声main(){ucharx;count1=0;//流水灯无花样count2=1;//唱第一首歌P34=0;//选取矩阵键盘的一列EA=1;//开总中断EX0=1;//开外部中断0IT0=1;//外部中断0下降沿触发方式EX1=1;//开外部中断1IT1=1;//外部中断1下降沿触发方式TMOD=0x01;//定时器0工作在方式1TH0=0;TL0=0;ET0=1;while(1){if(count1!=0){switch(count1){case1:for(x=0;x14;x++){duan=1;P1=huayang1[x];beep();delay1(300);duan=0;if(count1!=1)break;}break;case2:for(x=0;x14;x++){duan=1;P1=huayang2[x];beep();delay1(300);duan=0;if(count1!=2)break;}break;c