C51单片机利用定时器和蜂鸣器唱歌
创作时间:
作者:
@小白创作中心
C51单片机利用定时器和蜂鸣器唱歌
引用
CSDN
1.
https://m.blog.csdn.net/weixin_42411288/article/details/142243441
实验目的
学会利用单片机的定时器,掌握蜂鸣器的发声操作。
实验现象
用单片机的P2.3口输出不同占空比的方波来控制与它相连的蜂鸣器,当方波的占空比不同时蜂鸣器发出的音调就不同,由此根据歌曲的音调编程实现让蜂鸣器唱歌。
实验说明
占空比:一个周期中,高电平的长度比上总周期的长度的值再乘上100%。标准方波的占空比为50%。
硬件电路
程序代码
#include <reg52.h>
//实验目的:学会利用单片机的定时器,掌握蜂鸣器的发声操作
//实验现象:用单片机的P2.3口输出不同占空比的方波来控制与它相连的蜂鸣器,
//当方波的占空比不同时蜂鸣器发出的音调就不同,由此根据歌曲的音调编程实现让蜂鸣器唱歌。
//占空比:一个周期中,高电平的长度比上总周期的长度的值再乘上100%。标准方波的占空比为50%。
sbit beep=P2^3; //蜂鸣器控制位
unsigned int num;
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,
};
void Time0_Init()
{
TMOD=0x01; //设置定时器0工作方式1
TH0=(65536-45872)/256; //装初值11.059M晶振定时50ms数为45872
//THX=(65536-N)/256 N=X(us)/H1(机器周期) H1=12*(1/晶振频率)
TL0=(65536-48572)%256;
EA=1; //开总中断
ET0=1; //开定时器0中断
}
void T0_time() interrupt 1
{
TH0=(65536-45872)/256; //装初值11.059M晶振定时50ms数为45872
//THX=(65536-N)/256 N=X(us)/H1(机器周期) H1=12*(1/晶振频率)
TL0=(65536-48572)%256;
num++;
}
void Delay_xMs(unsigned int x)
{
unsigned int i,j;
for( i =0;i < x;i++ )
{
for( j =0;j<3;j++ );
}
}
void Play_Song(unsigned char i)
{
unsigned char Temp1,Temp2;
unsigned int Addr;
num = 0; //中断计数器清 0
Addr = i * 217;
while(1)
{
Temp1 = SONG[Addr++];
if ( Temp1 == 0xFF ) //休止符
{
TR0 = 0;
Delay_xMs(100);
}
else if ( Temp1 == 0x00 ) //歌曲结束符
{
return;
}
else
{
Temp2 = SONG[Addr++];
TR0 = 1;
while(1)
{
beep = ~beep;
Delay_xMs(Temp1);
if ( Temp2 == num )
{
num = 0;
break;
}
}
}
}
}
void main()
{
Time0_Init(); //定时器 0 中断初始化
while(1)
{
Play_Song(0); //播放
}
}
热门推荐
相亲聊天没话题怎么办?实用攻略帮你轻松应对尴尬
营造良好沟通氛围
RAPTOR:索引树状 RAG,使用树结构来捕捉文本的高级和低级细节
刑事辩护律师费用一般多少钱?刑事律师收费标准解析
房产证红本和绿本的区别是什么
支付方式大变!数币钱包、碰一下支付更方便
彩椒的营养价值:五大健康益处全面解析
铸造造型的方法全解析:常见工艺及应用实例
Dunk和AJ的区别是什么?
甘肃敦煌几月份去最好?教你选对出行季
如何为企业Logo选择合适的发光字?费用全解析!
电影《一一》:生活的多面镜与生命的深度洞察
怎么移民日本?了解日本移民入籍的6个条件和优势!
新东方股价大跌25%,俞敏洪一天身价缩水23亿港元
儿童改名字的理由与方法探讨
照镜子被自己帅到的猪八戒,成了新的游戏圈“吴彦祖”
汽车市场“价格战”开年即起!超30个品牌入场,部分豪华品牌降价近20万元
碳元素是如何在自然界循环的?
幸福树的正确浇水方法(浇水注意事项及细节,让幸福树更健康)
花几万元就能入职央企,不要相信中介的“神通”
二氧化碳中毒的反应及处理方法
用土养巴西木的技巧(掌握巴西木的土壤需求,让它在家中健康茁壮成长)
猪梅肉是什么部位的肉?吃过几种做法
去日本留学,国内的手机在日本还能用吗?怎么设置才能正常使用?
怎么判断是甲流还是普通感冒
“蛟龙号”首个国际航次 中外科学家携手探深海
水泥墙面直接刮腻子前如何处理以及步骤
如何通过法院分配遗产
爱济南影像丨随手一拍即大片!森林公园水杉林尽显“独特浪漫”
723动车事故死亡人数及法律定性分析