51单片机蜂鸣器播放音乐代码(生日快乐 两只蝴蝶 祝你平安)

合集下载

51单片机温度计+计数器(可调时间)+蜂鸣器(生日歌)+花样流水灯6种

51单片机温度计+计数器(可调时间)+蜂鸣器(生日歌)+花样流水灯6种

51单片机课程设计蜂鸣器(生日歌)+温度计+计数器(可调时间)+花样流水灯(6种)#include<reg52.h>#include<intrins.h>#include<stdlib.h>#define uchar unsigned char#define uint unsigned int#define led P1 //将P1口定义位8个leduchar a, count, S1num, xqnum, temp, j, tm, key, coun, L, qdq, mb; //定义变量char yue, ri, shi, fen, miao; //定义月日时分秒int nian; //定义年// ==================定义初始化日历uchar code table0[] = "2020-09-4--FRI--";uchar code table1[] = "08:48:00";uchar code xingqi[][3] = { "MON","TUE","WED","THU","FRI","SAT","SUN" };uint led1[]={0x7e,0xbd,0xdb,0xe7,0xff};// ==================定义初始化日历sbit lcdrs=P2^6; //将lcd1602的RS端口(数据/命令选择端)定义到P2.0 sbit lcdrw=P2^7; //将lcd1602的R/W端口(读/写选择端)定义到P2.1 sbit lcden=P2^5; //将lcd1602的E端口(使能端口)定义到P2.2sbit DQ=P3^7;//ds18b20sbit S1=P3^1; //进入设置退出设置sbit S2=P3^2; //+1 或秒表模式sbit S3=P3^3; //-1 或开始秒表计时sbit S0=P3^0; //进入设置退出设置sbit S4=P3^4; //灯模式切换sbit S5=P3^5;sbit beep = P2^2; //定义蜂鸣器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};//延时unsigned char code str2[16]={" "};uchar data disdata[5];uint value;//温度值温度值uchar flag;//正负标志正负标志void delay1ms(unsigned int ms)//延时1毫秒{unsigned int i,j;for(i=0;i<ms;i++)for(j=0;j<100;j++);}void write_com1(unsigned char com1)//写指令//{ delay1ms(1);lcdrs=0;lcdrw=0;lcden=0;P0=com1;delay1ms(1);lcden=1;delay1ms(1);lcden=0;}void write_dat1(unsigned char dat1)//写数据// { delay1ms(1);lcdrs=1;lcdrw=0;lcden=0;P0=dat1;delay1ms(1);lcden=1;delay1ms(1);lcden=0;}void lcd_init()//初始化设置//{write_com1(0x38);delay1ms(5);write_com1(0x08);delay1ms(5);write_com1(0x01);delay1ms(5);write_com1(0x06);delay1ms(5);write_com1(0x0c);delay1ms(5);}void display(unsigned char *p)//显示{while(*p!='\0'){write_dat1(*p);p++;delay1ms(1);}}init_play()//初始化显示{lcd_init();write_com1(0xc0);display(str2);}void delay_18B20(unsigned int i)//延时1微秒{while(i--);}void ds1820rst()/*ds1820复位*/{unsigned char x=0;DQ = 1;//DQ复位delay_18B20(4);//延时DQ=0;//DQ拉低delay_18B20(100);//精确延时大于480usDQ=1;//拉高delay_18B20(40);}uchar ds1820rd()/*读数据*/{unsigned char i=0;unsigned char dat1 = 0;for (i=8;i>0;i--){DQ = 0; //给脉冲信号dat1>>=1;DQ=1;//给脉冲信号if(DQ)dat1|=0x80;delay_18B20(10);}return(dat1);}void ds1820wr(uchar wdata)/*写数据*/ {unsigned char i=0;for(i=8;i>0;i--){DQ=0;DQ=wdata&0x01;delay_18B20(10);DQ=1;wdata>>=1;}}read_temp()/*读取温度值并转换*/ {uchar a,b;ds1820rst();ds1820wr(0xcc);//*跳过读序列号*/ds1820wr(0x44);//*启动温度转换*/?ds1820rst();ds1820wr(0xcc);//*跳过读序列号*/ds1820wr(0xbe);//*读取温度*/a=ds1820rd();b=ds1820rd();value=b;value<<=8;value=value|a;if(value<0x0fff)flag=0;else{value=~value+1;flag=1;}value=value*(0.625);//温度值扩大10倍,精确到1位小数return(value);}/*******************************************************************/ void ds1820disp()//温度值显示{uchar flagdat;disdata[0]=value/1000+0x30;//百位数disdata[1]=value%1000/100+0x30;//十位数disdata[2]=value%100/10+0x30;//个位数disdata[3]=value%10+0x30;//小数位if(flag==0)flagdat=0x20;//正温度不显示符号elseflagdat=0x2d;//负温度显示负号:-if(disdata[0]==0x30){disdata[0]=0x20;//如果百位为0,不显示if(disdata[1]==0x30){disdata[1]=0x20;//如果百位为0,十位为0也不显示}}write_com1(0xc8);write_dat1(flagdat);//显示符号位write_com1(0xc9);write_dat1(disdata[0]);//显示百位write_com1(0xca);write_dat1(disdata[1]);//显示十位write_com1(0xcb);write_dat1(disdata[2]);//显示个位write_com1(0xcc);write_dat1(0x2e);//显示小数点write_com1(0xcd);write_dat1(disdata[3]);//显示小数位}void delay(uint t) //@12.000MHz延时函数{uint x, y;for (x = t; x > 0; x--)for (y = 110; y > 0; y--);}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 write_com(uchar com) //液晶写指令{lcdrw = 0;lcdrs = 0;P0 = com;delay(2);lcden = 1;delay(2);lcden = 0;}void write_data(uchar dat) //液晶写数据{lcdrw = 0;lcdrs = 1;P0 = dat;delay(2);lcden = 1;delay(2);lcden = 0;}void write_sfm(uchar add, uchar dat) //写时分秒函数{uchar shi, ge;shi = dat / 10;ge = dat % 10;write_com(0x80 + 0x40 + add);write_data(0x30 + shi);write_data(0x30 + ge);}void write_yr(uchar add, uchar dat) //写月日函数{uchar shi, ge;shi = dat / 10;ge = dat % 10;write_com(0x80 + add);write_data(0x30 + shi);write_data(0x30 + ge);}void write_nian(uchar add, uint dat) //写年函数{uint qian, bai, shi, ge;qian = dat / 1000;bai = dat % 1000 / 100;shi = dat % 100 / 10;ge = dat % 10;write_com(0x80 + add);write_data(0x30 + qian);write_data(0x30 + bai);write_data(0x30 + shi);write_data(0x30 + ge);}void init_lcd() //液晶初始化{lcden = 0;nian = 2020;yue = 1;ri = 3;shi = 0; //初始shi、fen、miaofen = 0;miao = 0;write_com(0x38); //设置16x2显示,5x7点阵,8位数据口write_com(0x0c); //设置开显示,不显示光标write_com(0x06); //写一个字符后地址指针加1write_com(0x01); //显示清0,数据指针清0}//==================液晶void init() //总初始化函数{init_lcd(); //液晶初始化write_com(0x80); //设置显示初始坐标for (a = 0; a < 14; a++) //显示年月日初始值{write_data(table0[a]);delay(3);}write_com(0x80 + 0x40); //设置显示初始坐标for (a = 0; a < 8; a++) //显示时分秒初始值{write_data(table1[a]);delay(3);}write_nian(0, nian);write_sfm(6, miao); //分别将shi、fen、miao送去液晶显示write_sfm(3, fen);write_sfm(0, shi);qdq=0;mb=0;j=0;L=0;coun=0;count = 0;xqnum = 0;S1num = 0;PT0=1;PT1=0;TH1=(65536-50000)/256;TL1=(65536-50000)%256;TR1=1;ET1=1;led=0xfe;key=0;TMOD = 0x011; //设置定时器0工作模式1TH0 = (65536-50000) / 256; //定时器装初始值TL0 = (65536-50000) % 256;EA = 1; //开总中断ET0 = 1; //开定时器0中断TR0 = 1; //启动定时器0}//=======================流水灯void t0isr() interrupt 3 //定时器2,用于流水灯{TH1=(65536-50000)/256;TL1=(65536-50000)%256;tm++;if(tm>=9){tm=0;switch(key) //六种样式{case 0:led=_crol_(led,1);break;case 1:led=_cror_(led,1);break;case 2:led=_cror_(led,1);break;case 3:led=_crol_(led,1);break;case 4:if(led==0x00)led=0xff;else led=led*2;break;case 5:led=~led;break;}}}//=======================流水灯//=========================== 独立键盘扫描函数void keyscan(){//————————————————————————————————————————————进入日历时钟设置if(S5 == 0){ read_temp();//读取温度ds1820disp();//显示}if (S0 == 0){ beep=0;PlayMusic(); //播放生日快乐beep=1;}if (S1 == 0){delay(2); //确认定义键被按下if (S1 == 0){while(!S1); //确认按键松开S1num++; //定义键S1按下次数记录if (S1num == 1) //S1按下一次时{TR0 = 0; //关闭定时器write_com(0xc0 + 7); //光标定位到秒位置write_com(0x0f); //光标闪烁}if (S1num == 2) //S1按下两次时{write_com(0xc0 + 4); //光标定位到分位置}if (S1num == 3) //S1按下三次时{write_com(0xc0 + 1); //光标定位到时位置}if (S1num == 4) //S1按下四次时{write_com(0x80 + 13); //光标定位到星期位置}if (S1num == 5) //S1按下五次时{write_com(0x80 + 9); //光标定位到日位置}if (S1num == 6) //S1按下六次时{write_com(0x80 + 6); //光标定位到月位置}if (S1num == 7) //S1按下七次时{write_com(0x80 + 3); //光标定位到年位置}if (S1num == 8) //退出设置,开启中断{S1num = 0;delay(3);TR0 = 1;write_com(0x0c);}}}if (S1num != 0) //只有定义键按下后S2、S3、S4才有效{if (S2 == 0){delay(2); //防抖if (S2 == 0) //确认按键被按下{while (!S2); //释放按键确认if (S1num == 1) //S1按下一次时{miao++; //调整秒加1if (miao == 60) //满60清零miao = 0;write_sfm(6, miao); //每调节一次送液晶显示一次write_com(0x80 + 0x40 + 6); //显示位置重新回到调节处}if (S1num == 2) //S1按下两次时{fen++; //调整分加1if (fen == 60)fen = 0;write_sfm(3, fen);write_com(0x80 + 0x40 + 3);}if (S1num == 3) //S1按下三次时{shi++; //调整时加1if (shi == 24)shi = 0;write_com(0x80 + 0x40);write_sfm(0, shi);}if (S1num == 4) //星期加调整{xqnum++;if (xqnum == 7)xqnum = 0;write_com(0x80 + 0x0b);for (a = 0; a < 3; a++){write_data(xingqi[xqnum][a]);delay(5);}}if (S1num == 5) //日加调整{ri++;if (yue == 2){if (nian % 400 == 0){if (ri == 30)ri = 1;}if (nian % 400 != 0){if (ri == 29)ri = 1;}}else if (yue <= 7){if (yue % 2 == 0 & yue != 2){if (ri == 31){ ri = 1; }}else if (yue % 2 != 0 & yue != 2){if (ri == 32){ ri = 1; }}}else if (yue >= 8){if (yue % 2 == 0){if (ri == 32){ ri = 1; }}else if (yue % 2 != 0){if (ri == 31){ ri = 1; }}}write_yr(8, ri);}if (S1num == 6) //月加调整{yue++;if (yue == 13)yue = 1;write_yr(5, yue);}if (S1num == 7) //年加调整{nian++;if (nian == 2030)nian = 2019;write_nian(0, nian);}}}if (S3 == 0){delay(2);if (S3 == 0) //确认按键被按下{while (!S3); //确认按键松开if (S1num == 1){miao--; //调整秒减1if (miao == -1) //减到00后再减重新设置为59miao = 59;write_sfm(6, miao);write_com(0x80 + 0x40 + 6);}if (S1num == 2){fen--; //调整分减1if (fen == -1)fen = 59;write_sfm(3, fen);write_com(0x80 + 0x40 + 3);}if (S1num == 3){shi--; //调整时减1if (shi == -1)shi = 23;write_sfm(0, shi);write_com(0x80 + 0x40);}if (S1num == 4){xqnum--; //调整星期减一if (xqnum == -1)xqnum = 6;write_com(0x80 + 0x0b);for (a = 0; a < 3; a++){write_data(xingqi[xqnum][a]);delay(5);}}if (S1num == 5) //调整日{ri--;if (yue == 2){if (nian % 400 == 0){if (ri == 0){ ri = 29; }}if (nian % 400 != 0){if (ri == 0){ ri = 28; }}}else if (yue <= 7){if (yue % 2 == 0 & yue != 2){if (ri == 0){ ri = 30; }}else if (yue % 2 != 0 & yue != 2){if (ri == 0){ ri = 31; }}}else if (yue >= 8){if (yue % 2 == 0){if (ri == 0){ ri = 31; }}else if (yue % 2 != 0){if (ri == 0){ ri = 30; }}}write_yr(8, ri);}if (S1num == 6) //调整月{yue--;if (yue == 0)yue = 12;write_yr(5, yue);}if (S1num == 7) //调整年{nian--;if (nian == 2030)nian = 2019;write_nian(0, nian);}}}}//————————————————————————————————————-推出日历时钟设置//——————————————————————————————————流水灯设置初值if(S4==0){delay(2);if(S4==0) //确认按键按下{while(!S4);qdq=0; //抢答器重置TR1=1;delay(2);while(S4==0);delay(2);key++;key%=6;if(key==0)led=0xfe;else if(key==1)led=0x7f;else if(key==2)led=0x80;else if(key==3)led=0x01;else if(key==4)led=0xfe;else led=0x55;}}//——————————————————————————————————结束流水灯设置初值}//=========================== 独立键盘扫描函数//=======================定时器0中断服务程序,用于日历时钟、秒表void timer0() interrupt 1{TH0 = (65536-50000) / 256; //重装定时器初始值TL0 = (65536-50000) % 256;count++; //中断次数累加//————————————————————————————————————————————日历时钟if (count == 20) //20次50毫秒即一秒{count = 0;miao++;if (miao == 60) //秒加到60时分进位{miao = 0;fen++;if (fen == 60) //分加到60时时进位{fen = 0;shi++;if (shi == 24) //时加到24时清0{shi = 0;xqnum++;ri++;if (yue == 2) //如果是二月{if (nian % 400 == 0) //闰年判断{if (ri == 30){ri = 1;yue++;write_yr(5, yue);}}if (nian % 400 != 0) //平年判断{if (ri == 29){ri = 1;yue++;write_yr(5, yue);}}}else if (yue <= 7 & yue != 2) //一月到七月{if (yue % 2 == 0) //偶数月(除二月){if (ri == 31){ri = 1;yue++;}}else if (yue % 2 != 0) //奇数月{if (ri == 32){ri = 1;yue++;}}write_yr(5, yue);}else if (yue >= 8) //八月到12月{if (yue % 2 == 0) //偶数月(除二月){if (ri == 32){ri = 1;yue++;if (yue == 13) //如果判断月份为12月,则加一后重新定义月份为1{yue = 1;nian++;write_nian(0, nian);}write_yr(5, yue);}}else if (yue % 2 != 0) //奇数月{if (ri == 31){ri = 1;yue++;write_yr(5, yue);}}}write_yr(8, ri);if (xqnum == 7) //星期写入xqnum = 0;write_com(0x80 + 0x0b);for (a = 0; a < 3; a++){write_data(xingqi[xqnum][a]);delay(5);}}write_sfm(0, shi); //重新写入数据}write_sfm(3, fen); //重新写入数据}write_sfm(6, miao); //重新写入数据}//——————————————————————————————————————————日历时钟结束}//=======================定时器0中断服务程序,用于日历时钟void main() //主函数{init(); //初始化while (1){keyscan(); //检测按键是否被按下}}相关视频在哔哩哔哩:BV1ef4y1q7xY。

