c51单片机_音乐c程序代码

合集下载

单片机_音乐播放的c代码

单片机_音乐播放的c代码

单片机音乐播放的c代码#include "reg52.h"unsigned char Count;sbit _Speak =P2^3 ; //讯响器控制脚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;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++ );}}/*-------------------------------------------------功能:歌曲播放子程序i为播放哪一段曲目-------------------------------------------------*/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); //播放}}6回答者:小崔凡凡- 二级2009-10-16 21:47我来评论>>提问者对于答案的评价:谢谢!相关内容• 单片机播放音乐里的音乐代码是怎么得到的?有什么软件能将歌曲直接转换成16进制代码 4 2008-11-14• 给个单片机C语言编的音乐程序,能够播放音乐 6 2009-7-22• 求用c语言编写的播放背景音乐的代码 5 2009-6-12• 单片机音乐代码问题 2009-9-28• 单片机音乐代码 5 2009-5-18更多相关问题>>查看同主题问题:单片机音乐音乐播放播放代码等待您来回答∙nokia 3100c音乐播放器如何跟新∙为什么用酷我音乐盒听歌后那歌会在C:\ProgramData\mcache里面呢∙深圳龙华天龙大道珍藏版狂嗨现场dj明仔vs女mc小黎茂名细飞11分钟半时候的那音乐谁知道叫什么名字!∙夏普9020c合上盖子怎么听音乐?∙C:Documents and SettingsAdministrator「开始」菜单酷我音乐盒2010.lnk∙nokia 2600c手机可以用什么音乐播放器?∙『windows』在网页制作中,下列不是背景音乐格式的是那一个?A.tem B mp3 C wav D∙3110c播放音乐时会卡,之后会自动到下一首,怎么办?其他回答共3 条我的博客上有三首连放的,你只要加两首就行了。

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

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单片机蜂鸣器播放音乐代码(生日快乐 两只蝴蝶 祝你平安)精编版

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); //播放}}。

简易电子琴单片机c51程序

简易电子琴单片机c51程序

#include "reg51.h"void tinit(void);void delay_ms(unsigned int);sbit P27=P2^7;sbit P26=P2^6;sbit P25=P2^5;unsigned char code Music_Code[]={0xf8,0x8b,0xf9,0x5b,0xfa,0x14,0xfa,0x66,0xfb,0x03,0xfb,0x8f,0xfc,0x0b,0xfb,0xee} ;unsigned char const yinful[3][14]={0xf8,0x8b,0xf9,0x5b,0xfa,0x14,0xfa,0x66,0xfb,0x03,0xfb,0x8f,0xfc,0x0b, 0xfc,0x43,0xfc,0xab,0xfd,0x08,0xfd,0x33,0xfd,0x81,0xfd,0xc7,0xfe,0x05, 0xfb,0x21,0xfe,0x55,0xfe,0x84,0xfe,0x99,0xfe,0xc0,0xfe,0xe3,0xff,0x02 } ;unsigned char const length[]={1,2,4,8,16,32,64};unsigned char sth,stl;void main(void){unsigned char j;unsigned int i;tinit();while(1){switch(P1){case 0xfe: delay_ms(10);if(P1==0xfe){i=0;TR0=0;sth=Music_Code[2*i];stl=Music_Code[2*i+1];TR0=1;for(j=0;j<=100;j++){delay_ms(10);if(P1==0xfe)continue;else break;}TR0=0;for(;;){while(P1==0xfe)delay_ms(10);if(P1==0xfe) continue;else break;}}TR0=0;break;case 0xfd: delay_ms(10);if(P1==0xfd){i=1;TR0=0;sth=Music_Code[2*i];stl=Music_Code[2*i+1];TR0=1;for(j=0;j<=100;j++){delay_ms(10);if(P1==0xfd)continue;else break;}TR0=0;for(;;){while(P1==0xfd)delay_ms(10);if(P1==0xfd) continue;else break;}}TR0=0;break;case 0xfb: delay_ms(10);if(P1==0xfb){i=2;TR0=0;sth=Music_Code[2*i];stl=Music_Code[2*i+1];TR0=1;for(j=0;j<=100;j++){delay_ms(10);if(P1==0xfb)continue;else break;}TR0=0;for(;;){while(P1==0xfb)delay_ms(10);if(P1==0xfb) continue;else break;}}TR0=0;break;case 0xf7: delay_ms(10) ;if(P1==0xf7){i=3;TR0=0;sth=Music_Code[2*i];stl=Music_Code[2*i+1];TR0=1;for(j=0;j<=100;j++){delay_ms(10);if(P1==0xf7)continue;else break;}TR0=0;for(;;){while(P1==0xf7)delay_ms(10);if(P1==0xf7) continue;else break;}}TR0=0;break;case 0xef: delay_ms(10);if(P1==0xef){i=4;TR0=0;sth=Music_Code[2*i];stl=Music_Code[2*i+1];TR0=1;for(j=0;j<=100;j++){delay_ms(10);if(P1==0xef)continue;else break;}TR0=0;for(;;){while(P1==0xef)delay_ms(10);if(P1==0xef) continue;else break;}}TR0=0;break;case 0xdf: delay_ms(10);if(P1==0xdf){i=5;TR0=0;sth=Music_Code[2*i];stl=Music_Code[2*i+1];TR0=1;for(j=0;j<=100;j++){delay_ms(10);if(P1==0xdf)continue;else break;}TR0=0;for(;;){while(P1==0xdf)delay_ms(10);if(P1==0xdf) continue;else break;}}TR0=0;break;case 0xbf: delay_ms(10);if(P1==0xbf){i=6;TR0=0;sth=Music_Code[2*i];stl=Music_Code[2*i+1];TR0=1;for(j=0;j<=100;j++){delay_ms(10);if(P1==0xbf)continue;else break;}TR0=0;for(;;){while(P1==0xbf)delay_ms(10);if(P1==0xbf) continue;else break;}}TR0=0;break;case 0x7f: delay_ms(10);if(P1==0x7f){i=7;TR0=0;sth=Music_Code[2*i];stl=Music_Code[2*i+1];TR0=1;for(j=0;j<=100;j++){delay_ms(10);if(P1==0x7f)continue;else break;}TR0=0;for(;;){while(P1==0x7f)delay_ms(10);if(P1==0x7f) continue;else break;}}TR0=0;break;default: break;}}}void delay_ms(unsigned int k){unsigned int i0;unsigned char i,j;for(i0=0;i0<k;i0++){for(i0=0;i0<k;i0++)for(i=5;i>0;i--)for(j=97;j>0;j--);}}void tinit(void){TMOD=0x01;TH0=sth;TL0=stl;ET0=1;EA=1;TR0=0;}void t0(void) interrupt 1 {TR0=0;TH0=sth;TL0=stl;P27=!P27;TR0=1;}。

