《单片机电子琴课程设计报告.doc》由会员分享,可在线阅读,更多相关《单片机电子琴课程设计报告.doc(43页珍藏版)》请在三一办公上搜索。
1、课程设计报告设计题目 单片机电子琴 专 业 * 班 级 0802 姓 名 * 学 号 * 同组成员 * 指导教师 * 提交日期 2011-05-17 摘要随着社会的发展进步,音乐逐渐成为我们生活中很重要的一部分,有人曾说喜欢音乐的人不会向恶。我们都会抽空欣赏世界名曲,作为对精神的洗礼。本论文设计一个基于单片机的简易电子琴。电子琴是现代电子科技与音乐结合的产物,是一种新型的键盘乐器。它在现代音乐扮演着重要的角色,单片机具有强大的控制功能和灵活的编程实现特性,它已经溶入现代人们的生活中,成为不可替代的一部分。本系统是以51系列单片机AT89C51为主控制器,附有矩阵键盘、LED显示管、扬声器组成。
2、系统完成显示输入信息、播放相应音符等基本功能。本系统运行稳定,其优点是硬件电路简单,软件功能完善,控制系统可靠,性价比较高等,具有一定的实用和参考价值。关键词:AT89C51,矩阵键盘,LED显示管,蜂鸣器,1602液晶显示,1307时钟芯片。目录第1章 概述 -4第2章 课程设计任务及要求 -4 2.1 设计任务 -4 2.2 设计要求 -4第3章 系统设计-5 3.1设计方案 -5 3.2 系统设计 -53.2.2 系统流程图 -5 3.2.2 系统原理图 - -73.3 文件程序代码-7 第4章 心得体会-42第1章 概述单片微型计算机是大规模集成电路技术发展的产物,属第四代电子计算机,
3、它具有高性能、高速度、体积小、价格低廉、稳定可靠、应用广泛的特点。它的应用必定导致传统的控制技术从根本上发生变革。因此,单片机的开发应用已成为高科技和工程领域的一项重大课题。 电子琴是现代电子科技与音乐结合的产物,是一种新型的键盘乐器。它在现代音乐扮演着重要的角色,单片机具有强大的控制功能和灵活的编程实现特性,它已经溶入现代人们的生活中,成为不可替代的一部分。本文的主要内容是用单片机为核心控制元件,设计一个电子琴。以单片机作为主控核心,与键盘、扬声器等模块组成核心主控制模块,在主控模块上设有16个按键和扬声器。 本文主要对使用单片机设计简易电子琴进行了分析,并介绍了基于单片机电子琴统硬件组成。
4、利用单片机产生不同频率来获得我们要求的音阶,最终可随意弹奏想要表达的音乐。并且本文分别从原理图,主要芯片,各模块原理及各模块的程序的调试来详细阐述。本系统是简易电子琴的设计,按下键盘矩阵中的按键会使数码管显示当前按键,扬声器播放器对应的音符。通过设计本系统可了解单片机的基本功能。对单片机的了解有一个小的飞跃。第2章 课程设计任务及要求2.1 设计任务 1、任务设计一简易电子琴,要求能够发出1,2,3,4,5,6,7等7个音符,具有一般演奏功能。2.2 设计要求1)具有一般演奏功能,利用所给键盘的1,2,3,4,5,6,7,8八个键,能够发出8个不同的音调,并且要求按下按键发声,松开延时一段时间
5、停止,中间再按别的键则发另一个音调的声音2)具有自动播放已存曲目功能3)显示当前正在播放的歌曲的名称和歌词4)显示北京时间第3章 系统设计3.1设计方案该电路由时钟模块,液晶模块,键盘扫描模块组成。时钟模块:由时钟模块1307实现,能够正确显示北京时间,以及扩展显示星期等等。液晶模块:将时钟模块的信息正确的显示到液晶显示屏上,使用1602的液晶。键盘扫描模块:对十六个按键进行逐次扫描,扫描到前八个按键时,蜂鸣器依次发出1,2,3,4,5,6,7,8的音调声,岸其余按键时,可以跳转到不同的曲目以及液晶模块显示,保留几个按键做以后的扩展用。3.2 系统设计 3.2.1 系统流程图分析各项设计要求后
6、,可绘制如下程序流程图3.2.2 系统原理图3.3 文件程序代码(1). 液晶模块头文件sbit LCD_RS= P36;sbit LCD_RW= P35;sbit LCD_EN = P33;sfr LCDData=0x80;void LCDWrite(bit cd,unsigned char input);void LCDWait(unsigned char CiShu);void LCDDelay();void LCDInitialize();void LCDPrintString(unsigned char x,unsigned char y,unsigned char *string)
7、;void LCDPrintNumber(unsigned char x,unsigned char y,unsigned char bits,unsigned int number);unsigned char ReadState();void LCDInitialize()LCDDelay();LCDWrite(0,0x38);LCDDelay();LCDWrite(0,0x38);LCDDelay();LCDWrite(0,0x38);LCDDelay();LCDWrite(0,0x38);LCDDelay();LCDWrite(0,0x08);LCDDelay();LCDWrite(0
8、,0x01);LCDDelay();LCDWrite(0,0x06);LCDDelay();LCDWrite(0,0x0c);void LCDWrite(bit cd,unsigned char DATA)LCDWait(20);LCD_RS=cd;LCD_RW=0;LCD_EN=0;LCDData=DATA;LCD_EN=1;LCD_EN=0;void LCDPrintString(unsigned char x,unsigned char y,unsigned char *string)unsigned char address;address=x+(1-y)*0x40+0x80;LCDW
9、rite(0,address);while(*string!=0)LCDWrite(1,*string);string+;void LCDPrintNumber(unsigned char x,unsigned char y,unsigned char WeiShu,unsigned int number)unsigned char address;unsigned char j,temp5;address=x+(1-y)*0x40+0x80;LCDWrite(0,address);/if(number0)temp0=(number%100000)/10000+48;temp1=(number
10、%10000)/1000+48;temp2=(number%1000)/100+48;temp3=(number%100)/10+48;temp4=number%10+48;for(j=5-WeiShu;j5;j+)LCDWrite(1,tempj);void LCDWait(unsigned char CiShu)unsigned char i;for(i=0;iCiShu;i+)if(ReadState()&0x80)continue;else break; unsigned char ReadState()unsigned char temp;P0=0xff;LCD_EN=0;LCD_R
11、S=0;LCD_RW=1;LCD_EN=1;temp=LCDData;LCD_EN=0;return temp; void LCDDelay()unsigned int a;for(a=0;a1000;a+); (2).时钟芯片模块头文件void iicdelay(unsigned int times);void iicstart() MDE=1; MDO=1; iicdelay(2); MCO=1; iicdelay(2); MDO=0; iicdelay(2);void iicstop() MDE=1; MDO=0; iicdelay(2); MCO=1; iicdelay(2); MDO
12、=1;void iicack() MDE=1; MCO=0; MDO=0; iicdelay(2); MCO=1; iicdelay(2); MCO=0; iicdelay(1); MDO=1;void iicnack() MDE=1; /MCO=0; MDO=1; iicdelay(2); MCO=1; iicdelay(2); MCO=0; bit iicreadack() bit ack; MCO = 0; iicdelay(2); MDE=1; MDO=1; MCO=1; iicdelay(2); MDE=0; ack=MDI; MCO=0; return ack; void iicw
13、riteda(unsigned char da) unsigned char i; MDE=1; for(i=0;i8;i+) MCO=0; iicdelay(0); if(dai)&0x80) MDO=1; else MDO=0; iicdelay(2); MCO=1; iicdelay(3); MCO=0;unsigned char iicreadda() unsigned char da; int i; da=0; MDE=1; MDO=1; MDE=0; for(i=0;i8;i+) MCO=0; iicdelay(3); MCO=1; iicdelay(1); if(MDI=1) d
14、a=da1;da+; else da=da1; MCO=0; return da;void iicdelay(unsigned int t) unsigned int i,j; for(i=0;i0;j-);void ReadTime(unsigned char TIME7) bit EATemp; unsigned char temp; EATemp=EA;iicstart(); iicwriteda(0xd0); iicreadack(); iicwriteda(0x00); iicreadack(); iicstop(); iicdelay(1); iicstart(); iicwrit
15、eda(0xd1); iicreadack(); TIME0=iicreadda(); iicack(); TIME1=iicreadda(); iicack(); TIME2=iicreadda(); iicack(); TIME3=iicreadda(); iicack(); TIME4=iicreadda(); iicack(); TIME5=iicreadda(); iicack(); TIME6=iicreadda(); iicnack(); iicstop(); iicdelay(1); TIME0=(TIME0&0x7f)4)*10+(TIME0&0x0f); /second T
16、IME1=(TIME1&0x7f)4)*10+(TIME1&0x0f); /minute if(TIME2&0x40) if(TIME2&0x20)temp=0x80;else temp=0x40; TIME2=(TIME2&0x1f)4)*10+(TIME2&0x0f); /hour TIME2|=temp; else TIME2=(TIME2&0x3f)4)*10+(TIME2&0x0f); TIME3=TIME3&0x07; /week TIME4=(TIME4&0x3f)4)*10+(TIME4&0x0f); /date TIME5=(TIME5&0x1f)4)*10+(TIME5&0
17、x0f); /month TIME6=(TIME64)*10+(TIME6&0x0f); /year EA=EATemp;void SetTime(unsigned char TIME7) bit EATemp; unsigned char temp=0; unsigned char TimeTemp7; EATemp=EA; for(temp=0;temp7;temp+)TimeTemptemp=TIMEtemp; temp=0; TimeTemp6=(TimeTemp6/10)4)+(TimeTemp6%10); TimeTemp5=(TimeTemp5/10)4)+(TimeTemp5%
18、10); TimeTemp4=(TimeTemp4/10)4)+(TimeTemp4%10); TimeTemp3=(TimeTemp3/10)4)+(TimeTemp3%10); if(TimeTemp2&0xc0)=0x00) TimeTemp2=(TimeTemp2/10)4)+(TimeTemp2%10); else if(TimeTemp2&0xc0)=0x01)temp=0x40;else temp=0x60; TimeTemp2&=0x3f; TimeTemp2=(TimeTemp2/10)4)+(TimeTemp2%10); TimeTemp2|=temp; TimeTemp1
19、=(TimeTemp1/10)4)+(TimeTemp1%10); TimeTemp0=(TimeTemp0/10)4)+(TimeTemp0%10); TimeTemp0&=0x7f; iicstart(); iicwriteda(0xd0); iicreadack(); iicwriteda(0x00); iicreadack(); iicwriteda(TimeTemp0); iicreadack(); iicwriteda(TimeTemp1); iicreadack(); iicwriteda(TimeTemp2); iicreadack(); iicwriteda(TimeTemp
20、3); iicreadack(); iicwriteda(TimeTemp4); iicreadack(); iicwriteda(TimeTemp5); iicreadack(); iicwriteda(TimeTemp6); iicreadack(); iicstop(); iicdelay(1); EA=EATemp; void SwitchRTC(unsigned char SWITCH) unsigned char temp; bit EATemp; EATemp=EA; iicstart(); iicwriteda(0xd0); iicreadack(); iicwriteda(0
21、x00); iicreadack(); iicstop(); /iicdelay(1); iicstart(); iicwriteda(0xd1); iicreadack(); temp=iicreadda(); iicnack(); iicstop(); /iicdelay(1); if(SWITCH)temp&=0x7f; else temp|=0x80; iicstart(); iicwriteda(0xd0); iicreadack(); iicwriteda(0x00); iicreadack(); iicwriteda(temp); iicreadack(); iicstop();
22、 /iicdelay(1);EA=EATemp;(3) 主体程序#include#include #include#includesbit lie0=P23;sbit lie1=P22;sbit lie2=P21;sbit lie3=P20;sbit PWM1=P26;unsigned char temp;unsigned char key;unsigned char STH0,STL0;unsigned char jishi=0;unsigned char n=0; /n为节拍常数变量 unsigned char a7=00,59,19,3,4,8,10;unsigned char b7;
23、void delay1s(void)unsigned char i,k;for(k=100;k0;k-)for(i=100;i0;i-);/*定时器0中断*/void TIMER0(void) interrupt 1 TH0=STH0; TL0=STL0; PWM1=PWM1; /*定时器1中断*/void int0() interrupt 3 /采用中断3 控制节拍 节拍延时 TH1=0xd8; TL1=0xef; n-; /*定时器2中断*/void int1() interrupt 5 /采用中断3 控制节拍 节拍延时 TH2=0xd8; TL2=0xef; n-; void delay
24、 (unsigned char m) /控制频率延时 unsigned i=3*m; while(-i); void delayms(unsigned char a) /豪秒延时子程序 while(-a); /采用while(-a) 不要采用while(a-); 各位可编译一下看看汇编结果就知道了! unsigned int code tab=0xfc43,0xfd08,0,0,0xfd08,0xfd32,0,0,0xfd81,0xfdc7,0,0,0xfe05,0xfe21,0,0,;/*unsigned char code table=0x03,0x9f,0x25,0x0d,0x99,0x
25、49,0x41,0x1f,0x01,0x09,0x11,0xc1,0x63,0x85,0x61,0x71,0xff; */unsigned char code music_tab_1= /*1*/0x20,0x20,0x20,0x10,0x20,0x10,0x20,0x20,0x20,0x20,0x20,0x20,0x1c,0x20,0x20,0x20,0x24,0x20,0x26,0x20,0x1c,0x20,0x19,0x20,0x18,0x10,0x19,0x10,0x20,0x30,0x01,0x20,0x20,0x10,0x20,0x10,0x1c,0x20,0x30,0x20,0x
26、30,0x20,0x2b,0x20,0x26,0x20,0x20,0x20,0x01,0x20,0x20,0x20,0x24,0x20,0x20,0x20,0x26,0x10,0x2b,0x20,0x30,0x10,0x2b,0x60,/*2*/0x20,0x20,0x20,0x10,0x20,0x10,0x20,0x20,0x20,0x20,0x20,0x20,0x1c,0x20,0x20,0x20,0x24,0x20,0x26,0x20,0x1c,0x20,0x19,0x20,0x18,0x10,0x19,0x10,0x20,0x30,0x01,0x20,0x20,0x10,0x20,0x
27、10,0x1c,0x20,0x30,0x20,0x30,0x20,0x2b,0x20,0x26,0x20,0x20,0x20,0x01,0x20,0x20,0x20,0x24,0x20,0x20,0x20,0x26,0x10,0x2b,0x20,0x30,0x10,0x2b,0x60,/11767112377550x30,0x40,0x18,0x20,0x19,0x20,0x1c,0x20,0x19,0x10,0x18,0x10,0x18,0x20,0x2b,0x20,0x13,0x20,0x19,0x20,0x19,0x20,0x20,0x10,0x20,0x10,/632211170x1c
28、,0x20,0x13,0x10,0x15,0x10,0x15,0x20,0x18,0x20,0x18,0x40,0x18,0x20,0x19,0x20,/67112377550x1c,0x20,0x19,0x10,0x18,0x10,0x18,0x20,0x15,0x20,0x13,0x20,0x19,0x20,0x19,0x20,0x20,0x10,0x20,0x10,/6322110x1c,0x20,0x13,0x10,0x15,0x10,0x15,0x20,0x18,0x20,0x18,0x60,/000000x01,0x40,0x01,0x40,0x01,0x40,0x01,0x40,
29、0x01,0x60,/1155555 5777550x18,0x10,0x18,0x10,0x10,0x20,0x10,0x10,0x10,0x10,0x10,0x20,0x10,0x20,0x10,0x20,0x19,0x10,0x19,0x10,0x19,0x20,0x20,0x10,0x20,0x10,/555565111120x10,0x20,0x10,0x10,0x10,0x10,0x10,0x20,0x0e,0x20,0x10,0x20,0x18,0x10,0x18,0x10,0x18,0x20,0x18,0x10,0x15,0x10, /32443324430x13,0x20,0
30、x15,0x10,0x12,0x10,0x12,0x20,0x13,0x20,0x13,0x20,0x15,0x10,0x12,0x10,0x12,0x20,0x13,0x20,/324430x13,0x20,0x15,0x10,0x12,0x10,0x12,0x20,0x13,0x20,/55110x10,0x40,0x10,0x20,0x18,0x10,0x18,0x10,/555555777550x10,0x20,0x10,0x10,0x10,0x10,0x10,0x20,0x10,0x20,0x10,0x20,0x19,0x10,0x19,0x10,0x19,0x20,0x20,0x1
31、0,0x20,0x10,/555565111120x10,0x20,0x10,0x10,0x10,0x10,0x10,0x20,0x0e,0x20,0x10,0x20,0x18,0x10,0x18,0x10,0x18,0x20,0x18,0x10,0x15,0x10,/32443321710x13,0x20,0x15,0x10,0x12,0x10,0x12,0x20,0x13,0x20,0x13,0x20,0x15,0x10,0x18,0x10,0x18,0x20,0x19,0x10,0x18,0x60,0x01,0x40,0x01,0x40,/第二次 /*3*/0x20,0x20,0x20,
32、0x10,0x20,0x10,0x20,0x20,0x20,0x20,0x20,0x20,0x1c,0x20,0x20,0x20,0x24,0x20,0x26,0x20,0x1c,0x20,0x19,0x20,0x18,0x10,0x19,0x10,0x20,0x30,0x01,0x20,0x20,0x10,0x20,0x10,0x1c,0x20,0x30,0x20,0x30,0x20,0x2b,0x20,0x26,0x20,0x20,0x20,0x01,0x20,0x20,0x20,0x24,0x20,0x20,0x20,0x26,0x10,0x2b,0x20,0x30,0x10,0x2b,
33、0x60,/*4*/0x20,0x20,0x20,0x10,0x20,0x10,0x20,0x20,0x20,0x20,0x20,0x20,0x1c,0x20,0x20,0x20,0x24,0x20,0x26,0x20,0x1c,0x20,0x19,0x20,0x18,0x10,0x19,0x10,0x20,0x30,0x01,0x20,0x20,0x10,0x20,0x10,0x1c,0x20,0x30,0x20,0x30,0x20,0x2b,0x20,0x26,0x20,0x20,0x20,0x01,0x20,0x20,0x20,0x24,0x20,0x20,0x20,0x26,0x10,
34、0x2b,0x20,0x30,0x10,0x2b,0x60,/11767112377550x30,0x40,0x18,0x20,0x19,0x20,0x1c,0x20,0x19,0x10,0x18,0x10,0x18,0x20,0x2b,0x20,0x13,0x20,0x19,0x20,0x19,0x20,0x20,0x10,0x20,0x10,/632211170x1c,0x20,0x13,0x10,0x15,0x10,0x15,0x20,0x18,0x20,0x18,0x40,0x18,0x20,0x19,0x20,/67112377550x1c,0x20,0x19,0x10,0x18,0
35、x10,0x18,0x20,0x15,0x20,0x13,0x20,0x19,0x20,0x19,0x20,0x20,0x10,0x20,0x10,/6322110x1c,0x20,0x13,0x10,0x15,0x10,0x15,0x20,0x18,0x20,0x18,0x60,/335350x01,0x40,0x01,0x40,0x01,0x40,0x01,0x40,0x01,0x60,/1155555 5777550x18,0x10,0x18,0x10,0x10,0x20,0x10,0x10,0x10,0x10,0x10,0x20,0x10,0x20,0x10,0x20,0x19,0x1
36、0,0x19,0x10,0x19,0x20,0x20,0x10,0x20,0x10,/555565111120x10,0x20,0x10,0x10,0x10,0x10,0x10,0x20,0x0e,0x20,0x10,0x20,0x18,0x10,0x18,0x10,0x18,0x20,0x18,0x10,0x15,0x10, /32443324430x13,0x20,0x15,0x10,0x12,0x10,0x12,0x20,0x13,0x20,0x13,0x20,0x15,0x10,0x12,0x10,0x12,0x20,0x13,0x20,/324430x13,0x20,0x15,0x1
37、0,0x12,0x10,0x12,0x20,0x13,0x20,/55110x10,0x40,0x10,0x20,0x18,0x10,0x18,0x10,/555555777550x10,0x20,0x10,0x10,0x10,0x10,0x10,0x20,0x10,0x20,0x10,0x20,0x19,0x10,0x19,0x10,0x19,0x20,0x20,0x10,0x20,0x10,/555565111120x10,0x20,0x10,0x10,0x10,0x10,0x10,0x20,0x0e,0x20,0x10,0x20,0x18,0x10,0x18,0x10,0x18,0x20,0x18,0x10,0x15,0x10,/32443321710x13,0x20,0x15,0x10,0x12,0x10,0x12,0x20,0x13,0x20,0x13,0x20,0x15,0x10,0x18,0x10,0x18,0x20,0x19,0x10,0x18,0x60,0x01,0x40,0x01,0x40,/结尾/3217121334233420x13,0x20,0x15,0x20,0x18,0x30,0x19,0x10,0x18,0x10,0x15,0x10,0x18,0x40,0x13,0x40,0x13,0x40,0x1
链接地址:https://www.31ppt.com/p-4147576.html