单片机音乐——生日歌(汇编)

单片机音乐——生日歌(汇编)

;本程序实现蜂鸣器演奏歌曲功能;乐曲名称:生日快乐;///////////////////////////////////////////BUZZER EQU P2.3org 0000h ;主程序起始地址ajmpmain ;跳至主程序org 000bh ;timer0中断起始地址jmp tim0 ;跳至timer0中断子程序;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;org 0030hmain:mov tmod,#01h ;设timer0在MODE1mov ie,#82h ;中断使能start0:mov 30h,#00h ;取简谱码指针next:mov a,30h ;简谱码指针载入Amov dptr,#table ;至table取简谱码movc a,@a+dptrmov r2,a ;取到的简谱码暂存于R2jz end0 ;是否取到00(结束码)?anl a,#0fh ;不是,则取低4位(节拍码)mov r5,a ;将节拍码存入R5mov a,r2 ;将取到的简谱码再载入Aswap a ;高低四位交换anl a,#0fh ;取低四位(音符码)jnz sing ;取到的音符码是否为零?clr tr0 ;是,则不发音jmp d1sing: d ec a ;取到的音符码减1(不含0).mov 22h,a ;存入(22H).rl a ;乘2mov dptr,#table1 ;至table1取相对的高位字节计数值movc a,@a+dptrmov th0,a ;取到的高位字节存入TH0mov 21h,a ;取到的高位字节存入(21H)mov a,22h ;再载入取的的音符码rl a ;乘2inc a ;加1movc a,@a+dptr ;至table1取相对的低位字节计数值mov tl0,a ;取到的高位字节存入TL0mov 20h,a ;取到的高位字节存入(20H)setb tr0 ;启动timer0d1: call delay ;基本单位时间1/4拍187毫秒inc 30h ;取简谱码指针加1jmp next ;取下一个码end0: clr tr0 ;停止timer0jmp start0 ;重复循环;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;time0中断服务子程序用来产生相应的音调脉冲频率。

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

