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); //播放
}
}
热门推荐
负责前后端开发,为什么要学 C 语言
属兔1975腊月处二
桂林最美的四大景区
在网上高效搜索的「秘密」,我想推荐这 3 个小技巧
英语写作方法:巧妙运用With的复合结构
Excel工具栏隐藏怎么办?10种防止工具栏隐藏的方法
湮灭之潮正式公布相关消息 背后是腾讯投资支持
山海经异兽录:睚眦——龙子之怒目战神
如何选择适合自己的炒锅:品牌、材质与保养全解析
婴儿呕吐的原因以及怎样解决
微信如何开通电子医保卡?简单几步,搞定微信上的电子医保卡
2025年起,湖南中考有新变化!
我的世界女巫,探秘我的世界女巫,神秘魅力解析
煤炭十大指标全解析:从发热量到煤灰熔融性
手术后遇上肠梗阻?别怕,这些干货帮你轻松过关
火星征途:挑战与机遇并存,人类探索新家园的希望之星
全面禁用?起底“夺命”面包中的脱氢乙酸钠
如何获得导师的推荐信?五个关键点
男人不需要你对他多好,这两个字才是他们的渴望,扎心了,多数人都做不到…
如何通过SQL对数据库进行操作
当家里的毛小孩会是戏精时,你的生活将会充满乐趣!
深度解读《流浪地球》系列:科幻设定与现实启示
二手车选购是综合性的决策过程,价格绝不能成为唯一的考量因素
冬季野钓鲫鱼技巧全攻略:调钓方法、打窝策略与漂相应对
谐振电路:谐振频率的计算方法、Q值的求法详解
王羲之《圣教序》偏旁字集特训教程,收藏慢慢学!
广州夜市攻略:探访大排档美食天堂,领略城市舌尖之旅
中国电影市场复苏势头强劲:票房增长潜力巨大
职场礼仪指南:提升专业形象的技巧
喝咖啡还是喝茶?90%都选错了!