单片机51_C语言音乐程序
51单片机蜂鸣器播放音乐代码
/*生日快乐歌曲*/#include <>#define uint unsigned int#define uchar unsigned charsbit 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<SONG_LONG[i]*20;j++){beep=~beep;//SONG_TONE 延时表决定了每个音符的频率for(k=0;k<SONG_TONE[i]/3;k++);}DelayMS(10);i++;}}void main(){beep=0;while(1){PlayMusic(); //播放生日快乐DelayMS(500); //播放完后暂停一段时间}}两只蝴蝶:#include <> // 这是单片机音乐代码生成器生成的代码#define uchar unsigned charsbit beepIO=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,0 xB9},{0xFB,0x03},{0xFB,0x4A},{0xFB,0x8F},{0xFB,0xCF},{0xFC,0x0B},{0xFC,0x43},{0xFC,0x78},{0xFC,0xAB},{0xFC,0xDB},{0xFD,0x08},{0xFD,0x33},{0xFD,0 x5B},{0xFD,0x81},{0xFD,0xA5},{0xFD,0xC7},{0xFD,0xE7},{0xFE,0x05},{0xFE,0x21},{0xFE,0x3C},{0xFE,0x55},{0xFE,0x6D},{0xFE,0x84},{0xFE,0x99},{0xFE,0 xAD},{0xFE,0xC0},{0xFE,0x02},{0xFE,0xE3},{0xFE,0xF3},{0xFF,0x02},{0xFF,0x10},{0xFF,0x1D},{0xFF,0x2A},{0xFF,0x36},{0xFF,0x42},{0xFF,0x4C},{0xFF,0 x56},{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},{2 3,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,0x200x39,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; //中断计数器清0Addr = 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单片机蜂鸣器播放音乐代码(生日快乐 两只蝴蝶 祝你平安)精编版
/*生日快乐歌曲*/#include <reg51.h>#define uint unsigned int#define uchar unsigned charsbit 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<SONG_LONG[i]*20;j++){beep=~beep;//SONG_TONE 延时表决定了每个音符的频率for(k=0;k<SONG_TONE[i]/3;k++);}DelayMS(10);i++;}}void main(){beep=0;while(1){PlayMusic(); //播放生日快乐DelayMS(500); //播放完后暂停一段时间}}两只蝴蝶:#include <reg51.h> // 这是单片机音乐代码生成器生成的代码#define uchar unsigned charsbit 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,0x8 1},{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 1beepIO=!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 "reg52.h"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; //中断计数器清0Addr = 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单片机实现的音乐程序
uint code notefreq[]={ 523, 587, 659, 698, 784, 880, 988,
+ _9 ~! I$ V8 V% L
: h+ H. t6 Z* Q% I! q& D/ `1 W{! c# l7 x0 c5 W' h1 C3 x, G2 W9 M
TH0=FreqSandH;
6 C/ U' p, \. } TL0=FreqSandL;
3 q) ?9 o) N( c! Q& { BEEP_PWR=!BEEP_PWR;2 Z8 V7 g8 G3 V
|! @1 S+ B' M' |) t
uchar i=0,ch,halfflg=0;: y e2 s$ _% N7 b3 C* L& j! h$ i
uchar lasttime;
7 q+ H' Q( F t2 w- f) _ uint freq;
; S5 g: L' f4 {& L% k if((ch==' ')||(ch=='|')||(ch=='\r')||(ch=='\n')) {i++; 7 l0 K9 b+ i3 l1 {6 R
continue;}# W6 W9 Y5 N3 Q
if(!ch) {SoundOff(); return;} //乐曲结束则播放完! a* ]8 R* p! T; x! {! A
#include <reg52.h>
基于c51单片机编写简单蜂鸣器音乐程序的方法
uint i;
for(i=0;i<jp;i++)
{
sound(s);
if(s==0xff)break;
}
}
//*******************************************************************
void main()//主函数
{
while(1)
{
c=0;//用来使数组从头开始读取
/*3*/case 0x0d:j=s/330;break;case 0x0e:j=s/659;break;case 0x0f:j=s/1318;break;
/*4*/case 0x10:j=s/349;break;case 0x11:j=s/370;break;case 0x12:j=s/698;break;case 0x13:j=s/740;break;case 0x14:j=s/1379;break;case 0x15:j=s/1480;break;
0x01,0x0d,0x16,0x1c,0xff,0x1c,0x1c,0x16,0x07,0x07,0x10,0x0d,0xff, 0x01,0x0d,0x16,0x1c,0xff,
0x1c,0x1c,0x22,0x1c,0x16,0x10,0x0d,0x10,0x0d,0x07,0x01,0x00} ;//*
uint code music[]=//这是一个“童话”谱子的数组,与m_t()子程序中的十六进制数一一对应,0xff代表每次音乐的停顿,0x00表示音乐的结束
{0x16,0x10,0x0d,0xff, 0x0d,0x10,0x0d, 0xff, 0x0d,0x10,0x0d,0x10,0x0d,0x07,0x01,0xff,
51单片机c语言音乐例程
//音阶频率表高八位
code unsigned char FREQH[]={
0xF2,0xF3,0xF5,0xF5,0xF6,0xF7,0xF8,
0xF9,0xF9,0xFA,0xFA,0xFB,0xFB,0xFC,0xFC, //1,2,3,4,5,6,7,8,i
0xFC,0xFD,0xFD,0xFD,0xFD,0xFE,
0xEE,0x44, 0x6B,0xB4,0xF4,0x2D,
0x47,0x77,0xA2,0xB6,0xDA,0xFA,0x16,
};
/******************************************************************/
/*延时函数*/
/******************************************************************/
timer0h=FREQH[k];
timer0l=FREQL[k];
time=sszymmh[i+2];
i=i+3;
song();
}
}
}
/*-----------------------------------------------
名称:音乐播放
公司:上海浩豚电子科技有限公司
网站:
编写:师访
日期:2009.5
修改:无
内容:
------------------------------------------------*/
void song()
{
TH0=timer0h;
TL0=timer0l;
TR0=1;
delay(time);
51单片机蜂鸣器播放音乐代码
/*生日快乐歌曲*/#include <>#define uint unsigned int#define uchar unsigned charsbit 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<SONG_LONG[i]*20;j++){beep=~beep;//SONG_TONE 延时表决定了每个音符的频率for(k=0;k<SONG_TONE[i]/3;k++);}DelayMS(10);i++;}}~void main(){beep=0;while(1){PlayMusic(); //播放生日快乐DelayMS(500); //播放完后暂停一段时间}}两只蝴蝶:%#include <> // 这是单片机音乐代码生成器生成的代码#define uchar unsigned charsbit beepIO=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,0x8 1},{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; //中断计数器清0Addr = 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单片机利用中断播放音乐程序
[整理]51单片机利用中断播放音乐程序中断播放音乐程序示例单片机机器周期的算法:比如一个12M的单片机,它的指令周期是1/12M,它的一个机器周期是12*1/12M,1/1M=1微秒在单片机中,它的中断源的定时记一个数是以机器周期为基准的,即对于一个12M的单片机来说,它的记数脉冲是1微秒,也就是说,记下两个数之间间隔的时间是1微秒。
单片机播放音乐其实也就是要干两件事情,能用单片机确定单调和节拍。
所谓音调,也就是音乐的频率,而节拍呢,就是间隔多少时间响一次,正好可以利用两个中断来完成这个工作。
怎么让一个单片机发出一个定频率的声音呢,比如一个440HZ的声音。
440HZ的声音的周期是T,1/440HZ,2272us 即控制给蜂鸣器输入端一个1136us的高电平,再给一个同时长的低电平,这样不断的交替进行,即可以得到440HZ的声音了。
那么这个过程如何用单片机的中断来实现呢, 关键就是该对中断TH0,TL0如何赋初值。
单片机的晶振是12M的,那么这个中断定时计一个数代表的时间是1us,即如果想要实现440HZ的声音,工作于方式1时,计数器最多可以记下65536的数,那么:TH0=(65536,1136)/256TL0=(65536,1136)%256在乐谱中,可以看到下图这样的数字和符号,其中1,C, 1,G表示曲调,与音频有很大的关系。
3/4表示乐谱中,以四分音符为节拍,每一小节有三拍,以下图为例说明:其中1,2为一拍,3,4,5为一拍,6为一拍,一共三拍。
1,2时长是四分音符的一半,即八分音符,3,4时长是八分音符,一半,即十六分音符,5时长是八分音符,6时长表示四分音符。
一拍的时长大约是400ms--500ms,以400ms为例,一拍是400ms,那么四分音符的时长就是400ms,那么八分音符的时长就是200ms,十六分音符的时长就是100ms,在单片机上,控制一个音符唱多长时间,可以用延时的方法,以十六分时长为最基本的延时单元,那么如果要延时八分时长,那么就运行两次这个延时单元,如果要延时四分音符的时长,那么就运行四次这个延时单元。
用51单片机播放音乐(附C程序)
用51单片机IO口播放几首音乐(附C程序)以下为51单片机播放音乐简谱的函数库:/*说明************************************************************************曲谱存贮格式 unsigned char code MusicName{音高,音长,音高,音长...., 0,0}; 末尾:0,0 表示结束(Important)音高由三位数字组成:●个位是表示 1~7 这七个音符;●十位是表示音符所在的音区:1-低音,2-中音,3-高音;●百位表示这个音符是否要升半音: 0-不升,1-升半音。
音长最多由三位数字组成:●个位表示音符的时值,其对应关系是: |数值(n): |0 |1 |2 |3 | 4 | 5 | 6 |几分音符: |1 |2 |4 |8 |16 |32 |64 音符=2^n ;●十位表示音符的演奏效果(0-2): 0-普通,1-连音,2-顿音;●百位是符点位: 0-无符点,1-有符点。
调用演奏子程序的格式:Play(乐曲名,调号,升降八度,演奏速度);|乐曲名 : 要播放的乐曲指针,结尾以(0,0)结束;|调号(0-11) : 是指乐曲升多少个半音演奏;|升降八度(1-3) : 1:降八度, 2:不升不降, 3:升八度;|演奏速度(1-12000): 值越大速度越快;***************************************************************************/#ifndef __SOUNDPLAY_H_REVISION_FIRST__#define __SOUNDPLAY_H_REVISION_FIRST__#include//**************************************************************************#define SYSTEM_OSC 11059200//12000000 //定义晶振频率12000000HZ#define SOUND_SPACE 4/5 //定义普通音符演奏的长度分率,//每4分音符间隔sbit BeepIO = P2^6; //定义输出管脚unsigned int code FreTab[12] = { 262,277,294,311,330,349,369,392,415,440,466,494 }; //原始频率表unsigned char code SignTab[7] = { 0,2,4,5,7,9,11 }; //1~7在频率表中的位置unsigned char code LengthTab[7]= { 1,2,4,8,16,32,64 };unsigned char Sound_Temp_TH0,Sound_Temp_TL0; //音符定时器初值暂存unsigned char Sound_Temp_TH1,Sound_Temp_TL1; //音长定时器初值暂存//**************************************************************************void InitialSound(void){BeepIO = 1;Sound_Temp_TH1 = (65535-(1/1200)*SYSTEM_OSC)/256; // 计算TL1应装入的初值 (10ms 的初装值)Sound_Temp_TL1 = (65535-(1/1200)*SYSTEM_OSC)%256; // 计算TH1应装入的初值TH1 = Sound_Temp_TH1;TL1 = Sound_Temp_TL1;TMOD |= 0x11;ET0 = 1;ET1 = 0;TR0 = 0;TR1 = 0;EA = 1;}void BeepTimer0(void) interrupt 1 //音符发生中断{BeepIO = !BeepIO;TH0 = Sound_Temp_TH0;TL0 = Sound_Temp_TL0;}//************************************************************************** void Play(unsigned char *Sound,unsigned char Signature,unsigned Octachord,unsigned int Speed){unsigned int NewFreTab[12]; //新的频率表unsigned char i,j;unsigned int Point,LDiv,LDiv0,LDiv1,LDiv2,LDiv4,CurrentFre,Temp_T,SoundLength; unsigned char Tone,Length,SL,SH,SM,SLen,XG,FD;for(i=0;i<12;i++) // 根据调号及升降八度来生成新的频率表{j = i + Signature;if(j > 11){j = j-12;NewFreTab[i] = FreTab[j]*2;}elseNewFreTab[i] = FreTab[j];if(Octachord == 1)NewFreTab[i]>>=2;else if(Octachord == 3)NewFreTab[i]<<=2;}SoundLength = 0;while(Sound[SoundLength] != 0x00) //计算歌曲长度{SoundLength+=2;}Point = 0;Tone = Sound[Point];Length = Sound[Point+1]; // 读出第一个音符和它时时值LDiv0 = 12000/Speed; // 算出1分音符的长度(几个10ms)LDiv4 = LDiv0/4; // 算出4分音符的长度LDiv4 = LDiv4-LDiv4*SOUND_SPACE; // 普通音最长间隔标准TR0 = 0;TR1 = 1;while(Point < SoundLength){SL=Tone%10; //计算出音符SM=Tone/10%10; //计算出高低音SH=Tone/100; //计算出是否升半CurrentFre = NewFreTab[SignTab[SL-1]+SH]; //查出对应音符的频率if(SL!=0){if (SM==1) CurrentFre >>= 2; //低音if (SM==3) CurrentFre <<= 2; //高音Temp_T = 65536-(50000/CurrentFre)*10/(12000000/SYSTEM_OSC);//计算计数器初值 Sound_Temp_TH0 = Temp_T/256;Sound_Temp_TL0 = Temp_T%256;TH0 = Sound_Temp_TH0;TL0 = Sound_Temp_TL0 + 12; //加12是对中断延时的补偿}SLen=LengthTab[Length%10]; //算出是几分音符XG=Length/10%10; //算出音符类型(0普通1连音2顿音)FD=Length/100;LDiv=LDiv0/SLen; //算出连音音符演奏的长度(多少个10ms)if (FD==1)LDiv=LDiv+LDiv/2;if(XG!=1)if(XG==0) //算出普通音符的演奏长度if (SLen<=4)LDiv1=LDiv-LDiv4;elseLDiv1=LDiv*SOUND_SPACE;elseLDiv1=LDiv/2; //算出顿音的演奏长度elseLDiv1=LDiv;if(SL==0) LDiv1=0;LDiv2=LDiv-LDiv1; //算出不发音的长度if (SL!=0){TR0=1;for(i=LDiv1;i>0;i--) //发规定长度的音{while(TF1==0);TH1 = Sound_Temp_TH1;TL1 = Sound_Temp_TL1;TF1=0;}}if(LDiv2!=0){TR0=0; BeepIO=1;for(i=LDiv2;i>0;i--) //音符间的间隔{while(TF1==0);TH1 = Sound_Temp_TH1;TL1 = Sound_Temp_TL1;TF1=0;}}Point+=2;Tone=Sound[Point];Length=Sound[Point+1];}BeepIO = 1;}//**************************************************************************#endif附录:以下为曲谱编码文件,自己可以根据规则,对照简谱编写曲谱编码表。
基于51单片机蜂鸣器发声的-C语言程序
本例使用延时函数实精品文档按键发音/* 名称:按键发音说明:按下不同的按键会是SOUNDE 发出不同频率的声音。
现不同频率的声音输出,以后也可使用定时器*/#include<reg51.h>#define uchar unsigned char#define uint unsigned intsbit BEEP=P3A 7;sbit K 仁 P1A4;sbit K2=P1A5;sbit K3=P1A6;sbit K4=P1A7;// 延时void DelayMS(uint x){uchar t;while(x--) for(t=0;t<120;t++);}// 按周期 t 发音void Play(uchar t){uchar i;for(i=0;i<100;i++){BEEP=~BEEP;DelayMS(t);}BEEP=0;}void main(){P1=0xff;BEEP=0;while(1){if(K1==0) Play(1);if(K2==0) Play(2);if(K3==0) Play(3);if(K4==0) Play(4);}}精品文档播放音乐/* 名称:播放音乐说明:程序运行时播放生日快乐歌, 未使用定时器中断,所有频率完全用延时 实现*/#include<reg51.h>#define uchar unsigned char#define uint unsigned intsbit BEEP=P3A7;// 生日快乐歌的音符频率表,不同频率由不同的延时来决定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_LON为拍子长度for(j=0;j<SONG_LONG[i]*20;j++){BEEP=~BEEP;〃SONG_TON延时表决定了每个音符的频率for(k=0;k<SONG_TONE[i]/3;k++);} DelayMS(10);i++;}}void main(){BEEP=0;while(1){PlayMusic(); // 播放生日快乐DelayMS(500); // 播放完后暂停一段时间}精品文档。
51单片机C语言蜂鸣器发声生日快乐播放程序
说明:按下不同的按键会是SOUNDER发出不同频率的声音。
本例使用延时函数实现不同频率的声音输出,以后也可使用定时器*/#include<reg51.h>#define uchar unsigned char#define uint unsigned intsbit BEEP=P3^7;sbit K1=P1^4;sbit K2=P1^5;sbit K3=P1^6;sbit K4=P1^7;//延时void DelayMS(uint x){uchar t;while(x--) for(t=0;t<120;t++);}//按周期t发音void Play(uchar t){uchar i;for(i=0;i<100;i++){BEEP=~BEEP;DelayMS(t);}BEEP=0;}void main(){P1=0xff;BEEP=0;while(1){if(K1==0) Play(1);if(K2==0) Play(2);if(K3==0) Play(3);if(K4==0) Play(4);}}说明:程序运行时播放生日快乐歌,未使用定时器中断,所有频率完全用延时实现*/#include<reg51.h>#define uchar unsigned char#define uint unsigned intsbit BEEP=P3^7;//生日快乐歌的音符频率表,不同频率由不同的延时来决定uchar code SONG_TONE[]={212,212,190,212,159,169,212,212,190,212,142,159,212,212,106,126,159,169,190,1 19,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<SONG_LONG[i]*20;j++){BEEP=~BEEP;//SONG_TONE延时表决定了每个音符的频率for(k=0;k<SONG_TONE[i]/3;k++);}DelayMS(10);i++;}}void main(){BEEP=0;while(1){PlayMusic(); //播放生日快乐DelayMS(500); //播放完后暂停一段时间}。
51单片机c语言编写电子琴+音乐播放器
此程序是51单片机c语言编写的电子琴+音乐播放器,当用杜邦线将P1^0管脚接地时,单片机就会放出《世上只有妈妈好》,将P1^1 管脚接地时单片机就播放《祝你生日快乐》,两者为高电平时进入电子钢琴模式,利用16个矩阵键盘发出相应的哆、瑞、米、发、索。
此文档的乐谱是借鉴别人的,因为本人是音乐白痴!这是我第一次发表,我希望能帮到有需要的人啦........#include <reg52.h>#define uchar unsigned char#define uint unsigned intsbit beep=P2^3; //定义蜂鸣器sbit music=P1^0; //music为低电平播放《世上只有妈妈好》(工作模式为音乐播放器),music1为低电平时播放祝你生日快乐,music、music1都为高电平时(工作模式为电子钢琴)sbit music1=P1^1;uchar timer0h,timer0l,time,key,flag; //flag==0时为电子钢琴模式,flag==1时为音乐播放器uchar code yuepu[]={ 6,2,3,5,2,1, 3,2,2, 5,2,2, 1,3,2, 6,2,1, 5,2,1, 6,2,4,3,2,2, 5,2,1, 6,2,1, 5,2,2, 3,2,2, 1,2,1, 6,1,1, 5,2,1, 3,2,1, 2,2,4, 2,2,3, 3,2,1, 5,2,2, 5,2,1,6,2,1, 3,2,2, 2,2,2, 1,2,4, 5,2,3, 3,2,1, 2,2,1, 1,2,1, 6,1,1, 1,2,1, 5,1,6, 0,0,0 }; //世上只有妈妈好乐谱uchar code yuepu1[]={5,1,1, 5,1,1, 6,1,2, 5,1,2, 1,2,2, 7,1,4,5,1,1, 5,1,1, 6,1,2, 5,1,2, 2,2,2, 1,2,4,5,1,1, 5,1,1, 5,2,2, 3,2,2, 1,2,2, 7,1,2, 6,1,2,4,2,1, 4,2,1, 3,2,2, 1,2,2, 2,2,2, 1,2,4};//祝你生日快乐乐谱uchar code TH_0[]={0xF2,0xF3,0xF5,0xF5,0xF6,0xF7,0xF8,0xF9,0xF9,0xFA,0xFA,0xFB,0xFB,0xFC,0xFC,0xFC,0xFD,0xFD,0xFD,0xFD,0xFE, // 28个音阶高八位频率表装进定时器高八位0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFF,} ;uchar code TL_0[]={0x42,0xC1,0x17,0xB6,0xD0,0xD1,0xB6,0x21,0xE1,0x8C,0xD8,0x68,0xE9,0x5B,0x8F, // 28个音阶低八位频率表装进定时器低八位0xEE,0x44, 0x6B,0xB4,0xF4,0x2D,0x47,0x77,0xA2,0xB6,0xDA,0xFA,0x16,};void delay(uchar t){uchar t1;unsigned long t2;for(t1=0;t1<t;t1++) //延时半拍函数,控制音乐节奏{for(t2=0;t2<8000;t2++){ ; }}}void song(){TH0=timer0h;TL0=timer0l; // 唱歌程序TR0=1;delay(time); //延时,让此音节播放的长短TR0=0; //关定时器,为下个音节响做准备}void delayms(uint x){uint i,j; //去抖延时for(i=x;i>0;i--)for(j=110;j>0;j--);}void keyscan() //检测十六个发出音乐按键函数{ uchar temp;P3=0xfe;temp=P3;temp=0xf0&temp;if(temp!=0x0f){ delayms(10); //检测第一行按键temp=P3 ;temp=temp&0xf0;if(temp!=0xf0){temp=P3;switch(temp){case 0xee: key=0;break;case 0xde: key=1;break;case 0xbe: key=2;break;case 0x7e: key=3;break;}TH0=TH_0[key]; //将按下的键对应发出此频率声音的初值装入TL0=TL_0[key];TR0=1; //打开定时器0while(temp!=0xf0) //一直按下就一直发声{temp=P3;temp=temp&0xf0;}TR0=0; //松手时关闭定时器0,不能发声beep=1;//使蜂鸣器置高关闭,预防在进入中断取反时将它置低打开}}P3=0xfd;temp=P3;temp=0xf0&temp;if(temp!=0x0f) // 检测第二行按键{ delayms(10);temp=P3 ;temp=temp&0xf0;if(temp!=0xf0){temp=P3;switch(temp){case 0xed: key=4;break;case 0xdd: key=5;break;case 0xbd: key=6;break;case 0x7d: key=7;break;}TH0=TH_0[key]; //将按下的键对应发出此频率声音的初值装入TL0=TL_0[key];定时器0while(temp!=0xf0) //一直按下就一直发声{temp=P3;temp=temp&0xf0;}TR0=0; //松手时关闭定时器0,不能发声beep=1; //使蜂鸣器置高关闭,预防在进入中断取反时将它置低打开}}P3=0xfb;temp=P3;temp=0xf0&temp;if(temp!=0x0f){ delayms(10);temp=P3 ;temp=temp&0xf0;if(temp!=0xf0){temp=P3;switch(temp){case 0xeb: key=8;break;case 0xdb: key=9;break;case 0xbb: key=10;break;case 0x7b: key=11;break;}TH0=TH_0[key]; //将按下的键对应发出此频率声音的初值装入TL0=TL_0[key];TR0=1; //打开定时器0while(temp!=0xf0) //一直按下就一直发声{temp=P3;temp=temp&0xf0;}手时关闭定时器0,不能发声beep=1; //使蜂鸣器置高关闭,预防在进入中断取反时将它置低打开}}P3=0xf7;temp=P3;temp=0xf0&temp;if(temp!=0x0f){ delayms(10);temp=P3 ;temp=temp&0xf0;if(temp!=0xf0){temp=P3;switch(temp){case 0xe7: key=12;break;case 0xd7: key=13;break;case 0xb7: key=14;break;case 0x77: key=15;break;}TH0=TH_0[key]; //将按下的键对应发出此频率声音的初值装入TL0=TL_0[key];TR0=1; //打开定时器0while(temp!=0xf0) //一直按下就一直发声{temp=P3;temp=temp&0xf0;}TR0=0; //松手时关闭定时器0,不能发声beep=1; //使蜂鸣器置高关闭,预防在进入中断取反时将它置低打开}}}void main(){uchar k,i;TMOD=1;EA=1;ET0=1;while(1){if(music&&music1) //当两个都为高电平时没有放音乐,扫描键盘{ flag=0;keyscan();}if(!music) //当P1^0管脚为低电平时,播放《世上只有妈妈好》{ flag=1;i=0;while(i<100) //音乐数组长度,唱完从头再来{if(music) //在播放音乐时P1^0为高电平时退出播放此歌曲{ beep=1; //使蜂鸣器置高关闭,预防在进入中断取反时将它置低打开break;}k=yuepu[i]+7*yuepu[i+1]-1;timer0h=TH_0[k]; // 把对应的音乐频率放进定时器0timer0l=TL_0[k];time=yuepu[i+2]; //音乐节拍i+=3;song();}}if(!music1) // music为低电平时,播放《祝你生日快乐》{ flag=1;i=0;while(i<75) //乐谱的长度{if(music1){beep=1;break;}k=yuepu1[i]+7*yuepu1[i+1]-1;timer0h=TH_0[k];timer0l=TL_0[k];time=yuepu1[i+2];i+=3;song();}}}}void time0() interrupt 1{if(flag==0){beep=!beep; //电子琴模式时进入此语句TH0=TH_0[key];TL0=TL_0[key];}else{ beep=!beep;TH0=timer0h; //音乐播放器模式进入此语句TL0=timer0l;}}。
51单片机唱歌 C51音乐程序
51单片机唱歌 C51音乐程序#i nclude <reg52.h>#i nclude <intrins.h>//本例采用89C52, 晶振为11.0592MHZ//关于如何编制音乐代码, 其实十分简单,各位可以看以下代码.//频率常数即音乐术语中的音调,而节拍常数即音乐术语中的多少拍;//所以拿出谱子, 试探编吧!unsigned char n=0; //n为节拍常数变量unsigned char code music_tab[] ={0x18, 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 int0() interrupt 1 //采用中断0 控制节拍{ TH0=0xd8;TL0=0xef;n--;}void delay (unsigned char m) //控制频率延时{unsigned i=3*m;while(--i);}void delayms(unsigned char a) //豪秒延时子程序{while(--a); //采用while(--a) 不要采用while(a--); 各位可编译一下看看汇编结果就知道了!}void main(){ unsigned char p,m; //m为频率常数变量unsigned char i=0;TMOD&=0x0f;TMOD =0x01;TH0=0xd8;TL0=0xef;IE=0x82;play:while(1){a: p=music_tab[i];if(p==0x00) { i=0, delayms(1000); goto play;} //如果碰到结束符,延时1秒,回到开始再来一遍else if(p==0xff) { i=i+1;delayms(100),TR0=0; goto a;} //若碰到休止符,延时100ms,继续取下一音符else {m=music_tab[i++], n=music_tab[i++];} //取频率常数和节拍常数TR0=1; //开定时器1while(n!=0) P1=~P1,delay(m); //等待节拍完成, 通过P1口输出音频(可多声道哦!)TR0=0; //关定时器1 }}。
单片机蜂鸣器音乐代码
单片机蜂鸣器音乐代码在单片机的应用中,蜂鸣器是一个常用的音频输出设备。
它可以通过产生不同频率的声音来实现音乐播放、提醒和警报等功能。
本文将介绍如何使用单片机控制蜂鸣器播放音乐,并提供一个简单的音乐代码示例。
首先,让我们了解一下单片机蜂鸣器的工作原理。
蜂鸣器实际上是一个压电陶瓷元件,当电压作用于其上时,它会振动产生声音。
为了产生不同的音调,我们需要控制蜂鸣器的频率和占空比。
单片机通过IO口与蜂鸣器连接,并使用定时器来产生所需的频率。
具体的代码实现将依赖于使用的单片机型号和开发环境。
在这里,我们将以C语言为例,并基于51单片机进行说明。
以下是一个简单的单片机蜂鸣器音乐代码示例,演奏的是《欢乐颂》的前几个音符:```c#include <reg52.h>// 定义各个音符的频率#define C4 4778#define D4 4257#define E4 3792#define F4 3579#define G4 3189#define A4 2841#define B4 2531#define C5 2388// 延时函数void delay(unsigned int count){while(count--);}// 发声函数void beep(unsigned int frequency, unsigned int duration) {unsigned int i;unsigned long time;time = 11059200 / frequency; time >>= 1;for(i = 0; i < duration; i++) {P1 = 0x08;delay(time);P1 = 0x00;delay(time);}}// 主函数void main(){// 设置定时器TMOD = 0x01;TH0 = (65536 - 50000) / 256; TL0 = (65536 - 50000) % 256; TR0 = 1;// 播放音乐beep(C4, 100);beep(D4, 100);beep(E4, 100);beep(F4, 100);beep(G4, 100);beep(A4, 100);beep(B4, 100);beep(C5, 100);while(1);}```以上代码中,我们首先定义了各个音符的频率,这些频率是通过实验测量得到的,具体数值可能因蜂鸣器型号、电源电压等因素而略有差异。
单片机音乐播放c语言程序
0x47,0x77,0xA2,0xB6,0xDA,0xFA,0x16,
};
4,2,1, 4,2,1, 3,2,2, 1,2,2, 2,2,2, 1,2,4
};
//生日歌
// 音阶频率表 高八位
code uchar FREQH[]={
0xF2,0xF3,0xF5,0xF5,0xF6,0xF7,0xF8,
0x42,0xC1,0x17,0xB6,0xD0,0xD1,0xB6,
0x21,0xE1,0x8C,0xD8,0x68,0xE9,0x5B,0x8F, //1,2,3,4,5,6,7,8,i
/* 函数声明 */
/*******************************************************/
uchar keyscan(void); //键盘扫描
void delay1(uint i); //演示程序
0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFF,
} ;
// 音阶频率表 低八位
code uchar FREQL[]={
0xF9,0xF9,0xFA,0xFA,0xFB,0xFB,0xFC,0xFC, //1,2,3,4,5,6,7,8,i
0xFC,0xFD,0xFD,0xFD,0xFD,0xFE,
延时程序
------------------------------------------------*/
cord_l=P3&0xf0; //读入行线值
return(cord_h+cord_l);//键盘最后组合码值
单片机音乐播放C语言程序
单片机音乐播放C语言程序#include ;#define uchar unsigned char#define uint unsinged intsbit speaker = P1^7;uchar t0h,t0l,time;//--------------------------------------//单片机晶振采用11.0592MHz// 频率-半周期数据表高八位本软件共保存了四个八度的28个频率数据uchar code FREQH[] = {0x01, //0的时候没有音符 0xF2, 0xF3, 0xF5, 0xF5, 0xF6, 0xF7, 0xF8, //低音12345670xF9, 0xF9, 0xFA, 0xFA, 0xFB, 0xFB, 0xFC,0xFC,//1,2,3,4,5,6,7,i0xFC, 0xFD, 0xFD, 0xFD, 0xFD, 0xFE, //高音2345670xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFF}; //超高音 1234567//-----------------------------------------// 频率-半周期数据表低八位uchar code FREQL[] = {0x01, //0的时候没有音符 0x42, 0xC1, 0x17, 0xB6, 0xD0, 0xD1, 0xB6, //低音12345670x21, 0xE1, 0x8C, 0xD8, 0x68, 0xE9, 0x5B, 0x8F, //1,2,3,4,5,6,7,i0xEE, 0x44, 0x6B, 0xB4, 0xF4, 0x2D, //高音2345670x47, 0x77, 0xA2, 0xB6, 0xDA, 0xFA, 0x16}; //超高音 1234567//--------------------------------------//一个音符有三个数字。
51单片机 音乐 天空之城 C语言
H6,H5, /**/H3,H2,H1,H1,/**/H2,H1,H2,Z7,/**/Z6,Z6,Z7, /**/Z6,
0xff}; //以0xff作为音符的结束标志
TH0=(8192-C)/32; //可证明这是13位计数器TH0高8位的赋初值方法
TL0=(8192-C)%32; //可证明这是13位计数器TL0低5位的赋初值方法
}
桂林电子科技大学
函数功能:定时器T0的中断服务子程序,使P3.7引脚输出音频的方波
************************************************************/
void Time0(void ) interrupt 1 using 1
{
sound=!sound; //将P3.7引脚输出电平取反,形成方波
#define L6 440 //将"l_a"宏定义为低音"6"的频率440Hz
#define L7 494 //将"L7"宏定义为低音"7"的频率494Hz
//以下是C调中音的音频宏定义
#define Z1 523 //将"Z1"宏定义为中音"1"的频率523Hz
#define Z2 587 //将"Z2"宏定义为中音"2"的频率587Hz
unsigned int code f[]={Z6,Z7,/**/H1,Z7,H1,H3,/**/Z7,Z3,Z3, /**/Z6,Z5,Z6,H1,/**/Z5,Z3,Z3, /**/Z4,Z3,Z4,H1,