51单片机唱歌 C51音乐程序[整理]

51单片机唱歌   C51音乐程序[整理]

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}}。

(完整版)C51单片机C语言程序100例_Keil

(完整版)C51单片机C语言程序100例_Keil

目录目录 (1)函数的使用和熟悉********************************/ (4)实例3:用单片机控制第一个灯亮 (4)实例4:用单片机控制一个灯闪烁:认识单片机的工作频率 (4)实例5:将P1口状态分别送入P0、P2、P3口:认识I/O口的引脚功能 (5)实例6:使用P3口流水点亮8位LED (5)实例7:通过对P3口地址的操作流水点亮8位LED (6)实例8:用不同数据类型控制灯闪烁时间 (7)实例9:用P0口、P1口分别显示加法和减法运算结果 (8)实例10:用P0、P1口显示乘法运算结果 (9)实例11:用P1、P0口显示除法运算结果 (9)实例12:用自增运算控制P0口8位LED流水花样 (10)实例13:用P0口显示逻辑"与"运算结果 (10)实例14:用P0口显示条件运算结果 (11)实例15:用P0口显示按位"异或"运算结果 (11)实例16:用P0显示左移运算结果 (11)实例17:"万能逻辑电路"实验 (11)实例18:用右移运算流水点亮P1口8位LED (12)实例19:用if语句控制P0口8位LED的流水方向 (13)实例20:用swtich语句的控制P0口8位LED的点亮状态 (13)实例21:用for语句控制蜂鸣器鸣笛次数 (14)实例22:用while语句控制LED (16)实例23:用do-while语句控制P0口8位LED流水点亮 (16)实例24:用字符型数组控制P0口8位LED流水点亮 (17)实例25:用P0口显示字符串常量 (18)实例26:用P0口显示指针运算结果 (19)实例27:用指针数组控制P0口8位LED流水点亮 (19)实例28:用数组的指针控制P0口8位LED流水点亮 (20)实例29:用P0、P1口显示整型函数返回值 (21)实例30:用有参函数控制P0口8位LED流水速度 (22)实例31:用数组作函数参数控制流水花样 (23)实例32:用指针作函数参数控制P0口8位LED流水点亮 (23)实例33:用函数型指针控制P1口灯花样 (25)实例34:用指针数组作为函数的参数显示多个字符串 (26)实例35:字符函数ctype.h应用举例 (27)实例36:内部函数intrins.h应用举例 (27)实例37:标准函数stdlib.h应用举例 (28)实例38:字符串函数string.h应用举例 (29)实例39:宏定义应用举例2 (29)1/192实例40:宏定义应用举例2 (30)实例41:宏定义应用举例3 (30)中断、定时器************************************************ (31)实例42:用定时器T0查询方式P2口8位控制LED闪烁 (31)实例43:用定时器T1查询方式控制单片机发出1KHz音频 (31)实例44:将计数器T0计数的结果送P1口8位LED显示 (32)实例45:用定时器T0的中断控制1位LED闪烁 (33)实例46:用定时器T0的中断实现长时间定时 (34)实例47:用定时器T1中断控制两个LED以不同周期闪烁 (34)实例48:用计数器T1的中断控制蜂鸣器发出1KHz音频 (36)实例49:用定时器T0的中断实现"渴望"主题曲的播放 (36)实例50-1:输出50个矩形脉冲 (39)实例50-2:计数器T0统计外部脉冲数 (40)实例51-2:定时器T0的模式2测量正脉冲宽度 (40)实例52:用定时器T0控制输出高低宽度不同的矩形波 (41)实例53:用外中断0的中断方式进行数据采集 (42)实例54-1:输出负脉宽为200微秒的方波 (43)实例54-2:测量负脉冲宽度 (43)实例55:方式0控制流水灯循环点亮 (44)实例56-1:数据发送程序 (45)实例56-2:数据接收程序 (47)实例57-1:数据发送程序 (47)实例57-2:数据接收程序 (49)实例58:单片机向PC发送数据 (50)实例59:单片机接收PC发出的数据 (51)*********************************数码管显示 (52)实例60:用LED数码显示数字5 (52)实例61:用LED数码显示器循环显示数字0~9 (52)实例62:用数码管慢速动态扫描显示数字"1234" (53)实例63:用LED数码显示器伪静态显示数字1234 (54)实例64:用数码管显示动态检测结果 (54)实例65:数码秒表设计 (56)实例66:数码时钟设计 (58)实例67:用LED数码管显示计数器T0的计数值 (62)实例68:静态显示数字“59” (63)*****************************键盘控制2/192*****************************************************/ (63)实例69:无软件消抖的独立式键盘输入实验 (64)实例70:软件消抖的独立式键盘输入实验 (64)实例71:CPU控制的独立式键盘扫描实验 (65)实例72:定时器中断控制的独立式键盘扫描实验 (68)实例73:独立式键盘控制的4级变速流水灯 (71)实例74:独立式键盘的按键功能扩展:"以一当四" (73)实例75:独立式键盘调时的数码时钟实验 (75)实例76:独立式键盘控制步进电机实验 (79)实例77:矩阵式键盘按键值的数码管显示实验 (82)//实例78:矩阵式键盘按键音 (85)实例79:简易电子琴 (86)实例80:矩阵式键盘实现的电子密码锁 (92)**************************************************************************液晶显示LCD*********液晶显示LCD*****液晶显示LCD************************************************************************/ (95)实例81:用LCD显示字符'A' (96)实例82:用LCD循环右移显示"Welcome to China" (99)实例83:用LCD显示适时检测结果 (102)实例84:液晶时钟设计 (106)******************************************一些芯片的使用*****24c02........ DS18B20X5045ADC0832DAC0832DS1302红外遥控**********************************************/ (112)实例85:将数据"0x0f"写入AT24C02再读出送P1口显示 (112)实例86:将按键次数写入AT24C02,再读出并用1602LCD显示 (117)实例87:对I2C总线上挂接多个AT24C02的读写操作 (124)实例88:基于AT24C02的多机通信读取程序 (129)实例88:基于AT24C02的多机通信写入程序 (133)实例90:DS18B20温度检测及其液晶显示 (144)实例91:将数据"0xaa"写入X5045再读出送P1口显示 (153)实例92:将流水灯控制码写入X5045并读出送P1口显示 (157)实例93:对SPI总线上挂接多个X5045的读写操作 (161)实例94:基于ADC0832的数字电压表 (165)实例95:用DAC0832产生锯齿波电压 (171)实例96:用P1口显示红外遥控器的按键值 (171)实例97:用红外遥控器控制继电器 (174)实例98:基于DS1302的日历时钟 (177)实例99:单片机数据发送程序 (185)实例100:电机转速表设计 (186)模拟霍尔脉冲 (192)/********************************************************* ***函数的使用和熟悉***************************************************************///实例3:用单片机控制第一个灯亮#include<reg51.h>//包含51单片机寄存器定义的头文件void main(void){P1=0xfe;//P1=11111110B,即P1.0输出低电平}//4//实例4:用单片机控制一个灯闪烁:认识单片机的工作频率#include<reg51.h>//包含单片机寄存器的头文件/****************************************函数功能:延时一段时间*****************************************/void delay(void)//两个void意思分别为无需返回值,没有参数传递{unsigned int i;//定义无符号整数,最大取值范围65535for(i=0;i<20000;i++)//做20000次空循环;//什么也不做,等待一个机器周期}/*******************************************************函数功能:主函数(C语言规定必须有也只能有1个主函数)********************************************************/void main(void){while(1)//无限循环{P1=0xfe;//P1=11111110B,P1.0输出低电平delay();//延时一段时间P1=0xff;//P1=11111111B,P1.0输出高电平delay();//延时一段时间// 5 P1 P0 P2 P3 I/O //实例 5:将 #include<reg51.h> P1 口状态分别送入 P0、P2、P3 口:认识 I/O口 的引脚功能//包含单片机寄存器的头文件/*******************************************************函数功能:主函数 (C 语言规定必须有也只能有 1个主函数)********************************************************/ void main(void){while(1) //无限循环{P1=0xff; // P1=1111 1111B,熄灭 LEDP0=P1; // 将 P1 口状态送入 P0 口P2=P1; // 将 P1 口状态送入 P2 口P3=P1; // 将 P1 口状态送入 P3口}}//实例 6:使用 P3 口流水点亮 8 位LED #include<reg51.h> //包含单片机寄存器的头文件/****************************************函数功能:延时一段时间*****************************************/void delay(void){unsigned char i,j;for(i=0;i<250;i++)for(j=0;j<250;j++);}/*******************************************************函数功能:主函数********************************************************/ voidmain(void){while(1){P3=0xfe; delay(); P3=0xfd; delay(); P3=0xfb; delay(); P3=0xf7; delay(); P3=0xef; //第一个灯亮//调用延时函数//第二个灯亮//调用延时函数//第三个灯亮//调用延时函数//第四个灯亮//调用延时函数//第五个灯亮delay(); //调用延时函数P3=0xdf; delay(); P3=0xbf;//第六个灯亮//调用延时函数//第七个灯亮delay(); //调用延时函数P3=0x7f; //第八个灯亮} }delay(); //调用延时函数//实例7:通过对P3口地址的操作流水点亮8位LED#include<reg51.h>//包含单片机寄存器的头文件sfr x=0xb0;//P3口在存储器中的地址是b0H,通过sfr可定义8051内核单片机//的所有内部8位特殊功能寄存器,对地址x的操作也就是对P1口的操作/****************************************函数功能:延时一段时间*****************************************/void delay(void){unsigned char i,j;for(i=0;i<250;i++)for(j=0;j<250;j++);//利用循环等待若干机器周期,从而延时一段时间}/*****************************************函数功能:主函数******************************************/void main(void){while(1){x=0xfe;//第一个灯亮delay();//调用延时函数x=0xfd;//第二个灯亮delay();//调用延时函数x=0xfb;//第三个灯亮delay();//调用延时函数x=0xf7;//第四个灯亮delay();//调用延时函数x=0xef;//第五个灯亮delay();//调用延时函数x=0xdf;//第六个灯亮delay();//调用延时函数x=0xbf;//第七个灯亮delay();//调用延时函数x=0x7f;//第八个灯亮delay();//调用延时函数}}//实例8:用不同数据类型控制灯闪烁时间#include<reg51.h>//包含单片机寄存器的头文件/******************************************************函数功能:用整形数据延时一段时间******************************************************/void int_delay(void)//延时一段较长的时间{unsigned int m;//定义无符号整形变量,双字节数据,值域为0~65535 for(m=0;m<36000;m++);//空操作}/******************************************************函数功能:用字符型数据延时一段时间******************************************************/void char_delay(void)//延时一段较短的时间{unsigned char i,j;//定义无符号字符型变量,单字节数据,值域0~255 for(i=0;i<200;i++)for(j=0;j<180;j++);//空操作}/******************************************************函数功能:主函数******************************************************/void main(void){unsigned char i;while(1){for(i=0;i<3;i++){P1=0xfe;//P1.0口的灯点亮int_delay();//延时一段较长的时间P1=0xff;//熄灭int_delay();//延时一段较长的时间}for(i=0;i<3;i++){P1=0xef;//P1.4口的灯点亮char_delay();//延时一段较长的时间P1=0xff;//熄灭char_delay();//延时一段较长的时间}}}//实例9:用P0口、P1口分别显示加法和减法运算结果#include<reg51.h>void main(void){unsigned char m,n;m=43; //即十进制数2x16+11=43n=60;P1=m+n; //即十进制数3x16+12=60//P1=103=01100111B,结果P1.3、P1.4、P1.7 口的灯被点亮}P0=n-m; //P0=17=00010001B,结果P0.0、P0.4的灯被熄灭//实例10:用P0、P1口显示乘法运算结果#include<reg51.h>//包含单片机寄存器的头文件void main(void){unsigned char m,n;unsigned int s;m=64;n=71;s=m*n;//s=64*71=4544,需要16位二进制数表示,高8位送P1口,低8位送P0口//由于4544=17*256+192=H3*16*16*16+H2*16*16+H1*16+H0//两边同除以256,可得17+192/256=H3*16+H2+H1*16+H0)/256//因此,高8位16进制数H3*16+H2必然等于17,即4544除以256的商//低8位16进制数H1*16+H0必然等于192,即4544除以256的余数P1=s/256;//高8位送P1口,P1=17=11H=00010001B,P1.0和P1.4口灭,其余亮P0=s%256;//低8位送P0口,P3=192=c0H=11000000B,P3.1,P3.6,P3.7口灭,其余亮}//实例11:用P1、P0口显示除法运算结果#include<reg51.h>//包含单片机寄存器的头文件void main(void){P1=36/5;//求整数P0=((36%5)*10)/5;//求小数while(1); //无限循环防止程序“跑飞”}//实例12:用自增运算控制P0口8位LED流水花样#include<reg51.h>//包含单片机寄存器的头文件/******************************************************函数功能:延时一段时间******************************************************/void delay(void){unsigned int i;for(i=0;i<20000;i++);}/******************************************************函数功能:主函数******************************************************/void main(void){unsigned char i;for(i=0;i<255;i++)//注意i的值不能超过255{P0=i;//将i的值送P0口delay();//调用延时函数}}//实例13:用P0口显示逻辑"与"运算结果#include<reg51.h>//包含单片机寄存器的头文件void main(void){P0=(4>0)&&(9>0xab);//将逻辑运算结果送P0口while(1);//设置无限循环,防止程序“跑飞”}//14P0//实例14:用P0口显示条件运算结果#include<reg51.h>//包含单片机寄存器的头文件void main(void){P0=(8>4)?8:4;//将条件运算结果送P0口,P0=8=00001000B while(1);//设置无限循环,防止程序“跑飞”}//实例15:用P0口显示按位"异或"运算结果#include<reg51.h>//包含单片机寄存器的头文件void main(void){P0=0xa2^0x3c;//将条件运算结果送P0口,P0=8=00001000B while(1);//设置无限循环,防止程序“跑飞”}//16P0//实例16:用P0显示左移运算结果#include<reg51.h>//包含单片机寄存器的头文件void main(void){P0=0x3b<<2;//将左移运算结果送P0口,P0=11101100B=0xec while(1);//无限循环,防止程序“跑飞”}#include<reg51.h> //实例17:"万能逻辑电路"实验//包含单片机寄存器的头文件sbit X=P1^5; sbit Y=P1^6; sbit Z=P1^7; void main(void) {while(1){ //将X位定义为//将Y位定义为//将Z位定义为P1.5P1.6P1.7} }F=((~X)&Y)|Z;//将逻辑运算结果赋给F;//实例18:用右移运算流水点亮P1口8位LED#include<reg51.h>//包含单片机寄存器的头文件/*****************************函数功能:延时一段时间*****************************/void delay(void){unsigned int n;for(n=0;n<30000;n++);}/*****************************函数功能:主函数*****************************/void main(void){unsigned char i;while(1){P1=0xff;delay();for(i=0;i<8;i++)//设置循环次数为8{P1=P1>>1;//每次循环P1的各二进位右移1位,高位补0delay();//调用延时函数}}}//19iff P08LED//实例19:用iff语句控制P0口8位LED的流水方向#include<reg51.h>//包含单片机寄存器的头文件sbit S1=P1^4;//将S1位定义为P1.4sbit S2=P1^5;//将S2位定义为P1.5/*****************************函数功能:主函数*****************************/void main(void){while(1){if(S1==0)//如果按键S1按下P0=0x0f;//P0口高四位LED点亮if(S2==0)//如果按键S2按下P0=0xf0;//P0口低四位LED点亮}}//实例20:用swtich语句的控制P0口8位LED的点亮状态#include<reg51.h>//包含单片机寄存器的头文件sbit S1=P1^4;//将S1位定义为P1.4/*****************************函数功能:延时一段时间*****************************/void delay(void){unsigned int n;for(n=0;n<10000;n++);}/*****************************函数功能:主函数*****************************/void main(void){unsigned char i;i=0;while(1){//将i初始化为0if(S1==0) {delay();//如果S1键按下//延时一段时间} if(S1==0)//如果再次检测到S1键按下i++;//i自增1if(i==9)//如果i=9,重新将其置为1 i=1;} switch(i)}{}//使用多分支选择语句case1:P0=0xfe;//第一个LED亮break;case2:P0=0xfd;//第二个LED亮break;case3:P0=0xfb;//第三个LED亮break;case4:P0=0xf7;//第四个LED亮break;case5:P0=0xef;//第五个LED亮break;case6:P0=0xdf;//第六个LED亮break;case7:P0=0xbf;//第七个LED亮break;case8:P0=0x7f;//第八个LED亮break;default://缺省值,关闭所有LEDP0=0xff;//21for//实例21:用for语句控制蜂鸣器鸣笛次数#include<reg51.h>//包含单片机寄存器的头文件sbit sound=P3^7;//将sound位定义为P3.7/**************************************** 函数功能:延时形成1600Hz音频****************************************/ void delay1600(void){unsigned char n;for(n=0;n<100;n++);}/**************************************** 函数功能:延时形成800Hz音频****************************************/ void delay800(void){unsigned char n;for(n=0;n<200;n++);}/**************************************** 函数功能:主函数****************************************/ void main(void){unsigned int i;while(1){for(i=0;i<830;i++){sound=0;//P3.7输出低电平delay1600();sound=1;//P3.7输出高电平delay1600();}for(i=0;i<200;i++){sound=0;//P3.7输出低电平delay800();sound=1;//P3.7输出高电平delay800();}}}//实例22:用whille语句控制LED#include<reg51.h> //包含单片机寄存器的头文件/****************************************函数功能:延时约60ms(3*100*200=60000μs)****************************************/void delay60ms(void){unsigned char m,n;for(m=0;m<100;m++)for(n=0;n<200;n++);}/****************************************函数功能:主函数****************************************/void main(void){unsigned char i;while(1)//无限循环{i=0;//将i初始化为0while(i<0xff)//当i小于0xff(255)时执行循环体{P0=i;//将i送P0口显示delay60ms();//延时i++;//i自增1}}}//实例23:用do-whiile语句控制P0口8位LED流水点亮#include<reg51.h>//包含单片机寄存器的头文件/****************************************函数功能:延时约60ms(3*100*200=60000μs)****************************************/void delay60ms(void){unsigned char m,n;for(m=0;m<100;m++)for(n=0;n<200;n++);}/****************************************函数功能:主函数****************************************/void main(void){do{P0=0xfe;//第一个LED亮delay60ms();P0=0xfd;//第二个LED亮delay60ms();P0=0xfb;//第三个LED亮delay60ms();P0=0xf7;//第四个LED亮delay60ms();P0=0xef;//第五个LED亮delay60ms();P0=0xdf;//第六个LED亮delay60ms();delay60ms();P0=0xbf;//第七个LED亮delay60ms();P0=0x7f;//第八个LED亮delay60ms();}while(1);//无限循环,使8位LED循环流水点亮}//实例24:用字符型数组控制P0口8位LED流水点亮#include<reg51.h>//包含单片机寄存器的头文件/****************************************函数功能:延时约60ms(3*100*200=60000μs)****************************************/void delay60ms(void){unsigned char m,n;for(m=0;m<100;m++)} for(n=0;n<200;n++);/****************************************函数功能:主函数****************************************/void main(void){unsigned char i;unsigned char code Tab[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};//定义无符号字符型数组while(1){for(i=0;i<8;i++){P0=Tab[i];//依次引用数组元素,并将其送P0口显示delay60ms();//调用延时函数}}}//25P0//实例25:用P0口显示字符串常量#include<reg51.h> //包含单片机寄存器的头文件/*************************************************函数功能:延时约150ms(3*200*250=150000μs=150ms*************************************************/void delay150ms(void){unsigned char m,n;for(m=0;m<200;m++)for(n=0;n<250;n++);}/*************************************************函数功能:主函数*************************************************/void main(void){unsigned char str[]={"Now,Temperature is:"};//将字符串赋给字符型全部元素赋值unsigned char i;while(1){i=0; //将i初始化为0,从第一个元素开始显示} }while(str[i]!='\0')//只要没有显示到结束标志'\0'{P0=str[i];//将第i个字符送到P0口显示delay150ms();//调用150ms延时函数i++;//指向下一个待显字符}//实例26:用P0#include<reg51.h>void main(void){口显示指针运算结果unsigned char*p1,*p2; //定义无符号字符型指针变量p1,p2 unsigned char i,j; //定义无符号字符型数据i=25; j=15;p1=&i ;p2=&j ; //给i赋初值25//使指针变量指向i//使指针变量指向j,对指针初始化,对指针初始化P0=*p1+*p2; //*p1+*p2相当于i+j,所以P0=25+15=40=0x28}//则P0=00101000B,结果P0.3、P0.5引脚LED熄灭,其余点亮while(1);//无限循环,防止程序“跑飞”//27P08LED//实例27:用指针数组控制P0口8位LED流水点亮#include<reg51.h>/************************************************* 函数功能:延时约150ms(3*200*250=150000μs=150ms *************************************************/ void delay150ms(void){} for(n=0;n<250;n++) ;/*************************************************函数功能:主函数*************************************************/void main(void){unsigned char code Tab[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f}; unsigned char*p[]={&Tab[0],&Tab[1],&Tab[2],&Tab[3],&Tab[4],&Tab[5],&Tab[6],&Tab[7]};unsigned char i;//定义无符号字符型数据while(1){for(i=0;i<8;i++){P0=*p[i];delay150ms();}}}//28P08LED//实例28:用数组的指针控制P0#include<reg51.h>口8位LED流水点亮/*************************************************函数功能:延时约150ms(3*200*250=150000μs=150ms*************************************************/void delay150ms(void){unsigned char m,n;for(m=0;m<200;m++)for(n=0;n<250;n++);}/*************************************************函数功能:主函数*************************************************/void main(void){} unsigned char i;unsigned char Tab[]={0xFF,0xFE,0xFD,0xFB,0xF7,0xEF,0xDF,0xBF,0x7F,0xBF,0xDF,0xEF,0xF7,0xFB,0xFD,0xFE,0xFE,0xFC,0xFB,0xF0,0xE0,0xC0,0x80,0x00,0xE7,0xDB,0xBD,0x7E,0x3C,0x18,0x00,0x81,0xC3,0xE7,0x7E,0xBD,0xDB,0xE7,0xBD,0xDB};//流水灯控制码unsigned char*p;//定义无符号字符型指针p=Tab;//将数组首地址存入指针pwhile(1){for(i=0;i<32;i++)//共32个流水灯控制码{P0=*(p+i);//*(p+i)的值等于a[i]delay150ms();//调用150ms延时函数}}//29P0P1//实例29:用P0#include<reg51.h>、P1口显示整型函数返回值/*************************************************函数功能:计算两个无符号整数的和*************************************************/unsigned int sum(int a,int b){unsigned int s;s=a+b;return(s);}/*************************************************函数功能:主函数*************************************************/void main(void){unsigned z;z=sum(2008,2009);P1=z/256;//取得z的高8位P0=z%256;//取得z的低8位while(1);}//实例30:用有参函数控制P0口8位LED流水速度#include<reg51.h>/*************************************************函数功能:延时一段时间*************************************************/void delay(unsigned char x){unsigned char m,n;for(m=0;m<x;m++)for(n=0;n<200;n++);}/*************************************************函数功能:主函数*************************************************/void main(void){unsigned char i;unsigned char code Tab[]={0xFE,0xFD,0xFB,0xF7,0xEF,0xDF,0xBF,0x7F};//流水灯控制码while(1){//快速流水点亮LEDfor(i=0;i<8;i++)//共8个流水灯控制码{P0=Tab[i];delay(100);//延时约60ms,(3*100*200=60000μs)}//慢速流水点亮LEDfor(i=0;i<8;i++)//共8个流水灯控制码{P0=Tab[i];delay(250);//延时约150ms,(3*250*200=150000μs)}}}22/192//31//实例31:用数组作函数参数控制流水花样#include<reg51.h>/*************************************************函数功能:延时约150ms*************************************************/void delay(void){unsigned char m,n;for(m=0;m<200;m++)for(n=0;n<250;n++);}/*************************************************函数功能:流水点亮P0口8位LED*************************************************/void led_flow(unsigned char a[8]){unsigned char i;for(i=0;i<8;i++){P0=a[i];delay();}}/*************************************************函数功能:主函数*************************************************/void main(void){unsigned char code Tab[]={0xFE,0xFD,0xFB,0xF7,0xEF,0xDF,0xBF,0x7F};//流水灯控制码led_flow(Tab);}//32P08LED//实例32:用指针作函数参数控制P0口8位LED流水点亮/*************************************************函数功能:延时约150ms*************************************************/void delay(void){unsigned char m,n;for(m=0;m<200;m++)for(n=0;n<250;n++);}/*************************************************函数功能:流水点亮P0口8位LED*************************************************/void led_flow(unsigned char*p)//形参为无符号字符型指针{unsigned char i;while(1){i=0;//将i置为0,指向数组第一个元素while(*(p+i)!='\0')//只要没有指向数组的结束标志{P0=*(p+i);//取的指针所指变量(数组元素)的值,送P0口delay();//调用延时函数i++;//指向下一个数组元素}}}/*************************************************函数功能:主函数*************************************************/void main(void){unsigned char code Tab[]={0xFE,0xFD,0xFB,0xF7,0xEF,0xDF,0xBF,0x7F,0x7F,0xBF,0xDF,0xEF,0xF7,0xFB,0xFD,0xFE, 0xFF,0xFE,0xFC,0xFB,0xF0,0xE0,0xC0,0x80,0x00,0xE7,0xDB,0xBD,0x7E,0xFF,0xFF,0x3C,0x18,0x0,0x81,0xC3,0xE7,0xFF, 0xFF,0x7E};//流水灯控制码unsigned char*pointer;224/192} pointer=Tab;led_flow(pointer);//33P1//实例33:用函数型指针控制P1口灯花样#include<reg51.h>//包含51单片机寄存器定义的头文件unsigned char code Tab[]={0xFE,0xFD,0xFB,0xF7,0xEF,0xDF,0xBF,0x7F}; //流水灯控制码,该数组被定义为全局变量/************************************************************** 函数功能:延时约150ms**************************************************************/ void delay(void){unsigned char m,n;for(m=0;m<200;m++)for(n=0;n<250;n++);}/************************************************************** 函数功能:流水灯左移**************************************************************/ void led_flow(void){unsigned char i;for(i=0;i<8;i++)//8位控制码{P0=Tab[i];delay();}}/************************************************************** 函数功能:主函数**************************************************************/ void main(void){void(*p)(void);//定义函数型指针,所指函数无参数,无返回值p=led_flow;//将函数的入口地址赋给函数型指针pwhile(1)(*p)(); //通过函数的指针p调用函数led_flow()}//34//实例34:用指针数组作为函数的参数显示多个字符串#include<reg51.h>//包含51单片机寄存器定义的头文件unsigned char code str1[]="Temperature is tested by DS18B20";//C语言中,字符串是作为字符数组来处理的unsigned char code str2[]="Now temperature is:";//所以,字符串的名字就是字符串的首地址unsigned char code str3[]="The Systerm is designed by Zhang San";unsigned char code str4[]="The date is2008-9-30";unsigned char*p[]={str1,str2,str3,str4};//定义p[4]为指向4个字符串的字符型指针数组/**************************************************************函数功能:延时约150ms**************************************************************/ void delay(void){unsigned char m,n;for(m=0;m<200;m++)for(n=0;n<250;n++);}/**************************************************************函数功能:流水点亮P0口8位LED**************************************************************/ void led_display(unsigned char*x[])//形参必须为指针数组{unsigned char i,j;for(i=0;i<4;i++)//有4个字符串要显示{j=0;//指向待显字符串的第0号元素while(*(x[i]+j)!='\0')//只要第i个字符串的第j号元素不是结束标志{P0=*(x[i]+j);//取得该元素值送到P0口显示delay();//调用延时函数j++;//指向下一个元素}}}/************************************************************** 函数功能:主函数**************************************************************/ void main(void){unsigned char i;while(1){for(i=0;i<4;i++)led_display(p);//将指针数组名作实际参数传递}}//实例35:字符函数ctype.h应用举例#include<reg51.h>//包含51单片机寄存器定义的头文件#include<ctype.h>void main(void){while(1){P3=isalpha('_')?0xf0:0x0f;//条件运算,若'_'是英文字母,P3=0xf0 }}//实例36:内部函数intrins..h应用举例#include<reg51.h>//包含51单片机寄存器定义的头文件#include<intrins.h>//包含函数isalpha()声明的头文件/*************************************************函数功能:延时约150ms*************************************************/void delay(void){unsigned char m,n;for(m=0;m<200;m++)for(n=0;n<250;n++);}227/192/*************************************************函数功能:主函数*************************************************/void main(void){P3=0xfe;//P3=11111110Bwhile(1){P3=_crol_(P3,1);//将P3的二进制位循环左移1位后再赋给P3 delay();//调用延时函数}}//37stdlib.h//实例37:标准函数stdliib.h应用举例#include<reg51.h>//包含51单片机寄存器定义的头文件#include<stdlib.h>//包含函数isalpha()声明的头文件/*************************************************函数功能:延时约150ms*************************************************/void delay(void){unsigned char m,n;for(m=0;m<200;m++)for(n=0;n<250;n++);}/*************************************************函数功能:主函数*************************************************/void main(void){unsigned char i;while(1){for(i=0;i<10;i++)//产生10个随机数{P3=rand()/160;//将产生的随机数缩小160倍后送P3显示delay();}}}//实例 38:字符串函数 striing.h应用举例 #include<reg51.h> //包含 51 单片机寄存器定义的头文件 #include<string.h> //包含函数 isalpha ()声明的头文件 void main(void){unsigned char str1[ ]="Now, The temperature is :";unsigned char str2[ ]="Now, The temperature is 36 Centgrade:"; unsigned char i;i=strcmp(str1,str2); //比较两个字符串,并将结果存入i if(i==0) //str1=str2P3=0x00;elseif(i<0) //str1<str2P3=0xf0;else //str1>str2P3=0x0f;while(1); //防止程序“跑飞”}// 39 2 #include<reg51.h> //实例 39:宏定义应用举例2 //包含 51 单片机寄存器定义的头文件 # define F(a,b) (a)+(a)*(b)/256+(b) void main(void){unsigned char i,j,k;i=40;j=30;k=20;//带参数的宏定义,a 和 b 为形参 参} P3=F(i,j+k); while(1);//i 和 j+k分别为实参,宏展开时,实参将替代宏定义中的形//实例40:宏定义应用举例2 #include<AT89X51.h>#include<ctype.h>void main(void){P3_0=0;//将P3.0引脚置低电平,LED点亮P3_1=0;//将P3.0引脚置低电平,LED点亮P3_2=0;//将P3.0引脚置低电平,LED点亮P3_3=0;//将P3.0引脚置低电平,LED点亮P3_4=1;//将P3.4引脚置高电平,LED熄灭P3_5=1;//将P3.5引脚置高电平,LED熄灭P3_6=1;//将P3.7引脚置高电平,LED熄灭P3_7=1;//将P3.7引脚置高电平,LED熄灭while(1);}//实例41:宏定义应用举例3#include<reg51.h >#define MAX100 void main(void) {#if MAX>80P3=0xf0;#elseP3=0x0f;#endif}//包含51单片机寄存器定义的头文件//将MAX宏定义为字符串100//如果字符串100大于80//P3口低四位LED点亮//否则,P3口高四位LED点亮//结束本次编译/***************************************************** ************中断、定时器********中断、定时器*********************中断、定时器*********中断、定时器****************************************************************** **///实例42:用定时器T0查询方式P2口8位控制LED闪烁#include<reg51.h>//包含51单片机寄存器定义的头文件/**************************************************************函数功能:主函数**************************************************************/void main(void){//EA=1;//开总中断//ET0=1;//定时器T0中断允许TMOD=0x01;//使用定时器T0的模式1TH0=(65536-46083)/256;//定时器T0的高8位赋初值TL0=(65536-46083)%256;//定时器T0的高8位赋初值TR0=1;//启动定时器T0TF0=0;P2=0xff;while(1)//无限循环等待查询{while(TF0==0);TF0=0;P2=~P2;TH0=(65536-46083)/256;//定时器T0的高8位赋初值TL0=(65536-46083)%256;//定时器T0的高8位赋初值}}//实例43:用定时器T1查询方式控制单片机发出1KHz音频/************************************************************** 函数功能:主函数**************************************************************/ void main(void){//EA=1;//开总中断//ET0=1;//定时器T0中断允许TMOD=0x10;//使用定时器T1的模式1TH1=(65536-921)/256;//定时器T1的高8位赋初值TL1=(65536-921)%256;//定时器T1的高8位赋初值TR1=1;//启动定时器T1TF1=0;while(1)//无限循环等待查询{while(TF1==0);TF1=0;sound=~sound;//将P3.7引脚输出电平取反TH1=(65536-921)/256;//定时器T0的高8位赋初值TL1=(65536-921)%256;//定时器T0的高8位赋初值}}//44T0P18LED //实例44:将计数器T0计数的结果送P1口8位LED显示#include<reg51.h>//包含51单片机寄存器定义的头文件sbit S=P3^4;//将S位定义为P3.4引脚/************************************************************** 函数功能:主函数**************************************************************/ void main(void){//EA=1;//开总中断//ET0=1;//定时器T0中断允许TMOD=0x02;//使用定时器T0的模式2TH0=256-156;//定时器T0的高8位赋初值TL0=256-156;//定时器T0的高8位赋初值TR0=1;//启动定时器T0while(1)//无限循环等待查询{while(TF0==0)//如果未计满就等待。

