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); //播放
}
}
热门推荐
创新管理机制怎么建立?
Excel透视表求和项显示为0的原因及解决方法
为什么很多人吃虾都“不挑虾线”?吃了对身体会有什么影响呢?
代码编写不规范?IDEA 和 GitHub如何实现高质量代码评审
欧冠焦点:拜仁与勒沃库森的战术分析,揭示淘汰赛的关键思路
PLC编程在工业自动化中的关键作用
缺铁性贫血人群水果补铁,不可不知的注意事项
飞机上吸烟后果很严重 这些乘机规定一起来学习
详解到店率的计算方法与提升策略
丙寅日最佳出生时辰 丙寅日生于各时详解
面试时的成就展示
十二星座的守护神都是什么?
AI正在改变项目经理的工作方式
类风湿因子超标应该如何应对
马拉松比赛奖金分配:从菏泽到北京的详细解析
Vue.js中的计算属性:如何让数据自动更新
猫咪的心门,怎么开?5个让猫咪信任你的必杀技
春季工装夹克时尚穿搭指南:潮流与年轻的完美碰撞
医保卡里的余额怎么用
糖尿病患者如何确定个人血糖监测频率
魔兽世界:流水团开发国服打法,两招解决H小强,HTOC不用削弱了
燕麦配料表中营养成分是干重还是湿重?-十分重要
深度强化学习中的剪枝网络:提升性能的新途径
加拿大进口关税与GST详解:成本计算与合规指南
退一赔三!买到假虫草别轻易认栽
医生解答:巧克力真的会降低安眠药效果吗?
不同形式的基因排序方法会影响gsea富集分析结果
图片清晰度评测方法探讨
用人单位查征信和犯罪记录:探究其合理性和目的
INTJ型人格社交(intj 社交)