单片机驱动蜂鸣器音乐生日快乐的播放程序

单片机驱动蜂鸣器音乐生日快乐的播放程序

单片机驱动蜂鸣器音乐生日快乐的播放程序;音乐生日快乐的播放;P1.6是喇叭输出端口,用杜邦线连接到J42插针 B1针脚,喇叭即播放音乐numtim equ 20hgewei equ 21hshiwei equ 22hscanled equ 23horg 000hjmp startorg 00bhjmp tim0org 100hstart:; JB P3.7,$ ;检测播放按键是否按下mov tmod,#00000001bmov ie,#10000010bmov numtim,#01h start0:mov 30h,#00hnext:mov a,30hmov dptr,#tablemovc a,@a+dptrmov r2,ajz end0anl a,#0fhmov r5,amov a,r2swap aanl a,#0fhjnz singclr tr0jmp d1sing: dec amov 22h,arl amov dptr,#table1movc a,@a+dptrmov th0,amov 21h,amov a,22hrl ainc amovc a,@a+dptrmov tl0,amov 20h,asupplies and get to use, the registration of company assets, record maintenance, maintenance of effective management; 2.6 cost management: preparation of the annual administrative budget, control administrative costs, determine the scope of cost-sharing, sharing the office expenses by month; 2.7 information management: responsible for documents,materials, information and publicity, communication links, and ensure promptly, ranking officers reporting; responsible for the collation and analysis of information, provide the basis for corporate decisions; corporate culture construction, planning, editing, internal publications;2.8 vehicle management: responsible for the management, repairs and maintenance of vehicles; 2.9 legal disputes: the company responsible for the handling of disputes; 2.10 other: maintaining normal working order, and security. General Manager General Manager job description 1, 1.1 develop strategies and plans: according to company strategic planning, establish the annual management plan, reported to the Board of Directors for approval; 1.2 decomposition measures: according to the Board of Directors issued the annual management plan, annual index of decomposition, through the Organization of coordination mandate incharge of the Department Organization and implementation, ensuring comprehensive implementation of annual indicators; 1.3 inspection: convenes the General Manager Office, throughsetb tr0d1:lcall delayinc 30hjmp nextend0:clr tr0jmp start0tim0:push accpush pswmov tl0,20hmov th0,21hcpl p1.6 ;由该口输出音频数据pop pswpop accretidelay: mov r7,#02hd2: mov r4,#187d3: mov r3,#248djnz r3,$djnz r4,d3djnz r7,d2djnz r5,delayrettable1: ;音乐码表dw 64260,64400,64524,64580 dw 64684,64777,64820,64898 dw 64968,65030,65058,65110 dw 65157,65178,65217table:db 82h,01h,81h,94h,84h db 0b4h,0a4h,04hdb 82h,01h,81h,94h,84hdb 0c4h,0b4h,04hdb 82h,01h,81h,0f4h,0d4hdb 0b4h,0a4h,94hdb 0e2h,01h,0e1h,0d4h,0b4hdb 0c4h,0b4h,04hdb 82h,01h,81h,94h,84hdb 0b4h,0a4h,04hdb 82h,01h,81h,94h,84hdb 0c4h,0b4h,04h supplies and get to use, the registration of company assets, record maintenance, maintenance of effective management;2.6 cost management: preparation of the annual administrative budget, control administrative costs, determine the scope of cost-sharing, sharing the office expenses by month; 2.7 information management: responsible for documents, materials, information and publicity, communication links, and ensure promptly, ranking officers reporting; responsible for the collation and analysis of information, provide the basis for corporate decisions; corporate culture construction, planning, editing, internal publications; 2.8 vehicle management: responsible for the management, repairs and maintenance of vehicles; 2.9 legal disputes: the company responsible for the handling of disputes; 2.10 other: maintaining normal working order, and security. General Manager General Manager job description 1, 1.1 develop strategies and plans: according to company strategic planning, establish the annual management plan, reported to the Board of Directors for approval; 1.2 decomposition measures: according to the Board of Directors issued the annual management plan, annual index of decomposition, through the Organizationof coordination mandate in charge of the Department Organization and implementation, ensuring comprehensive implementation of annual indicators; 1.3 inspection: convenes the General Manager Office, through db 82h,01h,81h,0f4h,0d4hdb 0b4h,0a4h,94hdb 0e2h,01h,0e1h,0d4h,0b4hdb 0c4h,0b4h,04hdb 00TABLE2:DB 0C0H,0F9H,0A4H,0B0H,99H,92H,82H,0F8H,80H,90Hend另外的一个C文件。

(原创)51单片机C语言程序设计--速学教程实例(进阶篇)之蜂鸣器奏乐_

(原创)51单片机C语言程序设计--速学教程实例(进阶篇)之蜂鸣器奏乐_

本文由sutao130贡献 pdf文档可能在WAP端浏览体验不佳。

建议您优先选择TXT,或下载源文件到本机查看。