求单片机 音乐播放的c代码

求单片机 音乐播放的c代码

#include "reg52.h"unsigned char Count;sbit _Speak =P2^3 ; //讯响器控制脚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;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++ );}}/*-------------------------------------------------功能:歌曲播放子程序i为播放哪一段曲目-------------------------------------------------*/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); //播放}}6回答者:小崔凡凡- 二级2009-10-16 21:47我来评论>>提问者对于答案的评价:谢谢!相关内容• 单片机播放音乐里的音乐代码是怎么得到的?有什么软件能将歌曲直接转换成16进制代码 4 2008-11-14• 给个单片机C语言编的音乐程序,能够播放音乐 6 2009-7-22• 求用c语言编写的播放背景音乐的代码 5 2009-6-12• 单片机音乐代码问题 2009-9-28• 单片机音乐代码 5 2009-5-18更多相关问题>>查看同主题问题:单片机音乐音乐播放播放代码等待您来回答∙nokia 3100c音乐播放器如何跟新∙为什么用酷我音乐盒听歌后那歌会在C:\ProgramData\mcache里面呢∙深圳龙华天龙大道珍藏版狂嗨现场dj明仔vs女mc小黎茂名细飞11分钟半时候的那音乐谁知道叫什么名字!∙夏普9020c合上盖子怎么听音乐?∙C:Documents and SettingsAdministrator「开始」菜单酷我音乐盒2010.lnk∙nokia 2600c手机可以用什么音乐播放器?∙『windows』在网页制作中,下列不是背景音乐格式的是那一个?A.tem B mp3 Cwav D∙3110c播放音乐时会卡,之后会自动到下一首,怎么办?其他回答共3 条我的博客上有三首连放的,你只要加两首就行了。

