51单片机+ds12c887+12864+ds18b20文档格式.docx
- 文档编号:17909321
- 上传时间:2022-12-12
- 格式:DOCX
- 页数:57
- 大小:31.37KB
51单片机+ds12c887+12864+ds18b20文档格式.docx
《51单片机+ds12c887+12864+ds18b20文档格式.docx》由会员分享,可在线阅读,更多相关《51单片机+ds12c887+12864+ds18b20文档格式.docx(57页珍藏版)》请在冰豆网上搜索。
#defineLcd_dataP0//LCM12864数据总线
/*定义DS12CR887数据线--------------------------------------------------------*/
sbitDS_CS=P3^7;
sbitDS_AS=P3^6;
sbitDS_RW=P3^5;
sbitDS_DS=P3^4;
#defineDs_BusP2//DS12CR887数据总线
/*定义按键-------------------------------------------------------------------*/
sbitK1=P1^7;
//K1-设置
sbitK2=P1^4;
//K2-确认
sbitK3=P1^6;
//K3-加
sbitK4=P1^5;
//K4-减
/*定义DS18B20数据线----------------------------------------------------------*/
sbitDQ=P3^1;
/*定义蜂鸣器-----------------------------------------------------------------*/
sbitbeep=P3^0;
/*定义累加器A中的各位--------------------------------------------------------*/
sbita0=ACC^0;
sbita1=ACC^1;
sbita2=ACC^2;
sbita3=ACC^3;
sbita4=ACC^4;
sbita5=ACC^5;
sbita6=ACC^6;
sbita7=ACC^7;
/**/
#defineSYSTEM_OSC11059200//定义晶振频率12000000HZ
#defineSOUND_SPACE4/5//定义普通音符演奏的长度分率,//每4分音符间隔
unsignedintcodeFreTab[12]={262,277,294,311,330,349,369,392,415,440,466,494};
//原始频率表
unsignedcharcodeSignTab[7]={0,2,4,5,7,9,11};
//1~7在频率表中的位置
unsignedcharcodeLengthTab[7]={1,2,4,8,16,32,64};
unsignedcharSound_Temp_TH0,Sound_Temp_TL0;
//音符定时器初值暂存
unsignedcharSound_Temp_TH1,Sound_Temp_TL1;
//音长定时器初值暂存
/*延时程序-------------------------------------------------------------------*/
//延时函数1MS/次
voidDelayM(uinta)
{
uchari;
while(--a!
=0)
{
for(i=0;
i<
125;
i++);
//一个;
表示空语句,CPU空转。
}//i从0加到125,CPU大概就耗时1毫秒
}
//延时函数
voidDelay(intnum)
while(num--);
voidInitialSound(void)//音乐相关
beep=0;
Sound_Temp_TH1=(65535-(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;
voidBeepTimer0(void)interrupt1//音符发生中断
beep=!
beep;
TH0=Sound_Temp_TH0;
TL0=Sound_Temp_TL0;
//**************************************************************************
voidPlay(unsignedchar*Sound,unsignedcharSignature,unsignedOctachord,unsignedintSpeed)
unsignedintNewFreTab[12];
//新的频率表
unsignedchari,j;
unsignedintPoint,LDiv,LDiv0,LDiv1,LDiv2,LDiv4,CurrentFre,Temp_T,SoundLength;
//Point指针索引值
//LDiv音音符演奏的长度(多少个10ms)
//LDiv01分音符的长度(几个10ms)即一个节拍
//LDiv44分音符的长度
//CurrentFre查出对应音符的频率
//SoundLength歌曲长度
//Temp_T计算计数器初值
unsignedcharTone,Length,SL,SH,SM,SLen,XG,FD;
//Tone音调
//Length音长
//SL音符
//SM高低音
//SH是否升半
//XG音符类型(0普通1连音2顿音)
for(i=0;
i<
12;
i++)//根据调号及升降八度来生成新的频率表
{
j=i+Signature;
if(j>
11)
j=j-12;
NewFreTab[i]=FreTab[j]*2;
}
else
NewFreTab[i]=FreTab[j];
if(Octachord==1)
NewFreTab[i]>
>
=2;
elseif(Octachord==3)
NewFreTab[i]<
<
}
SoundLength=0;
while(Sound[SoundLength]!
=0x00)//计算歌曲长度
SoundLength+=2;
Point=0;
Tone=Sound[Point];
Length=Sound[Point+1];
//读出第一个音符和它时时值
LDiv0=12000/Speed;
//算出1分音符的长度(几个10ms)
LDiv4=LDiv0/4;
//算出4分音符的长度
LDiv4=LDiv4-LDiv4*SOUND_SPACE;
//普通音最长间隔标准
TR1=1;
while(Point<
SoundLength)
SL=Tone%10;
//计算出音符
SM=Tone/10%10;
//计算出高低音
SH=Tone/100;
//计算出是否升半
CurrentFre=NewFreTab[SignTab[SL-1]+SH];
//查出对应音符的频率
if(SL!
=0)
if(SM==1)CurrentFre>
=2;
//低音
if(SM==3)CurrentFre<
//高音
Temp_T=65536-(50000/CurrentFre)*10/(12000000/SYSTEM_OSC);
//计算计数器初值
Sound_Temp_TH0=Temp_T/256;
Sound_Temp_TL0=Temp_T%256;
TL0=Sound_Temp_TL0+12;
//加12是对中断延时的补偿
SLen=LengthTab[Length%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(SLen<
=4)
LDiv1=LDiv-LDiv4;
LDiv1=LDiv*SOUND_SPACE;
LDiv1=LDiv/2;
//算出顿音的演奏长度
LDiv1=LDiv;
if(SL==0)LDiv1=0;
LDiv2=LDiv-LDiv1;
//算出不发音的长度
if(SL!
TR0=1;
for(i=LDiv1;
i>
0;
i--)//发规定长度的音
while(TF1==0);
TF1=0;
}
if(LDiv2!
TR0=0;
beep=0;
for(i=LDiv2;
i--)//音符间的间隔
Point+=2;
Tone=Sound[Point];
Length=Sound[Point+1];
if(K1==0||K2==0||K3==0||K4==0)//常案按键两秒跳出歌曲
{
DelayM(20);
if(K1==0||K2==0||K3==0||K4==0)
{
Point=SoundLength;
if(rt==1)
{
rt=0;
rr=1;
}
}
}
#endif
unsignedcharcodeMusic_happy[]=
{//生日歌
0x19,0x03,0x19,0x03,0x1A,0x02,0x19,0x02,0x1F,0x02,
0x1B,0x01,0x19,0x03,0x19,0x03,0x1A,0x02,0x19,0x02,
0x20,0x02,0x1F,0x01,0x19,0x03,0x19,0x03,0x23,0x02,
0x21,0x02,0x1F,0x02,0x1B,0x0C,0x1A,0x15,0x22,0x03,
0x22,0x03,0x21,0x02,0x1F,0x02,0x20,0x02,0x1F,0x15,
0x19,0x03,0x19,0x03,0x1A,0x02,0x19,0x02,0x1F,0x02,
0x00,0x00
};
//挥着翅膀的女孩---容祖儿
unsignedcharcodeMusic_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,0x03,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,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,
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,0x03,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,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,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
//昨日重现
unsignedcharcodeMusic_yesterday[]=
0x15,0x03,0x15,0x03,0x16,0x03,0x17,0x02,0x19,0x02,
0x19,0x03,0x17,0x03,0x19,0x03,0x17,0x03,0x1A,0x02,
0x19,0x66,0x17,0x02,0x17,0x03,0x19,0x03,0x1A,0x02,
0x16,0x02,0x17,0x03,0x19,0x66,0x1A,0x15,0x17,0x03,
0x19,0x03,0x1A,0x02,0x21,0x02,0x20,0x03,0x1F,0x02,
0x1B,0x0D,0x1B,0x66,0x19,0x03,0x17,0x03,0x19,0x02,
0x17,0x0C,0x16,0x14,0x15,0x03,0x15,0x03,0x16,0x03,
0x17,0x03,0x19,0x02,0x19,0x0D,0x19,0x03,0x17,0x03,
0x19,0x03,0x17,0x03,0x1A,0x03,0x19,0x02,0x17,0x0D,
0x17,0x02,0x17,0x03,0x19,0x03,0x1A,0x02,0x1B,0x02,
0x17,0x03,0x19,0x02,0x1A,0x0D,0x1A,0x01,0x1B,0x02,
0x20,0x02,0x1F,0x03,0x1B,0x03,0x1A,0x66,0x1B,0x03,
0x1F,0x03,0x1B,0x03,0x1F,0x03,0x1B,0x02,0x1A,0x66,
0x1A,0x03,0x1B,0x03,0x1F,0x02,0x1F,0x02,0x1A,0x03,
0x1F,0x02,0x20,0x0D,0x20,0x16,0x1F,0x02,0x20,0x02,
0x21,0x03,0x21,0x03,0x21,0x03,0x21,0x02,0x20,0x03,
0x1F,0x03,0x1B,0x0D,0x1F,0x03,0x1B,0x0D,0x1A,0x66,
0x17,0x03,0x19,0x0D,0x19,0x14,0x1F,0x03,0x20,0x03,
0x21,0x03,0x21,0x03,0x21,0x03,0x21,0x03,0x21,0x02,
0x20,0x03,0x1F,0x03,0x1B,0x0D,0x1F,0x03,0x1B,0x03,
0x1A,0x66,0x17,0x03,0x19,0x0D,0x19,0x14,0x1A,0x03,
0x1B,0x03,0x1F,0x03,0x1B,0x03,0x1F,0x03,0x20,0x66,
0x1F,0x03,0x1B,0x03,0x1F,0x03,0x1B,0x03,0x1F,0x03,
0x20,0x66,0x1F,0x03,0x20,0x03,0x21,0x02,0x21,0x02,
0x20,0x03,0x1F,0x02,0x1A,0x0D,0x1A,0x02,0x17,0x02,
0x17,0x0D,0x1A,0x03,0x
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 51 单片机 ds12c887 12864 ds18b20
![提示](https://static.bdocx.com/images/bang_tan.gif)