/******************************************************************/ /* /* /* /* /* #include <reg52.h> #include "SoundPlay.h" /******************************************************************/ /* /* /* void Delay1ms(uint count) { uint i,j; for(i=0;i<count;i++) for(j=0;j<120;j++); } /*****************************Music***************************************************/ //挥着翅膀的女孩 unsigned char code Music_Girl[]={ 0x17,0x02, 0x17,0x03, 0x18,0x03, 0x19,0x02, 0x15,0x03, 0x16,0x03, 0x17,0x03, 0x17,0x03, 0x17,0x03, 0x18,0x03, 0x19,0x02, 0x16,0x03, 0x17,0x03, 0x18,0x02, 0x18,0x03, 0x17,0x03, 0x15,0x02, 0x18,0x03, 0x17,0x03, 0x18,0x02, 0x10,0x03, 0x15,0x03, 0x16,0x02, 0x15,0x03, 0x16,0x03, 0x17,0x02, 0x17,0x03, 0x18,0x03, 0x19,0x02, 0x1A,0x03, 0x1B,0x03, 0x1F,0x03, 0x1F,0x03, 0x17,0x03, 0x18,0x03, 0x19,0x02, 0x16,0x03, 0x17,0x03, 0x18,0x03, 0x17,0x03, 0x18,0x03, 0x1F,0x03, 0x1F,0x02, 0x16,0x03, 0x17,0x03, 0x18,0x03, 0x17,0x03, 0x18,0x03, 0x20,0x03, 0x20,0x02, 0x1F,0x03, 0x1B,0x03, 0x1F,0x66, 0x20,0x03, 0x21,0x03, 0x20,0x03, 0x1F,0x03, 0x1B,0x03, 0x1F,0x66, 0x1F,0x03, 0x1B,0x03, 0x19,0x03, 0x19,0x03, 0x15,0x03, 0x1A,0x66, 0x1A,0x03, 0x19,0x03, 0x15,0x03, 0x15,0x03, 0x17,0x03, 0x16,0x66, 0x17,0x04, 0x18,0x04, 0x18,0x03, 0x19,0x03, 0x1F,0x03, 0x1B,0x03, 0x1F,0x66, 0x20,0x03, 0x21,0x03, 0x20,0x03, 0x1F,0x03, 0x1B,0x03, 0x1F,0x66, 0x1F,0x03, 0x1B,0x03, 0x19,0x03, 0x19,0x03, 0x15,0x03, 0x1A,0x66, 0x1A,0x03, 0x19,0x03, 0x19,0x03, 0x1F,0x03, 0x1B,0x03, 0x1F,0x00, 0x1A,0x03, 0x1A,0x03, 0x1A,0x03, 0x1B,0x03, 0x1B,0x03, 0x1A,0x03, 0x19,0x03, 0x19,0x02, 0x17,0x03, 0x15,0x17, 0x15,0x03, 0x16,0x03, 0x17,0x03, 0x18,0x03, 名称:延时函数 功能:C 语言中常用的 for 循环延时方法 延时长短可以通过变量初值来改变 */ */ */ 名称:蜂鸣器奏乐演示程序 作者:苏涛 时间:2011-01-18 单位:安徽建筑工业学院 电子与信息工程学院 07 级电子信息工程专业 二班 */ */ */ */ */ /******************************************************************/ //头文件调用,写程序时都要加上 /******************************************************************/ 0x17,0x04, 0x18,0x0E, 0x18,0x03, 0x17,0x04, 0x18,0x0E, 0x18,0x66, 0x17,0x03, 0x18,0x03, 0x17,0x03, 0x18,0x03, 0x20,0x03, 0x20,0x02, 0x1F,0x03, 0x1B,0x03, 0x1F,0x66, 0x20,0x03, 0x21,0x03, 0x20,0x03, 0x1F,0x03, 0x1B,0x03, 0x1F,0x66, 0x1F,0x04, 0x1B,0x0E, 0x1B,0x03, 0x19,0x03, 0x19,0x03, 0x15,0x03, 0x1A,0x66, 0x1A,0x03, 0x19,0x03, 0x15,0x03, 0x15,0x03, 0x17,0x03, 0x16,0x66, 0x17,0x04, 0x18,0x04, 0x18,0x03, 0x19,0x03, 0x1F,0x03, 0x1B,0x03, 0x1F,0x66, 0x20,0x03, 0x21,0x03, 0x20,0x03, 0x1F,0x03, 0x1B,0x03, 0x1F,0x66, 0x1F,0x03, 0x1B,0x03, 0x19,0x03, 0x19,0x03, 0x15,0x03, 0x1A,0x66, 0x1A,0x03, 0x19,0x03, 0x19,0x03, 0x1F,0x03, 0x1B,0x03, 0x1F,0x00, 0x18,0x02, 0x18,0x03, 0x1A,0x03, 0x19,0x0D, 0x15,0x03, 0x15,0x02, 0x18,0x66, 0x16,0x02, 0x17,0x02, 0x15,0x00, 0x00,0x00}; //同一首歌 unsigned char code Music_Same[]={ 0x0F,0x01, 0x15,0x02, 0x16,0x02, 0x17,0x66, 0x18,0x03, 0x17,0x02, 0x15,0x02, 0x16,0x01, 0x15,0x02, 0x10,0x02, 0x15,0x00, 0x0F,0x01, 0x15,0x02, 0x16,0x02, 0x17,0x02, 0x17,0x03, 0x18,0x03, 0x19,0x02, 0x15,0x02, 0x18,0x66, 0x17,0x03, 0x19,0x02, 0x16,0x03, 0x17,0x03, 0x16,0x00, 0x17,0x01, 0x19,0x02, 0x1B,0x02, 0x1B,0x70, 0x1A,0x03, 0x1A,0x01, 0x19,0x02, 0x19,0x03, 0x1A,0x03, 0x1B,0x02, 0x1A,0x0D, 0x19,0x03, 0x17,0x00, 0x18,0x66, 0x18,0x03, 0x19,0x02, 0x1A,0x02, 0x19,0x0C, 0x18,0x0D, 0x17,0x03, 0x16,0x01, 0x11,0x02, 0x11,0x03, 0x10,0x03, 0x0F,0x0C, 0x10,0x02, 0x15,0x00, 0x1F,0x01, 0x1A,0x01, 0x18,0x66, 0x19,0x03, 0x1A,0x01, 0x1B,0x02, 0x1B,0x03, 0x1B,0x03, 0x1B,0x0C, 0x1A,0x0D, 0x19,0x03, 0x17,0x00, 0x1F,0x01, 0x1A,0x01, 0x18,0x66, 0x19,0x03, 0x1A,0x01, 0x10,0x02, 0x10,0x03, 0x10,0x03, 0x1A,0x0C, 0x18,0x0D, 0x17,0x03, 0x16,0x00, 0x0F,0x01, 0x15,0x02, 0x16,0x02, 0x17,0x70, 0x18,0x03, 0x17,0x02, 0x15,0x03, 0x15,0x03, 0x16,0x66, 0x16,0x03, 0x16,0x02, 0x16,0x03, 0x15,0x03, 0x10,0x02, 0x10,0x01, 0x11,0x01, 0x11,0x66, 0x10,0x03, 0x0F,0x0C, 0x1A,0x02, 0x19,0x02, 0x16,0x03, 0x16,0x03, 0x18,0x66, 0x18,0x03, 0x18,0x02, 0x17,0x03, 0x16,0x03, 0x19,0x00, 0x00,0x00 }; //两只蝴蝶 unsigned char code Music_Two[] ={ 0x17,0x03, 0x16,0x03, 0x17,0x01, 0x16,0x03, 0x17,0x03, 0x16,0x03, 0x15,0x01, 0x10,0x03, 0x15,0x03, 0x16,0x02, 0x16,0x0D, 0x17,0x03, 0x16,0x03, 0x15,0x03, 0x10,0x03, 0x10,0x0E, 0x15,0x04, 0x0F,0x01, 0x17,0x03, 0x16,0x03, 0x17,0x01, 0x16,0x03, 0x17,0x03, 0x16,0x03, 0x15,0x01, 0x10,0x03, 0x15,0x03, 0x16,0x02, 0x16,0x0D, 0x17,0x03, 0x16,0x03, 0x15,0x03, 0x10,0x03, 0x15,0x03, 0x16,0x01, 0x17,0x03, 0x16,0x03, 0x17,0x01, 0x16,0x03, 0x17,0x03, 0x16,0x03, 0x15,0x01, 0x10,0x03, 0x15,0x03, 0x16,0x02, 0x16,0x0D, 0x17,0x03, 0x16,0x03, 0x15,0x03, 0x10,0x03, 0x10,0x0E, 0x15,0x04, 0x0F,0x01, 0x17,0x03, 0x19,0x03, 0x19,0x01, 0x19,0x03, 0x1A,0x03, 0x19,0x03, 0x17,0x01, 0x16,0x03, 0x16,0x03, 0x16,0x02, 0x16,0x0D, 0x17,0x03, 0x16,0x03, 0x15,0x03, 0x10,0x03, 0x10,0x0D, 0x15,0x00, 0x19,0x03, 0x19,0x03, 0x1A,0x03, 0x1F,0x03, 0x1B,0x03, 0x1B,0x03, 0x1A,0x03, 0x17,0x0D, 0x16,0x03, 0x16,0x03, 0x16,0x0D, 0x17,0x01, 0x17,0x03, 0x17,0x03, 0x19,0x03, 0x1A,0x02, 0x1A,0x02, 0x10,0x03, 0x17,0x0D, 0x16,0x03, 0x16,0x01, 0x17,0x03, 0x19,0x03, 0x19,0x03, 0x17,0x03, 0x19,0x02, 0x1F,0x02, 0x1B,0x03, 0x1A,0x03, 0x1A,0x0E, 0x1B,0x04, 0x17,0x02, 0x1A,0x03, 0x1A,0x03, 0x1A,0x0E, 0x1B,0x04, 0x1A,0x03, 0x19,0x03, 0x17,0x03, 0x16,0x03, 0x17,0x0D, 0x16,0x03, 0x17,0x03, 0x19,0x01, 0x19,0x03, 0x19,0x03, 0x1A,0x03, 0x1F,0x03, 0x1B,0x03, 0x1B,0x03, 0x1A,0x03, 0x17,0x0D, 0x16,0x03, 0x16,0x03, 0x16,0x03, 0x17,0x01, 0x17,0x03, 0x17,0x03, 0x19,0x03, 0x1A,0x02, 0x1A,0x02, 0x10,0x03, 0x17,0x0D, 0x16,0x03, 0x16,0x01, 0x17,0x03, 0x19,0x03, 0x19,0x03, 0x17,0x03, 0x19,0x03, 0x1F,0x02, 0x1B,0x03, 0x1A,0x03, 0x1A,0x0E, 0x1B,0x04, 0x17,0x02, 0x1A,0x03, 0x1A,0x03, 0x1A,0x0E, 0x1B,0x04, 0x17,0x16, 0x1A,0x03, 0x1A,0x03, 0x1A,0x0E, 0x1B,0x04, 0x1A,0x03, 0x19,0x03, 0x17,0x03, 0x16,0x03, 0x0F,0x02, 0x10,0x03, 0x15,0x00, 0x00,0x00 }; /*************************************************************************************/ void main() { InitialSound(); while(1) { Play(Music_Girl,0,3,360); Delay1ms(500); Play(Music_Same,0,3,360); Delay1ms(500); Play(Music_Two,0,3,360); Delay1ms(500); } } /******************************************************************/ /* /* 头文件:SOUNDPLAY.H 说明:此头文件来源于网络 */ */ /******************************************************************/ /*说明************************************************************** 曲谱存贮格式 unsigned char code MusicName{音高,音长,音高,音长……, 0,0}; 末尾:0,0 表示结束(Important) 音高由三位数字组成: 个位是表示 1 ̄7 这七个音符 十位是表示音符所在的音区:1-低音,2-中音,3-高音; 百位表示这个音符是否要升半音: 0-不升,1-升半音。

