51单片机蜂鸣器播放音乐代码(生日快乐 两只蝴蝶 祝你平安)
更新时间:2023-11-18 15:01:01 阅读量: 教育文库 文档下载
/*生日快乐歌曲*/ #include
#define uint unsigned int #define uchar unsigned char sbit beep = P1^5;
uchar code SONG_TONE[]={212,212,190,212,159,169,212,212,190,212,142,159, 212,212,106,126,159,169,190,119,119,126,159,142,159,0};
uchar code SONG_LONG[]={9,3,12,12,12,24,9,3,12,12,12,24, 9,3,12,12,12,12,12,9,3,12,12,12,24,0};
//延时
void DelayMS(uint x) {
uchar t;
while(x--) for(t=0;t<120;t++); }
void PlayMusic() {
uint i=0,j,k;
while(SONG_LONG[i]!=0||SONG_TONE[i]!=0) { //播放各个音符,SONG_LONG 为拍子长度 for(j=0;j beep=~beep; //SONG_TONE 延时表决定了每个音符的频率 for(k=0;k DelayMS(10); i++; } } void main() { beep=0; while(1) { PlayMusic(); //播放生日快乐 DelayMS(500); //播放完后暂停一段时间 } } 两只蝴蝶: #include sbit beepIO=P1^5; // 输出 为 P1.5 可以修改 成 其它 IO 口 uchar m,n; uchar code T[49][2]={{0,0}, {0xF8,0x8B},{0xF8,0xF2},{0xF9,0x5B},{0xF9,0xB7},{0xFA,0x14},{0xFA,0x66},{0xFA,0xB9},{0xFB,0x03},{0xFB,0x4A},{0xFB,0x8F},{0xFB,0xCF},{0xFC,0x0B}, {0xFC,0x43},{0xFC,0x78},{0xFC,0xAB},{0xFC,0xDB},{0xFD,0x08},{0xFD,0x33},{0xFD,0x5B},{0xFD,0x81},{0xFD,0xA5},{0xFD,0xC7},{0xFD,0xE7},{0xFE,0x05}, {0xFE,0x21},{0xFE,0x3C},{0xFE,0x55},{0xFE,0x6D},{0xFE,0x84},{0xFE,0x99},{0xFE,0xAD},{0xFE,0xC0},{0xFE,0x02},{0xFE,0xE3},{0xFE,0xF3},{0xFF,0x02}, {0xFF,0x10},{0xFF,0x1D},{0xFF,0x2A},{0xFF,0x36},{0xFF,0x42},{0xFF,0x4C},{0xFF,0x56},{0xFF,0x60},{0xFF,0x69},{0xFF,0x71},{0xFF,0x79},{0xFF,0x81} }; uchar code music[][2]={{0,4}, {23,4},{21,4},{23,16},{23,4},{21,4},{23,4},{21,4},{19,16},{16,4},{19,4},{21,8},{21,4},{23,4},{21,4},{19,4},{16,4},{19,4},{14,24}, {23,4},{21,4},{23,16},{23,4},{21,4},{23,4},{21,4},{19,24},{16,4},{19,4},{21,8},{21,4},{23,4},{21,4},{19,4},{16,4},{19,4},{21,24}, {23,4},{21,4},{23,16},{23,4},{21,4},{23,4},{21,4},{19,16},{16,4},{19,4},{21,8},{21,4},{23,4},{21,4},{19,4},{16,4},{19,4},{14,24}, {23,4},{26,4},{26,16},{26,4},{28,4},{26,4},{23,24},{21,4},{23,4},{21,8},{21,4},{23,4},{21,4},{19,4},{16,4},{16,2},{19,2},{19,24},{0,20}, {26,4},{26,4},{28,4},{31,4},{30,4},{30,4},{28,4},{23,4},{21,4},{21,4},{23,16},{0,4},{23,4},{23,4},{26,4},{28,8},{28,12},{16,4},{23,4},{21,4}, {21,24},{23,4},{26,4},{26,4},{23,4},{26,8},{0,4},{31,8},{30,4},{28,4},{30,4},{23,8},{0,4},{28,4},{28,4},{30,4},{28,4},{26,4},{23,4},{21,8},{23,4},{21,4},{23,4},{26,16}, {0xFF,0xFF}}; void delay(uchar p) { uchar i,j; for(;p>0;p--) for(i=181;i>0;i--) for(j=181;j>0;j--); } void pause() { uchar i,j; for(i=150;i>0;i--) for(j=150;j>0;j--); } void T0_int() interrupt 1 { beepIO=!beepIO; TH0=T[m][0]; TL0=T[m][1]; } void main() { uchar i=0; TMOD=0x01; EA=1; ET0=1; while(1) { m=music[i][0];n=music[i][1]; if(m==0x00) {TR0=0;delay(n);i++;} else if(m==0xFF) {TR0=0;delay(30);i=0;} else if(m==music[i+1][0]) {TR0=1;delay(n);TR0=0;pause();i++;} else {TR0=1;delay(n);i++;} } } 祝你平安: #include %unsigned char Count; sbit _Speak =P1^5 ; 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}; void Time0_Init() { TMOD = 0x01; IE = 0x82; TH0 = 0xD8; TL0 = 0xEF; //12MZ晶振,10ms } void Time0_Int() interrupt 1 { TH0 = 0xD8; TL0 = 0xEF; Count++; //长度加1 } /*------------------------------------------------- 功能:1MS延时子程序 -------------------------------------------------*/ 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; Count = 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) { _Speak = ~_Speak; Delay_xMs(Temp1); if ( Temp2 == Count ) { Count = 0; break; } } } } } /*------------------------------------------------- 功能:主程序 -------------------------------------------------*/ void main() { Time0_Init(); //定时器0中断初始化 while(1) { Play_Song(0); //播放 } }
正在阅读:
51单片机蜂鸣器播放音乐代码(生日快乐 两只蝴蝶 祝你平安)11-18
多维度-多归因因果量表(MMCS)01-06
资产评估平时作业09-25
夯实党建工作基础“四不留”08-19
关于印发山东省贯彻《工伤保险条例》实施办法的通知(鲁政发〔2011〕25号)10-15
商务英语( 第二版)课文翻译04-06
六年级上册生命生态安全教学计划06-01
2015年江苏对口高考计算机(DOC)06-28
- exercise2
- 铅锌矿详查地质设计 - 图文
- 厨余垃圾、餐厨垃圾堆肥系统设计方案
- 陈明珠开题报告
- 化工原理精选例题
- 政府形象宣传册营销案例
- 小学一至三年级语文阅读专项练习题
- 2014.民诉 期末考试 复习题
- 巅峰智业 - 做好顶层设计对建设城市的重要意义
- (三起)冀教版三年级英语上册Unit4 Lesson24练习题及答案
- 2017年实心轮胎现状及发展趋势分析(目录)
- 基于GIS的农用地定级技术研究定稿
- 2017-2022年中国医疗保健市场调查与市场前景预测报告(目录) - 图文
- 作业
- OFDM技术仿真(MATLAB代码) - 图文
- Android工程师笔试题及答案
- 生命密码联合密码
- 空间地上权若干法律问题探究
- 江苏学业水平测试《机械基础》模拟试题
- 选课走班实施方案
- 蜂鸣器
- 平安
- 两只蝴蝶
- 单片机
- 生日
- 代码
- 播放
- 快乐
- 音乐
- 祝你
- 南京烟草配送中心运作现状及优化策略
- 河南中医学院中医基础理论历年考题
- 良种工程
- 入党积极分子考察谈话问题
- 建筑工程技术毕业论文
- 销售与生产对接流程与责任文档
- 在阅读教学中如何引导学生对词语的理解与记忆
- 《丰富幻灯片内容》教学设计
- 《化学实验基本操作训练》考题及要求(有答案)
- 3、隧道超前小导管施工作业指导书
- 共青团镇江市润州区委员会
- 2008年椒江区农村街道小学五年级抽查测试 - 图文
- 人教新版 英语六年级上册 第四单元中文翻译
- 会计软件应用复习题及答案
- 接种培菌方法
- 宏观经济学习题答案(曼昆第五版)
- 行知天下第五单元答案四年级下册人教版
- 冷长志演讲稿
- 精品--2016年中国煤炭工业固废处理行业分析报告
- 吉林大学2014-2015学年第一学期期末《生物药剂学与药物动力学》大作业答案