用51单片机播放音乐(附C程序)

用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附录:以下为曲谱编码文件,自己可以根据规则,对照简谱编写曲谱编码表。

KeilC51学习6控制播放音乐

KeilC51学习6控制播放音乐

KeilC51学习6控制播放⾳乐变频救护车#include<reg52.h>//包含访问sfr库函数#define uchar unsigned char //定义⼀下⽅便使⽤#define uint unsigned int#define ulong unsigned longsbit BEEP=P1^0;sbit K1=P3^2;//变频声救护车报警信号输出试验main(){uint n;ulong ul;while(1) //程序循环{//输出约 1 秒种⼀个频率的声⾳for(ul=0;ul<3000;ul++){for(n=0;n<80;n++); //延时BEEP=~BEEP; //取反输出到喇叭的信号}//输出约 1 秒种另⼀个频率的声⾳for(ul=0;ul<2500;ul++){for(n=0;n<100;n++); //延时BEEP=~BEEP; //取反输出到喇叭的信号}}}变频声救护车报警信号输出试验⾳阶声⾳⾃动输出试验#include<reg52.h>//包含访问sfr库函数#define uchar unsigned char //定义⼀下⽅便使⽤#define uint unsigned int#define ulong unsigned longsbit BEEP=P1^0;uchar th0_f; //在中断中装载的 T0 的值⾼ 8 位uchar tl0_f; //在中断中装载的 T0 的值低 8 位//T0 的值,及输出频率对照表uchar code freq[36*2]={0xA9,0xEF,//00220HZ ,1 //00x93,0xF0,//00233HZ ,1#0x73,0xF1,//00247HZ ,20x49,0xF2,//00262HZ ,2#0x07,0xF3,//00277HZ ,30xC8,0xF3,//00294HZ ,40x73,0xF4,//00311HZ ,4#0x1E,0xF5,//00330HZ ,50xB6,0xF5,//00349HZ ,5#0x4C,0xF6,//00370HZ ,60xD7,0xF6,//00392HZ ,6#0x5A,0xF7,//00415HZ ,70xD8,0xF7,//00440HZ 1 //120x4D,0xF8,//00466HZ 1# //130xBD,0xF8,//00494HZ 2 //140x24,0xF9,//00523HZ 2# //150x87,0xF9,//00554HZ 3 //160xE4,0xF9,//00587HZ 4 //170x3D,0xFA,//00622HZ 4# //180x90,0xFA,//00659HZ 5 //190xDE,0xFA,//00698HZ 5# //200x29,0xFB,//00740HZ 6 //210x6F,0xFB,//00784HZ 6# //220xB1,0xFB,//00831HZ 7 //230xEF,0xFB,//00880HZ `10x2A,0xFC,//00932HZ `1#0x62,0xFC,//00988HZ `20x95,0xFC,//01046HZ `2#0xC7,0xFC,//01109HZ `30xF6,0xFC,//01175HZ `40x22,0xFD,//01244HZ `4#0x4B,0xFD,//01318HZ `5};//定时中断 0,⽤于产⽣唱歌频率timer0() interrupt 1{TL0=tl0_f;TH0=th0_f; //调⼊预定时值BEEP=~BEEP; //取反⾳乐输出 IO}//⾳阶声⾳⾃动输出试验void main(void) // 主程序{ulong n;uchar i;uchar code jie8[8]={12,14,16,17,19,21,23,24};//1234567`1 ⼋个⾳符在频率表中的位置 TMOD = 0x01; //使⽤定时器 0 的 16 位⼯作模式TR0 = 1;ET0 = 1;EA = 1;while(1){for(i=0;i<8;i++) //循环播放 8 个⾳符{tl0_f=freq[jie8[i]*2]; //置⼀个⾳符的值th0_f=freq[jie8[i]*2+1];for(n=0;n<50000;n++); //延时 1 秒左右}}}//⾳阶声⾳⾃动输出试验按键控制⾳阶声⾳输出(电⼦琴)#include<reg52.h>//包含访问sfr库函数#define uchar unsigned char //定义⼀下⽅便使⽤#define uint unsigned int#define ulong unsigned longsbit BEEP=P1^0; //喇叭输出脚sbit K1= P3^2;sbit K2= P3^3;sbit K3= P3^4;sbit K4= P3^5;uchar th0_f; //在中断中装载的 T0 的值⾼ 8 位uchar tl0_f; //在中断中装载的 T0 的值低 8 位//T0 的值,及输出频率对照表uchar code freq[36*2]={0xA9,0xEF,//00220HZ ,1 //00x93,0xF0,//00233HZ ,1#0x73,0xF1,//00247HZ ,20x49,0xF2,//00262HZ ,2#0x07,0xF3,//00277HZ ,30xC8,0xF3,//00294HZ ,40x73,0xF4,//00311HZ ,4#0x1E,0xF5,//00330HZ ,50xB6,0xF5,//00349HZ ,5#0x4C,0xF6,//00370HZ ,60xD7,0xF6,//00392HZ ,6#0x5A,0xF7,//00415HZ ,70xD8,0xF7,//00440HZ 1 //120x4D,0xF8,//00466HZ 1# //130xBD,0xF8,//00494HZ 2 //140x24,0xF9,//00523HZ 2# //150x87,0xF9,//00554HZ 3 //160xE4,0xF9,//00587HZ 4 //170x3D,0xFA,//00622HZ 4# //180x90,0xFA,//00659HZ 5 //190xDE,0xFA,//00698HZ 5# //200x29,0xFB,//00740HZ 6 //210x6F,0xFB,//00784HZ 6# //220xB1,0xFB,//00831HZ 7 //230xEF,0xFB,//00880HZ `10x2A,0xFC,//00932HZ `1#0x62,0xFC,//00988HZ `20x95,0xFC,//01046HZ `2#0xC7,0xFC,//01109HZ `30xF6,0xFC,//01175HZ `40x22,0xFD,//01244HZ `4#0xDC,0xFD,//01661HZ `7 //35};//定时中断 0,⽤于产⽣唱歌频率timer0() interrupt 1{TL0=tl0_f;TH0=th0_f; //调⼊预定时值BEEP=~BEEP; //取反⾳乐输出 IO}//按键控制⾳阶声⾳输出(电⼦琴)void main(void) // 主程序{ulong n;uchar code jie8[8]={12,14,16,17,19,21,23,24};//1234567`1 ⼋个⾳符在频率表中的位置TMOD = 0x01; //使⽤定时器 0 的 16 位⼯作模式TR0 = 0;ET0 = 1;EA = 1;while(1){if(!K1){tl0_f=freq[jie8[0]*2]; //置⼀个⾳符的值th0_f=freq[jie8[0]*2+1];TR0 = 1;for(n=0;n<10000;n++); //延时}if(!K2){tl0_f=freq[jie8[1]*2]; //置⼀个⾳符的值th0_f=freq[jie8[1]*2+1];TR0 = 1;for(n=0;n<10000;n++); //延时}if(!K3){tl0_f=freq[jie8[2]*2]; //置⼀个⾳符的值th0_f=freq[jie8[2]*2+1];TR0 = 1;for(n=0;n<10000;n++); //延时}if(!K4){tl0_f=freq[jie8[3]*2]; //置⼀个⾳符的值th0_f=freq[jie8[3]*2+1];TR0 = 1;for(n=0;n<10000;n++); //延时}TR0 = 0;}}按键控制⾳阶声⾳输出(电⼦琴)乐谱⽅式输⼊的⾳乐播放,仙剑奇侠传乐谱解释函数,是逐个字符解释的。

