您的位置 首页 测评

STC89C52单片机蜂鸣器歌唱试验

实现功能:通过对单片机定时器的配置,让其产生不同频率的方波

////////////////////////////////////////////////////////////////////////

完结功用:经过对单片机定时器的装备,让其发生不同频率的方波,蜂鸣器依据频
率的不同而宣布不同的声响,来到达歌唱的意图
试验板类型:KBL-XYD-C52
试验称号: 蜂鸣器歌唱试验
编写人: 谢应东
编写日期: 2012-4-26
////////////////////////////////////////////////////////////////////////
#include

#define uchar unsigned char
#define uint unsigned int

uchar cCount;

sbit FMQ =P2^4; //界说试验板上蜂鸣器器操控脚

unsigned char code SONG[] ={ //祝你平安
0x26,0x20,0x20,0x20,0x20,0x20,0x26,0x10,0x20,0x10,0x20,0x80,0x26,0x20,0x30,0x20,
0x30,0x20,0x39,0x10,0x30,0x10,0x30,0x80,0x26,0x20,0x20,0x20,0x20,0x20,0x1c,0x20,
0x20,0x80,0x2b,0x20,0x26,0x20,0x20,0x20,0x2b,0x10,0x26,0x10,0x2b,0x80,0x26,0x20,
0x30,0x20,0x30,0x20,0x39,0x10,0x26,0x10,0x26,0x60,0x40,0x10,0x39,0x10,0x26,0x20,
0x30,0x20,0x30,0x20,0x39,0x10,0x26,0x10,0x26,0x80,0x26,0x20,0x2b,0x10,0x2b,0x10,
0x2b,0x20,0x30,0x10,0x39,0x10,0x26,0x10,0x2b,0x10,0x2b,0x20,0x2b,0x40,0x40,0x20,
0x20,0x10,0x20,0x10,0x2b,0x10,0x26,0x30,0x30,0x80,0x18,0x20,0x18,0x20,0x26,0x20,
0x20,0x20,0x20,0x40,0x26,0x20,0x2b,0x20,0x30,0x20,0x30,0x20,0x1c,0x20,0x20,0x20,
0x20,0x80,0x1c,0x20,0x1c,0x20,0x1c,0x20,0x30,0x20,0x30,0x60,0x39,0x10,0x30,0x10,
0x20,0x20,0x2b,0x10,0x26,0x10,0x2b,0x10,0x26,0x10,0x26,0x10,0x2b,0x10,0x2b,0x80,
0x18,0x20,0x18,0x20,0x26,0x20,0x20,0x20,0x20,0x60,0x26,0x10,0x2b,0x20,0x30,0x20,
0x30,0x20,0x1c,0x20,0x20,0x20,0x20,0x80,0x26,0x20,0x30,0x10,0x30,0x10,0x30,0x20,
0x39,0x20,0x26,0x10,0x2b,0x10,0x2b,0x20,0x2b,0x40,0x40,0x10,0x40,0x10,0x20,0x10,
0x20,0x10,0x2b,0x10,0x26,0x30,0x30,0x80,0x00,
//路旁边的野华不要采
0x30,0x1C,0x10,0x20,0x40,0x1C,0x10,0x18,0x10,0x20,0x10,0x1C,0x10,0x18,0x40,0x1C,
0x20,0x20,0x20,0x1C,0x20,0x18,0x20,0x20,0x80,0xFF,0x20,0x30,0x1C,0x10,0x18,0x20,
0x15,0x20,0x1C,0x20,0x20,0x20,0x26,0x40,0x20,0x20,0x2B,0x20,0x26,0x20,0x20,0x20,
0x30,0x80,0xFF,0x20,0x20,0x1C,0x10,0x18,0x10,0x20,0x20,0x26,0x20,0x2B,0x20,0x30,
0x20,0x2B,0x40,0x20,0x20,0x1C,0x10,0x18,0x10,0x20,0x20,0x26,0x20,0x2B,0x20,0x30,
0x20,0x2B,0x40,0x20,0x30,0x1C,0x10,0x18,0x20,0x15,0x20,0x1C,0x20,0x20,0x20,0x26,
0x40,0x20,0x20,0x2B,0x20,0x26,0x20,0x20,0x20,0x30,0x80,0x20,0x30,0x1C,0x10,0x20,
0x10,0x1C,0x10,0x20,0x20,0x26,0x20,0x2B,0x20,0x30,0x20,0x2B,0x40,0x20,0x15,0x1F,
0x05,0x20,0x10,0x1C,0x10,0x20,0x20,0x26,0x20,0x2B,0x20,0x30,0x20,0x2B,0x40,0x20,
0x30,0x1C,0x10,0x18,0x20,0x15,0x20,0x1C,0x20,0x20,0x20,0x26,0x40,0x20,0x20,0x2B,
0x20,0x26,0x20,0x20,0x20,0x30,0x30,0x20,0x30,0x1C,0x10,0x18,0x40,0x1C,0x20,0x20,
0x20,0x26,0x40,0x13,0x60,0x18,0x20,0x15,0x40,0x13,0x40,0x18,0x80,0x00,

};
////////////////////////////////////////////////////////////////////////
函数称号:Time0_Init
函数功用:定时器的初始化装备,使其能中止,中止的时刻距离为10ms
参数介绍:无
返回值:
留意事项:留意此刻试验板上用到的晶振为11.0592M的,要选用其他的,得从头装备
////////////////////////////////////////////////////////////////////////
void Init_Time0()
{
TMOD = 0x01; //设置定时器0为形式1(16bit)

TH0 = 0xDC; //给定时器0高八位赋初值
TL0 = 0x00; //给定时器0第八位赋初值

ET0 = 1; //答应定时器0中止
EA = 1; //翻开大局中止
}
////////////////////////////////////////////////////////////////////////
函数称号:Time0_Routine
函数功用:定时器0中止服务函数,从头给定时器0赋值,一起把大局变量cCount加1
参数介绍:无
返回值:
留意事项:无
////////////////////////////////////////////////////////////////////////
void Time0_Routine() interrupt 1 using 1
{
TH0 = 0xDC;//从头给高八位赋新值
TL0 = 0x00;//从头给第八位赋新值
cCount++;//计数变量加1
}
////////////////////////////////////////////////////////////////////////
函数称号:DelayUs
函数功用:延时函数,经过示波器测验,此函数每履行一次大约能延时55us
参数介绍:iDelay_Us:履行此延时函数的次数
iNumber1:第一个for句子所用的记载循环的变量
iNumber2:第二个for句子所用的记载循环的变量
返回值:
留意事项:无
////////////////////////////////////////////////////////////////////////
void DelayUs(unsigned int iDelay_Us)
{
uint iNumber1,iNumber2;
for(iNumber1=0;iNumber1 {
for(iNumber2=0;iNumber2<3;iNumber2++);
}
}
////////////////////////////////////////////////////////////////////////
函数称号:FMQ_PlaySong
函数功用:操控试验板上与蜂鸣器相连的引脚输出不同的频率的方波,顺次来操控蜂鸣
器歌唱
参数介绍:cNumber:歌曲的挑选,0代表“祝你平安”,‘1’代表“路旁边的野花不要采”
返回值:
留意事项:无
////////////////////////////////////////////////////////////////////////
void FMQ_PlaySong(unsigned char cNumber)
{
uchar cTemp1,cTemp2;
uint iAddr;
cCount = 0;//中止计数器清0
iAddr = cNumber * 217;//歌曲的地址,能够参看SONG[],你就会理解
while(1)
{ //把音码变量数组里的值赋给暂时变量
cTemp1 = SONG[iAddr++];
if (cTemp1 == 0xFF) //休止符
{
TR0 = 0;
DelayUs(100); //延时
}
else if (cTemp1 == 0x00) //判别是否歌曲完毕
{
return;
}
else
{
cTemp2 = SONG[iAddr++];//判别完,不是休止符与完毕符后,让蜂鸣器歌唱
TR0 = 1; //让定时器0开端作业
while(1)
{
FMQ = ~FMQ; //让蜂鸣器不断地在开与关之间切换
DelayUs(cTemp1);
if(cTemp2 == cCount)
{
cCount = 0;
break;
}
}
}
}
}
////////////////////////////////////////////////////////////////////////
函数称号:main
函数功用:完结体系的初始化,让试验板上的蜂鸣器不断循环的唱上面的两首歌
参数介绍:无
返回值:
留意事项:无
////////////////////////////////////////////////////////////////////////
void main()
{
DelayUs(100); //安稳体系
Init_Time0();//定时器0中止初始化
while(1)
{
FMQ_PlaySong(0); //播映“祝你平安”
DelayUs(100); //延时一下,以便播映下一首歌
FMQ_PlaySong(1); //播映“祝你平安”
DelayUs(100); //延时一下,以便播映下一首歌
}
}

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/ceping/262883.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部