51单片机蜂鸣器播放音乐代码.pdf

上传人:g****s 文档编号:85906699 上传时间:2023-04-13 格式:PDF 页数:5 大小:150.90KB
返回 下载 相关 举报
51单片机蜂鸣器播放音乐代码.pdf_第1页
第1页 / 共5页
51单片机蜂鸣器播放音乐代码.pdf_第2页
第2页 / 共5页
点击查看更多>>
资源描述

《51单片机蜂鸣器播放音乐代码.pdf》由会员分享,可在线阅读,更多相关《51单片机蜂鸣器播放音乐代码.pdf(5页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

1、1 /*生日快乐歌曲*/#include#define uint unsigned int#define uchar unsigned char sbit beep=P15;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;

2、/延时 void DelayMS(uint x)uchar t;while(x-)for(t=0;t120;t+);void PlayMusic()uint i=0,j,k;while(SONG_LONGi!=0|SONG_TONEi!=0)/播放各个音符,SONG_LONG 为拍子长度 for(j=0;jSONG_LONGi*20;j+)beep=beep;/SONG_TONE 延时表决定了每个音符的频率 for(k=0;kSONG_TONEi/3;k+);DelayMS(10);i+;void main()beep=0;while(1)PlayMusic();/播放生日快乐 DelayMS

3、(500);/播放完后暂停一段时间 两只蝴蝶:#include /这是 单片机音乐代码生成器 生成的代码#define uchar unsigned char sbit beepIO=P15;/输出 为 可以修改 成 其它 IO 口 uchar m,n;2 uchar code T492=0,0,0 xF8,0 x8B,0 xF8,0 xF2,0 xF9,0 x5B,0 xF9,0 xB7,0 xFA,0 x14,0 xFA,0 x66,0 xFA,0 xB9,0 xFB,0 x03,0 xFB,0 x4A,0 xFB,0 x8F,0 xFB,0 xCF,0 xFC,0 x0B,0 xFC,0

4、 x43,0 xFC,0 x78,0 xFC,0 xAB,0 xFC,0 xDB,0 xFD,0 x08,0 xFD,0 x33,0 xFD,0 x5B,0 xFD,0 x81,0 xFD,0 xA5,0 xFD,0 xC7,0 xFD,0 xE7,0 xFE,0 x05,0 xFE,0 x21,0 xFE,0 x3C,0 xFE,0 x55,0 xFE,0 x6D,0 xFE,0 x84,0 xFE,0 x99,0 xFE,0 xAD,0 xFE,0 xC0,0 xFE,0 x02,0 xFE,0 xE3,0 xFE,0 xF3,0 xFF,0 x02,0 xFF,0 x10,0 xFF,0

5、 x1D,0 xFF,0 x2A,0 xFF,0 x36,0 xFF,0 x42,0 xFF,0 x4C,0 xFF,0 x56,0 xFF,0 x60,0 xFF,0 x69,0 xFF,0 x71,0 xFF,0 x79,0 xFF,0 x81;uchar code music2=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,

6、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,2

7、8,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,0 xFF,0 xFF;void delay(uchar p)uchar i,j;for(;p0;p-)for(i=181;i0;i-)for(j=181;j0;j-);void pause()uchar i,j;for(i=150;i0;i-)for(j=150;j0;j-);void T0_int()in

8、terrupt 1 beepIO=!beepIO;TH0=Tm0;TL0=Tm1;void main()3 uchar i=0;TMOD=0 x01;EA=1;ET0=1;while(1)m=musici0;n=musici1;if(m=0 x00)TR0=0;delay(n);i+;else if(m=0 xFF)TR0=0;delay(30);i=0;else if(m=musici+10)TR0=1;delay(n);TR0=0;pause();i+;else TR0=1;delay(n);i+;祝你平安:#include unsigned char Count;sbit _Speak=

9、P15;unsigned char code SONG=/祝你平安 0 x26,0 x20,0 x20,0 x20,0 x20,0 x20,0 x26,0 x10,0 x20,0 x10,0 x20,0 x80,0 x26,0 x20,0 x30,0 x20,0 x30,0 x20,0 x39,0 x10,0 x30,0 x10,0 x30,0 x80,0 x26,0 x20,0 x20,0 x20,0 x20,0 x20,0 x1c,0 x20,0 x20,0 x80,0 x2b,0 x20,0 x26,0 x20,0 x20,0 x20,0 x2b,0 x10,0 x26,0 x10,0

10、x2b,0 x80,0 x26,0 x20,0 x30,0 x20,0 x30,0 x20,0 x39,0 x10,0 x26,0 x10,0 x26,0 x60,0 x40,0 x10,0 x39,0 x10,0 x26,0 x20,0 x30,0 x20,0 x30,0 x20,0 x39,0 x10,0 x26,0 x10,0 x26,0 x80,0 x26,0 x20,0 x2b,0 x10,0 x2b,0 x10,0 x2b,0 x20,0 x30,0 x10,0 x39,0 x10,0 x26,0 x10,0 x2b,0 x10,0 x2b,0 x20,0 x2b,0 x40,0

11、x40,0 x20,0 x20,0 x10,0 x20,0 x10,0 x2b,0 x10,0 x26,0 x30,0 x30,0 x80,0 x18,0 x20,0 x18,0 x20,0 x26,0 x20,0 x20,0 x20,0 x20,0 x40,0 x26,0 x20,0 x2b,0 x20,0 x30,0 x20,0 x30,0 x20,0 x1c,0 x20,0 x20,0 x20,0 x20,0 x80,0 x1c,0 x20,0 x1c,0 x20,0 x1c,0 x20,0 x30,0 x20,0 x30,0 x60,0 x39,0 x10,0 x30,0 x10,0

12、x20,0 x20,0 x2b,0 x10,0 x26,0 x10,0 x2b,0 x10,0 x26,0 x10,0 x26,0 x10,0 x2b,0 x10,0 x2b,0 x80,0 x18,0 x20,0 x18,0 x20,0 x26,0 x20,0 x20,0 x20,0 x20,0 x60,0 x26,0 x10,0 x2b,0 x20,0 x30,0 x20,0 x30,0 x20,0 x1c,0 x20,0 x20,0 x20,0 x20,0 x80,0 x26,0 x20,0 x30,0 x10,0 x30,0 x10,0 x30,4 0 x20,0 x39,0 x20,

13、0 x26,0 x10,0 x2b,0 x10,0 x2b,0 x20,0 x2b,0 x40,0 x40,0 x10,0 x40,0 x10,0 x20,0 x10,0 x20,0 x10,0 x2b,0 x10,0 x26,0 x30,0 x30,0 x80,0 x00;void Time0_Init()TMOD=0 x01;IE =0 x82;TH0 =0 xD8;TL0 =0 xEF;/12MZ 晶振,10ms void Time0_Int()interrupt 1 TH0=0 xD8;TL0=0 xEF;Count+;/长度加 1 /*-功能:1MS 延时子程序-*/void Del

14、ay_xMs(unsigned int x)unsigned int i,j;for(i=0;i x;i+)for(j=0;j3;j+);void Play_Song(unsigned char i)unsigned char Temp1,Temp2;unsigned int Addr;Count=0;/中断计数器清 0 Addr=i*217;while(1)Temp1=SONGAddr+;if(Temp1=0 xFF)/休止符 TR0=0;Delay_xMs(100);else if(Temp1=0 x00)/歌曲结束符 5 return;else Temp2=SONGAddr+;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);/播放

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 应用文书 > 文案大全

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