基于c51单片机编写简单蜂鸣器音乐程序的方法

基于c51单片机编写简单蜂鸣器音乐程序的方法
/*7*/case 0x22:j=s/494;break;case 0x23:j=s/998;break;case 0x24:j=s/1967;break;
/*o*/case 0xff:j=0;break;
}
return j;
}
void sound(uint s)//给一次脉冲来进行发声的子程序,其中参数s与m_t()中的0x01等十六进制数是对应的
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} ;//*
以下是本人编写的一段简单音乐程序,程序已经在板子上调试成功,水平有限,如有不正确的地方请多包涵
*******************************************************************************************************/
#include<reg52.H>
#include <intrins.h> //内部包含延时函数_nop_();
typedef unsigned char uchar;
typedef unsigned int uint;
uint c;
sbit spk =P2^0 ; //定义p2.0口为电平信号输出端
最后在主程序中运行子程序,通过读rom里面的数组来达到连续播放各种不同音调的音的目的,这样听起来就像一首曲子了。
以下是我按以上方法编写的一个小程序:

51单片机c语言编写电子琴+音乐播放器

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音乐程序

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 }}。

51单片机 音乐 天空之城 C语言

51单片机 音乐 天空之城 C语言
H3, /**/Z3,H3, /**/H6,H5,H5, /**/H3,H2,H1,H1,/**/H2,H1,H2,H5,/**/H3,H3,
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,
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
相关文档
最新文档