51单片机蜂鸣器播放音乐代码(生日快乐 两只蝴蝶 祝你平安)

51单片机蜂鸣器播放音乐代码(生日快乐 两只蝴蝶 祝你平安)

精心整理/*生日快乐歌曲*/#include<reg51.h>#defineuintunsignedint#defineucharunsignedcharsbitbeep=P1^5;ucharcodeSONG_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};ucharcodeSONG_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};//延时voidDelayMS(uintx){uchart;while(x--)for(t=0;t<120;t++);}voidPlayMusic(){uinti=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++;}}voidmain(){beep=0;while(1){PlayMusic();//播放生日快乐DelayMS(500);//播放完后暂停一段时间}}两只蝴蝶:#include<reg51.h>//这是单片机音乐代码生成器生成的代码#defineucharunsignedcharsbitbeepIO=P1^5;//输出为P1.5可以修改成其它IO口ucharm,n;ucharcodeT[49][2]={{0,0},{0xF8,0x8B},{0xF8,0xF2},{0xF9,0x5B},{0xF9,0xB7},{0xFA,0x14},{0xFA,0x66},{0xFA,0xB9},{0xFB,0x03},{0xFB,0x4A },{0xFB,0x8F},{0xFB,0xCF},{0xFC,0x0B},{0xFC,0x43},{0xFC,0x78},{0xFC,0xAB},{0xFC,0xDB},{0xFD,0x08},{0xFD,0x33},{0xFD,0x5B},{0xFD,0x81},{0xFD,0x A5},{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}};ucharcodemusic[][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},{2 6,4},{23,4},{21,8},{23,4},{21,4},{23,4},{26,16},{0xFF,0xFF}};voiddelay(ucharp){uchari,j;for(;p>0;p--)for(i=181;i>0;i--)for(j=181;j>0;j--);}voidpause(){uchari,j;for(i=150;i>0;i--)for(j=150;j>0;j--);}voidT0_int()interrupt1{beepIO=!beepIO;TH0=T[m][0];TL0=T[m][1];}voidmain(){uchari=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++;}elseif(m==0xFF){TR0=0;delay(30);i=0;}elseif(m==music[i+1][0]){TR0=1;delay(n);TR0=0;pause();i++;}else{TR0=1;delay(n);i++;}}}祝你平安:#include"reg52.h"unsignedcharCount;sbit_Speak=P1^5;unsignedcharcodeSONG[]={//祝你平安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};voidTime0_Init(){TMOD=0x01;IE=0x82;TH0=0xD8;TL0=0xEF;//12MZ晶振,10ms}voidTime0_Int()interrupt1{TH0=0xD8;TL0=0xEF;Count++;//长度加1}/*-------------------------------------------------功能:1MS延时子程序-------------------------------------------------*/voidDelay_xMs(unsignedintx){unsignedinti,j;for(i=0;i<x;i++){for(j=0;j<3;j++);}}voidPlay_Song(unsignedchari){unsignedcharTemp1,Temp2; unsignedintAddr;Count=0;//中断计数器清0Addr=i*217;while(1){Temp1=SONG[Addr++];if(Temp1==0xFF)//休止符{TR0=0;Delay_xMs(100);}elseif(Temp1==0x00)//歌曲结束符{return;}else{Temp2=SONG[Addr++];TR0=1;while(1){_Speak=~_Speak;Delay_xMs(Temp1);if(Temp2==Count){Count=0;break;}}}}}/*------------------------------------------------- 功能:主程序-------------------------------------------------*/ voidmain(){Time0_Init();//定时器0中断初始化while(1){Play_Song(0);//播放}}。

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

单片机蜂鸣器播放音乐代码生日快乐两只蝴蝶祝你平安

单片机蜂鸣器播放音乐代码生日快乐两只蝴蝶祝你平安

单片机蜂鸣器播放音乐代码生日快乐两只蝴蝶祝你平安/*生日快乐歌曲*/#include<reg51.h>#defineuintunsignedint#defineucharunsignedcharsbitbeep=P1^5;ucharcodeSONG_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};ucharcodeSONG_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};//延时voidDelayMS(uintx){uchart;while(x--)for(t=0;t<120;t++);}voidPlayMusic(){uinti=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++;}}voidmain(){beep=0;while(1){PlayMusic();//播放生日快乐DelayMS(500);//播放完后暂停一段时间}}两只蝴蝶:#include<reg51.h>//这是单片机音乐代码生成器生成的代码#defineucharunsignedcharsbitbeepIO=P1^5;//输出为P1.5可以修改成其它IO口ucharcodeT[49][2]={{0,0},{0xF8,0x8B},{0xF8,0xF2},{0xF9,0x5B},{0xF9,0xB7},{0xFA,0x14},{0xFA,0x66},{0xFA,0xB9},{ 0xFB,0x03},{0xFB,0x4A},{0xFB,0x8F},{0xFB,0xCF},{0xFC,0x0B},{0xFC,0x43},{0xFC,0x78},{0xFC,0xAB},{0xFC,0xDB},{0xFD,0x08},{0xFD,0x33},{0xFD,0x5B},{ 0xFD,0x81},{0xFD,0xA5},{0xFD,0xC7},{0xFD,0xE7},{0xFE,0x05},{0xFE,0x21},{0xFE,0x3C},{0xFE,0x55},{0xFE,0x6D},{0xFE,0x84},{0xFE,0x99},{0xFE,0xAD},{ 0xFE,0xC0},{0xFE,0x02},{0xFE,0xE3},{0xFE,0xF3},{0xFF,0x02},{0xFF,0x10},{0xFF,0x1D},{0xFF,0x2A},{0xFF,0x36},{0xFF,0x42},{0xFF,0x4C},{0xFF,0x56},{ 0xFF,0x60},{0xFF,0x69},{0xFF,0x71},{0xFF,0x79},{0xFF,0x81}};ucharcodemusic[][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}};voiddelay(ucharp){uchari,j;for(;p>0;p--)for(i=181;i>0;i--)for(j=181;j>0;j--);}voidpause(){uchari,j;for(i=150;i>0;i--)for(j=150;j>0;j--);}voidT0_int()interrupt1{beepIO=!beepIO;TH0=T[m][0];TL0=T[m][1];}voidmain(){uchari=0;while(1){m=music[i][0];n=music[i][1];if(m==0x00){TR0=0;delay(n);i++;}elseif(m==0xFF){TR0=0;delay(30);i=0;}elseif(m==music[i+1][0]){TR0=1;delay(n);TR0=0;pause();i++;}else{TR0=1;delay(n);i++;}}}祝你平安:#include"reg52.h"unsignedcharCount;sbit_Speak=P1^5;unsignedcharcodeSONG[]={//祝你平安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};voidTime0_Init(){TMOD=0x01;IE=0x82;TH0=0xD8;TL0=0xEF;//12MZ晶振,10ms}voidTime0_Int()interrupt1{TH0=0xD8;TL0=0xEF;Count++;//长度加1}功能:1MS延时子程序-------------------------------------------------*/ voidDelay_xMs(unsignedintx){unsignedinti,j;for(i=0;i<x;i++){for(j=0;j<3;j++);}}voidPlay_Song(unsignedchari){unsignedcharTemp1,Temp2;unsignedintAddr;Count=0;//中断计数器清0Addr=i*217;while(1){Temp1=SONG[Addr++];if(Temp1==0xFF)//休止符{TR0=0;Delay_xMs(100);}elseif(Temp1==0x00)//歌曲结束符{return;}else{Temp2=SONG[Addr++];TR0=1;while(1){_Speak=~_Speak;Delay_xMs(Temp1);if(Temp2==Count){Count=0;break;}}}}}功能:主程序-------------------------------------------------*/ voidmain(){Time0_Init();//定时器0中断初始化while(1){Play_Song(0);//播放}}。

单片机按键控制蜂鸣器发声程序

