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); //播放
}
}
热门推荐
如果不能永远在一起,那么相遇的意义是什么?
深入学习PS,成为专业设计师(掌握PS的关键技巧,提升设计能力)
描摹现实并非只靠真人电影,动画片一样可以做到
抉择与未来:高考后专业与学校的深度选择思考
宠物日常趣事分享集
股票市场交易策略与投资者心理:理解市场行为
“汉寿亭侯”小考
基础结之「雀头结、平结的编法」详细手工编织教程
别人给你发新年祝福,别只会说“新年快乐”,高情商的人这样回复
阿司匹林的作用长达10天,可以隔一天吃一次吗?医生辟谣讲清楚
高杆白菜:从栽培到营养价值的全面解析
《网络主播新职业发展报告》发布:1508万人将直播作为主业
国际航线托运行李多少斤?国际航班行李托运重量详解
朗逸的英文名称怎么读?这种发音对品牌传播有何影响?
生辰八字起名,如何根据八字五行来取高分名字
2025年黄金市场深度分析:供需格局、财政赤字与地缘政治影响
ACPI:提升计算机电源管理与硬件配置智能化的重要标准解析
重金求子诈骗案系列:警惕生育诈骗陷阱
全球户外家具市场洞察:趋势、挑战与机遇
揭秘NK细胞的多重“身份”:抗癌先锋,抗衰老的卫士,免疫调节的超级英雄
9 种PCB丝印设计方法
全面训练计划:打造健康体魄的策略
人机智能的边界:心理与芯理
在家如何锻炼腹肌?有效腹肌训练方法有哪些?
海豹突击队教你2分钟快速入睡:4-7-8呼吸法详解
美女刷屏、分身泛滥,当AI成为污染源,如何面对信息质量的危机?
乳铁蛋白的十大作用:不只是增强抵抗力!
小红书上的"玄学密码":10个赛道的流量新玩法
理赔责任人现场处理:提升服务效率与客户满意度的关键策略
量化投资中的回测:概念、流程与常见问题解析