单片机课程设计报告电子音调发生器
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
单片机课程设计
实验报告
电子音调发生器
:
日期:2009年6月29日星期一
目录
〔一〕实验目的0
〔二〕设计任务及要求1
〔三〕工作原理及设计思路1
〔四〕实现功能设计2
〔五〕软件设计3
〔六〕电路设计58
〔七〕调试59
〔八〕实验体会60
〔一〕实验目的
1.了解计算机发声原理。
2.熟悉定时器和键盘扫描电路的工作原理及编程方法。
〔二〕设计任务及要求
利用实验平台上的开关k0-k7和蜂鸣器设计电子音调发生器,要求:
1.利用实验平台上开关k0-k7进展音调选择,即拨动不同的开关产生不同的音调,依次拨动K0~K7,蜂鸣器发出1234567i八个音调。
2.编写2支歌曲,并可进展选择播放。
〔三〕工作原理及设计思路
音节由不同频率的方波产生,音节与频率的关系如表1所示。
要产生音频方波,只要算出*一音频的周期〔1/频率〕,然后将此周期除以2,即为半周期的时间。
利用计时器计时此半周期时间,每当计时到后就将输出方波的I/0反相,然后重复计时此半周期时间再对I/O反相,就可在I/O脚得到此频率的方波。
在ZKS-03实验仪上,产生方波的I/O脚选用P1.7,通过跳线选择器JP1将单片机的P1.7与蜂鸣器的驱动电路相连。
这样P1.7输出不同频率的方波,蜂鸣器便会发出不同的声音。
另外,音乐的节拍是由延时实现的,如果1拍为0.4秒,1/4拍是0.1秒。
只要设定延时时间,就可求得节拍的时间。
延时作为根本延时时间,节拍值只能是它的整数倍,
每个音节相应的定时器初值*可按下法计算:
〔1/2〕*(1/f)=(12/fose)*(216-*)
即*=216-(fose/24f)
其中f:音调频率,当晶振fose=11.0592MHz时,音节“1〞相应的定时器初值为*,则可得*=63777D=F921H 其它的可同样求得。
表1
〔四〕实现功能设计
实现两个主要功能:电子琴与音乐播放;一个辅助功能:在液晶上显示相关信息。
1、电子琴:4*4的矩阵16个按键,按键0实现两个功能的切换,其余按键分别发出音调低音5、6、7,中音1、
2、
3、
4、
5、
6、7,高音1、2、3、4、5。
发音的同时在液晶显示屏上显示出对应的音调。
2、音乐播放:按键0实现两个功能的切换,按键1实现播放类型选择〔单曲循环播放、所有循环播放〕,其余按键分别对应一首歌。
目前已经有9首歌。
播放的同时,在液晶上显示出歌名。
〔五〕软件设计
根据要实现的功能,流程图如下:
*include <AT89*52.H>
*include <intrins.h>
*define BEEP P3_3
/***************液晶显示相关变量***********************************/
sbit LCD_RS = P2^7;
sbit LCD_RW = P2^6;
sbit LCD_EN = P2^5;
static uchar th0_f; //在中断中装载的T0 的值高8 位
static uchar tl0_f; //在中断中装载的T0 的值低8 位
static timebuffer,Record_counter,Record_flag=0;
static uchar key2,key=16,scounter=0,Record_time;
static uchar s_musicCounter=0*00,s_tableCounter;
bit Rplay_flag=0;
bit K0_flag=0;
bit Music_flag0;
bit Music_flag=0;
bit Freq_flag=0;
bit disptime_flag=0;
bit musicAutoFlag=0;
//sbit BEEP = P3_3; //喇叭输出脚//延时子程序1
void delay(int ms)
{
int i;
while(ms--)
{
for(i = 0; i< 250; i++)
{
_nop_();
_nop_();
_nop_();
_nop_();
}
}
}
/*******************************************************************/
/*
*/
/*检查LCD忙状态*/
/*lcd_busy为1时,忙,等待。
lcd-busy为0时,闲,可写指令与数据。
*/
/*
*/
/*******************************************************************/
bit lcd_busy()
{
bit result;
LCD_RS = 0;
LCD_RW = 1;
LCD_EN = 1;
_nop_();
_nop_();
_nop_();
_nop_();
result = (bit)(P0&0*80);
LCD_EN = 0;
return result;
}
/*******************************************************************/
/*
*/
/*写指令数据到LCD */
/*RS=L,RW=L,E=高脉冲,D0-D7=指令码。
*/
/*
*/
/*******************************************************************/
void lcd_wcmd(uchar cmd)
{
while(lcd_busy());
LCD_RS = 0;
LCD_RW = 0;
LCD_EN = 0;
_nop_();
_nop_();
P0 = cmd;
_nop_();
_nop_();
_nop_();
_nop_();
LCD_EN = 1;
_nop_();
_nop_();
_nop_();
_nop_();
LCD_EN = 0;
}
/*******************************************************************/
/*
*/
/*写显示数据到LCD */
/*RS=H,RW=L,E=高脉冲,D0-D7=数据。
*/
/*
*/
/*******************************************************************/
void lcd_wdat(uchar dat)
{
while(lcd_busy());
LCD_RS = 1;
LCD_RW = 0;
LCD_EN = 0;
P0 = dat;
_nop_();
_nop_();
_nop_();
_nop_();
LCD_EN = 1;
_nop_();
_nop_();
_nop_();
_nop_();
LCD_EN = 0;
}
/*******************************************************************/
/*
*/
/* 设定显示位置*/
/*
*/
/*******************************************************************/
void lcd_pos(uchar pos)
{
lcd_wcmd(pos|0*80); //数据指针=80+地址变量
}
/*******************************************************************/
/*
*/
/* LCD初始化设定*/
/*
*/
/*******************************************************************/
void lcd_init()
{
delay(15); //等待LCD电源稳定
lcd_wcmd(0*38); //16*2显示,5*7点阵,8位数据
delay(5);
lcd_wcmd(0*38);
delay(5);
lcd_wcmd(0*38);
delay(5);
lcd_wcmd(0*0c); //显示开,关光标
delay(5);
lcd_wcmd(0*06); //移动光标
delay(5);
lcd_wcmd(0*01); //去除LCD的显示容
delay(5);
}
/*******************************************************************/
/*
*/
/* 清屏子程序*/
/*
*/
/*******************************************************************/
void lcd_clr()
{
lcd_wcmd(0*01); //去除LCD的显示容
delay(5);
}
//静态显示字符串
void lcd_string(char *strpoint) /*在当前显示位置显示LCD 字符串*/ { register i=0;
while(strpoint[i]!=0){
lcd_wdat(strpoint[i]);
i++;
}
}
/*************************************************************************/ /*Constant Tables*/
const char code c_music1[]={0*82,0*01,0*81,0*94,0*84, /*生日快乐歌*/
0*b4,0*a4,0*04,
0*82,0*01,0*81,0*94,0*84,
0*c4,0*b4,0*04,
0*82,0*01,0*81,0*f4,0*d4,
0*b4,0*a4,0*94,
0*e2,0*01,0*e1,0*d4,0*b4,
0*c4,0*b4,0*04,
0*00};
const char code c_music2[]={0*62, 0*52, /*两只蝴蝶*/
0*64, 0*64, 0*02, 0*52, 0*62, 0*52,
0*44, 0*44, 0*04, 0*22, 0*42,
0*54, 0*52, 0*62, 0*52, 0*42, 0*22, 0*42,
0*14, 0*14, 0*04, 0*62, 0*52,
0*64, 0*64, 0*02, 0*52, 0*62, 0*52,
0*44, 0*44, 0*04, 0*22, 0*42,
0*54, 0*52, 0*62, 0*52, 0*42, 0*22, 0*42,
0*54, 0*54, 0*04, 0*62, 0*82,
0*84, 0*84, 0*02, 0*82, 0*92, 0*82,
0*64, 0*64, 0*04, 0*52, 0*52,
0*54, 0*52, 0*62, 0*52, 0*42, 0*22, 0*91, 0*41,
0*42, 0*42, 0*44, 0*44, 0*44,
0*02, 0*82, 0*82, 0*92,
0*b2, 0*a2, 0*a2, 0*92, 0*64, 0*52, 0*62,
0*64, 0*64, 0*02, 0*62, 0*62, 0*82,
0*94, 0*94, 0*02, 0*22, 0*62, 0*52,
0*54, 0*54, 0*04, 0*62, 0*82,
0*82, 0*62, 0*84, 0*02, 0*b2, 0*b2, 0*a2,
0*92, 0*a2, 0*64, 0*02, 0*92, 0*92, 0*a2,
0*92, 0*82, 0*62, 0*52, 0*52, 0*62, 0*52, 0*62,
0*84, 0*84, 0*02, 0*82, 0*82, 0*92,
0*92, 0*82, 0*62, 0*52, 0*52, 0*12, 0*12, 0*22,
0*44, 0*44, 0*44, 0*44,
0*00};
const char code c_music3[]={0*04, 0*42, 0*41, 0*51,
0*62, 0*62, 0*52, 0*41, 0*51, 0*54, 0*02, 0*41, 0*51, /*老鼠爱大米*/
0*62, 0*62, 0*52, 0*42, 0*44, 0*02, 0*11, 0*21,
0*14, 0*11, 0*11, 0*21, 0*41, 0*44, 0*02, 0*42,
0*42, 0*42, 0*42, 0*41, 0*51, 0*54, 0*42, 0*51, 0*61,
0*62, 0*62, 0*82, 0*91, 0*51, 0*54, 0*62, 0*51, 0*41,
0*42, 0*42, 0*52, 0*61, 0*81, 0*84, 0*02, 0*81, 0*81,
0*92, 0*41, 0*41, 0*42, 0*61, 0*01, 0*52, 0*42, 0*42, 0*51, 0*41,
0*44, 0*44, 0*44, 0*62, 0*61, 0*81,
0*82, 0*81, 0*81, 0*82, 0*81, 0*91, 0*94, 0*62, 0*52,
0*42, 0*42, 0*42, 0*51, 0*61, 0*64, 0*02,
0*82, 0*82, 0*82, 0*91, 0*b1, 0*b2, 0*92, 0*82, 0*62,
0*52, 0*42, 0*42, 0*22, 0*54, 0*02, 0*61, 0*81,
0*82, 0*82, 0*81, 0*92, 0*81, 0*94, 0*62, 0*52,
0*42, 0*42, 0*42, 0*51, 0*61, 0*64, 0*02, 0*61, 0*81,
0*82, 0*82, 0*82, 0*91, 0*b1, 0*b2, 0*92, 0*82, 0*61, 0*51,
0*52, 0*52, 0*54, 0*54, 0*41, 0*62, 0*71,
0*62, 0*52, 0*54, 0*54, 0*62, 0*51, 0*41,
0*44, 0*62, 0*51, 0*41, 0*44, 0*62, 0*52,
0*62, 0*42, 0*62, 0*91, 0*81, 0*84, 0*02, 0*61, 0*81,
0*92, 0*91, 0*81, 0*82, 0*91, 0*81, 0*84, 0*61, 0*51, 0*42,
0*52, 0*51, 0*61, 0*52, 0*41, 0*51, 0*41, 0*53, 0*62, 0*51, 0*41,
0*44, 0*62, 0*51, 0*41, 0*44, 0*62, 0*52,
0*62, 0*42, 0*61, 0*92, 0*81, 0*84, 0*02,
0*92, 0*92, 0*b2, 0*81, 0*91, 0*82, 0*01, 0*81, 0*61, 0*51, 0*41,
0*54, 0*51, 0*41, 0*51, 0*61, 0*54, 0*62, 0*62,
0*54, 0*02, 0*42, 0*44, 0*44,
0*00};
const char code c_music4[]={0*04, 0*02, 0*41, 0*51, 0*62, 0*61, 0*61, 0*62, 0*52, /*梦醒时分*/
0*62, 0*81, 0*61, 0*62, 0*61, 0*81, 0*92, 0*92, 0*92, 0*a2,
0*82, 0*62, 0*62, 0*62, 0*61, 0*51, 0*42, 0*41, 0*41, 0*42, 0*22,
0*62, 0*81, 0*41, 0*44, 0*51, 0*52, 0*01, 0*52, 0*42,
0*81, 0*61, 0*52, 0*52, 0*41, 0*51, 0*62, 0*61, 0*61, 0*62, 0*52,
0*62, 0*81, 0*61, 0*62, 0*61, 0*81, 0*92, 0*91, 0*91, 0*92, 0*a2,
0*82, 0*62, 0*61, 0*81, 0*81, 0*81, 0*92, 0*b2, 0*b2, 0*92,
0*72, 0*52, 0*52, 0*81, 0*81, 0*92, 0*b2,
0*b2, 0*c2, 0*c4, 0*c4, 0*01, 0*81, 0*81, 0*81,
0*d2, 0*d2, 0*d2, 0*d2, 0*d2, 0*c1, 0*c1, 0*c2, 0*b1, 0*a1,
0*b2, 0*b2, 0*b2, 0*c2, 0*62, 0*82, 0*82, 0*81, 0*81,
0*92, 0*b1, 0*b1, 0*b2, 0*b2, 0*c2, 0*91, 0*b1, 0*b2, 0*b1, 0*b1,
0*c2, 0*c1, 0*c1, 0*c2, 0*b2, 0*d2, 0*c1, 0*81, 0*81, 0*81, 0*81, 0*81,
0*d2, 0*d2, 0*d2, 0*f2, 0*d2, 0*c1, 0*c1, 0*c2, 0*b1, 0*a1,
0*b2, 0*b2, 0*b2, 0*c2, 0*62, 0*82, 0*82, 0*81, 0*81,
0*92, 0*b1, 0*b1, 0*b2, 0*b1, 0*b1, 0*c2, 0*91, 0*b1, 0*b2, 0*b1, 0*b1,
0*c2, 0*c1, 0*c1, 0*c2, 0*b2, 0*d4, 0*c2, 0*01, 0*b1,
0*b4, 0*b4, 0*b4, 0*b4,
0*00};
const char code c_music5[]={0*54, 0*64, 0*64, /*大长今*/
0*64, 0*02, 0*52, 0*44,
0*24, 0*44, 0*44,
0*51, 0*43, 0*44, 0*44,
0*54, 0*64, 0*64,
0*64, 0*02, 0*82, 0*64,
0*64, 0*54, 0*64,
0*64, 0*64, 0*64,
0*84, 0*94, 0*94,
0*94, 0*84, 0*64,
0*64, 0*84, 0*94,
0*81, 0*91, 0*82, 0*84, 0*84, 0*54, 0*64, 0*64,
0*54, 0*02, 0*62, 0*64,
0*54, 0*64, 0*24,
0*41, 0*23, 0*94, 0*94,
0*24, 0*24, 0*24,
0*54, 0*64, 0*64,
0*64, 0*02, 0*52, 0*44,
0*24, 0*44, 0*44,
0*51, 0*43, 0*44, 0*44,
0*54, 0*64, 0*64,
0*64, 0*02, 0*82, 0*64,
0*64, 0*54, 0*64,
0*64, 0*64, 0*64,
0*84, 0*94, 0*94,
0*94, 0*02, 0*82, 0*64,
0*64, 0*84, 0*94,
0*84, 0*84, 0*84,
0*54, 0*64, 0*64,
0*54, 0*02, 0*62, 0*64,
0*54, 0*64, 0*24,
0*41, 0*23, 0*24, 0*24,
0*24, 0*24, 0*24,
0*54, 0*02, 0*42, 0*24,
0*54, 0*02, 0*42, 0*24,
0*54, 0*64, 0*44,
0*54, 0*02, 0*62, 0*84,
0*94, 0*02, 0*82, 0*64,
0*54, 0*02, 0*42, 0*24,
0*24, 0*14, 0*24,
0*41, 0*23, 0*24, 0*24,
0*24, 0*24, 0*24,
0*14, 0*14, 0*14,
0*14, 0*02, 0*22, 0*42, 0*52,
0*62, 0*52, 0*44, 0*02, 0*22,
0*44, 0*44, 0*54,
0*64, 0*64, 0*64,
0*64, 0*64, 0*84,
0*94, 0*02, 0*b2, 0*94,
0*84, 0*84, 0*84,
0*84, 0*61, 0*81, 0*92, 0*04,
0*92, 0*82, 0*64, 0*02, 0*52,
0*64, 0*02, 0*52, 0*62, 0*52,
0*44, 0*44, 0*44,
0*41, 0*23, 0*24, 0*44,
0*54, 0*54, 0*54,
0*54, 0*64, 0*64,
0*44, 0*41, 0*23, 0*24,
0*24, 0*24, 0*24,
0*00};
const char code c_music6[]={0*24, 0*42, 0*52, 0*64, 0*62, 0*92, /*漫步人生路*/
0*84, 0*62, 0*52, 0*64, 0*02, 0*62,
0*72, 0*62, 0*62, 0*52, 0*52, 0*42, 0*41, 0*31, 0*22,
0*44, 0*02, 0*52, 0*34, 0*34,
0*24, 0*42, 0*52, 0*64, 0*62, 0*92,
0*a4, 0*82, 0*62, 0*64, 0*02, 0*62,
0*72, 0*62, 0*52, 0*42, 0*32, 0*42, 0*52, 0*62,
0*42, 0*32, 0*22, 0*12, 0*24, 0*24,
0*42, 0*32, 0*42, 0*52, 0*64, 0*52, 0*62,
0*74, 0*74, 0*72, 0*81, 0*71, 0*62, 0*52,
0*64, 0*74, 0*84, 0*02, 0*62,
0*94, 0*92, 0*82, 0*94, 0*92, 0*b2,
0*92, 0*82, 0*82, 0*92, 0*64, 0*52, 0*42,
0*54, 0*52, 0*42, 0*54, 0*42, 0*42, 0*82,
0*62, 0*62, 0*52, 0*42, 0*34, 0*62, 0*62,
0*94, 0*92, 0*82, 0*94, 0*92, 0*b2,
0*92, 0*82, 0*82, 0*92, 0*64, 0*52, 0*42,
0*54, 0*42, 0*82, 0*64, 0*52, 0*42,
0*52, 0*61, 0*51, 0*42, 0*32, 0*24, 0*24,
0*00};
const char code c_music7[]={0*02, 0*12, 0*12, 0*22, /*最浪漫的事*/
0*44, 0*42, 0*42, 0*42, 0*22, 0*22, 0*62,
0*64, 0*64, 0*02, 0*12, 0*24,
0*42, 0*42, 0*42, 0*42, 0*44, 0*62, 0*82,
0*84, 0*84, 0*84, 0*02, 0*82,
0*92, 0*82, 0*92, 0*82, 0*94, 0*82, 0*51, 0*61,
0*62, 0*51, 0*41, 0*44, 0*02, 0*42, 0*42, 0*22,
0*44, 0*42, 0*22, 0*42, 0*64, 0*52,
0*54, 0*54, 0*54, 0*54,
0*32, 0*12, 0*22, 0*42,
0*42, 0*42, 0*42, 0*42, 0*42, 0*42, 0*22, 0*52,
0*52, 0*62, 0*64, 0*62, 0*12, 0*22, 0*42,
0*42, 0*42, 0*42, 0*42, 0*44, 0*62, 0*82,
0*84, 0*84, 0*84, 0*82, 0*82,
0*92, 0*82, 0*92, 0*82, 0*94, 0*92, 0*82,
0*52, 0*62, 0*52, 0*44, 0*22, 0*22, 0*42,
0*14, 0*62, 0*52, 0*44, 0*22, 0*22,
0*41, 0*51, 0*42, 0*44, 0*44, 0*44,
0*82, 0*92, 0*b2,
0*94, 0*92, 0*82, 0*92, 0*82, 0*62, 0*82,
0*84, 0*84, 0*02, 0*82, 0*92, 0*b2,
0*94, 0*92, 0*82, 0*92, 0*82, 0*92, 0*42,
0*44, 0*44, 0*02, 0*42, 0*52, 0*62,
0*52, 0*52, 0*52, 0*42, 0*52, 0*42, 0*22,
0*62,
0*62, 0*52, 0*54, 0*02, 0*82, 0*92, 0*62,
0*84, 0*82, 0*62, 0*82, 0*62, 0*64,
0*92, 0*82, 0*92, 0*82, 0*62, 0*82, 0*81, 0*92, 0*b1,
0*94, 0*92, 0*82, 0*92, 0*82, 0*62, 0*82,
0*84, 0*84, 0*02, 0*82, 0*92, 0*b2,
0*94, 0*92, 0*82, 0*92, 0*82, 0*92, 0*42,
0*44, 0*44, 0*02, 0*42, 0*52, 0*62,
0*74, 0*72, 0*82, 0*92, 0*92, 0*82, 0*92,
0*92, 0*b2, 0*92, 0*92, 0*92, 0*b2, 0*92, 0*82,
0*84, 0*84, 0*82, 0*42, 0*92, 0*82,
0*84, 0*84, 0*82, 0*92, 0*b2, 0*01, 0*61,
0*52, 0*61, 0*51, 0*44, 0*44, 0*44,
0*00};
const char code c_music8[]={0*b2, 0*41, 0*51, 0*62, 0*81, 0*91, 0*84, 0*84, /*1000年以后*/
0*72, 0*61, 0*41, 0*42, 0*61, 0*51, 0*54, 0*54,
0*b2, 0*41, 0*51, 0*62, 0*92, 0*a1, 0*91, 0*82, 0*82, 0*61, 0*91,
0*94, 0*94, 0*a1, 0*a1, 0*a1, 0*b1, 0*c1, 0*b1, 0*a2,
0*b2, 0*a1, 0*b1, 0*b1, 0*d2, 0*01, 0*a2, 0*91, 0*a1, 0*a1, 0*81, 0*82,
0*91, 0*91, 0*a1, 0*b1, 0*c1, 0*b1, 0*c1, 0*e1, 0*d1, 0*c1, 0*c1, 0*c1, 0*c2, 0*c1, 0*b1,
0*b2, 0*a1, 0*b1, 0*b1, 0*d2, 0*01, 0*a2, 0*91, 0*a1, 0*a1, 0*81, 0*82,
0*92, 0*b4, 0*02, 0*b1, 0*b1, 0*b1, 0*c1, 0*d2, 0*c1, 0*b1,
0*b2, 0*c2, 0*c4, 0*02, 0*a1, 0*b1, 0*b1, 0*c2, 0*01,
0*d2, 0*c1, 0*b1, 0*b1, 0*82, 0*01, 0*c4, 0*02, 0*d1, 0*c1,
0*b2, 0*a1, 0*91, 0*92, 0*62, 0*a4, 0*02, 0*b1, 0*a1,
0*92, 0*a1, 0*b1, 0*b1, 0*c2, 0*01, 0*82, 0*c1, 0*d1, 0*d2, 0*01, 0*d1,
0*e2, 0*d2, 0*b1, 0*c2, 0*c1, 0*c2, 0*01, 0*a1, 0*b1, 0*c2, 0*01,
0*d2, 0*c1, 0*b1, 0*b1, 0*82, 0*01, 0*c4, 0*c1, 0*c1, 0*d1, 0*c1,
0*b2, 0*a2, 0*91, 0*c2, 0*01, 0*a4, 0*02, 0*b1, 0*a1,
0*92, 0*81, 0*91, 0*91, 0*b1, 0*82, 0*82, 0*c1, 0*b1, 0*b2, 0*d1, 0*d1,
0*e1, 0*d1, 0*c4, 0*b1, 0*c1, 0*d2, 0*c1, 0*b1, 0*d2, 0*c1, 0*81,
0*82, 0*b2, 0*b4, 0*b4, 0*b4,
0*00};
const char code c_music9[]={0*02, 0*11, 0*11, 0*62, 0*52, 0*52, 0*41, 0*61, /*一辈子的孤单*/
0*62, 0*41, 0*51, 0*54, 0*54, 0*04,
0*02, 0*11, 0*11, 0*61, 0*52, 0*51, 0*52, 0*42,
0*52, 0*62, 0*64, 0*64, 0*04,
0*02, 0*11, 0*11, 0*62, 0*52, 0*52, 0*41, 0*61,
0*62, 0*41, 0*51, 0*54, 0*54, 0*04,
0*02, 0*51, 0*41, 0*62, 0*52, 0*42, 0*52,
0*42, 0*42, 0*44, 0*44, 0*44,
0*04, 0*62, 0*52, 0*52, 0*42,
0*42, 0*52, 0*54, 0*54, 0*54,
0*04, 0*51, 0*63, 0*54, 0*44,
0*41, 0*11, 0*21, 0*11, 0*14, 0*14, 0*14,
0*04, 0*62, 0*52, 0*52, 0*42,
0*42, 0*52, 0*54, 0*54, 0*54,
0*02, 0*12, 0*62, 0*54, 0*42,
0*44, 0*02, 0*62, 0*64, 0*64,
0*02, 0*12, 0*62, 0*84, 0*92,
0*62, 0*52, 0*54, 0*54, 0*54,
0*02, 0*12, 0*62, 0*54, 0*42,
0*54, 0*02, 0*62, 0*64, 0*64,
0*02, 0*12, 0*62, 0*52, 0*52, 0*42,
0*51, 0*62, 0*51, 0*54, 0*54, 0*04,
0*02, 0*51, 0*41, 0*62, 0*52, 0*42, 0*52, 0*42, 0*42, 0*44, 0*44, 0*44,
0*02, 0*12, 0*62, 0*52, 0*52, 0*01, 0*41, 0*51, 0*62, 0*51, 0*54, 0*54, 0*54,
0*02, 0*12, 0*62, 0*52, 0*52, 0*01, 0*41, 0*52, 0*62, 0*64, 0*64, 0*64,
0*02, 0*12, 0*61, 0*52, 0*01, 0*52, 0*42, 0*51, 0*62, 0*51, 0*54, 0*54, 0*54,
0*02, 0*12, 0*61, 0*52, 0*01, 0*52, 0*42, 0*52, 0*62, 0*64, 0*64, 0*64,
0*02, 0*12, 0*62, 0*82, 0*82, 0*92,
0*62, 0*52, 0*54, 0*54, 0*54,
0*02, 0*12, 0*62, 0*52, 0*52, 0*42,
0*52, 0*62, 0*64, 0*64, 0*64,
0*02, 0*12, 0*62, 0*52, 0*52, 0*42,
0*62, 0*52, 0*54, 0*54, 0*54,
0*02, 0*51, 0*41, 0*62, 0*52, 0*42, 0*52,
0*42, 0*42, 0*44, 0*44, 0*44,
0*00};
const char code c_music0[]={0*00};
const uint code c_musicFreq[]={64260,64400,64524,64580, /*低音5,6,7,中音1*/
64684,64777,64820,64898,
/*中音2,3,4,5*/
64968,65030,65058,65110, /*中音6,7,高音1,2*/
65157,65178,65217}; /*高音3,4,5*/
uchar code key_code[]={0*77,0*7B,0*7D,0*7E,0*B7,0*BB,0*BD,0*BE,0*D7,0*DB,0 *DD,0*DE,0*E7,0*EB,0*ED,0*EE};
static uchar time[]={' ',' ',' ',' ','1','2',':','0','0',':','0','0','\0'};//45:78:10 11 static uchar Record_music[50];
//键盘扫描子程序
uchar keyscan(void)
{
unsigned char hang,lie,keycode;
uchar key1;
char i;
P1=0*f0;
hang=P1;
if((hang&0*f0)!=0*f0) //有键按下.
{
delay(50); //去抖动
hang=P1;
if((hang&0*f0)!=0*f0) //有键按下
{
P1=0*0f;
lie=P1;
keycode=hang|lie; //获得键码
for(i=15;i>=0;i--)
{
if(keycode==key_code[i]) //查找键码
{
key1=i;
return(key1);
.
}
else key1=key;
}
return(key1);
}
}
else
{
P1=0*ff;
key1=16;
return(key1);
}
}
//延时子程序2
void delay2(int ms)
{
int i;
while(ms--)
{
for(i = 0; i< 100; i++)
{
key=keyscan();
.
if(key!=16) break;
}
if(key!=16) break;
}
}
//闪动
void flash()
{int i=2;
while(i--)
{
lcd_wcmd(0*08); //关闭显示delay2(100); //延时if(key!=16) break;
lcd_wcmd(0*0c); //开显示
delay2(100);
if(key!=16) break;
}
}
//动态显示字符串
void lcd_moving(char *dis)
{
while(1)
uchar i,j;
lcd_init(); //初始化LCD
lcd_clr();
lcd_pos(0*10); //设置显示位置为第一行第17列
i = 0;
while(dis[i] != '\0')
{ //显示字符
lcd_wdat(dis[i]);
i++;
}
for(j=0;j<16;j++) //向左移动16格
{
lcd_wcmd(0*18); //字符同时左移一格
delay2(80); //控制移动时间
if(key!=16) break;
}
if(key!=16) break;
delay2(600); //控制停留时间
if(key!=16) break;
flash(); //闪动二次
if(key!=16) break;
}
//时间调整及显示
void disptime()
{
int i;
lcd_init();
lcd_string(" time");
disptime_flag=1;
TR0=0;
P3_3=0;
key=16;
delay(10);
while(1)
{
while(key2==(key=keyscan())) nop;
key2=key;
if(key==14)
{
time[5]++;
if(time[4]<'2')
{
.
if(time[5]>'9')
{
time[5]='0';
time[4]++;
}
}
else if(time[4]=='2')
{
if(time[5]>='4')
{
time[4]='0';
time[5]='0';
}
}
}
if(key==13)
{
time[8]++;
if(time[8]>'9')
{
time[8]='0';
time[7]++;
.
if(time[7]>'5') time[7]='0';
}
}
else if(key==12)
{
time[10]='0';
time[11]='0';
}
if(key==15)
{
lcd_init();
lcd_string(" Music");
Freq_flag=1;
Music_flag=1;
key=16;
s_musicCounter=10;
s_tableCounter=0;
TR0=0;
P3_3=0;
// disptime_flag=0;
break;
}
.
if(key==0)
{
P3_3=0;
Freq_flag=0;
Music_flag=0;
K0_flag=0;
TR0=0;
disptime_flag=0;
break;
}
lcd_pos(0*40); //设置显示位置为第二行第1列
i = 0;
while(time[i] != '\0')
{ //显示字符
lcd_wdat(time[i]);
i++;
}
}
}
//时间正常计数及显示
void timecounter()
{
int i;
if(scounter++==100)//用于时间计数{
scounter=0;
time[11]++;
if(time[11]>'9')
{
time[11]='0';
time[10]++;
if(time[10]>='6')
{
time[10]='0';
time[8]++;
if(time[8]>'9')
{
time[8]='0';
time[7]++;
if(time[7]>='6')
{
time[7]='0';
time[5]++;
if(time[4]<2)
{if(time[5]>'9')
{
time[5]='0';
time[4]++;
}
}
else if(time[4]=='2')
{
if(time[5]>='4')
{
time[4]='0';
time[5]='0';
}
}
}
}
}
}
if(Music_flag==1)
{
lcd_pos(0*50); //设置显示位置为第二行第17列
i = 0;
while(time[i] != '\0')
{ //显示时间
lcd_wdat(time[i]);
i++;
}
}
if(disptime_flag==1)
{
lcd_pos(0*40); //设置显示位置为第二行第1列
i = 0;
while(time[i] != '\0')
{ //显示时间
lcd_wdat(time[i]);
i++;
}
}
}
}
//录音
void Record()
{
if(Record_flag==1)
{
timebuffer=Record_time/PAI_BASE+1;
Record_time=0;
timebuffer=0*0F&timebuffer;
Record_music[Record_counter]=(Record_music[Record_counter]&0 *0F)<<4;
Record_music[Record_counter]=Record_music[Record_counter]|ti mebuffer;
if(Record_music[0]==0) Record_music[0]=1;
if(++Record_counter==49) Record_counter=48;
if(key==16) Record_music[Record_counter]=0;
else Record_music[Record_counter]=key;
}
}
//电子琴
void Electone(void)
{
P2_4=0;
Freq_flag=1;
lcd_init();
disptime_flag=1;
lcd_string(" Electone ");
while(1)
{
while(key2==(key=keyscan())) nop;
key2=key;
switch(key)
{
case 0:Freq_flag=0;K0_flag=1;break;
case 1:
disptime_flag=0;
th0_f=c_musicFreq[key-1]/256;
tl0_f=c_musicFreq[key-1]%256;
TR0 = 1;
lcd_init();
lcd_string(" 5 ................... .");
Record();
break;
case 2:
disptime_flag=0;
th0_f=c_musicFreq[key-1]/256;
tl0_f=c_musicFreq[key-1]%256;
TR0 = 1;
lcd_init();
lcd_string(" 6 ................... .");
Record();
break;
case 3:
disptime_flag=0;
th0_f=c_musicFreq[key-1]/256;
tl0_f=c_musicFreq[key-1]%256;
TR0 = 1;
lcd_init();
lcd_string(" 7 ................... .");
Record();
break;
case 4:
disptime_flag=0;
th0_f=c_musicFreq[key-1]/256;
tl0_f=c_musicFreq[key-1]%256;
TR0 = 1;
lcd_init();
lcd_string(" 1 ");
Record();
break;
case 5:
disptime_flag=0;
th0_f=c_musicFreq[key-1]/256;
tl0_f=c_musicFreq[key-1]%256;
TR0 = 1;
lcd_init();
lcd_string(" 2 ");
Record();
break;
case 6:
disptime_flag=0;
th0_f=c_musicFreq[key-1]/256;
tl0_f=c_musicFreq[key-1]%256;
TR0 = 1;
lcd_init();
lcd_string(" 3 ");
Record();
break;
case 7:
disptime_flag=0;
th0_f=c_musicFreq[key-1]/256;
tl0_f=c_musicFreq[key-1]%256;
TR0 = 1;
lcd_init();
lcd_string(" 4 ");
Record();
break;
case 8:
disptime_flag=0;
th0_f=c_musicFreq[key-1]/256;
tl0_f=c_musicFreq[key-1]%256;
TR0 = 1;
lcd_init();
lcd_string(" 5 ");
Record();
break;
case 9:
disptime_flag=0;
th0_f=c_musicFreq[key-1]/256;
tl0_f=c_musicFreq[key-1]%256;
TR0 = 1;
lcd_init();
lcd_string(" 6 ");
Record();
break;
case 10:
disptime_flag=0;
th0_f=c_musicFreq[key-1]/256;
tl0_f=c_musicFreq[key-1]%256;
TR0 = 1;
lcd_init();
lcd_string(" 7 ");
Record();
break;
case 11:
disptime_flag=0;
th0_f=c_musicFreq[key-1]/256;
tl0_f=c_musicFreq[key-1]%256;
TR0 = 1;
lcd_init();
lcd_string(" . ................... 1");
Record();
break;
case 12:
disptime_flag=0;
th0_f=c_musicFreq[key-1]/256;
tl0_f=c_musicFreq[key-1]%256;
TR0 = 1;
lcd_init();
lcd_string(" . ................... 2");
Record();
break;
case 13:
disptime_flag=0;
th0_f=c_musicFreq[key-1]/256;
tl0_f=c_musicFreq[key-1]%256;
TR0 = 1;
lcd_init();
lcd_string(" . ................... 3");
Record();
break;
case 14:
disptime_flag=0;
th0_f=c_musicFreq[key-1]/256;
tl0_f=c_musicFreq[key-1]%256;
TR0 = 1;
lcd_init();
lcd_string(" . ................... 4");
Record();
break;
case 15:
disptime_flag=0;
P2_4=0;
lcd_init();
Record_flag++;
if(Record_flag==1)
{
Record_counter=0;
Record_time=0;
lcd_string(" Record ");
}
else if(Record_flag==2)
{
lcd_init();
lcd_string(" Record play ");
Record_music[++Record_counter]=0*00;
Music_flag=1;
Rplay_flag=1;
s_tableCounter=0;\
TH0=0;
TL0=0;
TR0=0;
while(key=keyscan()!=15);
}
else if (Record_flag==3)
{
lcd_init();
lcd_string(" Electone ");
Music_flag=0;
Rplay_flag=0;
Record_flag=0;
Freq_flag=1;
}
break;
case 16:
P3_3=0;
TR0=0;
Record();
break;
}
if(key==0) {P3_3=0;break;}
. }
}
void Music(void)
{
P2_4=0;
Freq_flag=1;
lcd_init();
disptime_flag=1;
lcd_string(" Music");
Music_flag=0;
key=16;
delay(10);
s_musicCounter=10;
s_tableCounter=0;
TR0=0;
P3_3=0;
while(1)
{
if(s_musicCounter==1)
{
Music_flag=1;
disptime_flag=0;
lcd_moving(" Happy Birthday ");
// lcd_string(" Happy Birthday ");
}
else if(s_musicCounter==2)
{
Music_flag=1;
disptime_flag=0;
lcd_init();
lcd_moving(" Two Butterflies ");
}
else if(s_musicCounter==3)
{
Music_flag=1;
disptime_flag=0;
lcd_init();
lcd_moving("Mouse Loves Rice ");
}
else if(s_musicCounter==4)
{
Music_flag=1;
disptime_flag=0;
lcd_moving(" Awakening ");
}
else if(s_musicCounter==5)
{
Music_flag=1;
disptime_flag=0;
lcd_init();
lcd_moving(" dae jang geum ");
}
else if(s_musicCounter==6)
{
Music_flag=1;
disptime_flag=0;
lcd_init();
lcd_moving("walking on Road of Life ");
}
else if(s_musicCounter==7)
{
Music_flag=1;
disptime_flag=0;
lcd_init();。