单片机按键控制蜂鸣器发声程序
void Time0_Init()//定时器T0方式1,定时10ms
{
TMOD = 0x01;
IE= 0x82;
TH0 = 0xDC;
TL0 = 0x00;
}
void Time0_Int() interrupt 1
{
TH0 = 0xDC;
TL0 = 0x00;
Count++;
}
void delay (uint8 k)//按键防抖延时
{
return;
}
else
{
Temp2 = SONG[Addr++];
TR0 = 1;
while(1)
{
Speak = ~Speak;
Delay_xMs(Temp1);
if(Temp2 == Count)
{CΒιβλιοθήκη unt = 0;break;
}
}
}
}
}
void keyscan (void)//按键切换声音函数
单片机按键控制蜂鸣器发声程序单片机蜂鸣器音乐程序单片机蜂鸣器程序单片机蜂鸣器报警程序蜂鸣器发声程序单片机按键程序单片机独立按键程序单片机按键扫描程序单片机按键消抖程序单片机按键中断程序
#include<reg52.h>
typedef unsigned char uint8;
typedef unsigned int uint16;
{
uint8 Temp1,Temp2;
uint8 Addr;
Count = 0;//中断计数器清0
Addr = i *3;
while(1)
{
Temp1 = SONG[Addr++];

基于51单片机蜂鸣器发声的-C语言程序

基于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单片机

蜂鸣器演奏祝你平安——51单片机

蜂鸣器演奏祝你平安——51单片机的Proteus实验实验原理51单片机的一个I/O口控制speaker发声,演唱祝你平安歌曲。

主要器件以及电路图单片机——AT89C51,蜂鸣器——speaker。

汇编程序源码晶振12M,已在Proteus下测试,好像有点失真。

程序是从网上照的,感觉不是很对劲,删除了一部分之后还是没有变化。

部分注释是我后加上去的。

;------------------------------------; 蜂鸣器演奏--祝你平安; 功能:蜂鸣器-蜂鸣器奏乐-祝你平安;------------------------------------SPK bit P2.7 ;以后SPK就是speaker了ORG 0000HLJMP STARTORG 000BHINC 20H ;T0中断服务,中断计数器加1MOV TH0,#0D8HMOV TL0,#0EFH ;12M晶振,形成10毫秒中断RETISTART:MOV TH0,#0D8HMOV TL0,#0EFHMOV TMOD,#01HMOV IE,#82H ;10000010B——开T0中断MUSIC0:NOPMOV DPTR,#DAT ;表头地址送DPTRMOV 20H,#00H ;中断计数器清0MUSIC1:NOPCLR AMOVC A,@A+DPTR ;查表取代码JZ END0 ;是00H,则结束CJNE A,#0FFH,MUSIC5 ;只要A不等于FF,一直执行MUSIC5LJMP MUSIC3 ;当A到FF的时候,执行MUSIC3 MUSIC5:NOPMOV R6,A ;代码送R6INC DPTRMOVC A,@A+DPTR ;取节拍代码送R7MOV R7,ASETB TR0 ;启动计数MUSIC2:NOPCPL SPKMOV A,R6 ;按R6(代码)延时MOV R3,A ;LCALL DEL ;利用代码(R6)来调用延时CJNE A,20H,MUSIC2 ;中断计数器(20H)=R7否?不等,则继续循环 MOV 20H,#00H ;等于,则取下一代码INC DPTRLJMP MUSIC1MUSIC3:NOPCLR TR0 ;休止100毫秒MOV R2,#0DHMUSIC4:NOPMOV R3,#0FFHLCALL DELDJNZ R2,MUSIC4INC DPTRLJMP MUSIC1END0:NOPMOV R2,#64H ;歌曲结束,延时1秒后继续MUSIC6:MOV R3,#00HLCALL DELDJNZ R2,MUSIC6LJMP MUSIC0DEL: ;DEL子程序利用R3来延时一定时间 NOPDEL3:MOV R4,#02HDEL4:NOPDJNZ R4,DEL4NOPDJNZ R3,DEL3RETNOPDAT: ;祝你平安db 26h,20h,20h,20h,20h,20h,26h,10h,20h,10h,20h,80h,26h,20h,30h,20h db 30h,20h,39h,10h,30h,10h,30h,80h,26h,20h,20h,20h,20h,20h,1ch,20h db 20h,80h,2bh,20h,26h,20h,20h,20h,2bh,10h,26h,10h,2bh,80h,26h,20h db 30h,20h,30h,20h,39h,10h,26h,10h,26h,60h,40h,10h,39h,10h,26h,20h db 30h,20h,30h,20h,39h,10h,26h,10h,26h,80h,26h,20h,2bh,10h,2bh,10h db 2bh,20h,30h,10h,39h,10h,26h,10h,2bh,10h,2bh,20h,2bh,40h,40h,20h db 20h,10h,20h,10h,2bh,10h,26h,30h,30h,80h,18h,20h,18h,20h,26h,20h db 20h,20h,20h,40h,26h,20h,2bh,20h,30h,20h,30h,20h,1ch,20h,20h,20h db 20h,80h,1ch,20h,1ch,20h,1ch,20h,30h,20h,30h,60h,39h,10h,30h,10h db 20h,20h,2bh,10h,26h,10h,2bh,10h,26h,10h,26h,10h,2bh,10h,2bh,80h db 18h,20h,18h,20h,26h,20h,20h,20h,20h,60h,26h,10h,2bh,20h,30h,20h db 30h,20h,1ch,20h,20h,20h,20h,80h,26h,20h,30h,10h,30h,10h,30h,20h db 39h,20h,26h,10h,2bh,10h,2bh,20h,2bh,40h,40h,10h,40h,10h,20h,10h db 20h,10h,2bh,10h,26h,30h,30h,80h,00HEND ;程序结束。

(原创)51单片机C语言程序设计--速学教程实例(进阶篇)之蜂鸣器奏乐

(原创)51单片机C语言程序设计--速学教程实例(进阶篇)之蜂鸣器奏乐

/******************************************************************//*名称:蜂鸣器奏乐演示程序*//*作者:苏涛*//*时间:2011-01-18*//*单位:安徽建筑工业学院电子与信息工程学院*//*07级电子信息工程专业二班*//******************************************************************/#include<reg52.h>//头文件调用,写程序时都要加上#include"SoundPlay.h"/******************************************************************//*名称:延时函数*//*功能:C语言中常用的for循环延时方法*//*延时长短可以通过变量初值来改变*//******************************************************************/void Delay1ms(uint count){uint i,j;for(i=0;i<count;i++)for(j=0;j<120;j++);}/*****************************Music***************************************************/ //挥着翅膀的女孩unsigned char code Music_Girl[]={0x17,0x02,0x17,0x03,0x18,0x03,0x19,0x02,0x15,0x03,0x16,0x03,0x17,0x03,0x17,0x03,0x17,0x03,0x18,0x03,0x19,0x02,0x16,0x03,0x17,0x03,0x18,0x02,0x18,0x03,0x17,0x03,0x15,0x02,0x18,0x03,0x17,0x03,0x18,0x02,0x10,0x03,0x15,0x03,0x16,0x02,0x15,0x03,0x16,0x03,0x17,0x02,0x17,0x03,0x18,0x03,0x19,0x02,0x1A,0x03,0x1B,0x03,0x1F,0x03,0x1F,0x03,0x17,0x03,0x18,0x03,0x19,0x02,0x16,0x03,0x17,0x03,0x18,0x03,0x17,0x03,0x18,0x03,0x1F,0x03,0x1F,0x02,0x16,0x03,0x17,0x03,0x18,0x03,0x17,0x03,0x18,0x03,0x20,0x03,0x20,0x02,0x1F,0x03,0x1B,0x03,0x1F,0x66,0x20,0x03,0x21,0x03,0x20,0x03,0x1F,0x03,0x1B,0x03,0x1F,0x66,0x1F,0x03,0x1B,0x03,0x19,0x03,0x19,0x03,0x15,0x03,0x1A,0x66,0x1A,0x03,0x19,0x03,0x15,0x03,0x15,0x03,0x17,0x03,0x16,0x66,0x17,0x04,0x18,0x04,0x18,0x03,0x19,0x03,0x1F,0x03,0x1B,0x03,0x1F,0x66,0x20,0x03,0x21,0x03,0x20,0x03,0x1F,0x03,0x1B,0x03,0x1F,0x66,0x1F,0x03,0x1B,0x03,0x19,0x03,0x19,0x03,0x15,0x03,0x1A,0x66,0x1A,0x03,0x19,0x03,0x19,0x03,0x1F,0x03,0x1B,0x03,0x1F,0x00,0x1A,0x03,0x1A,0x03,0x1A,0x03,0x1B,0x03,0x1B,0x03,0x1A,0x03,0x19,0x03,0x19,0x02,0x17,0x03,0x15,0x17,0x15,0x03,0x16,0x03,0x17,0x03,0x18,0x03,0x17,0x04,0x18,0x0E,0x18,0x03,0x17,0x04,0x18,0x0E,0x18,0x66,0x17,0x03,0x18,0x03,0x17,0x03,0x18,0x03,0x20,0x03,0x20,0x02,0x1F,0x03,0x1B,0x03,0x1F,0x66,0x20,0x03,0x21,0x03,0x20,0x03,0x1F,0x03,0x1B,0x03,0x1F,0x66,0x1F,0x04,0x1B,0x0E,0x1B,0x03,0x19,0x03,0x19,0x03,0x15,0x03,0x1A,0x66,0x1A,0x03,0x19,0x03,0x15,0x03,0x15,0x03,0x17,0x03,0x16,0x66,0x17,0x04,0x18,0x04,0x18,0x03,0x19,0x03,0x1F,0x03,0x1B,0x03,0x1F,0x66,0x20,0x03,0x21,0x03,0x20,0x03,0x1F,0x03,0x1B,0x03,0x1F,0x66,0x1F,0x03,0x1B,0x03,0x19,0x03,0x19,0x03,0x15,0x03,0x1A,0x66,0x1A,0x03,0x19,0x03,0x19,0x03,0x1F,0x03,0x1B,0x03,0x1F,0x00,0x18,0x02,0x18,0x03,0x1A,0x03,0x19,0x0D,0x15,0x03,0x15,0x02,0x18,0x66,0x16,0x02,0x17,0x02,0x15,0x00,0x00,0x00}; //同一首歌unsigned char code Music_Same[]={0x0F,0x01,0x15,0x02,0x16,0x02,0x17,0x66,0x18,0x03,0x17,0x02,0x15,0x02,0x16,0x01,0x15,0x02,0x10,0x02,0x15,0x00,0x0F,0x01,0x15,0x02,0x16,0x02,0x17,0x02,0x17,0x03,0x18,0x03,0x19,0x02,0x15,0x02,0x18,0x66,0x17,0x03,0x19,0x02,0x16,0x03,0x17,0x03,0x16,0x00,0x17,0x01,0x19,0x02,0x1B,0x02,0x1B,0x70,0x1A,0x03,0x1A,0x01,0x19,0x02,0x19,0x03,0x1A,0x03,0x1B,0x02,0x1A,0x0D,0x19,0x03,0x17,0x00,0x18,0x66,0x18,0x03,0x19,0x02,0x1A,0x02,0x19,0x0C,0x18,0x0D,0x17,0x03,0x16,0x01,0x11,0x02,0x11,0x03,0x10,0x03,0x0F,0x0C,0x10,0x02,0x15,0x00,0x1F,0x01,0x1A,0x01,0x18,0x66,0x19,0x03,0x1A,0x01,0x1B,0x02,0x1B,0x03,0x1B,0x03,0x1B,0x0C,0x1A,0x0D,0x19,0x03,0x17,0x00,0x1F,0x01,0x1A,0x01,0x18,0x66,0x19,0x03,0x1A,0x01,0x10,0x02,0x10,0x03,0x10,0x03,0x1A,0x0C,0x18,0x0D,0x17,0x03,0x16,0x00,0x0F,0x01,0x15,0x02,0x16,0x02,0x17,0x70,0x18,0x03,0x17,0x02,0x15,0x03,0x15,0x03,0x16,0x66,0x16,0x03,0x16,0x02,0x16,0x03,0x15,0x03,0x10,0x02,0x10,0x01,0x11,0x01,0x11,0x66,0x10,0x03,0x0F,0x0C,0x1A,0x02,0x19,0x02,0x16,0x03,0x16,0x03,0x18,0x66,0x18,0x03,0x18,0x02,0x17,0x03,0x16,0x03,0x19,0x00,0x00,0x00};//两只蝴蝶unsigned char code Music_Two[]={0x17,0x03,0x16,0x03,0x17,0x01,0x16,0x03,0x17,0x03,0x16,0x03,0x15,0x01,0x10,0x03,0x15,0x03,0x16,0x02,0x16,0x0D,0x17,0x03,0x16,0x03,0x15,0x03,0x10,0x03,0x10,0x0E,0x15,0x04,0x0F,0x01,0x17,0x03,0x16,0x03,0x17,0x01,0x16,0x03,0x17,0x03,0x16,0x03,0x15,0x01,0x10,0x03,0x15,0x03,0x16,0x02,0x16,0x0D,0x17,0x03,0x16,0x03,0x15,0x03,0x10,0x03,0x15,0x03,0x16,0x01,0x17,0x03,0x16,0x03,0x17,0x01,0x16,0x03,0x17,0x03,0x16,0x03,0x15,0x01,0x10,0x03,0x15,0x03,0x16,0x02,0x16,0x0D,0x17,0x03,0x16,0x03,0x15,0x03,0x10,0x03,0x10,0x0E,0x15,0x04,0x0F,0x01,0x17,0x03,0x19,0x03,0x19,0x01,0x19,0x03,0x1A,0x03,0x19,0x03,0x17,0x01,0x16,0x03,0x16,0x03,0x16,0x02,0x16,0x0D,0x17,0x03,0x16,0x03,0x15,0x03,0x10,0x03,0x10,0x0D,0x15,0x00,0x19,0x03,0x19,0x03,0x1A,0x03,0x1F,0x03,0x1B,0x03,0x1B,0x03,0x1A,0x03,0x17,0x0D,0x16,0x03,0x16,0x03,0x16,0x0D,0x17,0x01,0x17,0x03,0x17,0x03,0x19,0x03,0x1A,0x02,0x1A,0x02,0x10,0x03,0x17,0x0D,0x16,0x03,0x16,0x01,0x17,0x03,0x19,0x03,0x19,0x03,0x17,0x03,0x19,0x02,0x1F,0x02,0x1B,0x03,0x1A,0x03,0x1A,0x0E,0x1B,0x04,0x17,0x02,0x1A,0x03,0x1A,0x03,0x1A,0x0E,0x1B,0x04,0x1A,0x03,0x19,0x03,0x17,0x03,0x16,0x03,0x17,0x0D,0x16,0x03,0x17,0x03,0x19,0x01,0x19,0x03,0x19,0x03,0x1A,0x03,0x1F,0x03,0x1B,0x03,0x1B,0x03,0x1A,0x03,0x17,0x0D,0x16,0x03,0x16,0x03,0x16,0x03,0x17,0x01,0x17,0x03,0x17,0x03,0x19,0x03,0x1A,0x02,0x1A,0x02,0x10,0x03,0x17,0x0D,0x16,0x03,0x16,0x01,0x17,0x03,0x19,0x03,0x19,0x03,0x17,0x03,0x19,0x03,0x1F,0x02,0x1B,0x03,0x1A,0x03,0x1A,0x0E,0x1B,0x04,0x17,0x02,0x1A,0x03,0x1A,0x03,0x1A,0x0E,0x1B,0x04,0x17,0x16,0x1A,0x03,0x1A,0x03,0x1A,0x0E,0x1B,0x04,0x1A,0x03,0x19,0x03,0x17,0x03,0x16,0x03,0x0F,0x02,0x10,0x03,0x15,0x00,0x00,0x00};/*************************************************************************************/ void main(){InitialSound();while(1){Play(Music_Girl,0,3,360);Delay1ms(500);Play(Music_Same,0,3,360);Delay1ms(500);Play(Music_Two,0,3,360);Delay1ms(500);}}/******************************************************************//*头文件:SOUNDPLAY.H*//*说明:此头文件来源于网络*//******************************************************************//*说明**************************************************************曲谱存贮格式unsigned char code MusicName{音高,音长,音高,音长....,0,0};末尾:0,0表示结束(Important)音高由三位数字组成:个位是表示1~7这七个音符十位是表示音符所在的音区:1-低音,2-中音,3-高音;百位表示这个音符是否要升半音: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);}```以上代码中,我们首先定义了各个音符的频率,这些频率是通过实验测量得到的,具体数值可能因蜂鸣器型号、电源电压等因素而略有差异。

51单片机电子音乐设计

51单片机电子音乐设计

实验三电子音乐设计一、实验内容
1.调试《生日快乐歌》,在实验板上运行程序播放。

2.编写《兰花草》程序,在实验板上运行程序播放。

二.实验设计
1.硬件电路
图3.1 硬件电路原理图
2.程序设计
《生日快乐》程序,参考课件。

《兰花草》程序设计流程图,见图3.2, 3.2. 单片机P3.7输出一定频率信号,经过9012驱动蜂鸣器发声。

三、实验操作
1. 打开KEILuvison3软件,建立工程,设置调试环境,实验板上电;
2. 输入源程序,编译,连接,加载;
3. 运行《生日快乐》歌曲程序,聆听歌曲播放效果;
4.运行《兰花草》歌曲程序,聆听歌曲播放效果。

四、实验报告要求
1.实验名称
2.实验内容
3.实验设计,包括:
(1)硬件电路原理图
(2)程序流程图和源程序
4.实验操作,包括调试程序的方法、过程、出现的问题及解决
5.实验总结
图3.3 T0中断子程序流程图图3.2电子音乐主程序流程图。

51单片机演奏音乐“祝你生日快乐”

51单片机演奏音乐“祝你生日快乐”

R7,A ;r7=30H 音符发音的时间 TR0 ;启动计数
;不等,则继续循环
MOV INC LJMP
20H,#00H DPTR MUSIC1
;等于,则取下一代码
MUSIC3: NOP CLR MOV TR0 ;休止 100 毫秒 R2,#0DH
MUSIC4: NOP MOV LCALL DJNZ INC LJMP END0: NOP R2,#64H ;歌曲结束,延时 1 秒后继续 R3,#0FFH DEL R2,MUSIC4 DPTR MUSIC1
MOV
MUSIC6: MOV LCALL DJNZ LJMP DEL: NOP DEL3: DEL4: MOV NOP DEL
R3,#00H
R2,MUSIC6 MUSIC0
R4,#02H
DJNZ NOP DJNZ RET MUSIC_DAT:
R4,DEL4
R3,DEL3
; 猪你生日快乐曲谱 DB 3FH,10H DB 0FFH DB 3FH, DB DB DB DB 0FFH DB DB DB DB 3FH, 10H 20H, 30H, 3FH, 40H 20H 10H 39H, 30H, 20H, 10H 20H, 33H, 3FH, 40H 20H
TH0,#0bbH ;d8 TL0,#0EFH ;12M 晶振,形成 10 毫秒中断
TH0,#0D8H TL0,#0EFH TMOD,#01H IE,#82H
MUSIC1: NOP
CLR MOVC JZ
A A,@A+DPTR E来自D0 ;查表取代码;是 00H,则结束
CPL P1.7 CPL P1.6 CJNE LJMP MUSIC5: NOP MOV INC MOV MOVC MOV SETB MUSIC2: NOP CPL MOV MOV LCALL MOV CJNE P2.3 ;音乐输出的管脚 A,R6 R3,A DEL A,R7 A,20H,MUSIC2 ;中断计数器(20H)=R7 否? R6,A DPTR A,B A,@A+DPTR ;取节拍代码送 R7 ;r6=18H 音符的频率 A,#0FFH,MUSIC5 MUSIC3 ;是休止符则继续执行

单片机实训报告

单片机实训报告

单片机原理及应用实训报告题目使用单片机使蜂鸣器发声编程产生一段《祝你平安》的音乐院系专业学生姓名学号指导教师完成日期一、系统设计(题目要求和设计思路)1.题目要求使用单片机使蜂鸣器发声,编程产生一段《祝你平安》的音乐。

2.设计思路通过单片机的定时器定时中断,将单片机上对应蜂鸣器的I/O来回取反,或者说来回清零,置位。

从而让蜂鸣器发出声音。

为了让单片机发出不同频率的声音,只需将定时器予置不同的定时值就可实现。

以标准音高A为例:A的频率f=440Hz,其对应的周期为:T=1/f=1/440=2272µs则单片机上对应蜂鸣器的I/O口来回取反的时间为:t=T/2=2272/2=1136µs这个t就是单片机上定时器应有的中断触发时间。

在单片机上控制一个音符唱多长,可采用循环延时的方法实现。

对于乐曲中的休止符,一般将其音调参数设为FFH,FFH,其节拍参数与其它音符的节拍参数确定方法一致,乐曲结束用节拍参数为00H来表示。

二、硬件设计(硬件设计方法和实验电路、实验照片)1.硬件设计方法:a.用二号导线连接80C51 MCU模块的P1.4端到蜂鸣器模块的IN 端。

b.用串行数据通信线连接计算机与仿真器。

把仿真器插到80C51 MCU模块的40P 锁紧插座中,c.将89C51 MCU模块的电源扭子开关SIC拨到上端,将蜂鸣器模块的电源短路帽J1A2打在上端。

将直流稳压电源模块的直流控制开关S1G1打到ON.2.实验电路:3.实验照片三、软件设计(流程图及源程序)1. 流程图:T0初始化,B 清零,开中断,中断计数器,20H 清0表头地址送DPTR,表头序号送B查表取常数是结束符00H启动T0计数器音符常数送R6取节拍常数送R7恢复初始条件B 清0P1.4取反按R6常数延迟延迟一秒后再循环T1停止计数,中断计数器,20H 清0,B 加1延时100毫秒结拍时间到是休止符FFH2.源程序:SPK bit P1.4;(位定义)ORG 0000H;(伪指令,指定程序从000H开始存放)LJMP START;(程序跳转至START处执行)ORG 000BHINC 20H ;(中断服务,中断计数器加1)MOV TH0,#0D8HMOV TL0,#0EFH ;(12M晶振,形成10ms中断)RETISTART:MOV SP,#50HMOV TH0,#0D8HMOV TL0,#0EFHMOV TMOD,#01HMOV IE,#82HMUSIC0:NOPMOV DPTR,#DAT ;(表头地址送DPTR)MOV 20H,#00H ;(中断计数器清0)MOV B,#00H ;(表序号清0)MUSIC1:NOPCLR AMOVC A,@A +DPTR ;(查表取代码)JZ END0 ;(是00H,则结束)CJNE A,#0FFH,MUSIC5LJMP MUSIC3MUSIC5:MOV R6,AINC DPTRMOV A,BMOVC A,@A+ DPTR ;(取节拍代码送R7)MOV R7,ASETB TR0 ;(启动计数)MUSIC2:NOPCPL SPKMOV A,R6MOV R3,ALCALL DELMOV A,R7CJNE A,20H,MUSIC2 ;(中断计数器(20H)是否等于R7,不等则继续循环) MOV 20H,#00H ;(等于,则取下一代码)INC DPTRLJMP MUSIC1MUSIC3:NOPCLR TR0 ;(休止100ms)MOV R2,#0DHMUSIC4:NOPMOV R3,#0FFHLCALL DELDJNZ R2,MUSIC4INC DPTRLJMP MUSIC1END0:MOV R2,#64H ;(歌曲结束,延时1s后继续)MUSIC6:MOV R3,#00HLCALL DELDJNZ R2,MUSIC6LJMP MUSIC0DEL:NOPDEL3:MOV R4,#02HDEL4:NOPDJNZ R4,DEL4NOPDJNZ R3,DEL3RETNOPDAT:db 26h,20h,20h,20h,20h,20h,26h,10h,20h,10h,20h,80h,26h,20h,30h,20h db 30h,20h,39h,10h,30h,10h,30h,80h,26h,20h,20h,20h,20h,20h,1ch,20h db 20h,80h,2bh,20h,26h,20h,20h,20h,2bh,10h,26h,10h,2bh,80h,26h,20h db 30h,20h,30h,20h,39h,10h,26h,10h,26h,60h,40h,10h,39h,10h,26h,20h db 30h,20h,30h,20h,39h,10h,26h,10h,26h,80h,26h,20h,2bh,10h,2bh,10h db 2bh,20h,30h,10h,39h,10h,26h,10h,2bh,10h,2bh,20h,2bh,40h,40h,20h db 20h,10h,20h,10h,2bh,10h,26h,30h,30h,80h,18h,20h,18h,20h,26h,20h db 20h,20h,20h,40h,26h,20h,2bh,20h,30h,20h,30h,20h,1ch,20h,20h,20h db 20h,80h,1ch,20h,1ch,20h,1ch,20h,30h,20h,30h,60h,39h,10h,30h,10h db 20h,20h,2bh,10h,26h,10h,2bh,10h,26h,10h,26h,10h,2bh,10h,2bh,80h db 18h,20h,18h,20h,26h,20h,20h,20h,20h,60h,26h,10h,2bh,20h,30h,20hdb 30h,20h,1ch,20h,20h,20h,20h,80h,26h,20h,30h,10h,30h,10h,30h,20hdb 39h,20h,26h,10h,2bh,10h,2bh,20h,2bh,40h,40h,10h,40h,10h,20h,10hdb 20h,10h,2bh,10h,26h,30h,30h,80h,00HEND四、总结通过本次实训,我更加了解和掌握了单片机知识,MCS-51汇编语言程序设计方法。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

/*生日快乐歌曲*/#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); //播放}}。

相关文档
最新文档