#include <reg52.h>#include <intrins.h>#define FREQ 11059200 #define uint unsigned int #define uchar unsigned charuchar temp0,temp1;uchar yi;uchar yi0,yi1; uint i,j,k,a,b;#define uchar unsigned char void delay(uint k) {uint i,j;for(i=k;i--;i>0)for(j=100;j--;j>0);}
// 音乐_编码2 -- 存储器定义 //unsigned char code music_tab1[]={ //音符码格式:| D7 |D6 D5 D4| D3 |D2 //D1 D0| //梁祝 |变调| 节拍|升降| 音
0xcb, //示例:0xcb=11001011B,即升调,4/16拍,3Mi低 0x35,0x16,0xb9,0x12,0x96,0x99,0xa5, 0xbd,0x99,0x96,0x15,0x13,0x15,0x62, 0x32,0x13,0xa7,0x26,0x25,0x16,0xa9,0x22, 0xa3,0xa9,0x96,0x15,0x16,0x99,0xe5, 0xbb,0x15,0xa7,0xaa,0x96,0x99,0xd5, 0x13,0x15,0x23,0x15,0x16,0x17,0x9a,0xd6,0x15,0x16, 0xb9,0x12,0x25,0x23,0x23,0x22,0x13,0x12,0x21,0x96,0x15, 0x43,0xc9,0x96,0x99,0x96,0x15,0x13,0x15,0x16,0x99, 0xd5, //功能码格式:| D7 D6 D5 D4 | D3 D2 //D1 D0| //|高4位=0时是功能码|根据低4位 0x00, //最后字节为功能码必须==0x00退出};unsigned char code music_tab2[]={ //康定情歌 0x23,0x25,0x26,0x16,0x15, 0x36,0x13,0x42, 0x23,0x25,0x26,0x16,0x15, 0x26,0x53, 0x23,0x25,0x26,0x16,0x15, 0x26,0x23,0x42, 0x25,0x23,0x12,0x13,0x12,0x11, 0x22,0xd6, 0x26,0xda, 0x25,0x53, 0x12,0x11,0xd6, 0xad,0x23,0x12,0x13,0x12,0x11, 0x22,0xd6, 0x00,};unsigned char code music_tab3[]={ //美酒加咖啡 0x55,0x23,0x25,0x26,0xa9,0x22, 0x73, 0x45,0x25,0x26,0x23,0x22,0x21,0x23, 0x72, 0x52,0x23,0x45,0x43, 0x22,0x21,0xa6,0x25,0x66, 0xc9,0xa6,0x25,0x23,0x25,0x26,0xa9, 0xf5, 0x45,0x23,0x25,0x46,0xc9, 0x22,0x13,0x12,0x21,0x22,0x63, 0x22,0x23,0x25,0x26,0x25,0x23,0x22,0x21, 0x72, 0x52,0x23,0x45,0x43, 0x22,0x21,0xa6,0x25,0x66, 0xb9,0x12,0x23,0x25,0x22,0xa5,0xab,0x22, 0x71, 0x00,};unsigned char code music_tab4[]={ //北国之春 0x15,0x16,0x75, 0x15,0x16,0x55,0x13,0x15,0x16,0x15,0x16,0x99,0x12,0x13, 0x65,0x45,0x13,0x12,0x11,0x12, 0x73, 0x62,0x42,0x12,0x11,0x96,0x15, 0x76, 0x55,0x23,0x45,0xa9,0x22, 0x53,0x12,0x13,0x65, 0x52,0x22,0x42,0x21,0xa6, 0xe9,0x91,0x12,0x13,0x12,0x13,0x15,0x99,0x12, //以上为过门 0x20,0x23,0x23,0x23,0x63, 0x22,0x23,0x23,0x22,0x41,0xa6,0x25, 0x53,0x22,0x21,0xa9,0x21,0xa6, 0x75, 0x26,0xa9,0x21,0xa6,0xc9,0x12,0x11,0x96,0x15, 0x23,0x25,0x25,0x26,0x45,0x21,0x22, 0x13,0x35,0x25,0xa9,0xa6,0x25,0x23,0x22, 0x71, 0x52,0x25,0x45,0x13,0x15,0x13,0x12, 0x21,0x41,0x22,0x43,0x45, 0x23,0x25,0x26,0xa9,0x42,0x12,0x33, 0x72, 0x51,0x22,0x43,0x22,0x21, 0xc6,0xc9,0x29,0xc6,0x45,0x23,0x23, 0x53,0x25,0x26,0x25,0x13,0x12,0x21, 0x62,0x42,0x23,0x25, 0x55,0x26,0x25,0x23,0x45, 0x26,0xa9,0x21,0x22,0x63, 0x52,0x22,0x22,0x13,0x12,0x21,0xa6,0xf9, 0x00,};
unsigned char code music_l_tab[8]={0,1,2,3,4,6,8,16};//节拍延时单位unsigned char code music_freq_tab[64]={ //音符定时器值表 0xff,0xea, //0 休止符 255-FREQ/24/131/256, //256-FREQ/24/x/256 //1 do 高字节 256-FREQ/24/131%256, //256-FREQ/24/x%256 //1 do 低字节 255-FREQ/24/147/256,256-FREQ/24/147%256, //2 re 255-FREQ/24/165/256,256-FREQ/24/165%256, //3 mi 255-FREQ/24/175/256,256-FREQ/24/175%256, //4 fa 255-FREQ/24/196/256,256-FREQ/24/196%256, //5 suo 255-FREQ/24/221/256,256-FREQ/24/221%256, //6 la 255-FREQ/24/248/256,256-FREQ/24/248%256, //7 xi
0xff,0xea, //0 休止符 255-FREQ/24/262/256,256-FREQ/24/262%256, //1 do 255-FREQ/24/294/256,256-FREQ/24/294%256, //2 re 255-FREQ/24/330/256,256-FREQ/24/330%256, //3 mi 255-FREQ/24/350/256,256-FREQ/24/350%256, //4 fa 255-FREQ/24/393/256,256-FREQ/24/393%256, //5 suo 255-FREQ/24/441/256,256-FREQ/24/441%256, //6 la 255-FREQ/24/495/256,256-FREQ/24/495%256, //7 xi
0xff,0xea, //0 休止符 255-FREQ/24/525/256,256-FREQ/24/525%256, //1 do 255-FREQ/24/589/256,256-FREQ/24/589%256, //2 re 255-FREQ/24/661/256,256-FREQ/24/661%256, //3 mi 255-FREQ/24/700/256,256-FREQ/24/700%256, //4 fa 255-FREQ/24/786/256,256-FREQ/24/786%256, //5 suo 255-FREQ/24/882/256,256-FREQ/24/882%256, //6 la 255-FREQ/24/990/256,256-FREQ/24/990%256, //7 xi
0xff,0xea, //0 休止符 255-FREQ/24/1049/256,256-FREQ/24/1049%256, //1 do 255-FREQ/24/1178/256,256-FREQ/24/1178%256, //2 re 255-FREQ/24/1322/256,256-FREQ/24/1322%256, //3 mi 255-FREQ/24/1400/256,256-FREQ/24/1400%256, //4 fa 255-FREQ/24/1572/256,256-FREQ/24/1572%256, //5 suo 255-FREQ/24/1665/256,256-FREQ/24/1665%256, //6 la 255-FREQ/24/1869/256,256-FREQ/24/1869%256, //7 xi
};
unsigned char code music_frequp_tab[64]={ //升半音 0xff,0xea, //0 休止符 255-FREQ/24/139/256, //1 do 高字节 //255-FREQ/24/x/256 256-FREQ/24/139%256, //1 do 低字节 //256-FREQ/24/x%256 255-FREQ/24/156/256,256-FREQ/24/156%256, //2 re 255-FREQ/24/175/256,256-FREQ/24/175%256, //3 mi 255-FREQ/24/185/256,256-FREQ/24/185%256, //4 fa 255-FREQ/24/208/256,256-FREQ/24/208%256, //5 suo 255-FREQ/24/234/256,256-FREQ/24/234%256, //6 la 255-FREQ/24/262/256,256-FREQ/24/262%256, //7 xi
0xff,0xea, //0 休止符 255-FREQ/24/278/256,256-FREQ/24/278%256, //1 do 255-FREQ/24/312/256,256-FREQ/24/312%256, //2 re 255-FREQ/24/350/256,256-FREQ/24/350%256, //3 mi 255-FREQ/24/371/256,256-FREQ/24/371%256, //4 fa 255-FREQ/24/416/256,256-FREQ/24/416%256, //5 suo 255-FREQ/24/467/256,256-FREQ/24/467%256, //6 la 255-FREQ/24/525/256,256-FREQ/24/525%256, //7 xi
0xff,0xea, //0 休止符 255-FREQ/24/556/256,256-FREQ/24/556%256, //1 do 255-FREQ/24/624/256,256-FREQ/24/624%256, //2 re 255-FREQ/24/700/256,256-FREQ/24/700%256, //3 mi 255-FREQ/24/742/256,256-FREQ/24/742%256, //4 fa 255-FREQ/24/833/256,256-FREQ/24/833%256, //5 suo 255-FREQ/24/935/256,256-FREQ/24/935%256, //6 la 255-FREQ/24/1049/256,256-FREQ/24/1049%256, //7 xi
0xff,0xea, //0 休止符 255-FREQ/24/1112/256,256-FREQ/24/1112%256, //1 do 255-FREQ/24/1248/256,256-FREQ/24/1248%256, //2 re 255-FREQ/24/1400/256,256-FREQ/24/1400%256, //3 mi 255-FREQ/24/1484/256,256-FREQ/24/1484%256, //4 fa 255-FREQ/24/1618/256,256-FREQ/24/1618%256, //5 suo 255-FREQ/24/1764/256,256-FREQ/24/1764%256, //6 la 255-FREQ/24/1968/256,256-FREQ/24/1968%256, //7 xi};unsigned char temp_TH1;unsigned char temp_TL1;
// 音乐_编码2 -- IO定义 //sbit PIN_MSC=P0^0; // 音乐输出端口 //
// 音乐_编码2 -- 函数原型//void music_delay(unsigned char x); //音乐节拍延时void music_play(unsigned char *msc); //播放音乐子程序
// 音乐_编码2 -- 函数 ////---------------------------------------------------------------------------//void music_int_t1 (void) interrupt 3 using 1 //定时中断1// { PIN_MSC=~PIN_MSC; TH1=temp_TH1; TL1=temp_TL1;}
//---------------------------------------------------------------------------//void music_delay(unsigned char n) //延时 125*n 毫秒 { unsigned char i=125,j; do { do { for (j=0; j<230; j++) _nop_(); //j=(11159KHz/12-10)/4 }while(--i); }while(--n); j=k=a=b=3;//全部亮P1=P2=P3=0x00;delay(700);P1=P2=P3=0xff;//每个io口独自亮for(i=2;i--;i>0){P1=0x00;delay(500);P1=0xff;P3=P2=0x00;delay(500);P3=P2=0xff;}//全部亮,闪三次for(i=1;i--;i>0){P1=P2=P3=0x00;delay(100);P1=P2=P3=0xff;delay(100);}//P1、P2亮,P3、P0暗for(i=3;i--;i>0){P1=0x00,P2=0x00; P3=0xff;delay(500);P1=0xff,P2=0xff,P3=0x00;delay(500);}
//四个点的流水
while(j>0){temp0=0xfe,temp1=0x7f;P1=P3=temp0,P2=temp1;delay(100);for(i=7;i--;i>0){temp0=_crol_(temp0,1),temp1=_cror_(temp1,1);P1=P3=temp0,P2=temp1;delay(100);} j--; } //全部亮,闪三次for(i=9;i--;i>0){P1=P2=P3=0x00;delay(300);P1=P2=P3=0xff;delay(300);}
//四个IO口同样跟踪流水while(k>0){temp0=0xfe,temp1=0x7f;P1=P3=temp0,P2=temp1;delay(60);for(i=7;i--;i>0){temp0=temp0<<1,temp1=temp1>>1;P1=P3=temp0,P2=temp1;delay(60);}k--; } P1=P3=P2=0xff; while(k<3) { temp0=0x7f,temp1=0xfe; P1=P3=temp0,P2=temp1; delay(60); for(i=7;i--;i>0) { temp0=temp0>>1,temp1=temp1<<1;P1=P3=temp0,P2=temp1;delay(60); } k++; } P3=P2=0xff; //两边单个从上向下流水yi=0xfe;P1=yi;delay(100);for(i=7;i--;i>0){yi=_crol_(yi,1);P1=yi;delay(100);}P1=0xff;yi=0xfe;P3=P2=yi;delay(100);for(i=7;i--;i>0){yi=_crol_(yi,1);P3=P2=yi;delay(100);}P3=P2=0Xff; //两边单个返回流水for(i=7;i--;i>0) {yi=_cror_(yi,1);P3=P2=yi;delay(100);}P3=P2=0xff;for(i=7;i--;i>0){yi=_cror_(yi,1);P1=yi;delay(100);} //全部亮,闪三次P1=P3=P2=0xff;for(i=1;i--;i>0){P1=P2=P3=0x00;delay(100);P1=P2=P3=0xff;delay(100);} //流水灯
yi0=0xfe,yi1=0x7f;P3=P2=yi1,P1=yi0;delay(100);for(i=7;i--;i>0){ yi0=yi0<<1, yi1=yi1>>1; P1=yi0,P3=P2=yi1; delay(100);}P1=P2=P3=0xff;delay(200);yi0=0x7f,yi1=0xfe;P1=yi0,P3=P2=yi1;for(i=7;i--;i>0){yi0=yi0>>1,yi1=yi1<<1;P1=yi0,P3=P2=yi1;delay(100);} //大循环跟踪流水P1=P2=P3=0xff;for(i=8;i--;i>0){ P1=P1<<1; delay(150);}for(i=8;i--;i>0){ P3=P3<<1; delay(150);}
for(i=8;i--;i>0){ P2=P2>>1; delay(150);} //逆向大循环跟踪流水
P1=P2=P3=0xff;for(i=8;i--;i>0){ P2=P2<<1; delay(150);}for(i=8;i--;i>0){ P3=P3>>1; delay(50);}for(i=8;i--;i>0){ P1=P1>>1; delay(50);}
//全部亮,闪4次P1=P3=P2=0xff;for(i=5;i--;i>0){P1=P2=P3=0x00;delay(100);P1=P2=P3=0xff;delay(100);} //全部亮,只有一个暗的在流水temp0=0x01,temp1=0x00;P1=temp0,P3=P2=temp1;delay(100);for(i=7;i--;i>0){temp0=_crol_(temp0,1);P1=P0=temp0;delay(100);}P1=0x00;temp1=0x01;P3=P2=temp1;delay(100);for(i=7;i--;i>0){temp1=_crol_(temp1,1);P3=P2=temp1;delay(100);} //全部亮,逆向一个暗在流水temp0=0x00,temp1=0x80;P1=P0=temp0,P3=P2=temp1;delay(100);for(i=7;i--;i>0){temp1=_cror_(temp1,1);P3=P2=temp1;delay(100);}P3=P2=0x00;temp0=0x80;P1=P0=temp0;delay(100); for(i=7;i--;i>0){temp0=_cror_(temp0,1);P1=temp0;delay(100);} //花样temp0=0xaa,temp1=0x55;P1=P3=temp0,P2=temp1;delay(500);for(;a--;a>0){for(i=7;i--;i>0){temp0=_crol_(temp0,1),temp1=_cror_(temp1,1);P1=P3=temp0,P2=temp1;delay(500);}
}for(;b--;b>0){temp0=0xee,temp1=0x77;P1=P3=temp0,P2=temp1;delay(300);for(i=7;i--;i>0){temp0=_crol_(temp0,1),temp1=_cror_(temp1,1);P1=P3=temp0,P2=temp1;delay(300);}} //环形逐个亮P1=P2=P3=0xff;for(i=8;i--;i>0){ P2=P2<<1; delay(100);}for(i=8;i--;i>0){ P3=P3>>1; delay(100);}for(i=8;i--;i>0){ P1=P1>>1; delay(100);} //逐个熄灭
for(i=7;i--;i>0){P2=~P2<<1;P2=~P2;delay(100);}P2=0xff;for(i=7;i--;i>0){P3=~P3>>1;P3=~P3;delay(100);}P3=0xff;for(i=7;i--;i>0){P1=~P1>>1;P1=~P1;delay(100);}P1=0xff;
}
//---------------------------------------------------------------------------//void music_play(unsigned char *msc) //音乐 { unsigned char music_freq=32; //音高 unsigned char music_long; //节拍 unsigned char music_data=0; //音符数据 bit music_up=0; //升半音 bit music_break=0; //断奏 temp_TH1=0xff; temp_TL1=0xea; //关输出(输出超声波) TMOD=0x11; //T0:16位定时器、T1:16位定时器 ET0=0; //关T0定时器中断 ET1=1; //开T1定时器中断//--------------------------------------------------------- while (*msc != 0x00) { music_data=*msc & 0x07; music_long=*msc>>4; //--------------------------------------------------------- if (music_long != 0) //是音符 { if (*msc >=128) //需升降调 { if (((*msc) & 0x08) == 0) {if (music_freq >= 16) music_freq -= 16;} //音高下降 else {if (music_freq < 48) music_freq += 16;} //音高上升 } //--------------------------------------------------------- if (music_up==1) //升半音 { temp_TH1=music_frequp_tab[(music_freq + (music_data <<1))]; temp_TL1=music_frequp_tab[(music_freq + (music_data <<1)+1)]; } else //不升半音 { temp_TH1=music_freq_tab[(music_freq + (music_data <<1))]; temp_TL1=music_freq_tab[(music_freq + (music_data <<1)+ 1)]; } //--------------------------------------------------------- if (music_break) //断奏 { music_delay(music_l_tab[music_long&0x07]-1); temp_TH1=0xff; temp_TL1=0xea; music_delay(1); } else //连奏 music_delay(music_l_tab[music_long&0x07]); } //--------------------------------------------------------- else //是功能码 { switch(music_data) { case 0x04: music_up=0; //不升半音 break; case 0x05: music_up=1; //升半音 break; case 0x06: music_break=0; //断奏 break; case 0x07: music_break=1; //连奏 break; default: break; } } //--------------------------------------------------------- msc++; } ET1=0; //播放结束,关T1中断 PIN_MSC=1; //关输出// temp_TH1=0xff; temp_TL1=0xea; //关输出(输出超声波) ET0=1; //开T0定时器中断 TMOD=0x51; //T0:16位定时器、T1:16位计数器}
//---------------------------------------------------------------------------//void main(void) //示例{ // 设置定时器-------------------------------------- TMOD = 0x11; // TMOD: 定时器1:模式1:16位;定时器2:模式1:16 TCON = 0x50; // TCON: 定时器 1运行; 定时器 2运行 // 设置并开启中断: IP = 0x08; //中断优先寄存器IP:|D7_x |x|x |PS|PT1|PX1|PT0|D0_PX0|// IE = 0x88; //中断允许寄存器IE:|D7_EA|x|ET2|ES|ET1|EX1|ET0|D0_EX0|// for (;;) { //在此调用键盘和显示函数// music_play(music_tab1); music_play(music_tab2); music_play(music_tab3); music_play(music_tab4); }}//----
I
// 音乐_编码2 -- 存储器定义 //unsigned char code music_tab1[]={ //音符码格式:| D7 |D6 D5 D4| D3 |D2 //D1 D0| //梁祝 |变调| 节拍|升降| 音
0xcb, //示例:0xcb=11001011B,即升调,4/16拍,3Mi低 0x35,0x16,0xb9,0x12,0x96,0x99,0xa5, 0xbd,0x99,0x96,0x15,0x13,0x15,0x62, 0x32,0x13,0xa7,0x26,0x25,0x16,0xa9,0x22, 0xa3,0xa9,0x96,0x15,0x16,0x99,0xe5, 0xbb,0x15,0xa7,0xaa,0x96,0x99,0xd5, 0x13,0x15,0x23,0x15,0x16,0x17,0x9a,0xd6,0x15,0x16, 0xb9,0x12,0x25,0x23,0x23,0x22,0x13,0x12,0x21,0x96,0x15, 0x43,0xc9,0x96,0x99,0x96,0x15,0x13,0x15,0x16,0x99, 0xd5, //功能码格式:| D7 D6 D5 D4 | D3 D2 //D1 D0| //|高4位=0时是功能码|根据低4位 0x00, //最后字节为功能码必须==0x00退出};unsigned char code music_tab2[]={ //康定情歌 0x23,0x25,0x26,0x16,0x15, 0x36,0x13,0x42, 0x23,0x25,0x26,0x16,0x15, 0x26,0x53, 0x23,0x25,0x26,0x16,0x15, 0x26,0x23,0x42, 0x25,0x23,0x12,0x13,0x12,0x11, 0x22,0xd6, 0x26,0xda, 0x25,0x53, 0x12,0x11,0xd6, 0xad,0x23,0x12,0x13,0x12,0x11, 0x22,0xd6, 0x00,};unsigned char code music_tab3[]={ //美酒加咖啡 0x55,0x23,0x25,0x26,0xa9,0x22, 0x73, 0x45,0x25,0x26,0x23,0x22,0x21,0x23, 0x72, 0x52,0x23,0x45,0x43, 0x22,0x21,0xa6,0x25,0x66, 0xc9,0xa6,0x25,0x23,0x25,0x26,0xa9, 0xf5, 0x45,0x23,0x25,0x46,0xc9, 0x22,0x13,0x12,0x21,0x22,0x63, 0x22,0x23,0x25,0x26,0x25,0x23,0x22,0x21, 0x72, 0x52,0x23,0x45,0x43, 0x22,0x21,0xa6,0x25,0x66, 0xb9,0x12,0x23,0x25,0x22,0xa5,0xab,0x22, 0x71, 0x00,};unsigned char code music_tab4[]={ //北国之春 0x15,0x16,0x75, 0x15,0x16,0x55,0x13,0x15,0x16,0x15,0x16,0x99,0x12,0x13, 0x65,0x45,0x13,0x12,0x11,0x12, 0x73, 0x62,0x42,0x12,0x11,0x96,0x15, 0x76, 0x55,0x23,0x45,0xa9,0x22, 0x53,0x12,0x13,0x65, 0x52,0x22,0x42,0x21,0xa6, 0xe9,0x91,0x12,0x13,0x12,0x13,0x15,0x99,0x12, //以上为过门 0x20,0x23,0x23,0x23,0x63, 0x22,0x23,0x23,0x22,0x41,0xa6,0x25, 0x53,0x22,0x21,0xa9,0x21,0xa6, 0x75, 0x26,0xa9,0x21,0xa6,0xc9,0x12,0x11,0x96,0x15, 0x23,0x25,0x25,0x26,0x45,0x21,0x22, 0x13,0x35,0x25,0xa9,0xa6,0x25,0x23,0x22, 0x71, 0x52,0x25,0x45,0x13,0x15,0x13,0x12, 0x21,0x41,0x22,0x43,0x45, 0x23,0x25,0x26,0xa9,0x42,0x12,0x33, 0x72, 0x51,0x22,0x43,0x22,0x21, 0xc6,0xc9,0x29,0xc6,0x45,0x23,0x23, 0x53,0x25,0x26,0x25,0x13,0x12,0x21, 0x62,0x42,0x23,0x25, 0x55,0x26,0x25,0x23,0x45, 0x26,0xa9,0x21,0x22,0x63, 0x52,0x22,0x22,0x13,0x12,0x21,0xa6,0xf9, 0x00,};
unsigned char code music_l_tab[8]={0,1,2,3,4,6,8,16};//节拍延时单位unsigned char code music_freq_tab[64]={ //音符定时器值表 0xff,0xea, //0 休止符 255-FREQ/24/131/256, //256-FREQ/24/x/256 //1 do 高字节 256-FREQ/24/131%256, //256-FREQ/24/x%256 //1 do 低字节 255-FREQ/24/147/256,256-FREQ/24/147%256, //2 re 255-FREQ/24/165/256,256-FREQ/24/165%256, //3 mi 255-FREQ/24/175/256,256-FREQ/24/175%256, //4 fa 255-FREQ/24/196/256,256-FREQ/24/196%256, //5 suo 255-FREQ/24/221/256,256-FREQ/24/221%256, //6 la 255-FREQ/24/248/256,256-FREQ/24/248%256, //7 xi
0xff,0xea, //0 休止符 255-FREQ/24/262/256,256-FREQ/24/262%256, //1 do 255-FREQ/24/294/256,256-FREQ/24/294%256, //2 re 255-FREQ/24/330/256,256-FREQ/24/330%256, //3 mi 255-FREQ/24/350/256,256-FREQ/24/350%256, //4 fa 255-FREQ/24/393/256,256-FREQ/24/393%256, //5 suo 255-FREQ/24/441/256,256-FREQ/24/441%256, //6 la 255-FREQ/24/495/256,256-FREQ/24/495%256, //7 xi
0xff,0xea, //0 休止符 255-FREQ/24/525/256,256-FREQ/24/525%256, //1 do 255-FREQ/24/589/256,256-FREQ/24/589%256, //2 re 255-FREQ/24/661/256,256-FREQ/24/661%256, //3 mi 255-FREQ/24/700/256,256-FREQ/24/700%256, //4 fa 255-FREQ/24/786/256,256-FREQ/24/786%256, //5 suo 255-FREQ/24/882/256,256-FREQ/24/882%256, //6 la 255-FREQ/24/990/256,256-FREQ/24/990%256, //7 xi
0xff,0xea, //0 休止符 255-FREQ/24/1049/256,256-FREQ/24/1049%256, //1 do 255-FREQ/24/1178/256,256-FREQ/24/1178%256, //2 re 255-FREQ/24/1322/256,256-FREQ/24/1322%256, //3 mi 255-FREQ/24/1400/256,256-FREQ/24/1400%256, //4 fa 255-FREQ/24/1572/256,256-FREQ/24/1572%256, //5 suo 255-FREQ/24/1665/256,256-FREQ/24/1665%256, //6 la 255-FREQ/24/1869/256,256-FREQ/24/1869%256, //7 xi
};
unsigned char code music_frequp_tab[64]={ //升半音 0xff,0xea, //0 休止符 255-FREQ/24/139/256, //1 do 高字节 //255-FREQ/24/x/256 256-FREQ/24/139%256, //1 do 低字节 //256-FREQ/24/x%256 255-FREQ/24/156/256,256-FREQ/24/156%256, //2 re 255-FREQ/24/175/256,256-FREQ/24/175%256, //3 mi 255-FREQ/24/185/256,256-FREQ/24/185%256, //4 fa 255-FREQ/24/208/256,256-FREQ/24/208%256, //5 suo 255-FREQ/24/234/256,256-FREQ/24/234%256, //6 la 255-FREQ/24/262/256,256-FREQ/24/262%256, //7 xi
0xff,0xea, //0 休止符 255-FREQ/24/278/256,256-FREQ/24/278%256, //1 do 255-FREQ/24/312/256,256-FREQ/24/312%256, //2 re 255-FREQ/24/350/256,256-FREQ/24/350%256, //3 mi 255-FREQ/24/371/256,256-FREQ/24/371%256, //4 fa 255-FREQ/24/416/256,256-FREQ/24/416%256, //5 suo 255-FREQ/24/467/256,256-FREQ/24/467%256, //6 la 255-FREQ/24/525/256,256-FREQ/24/525%256, //7 xi
0xff,0xea, //0 休止符 255-FREQ/24/556/256,256-FREQ/24/556%256, //1 do 255-FREQ/24/624/256,256-FREQ/24/624%256, //2 re 255-FREQ/24/700/256,256-FREQ/24/700%256, //3 mi 255-FREQ/24/742/256,256-FREQ/24/742%256, //4 fa 255-FREQ/24/833/256,256-FREQ/24/833%256, //5 suo 255-FREQ/24/935/256,256-FREQ/24/935%256, //6 la 255-FREQ/24/1049/256,256-FREQ/24/1049%256, //7 xi
0xff,0xea, //0 休止符 255-FREQ/24/1112/256,256-FREQ/24/1112%256, //1 do 255-FREQ/24/1248/256,256-FREQ/24/1248%256, //2 re 255-FREQ/24/1400/256,256-FREQ/24/1400%256, //3 mi 255-FREQ/24/1484/256,256-FREQ/24/1484%256, //4 fa 255-FREQ/24/1618/256,256-FREQ/24/1618%256, //5 suo 255-FREQ/24/1764/256,256-FREQ/24/1764%256, //6 la 255-FREQ/24/1968/256,256-FREQ/24/1968%256, //7 xi};unsigned char temp_TH1;unsigned char temp_TL1;
// 音乐_编码2 -- IO定义 //sbit PIN_MSC=P0^0; // 音乐输出端口 //
// 音乐_编码2 -- 函数原型//void music_delay(unsigned char x); //音乐节拍延时void music_play(unsigned char *msc); //播放音乐子程序
// 音乐_编码2 -- 函数 ////---------------------------------------------------------------------------//void music_int_t1 (void) interrupt 3 using 1 //定时中断1// { PIN_MSC=~PIN_MSC; TH1=temp_TH1; TL1=temp_TL1;}
//---------------------------------------------------------------------------//void music_delay(unsigned char n) //延时 125*n 毫秒 { unsigned char i=125,j; do { do { for (j=0; j<230; j++) _nop_(); //j=(11159KHz/12-10)/4 }while(--i); }while(--n); j=k=a=b=3;//全部亮P1=P2=P3=0x00;delay(700);P1=P2=P3=0xff;//每个io口独自亮for(i=2;i--;i>0){P1=0x00;delay(500);P1=0xff;P3=P2=0x00;delay(500);P3=P2=0xff;}//全部亮,闪三次for(i=1;i--;i>0){P1=P2=P3=0x00;delay(100);P1=P2=P3=0xff;delay(100);}//P1、P2亮,P3、P0暗for(i=3;i--;i>0){P1=0x00,P2=0x00; P3=0xff;delay(500);P1=0xff,P2=0xff,P3=0x00;delay(500);}
//四个点的流水
while(j>0){temp0=0xfe,temp1=0x7f;P1=P3=temp0,P2=temp1;delay(100);for(i=7;i--;i>0){temp0=_crol_(temp0,1),temp1=_cror_(temp1,1);P1=P3=temp0,P2=temp1;delay(100);} j--; } //全部亮,闪三次for(i=9;i--;i>0){P1=P2=P3=0x00;delay(300);P1=P2=P3=0xff;delay(300);}
//四个IO口同样跟踪流水while(k>0){temp0=0xfe,temp1=0x7f;P1=P3=temp0,P2=temp1;delay(60);for(i=7;i--;i>0){temp0=temp0<<1,temp1=temp1>>1;P1=P3=temp0,P2=temp1;delay(60);}k--; } P1=P3=P2=0xff; while(k<3) { temp0=0x7f,temp1=0xfe; P1=P3=temp0,P2=temp1; delay(60); for(i=7;i--;i>0) { temp0=temp0>>1,temp1=temp1<<1;P1=P3=temp0,P2=temp1;delay(60); } k++; } P3=P2=0xff; //两边单个从上向下流水yi=0xfe;P1=yi;delay(100);for(i=7;i--;i>0){yi=_crol_(yi,1);P1=yi;delay(100);}P1=0xff;yi=0xfe;P3=P2=yi;delay(100);for(i=7;i--;i>0){yi=_crol_(yi,1);P3=P2=yi;delay(100);}P3=P2=0Xff; //两边单个返回流水for(i=7;i--;i>0) {yi=_cror_(yi,1);P3=P2=yi;delay(100);}P3=P2=0xff;for(i=7;i--;i>0){yi=_cror_(yi,1);P1=yi;delay(100);} //全部亮,闪三次P1=P3=P2=0xff;for(i=1;i--;i>0){P1=P2=P3=0x00;delay(100);P1=P2=P3=0xff;delay(100);} //流水灯
yi0=0xfe,yi1=0x7f;P3=P2=yi1,P1=yi0;delay(100);for(i=7;i--;i>0){ yi0=yi0<<1, yi1=yi1>>1; P1=yi0,P3=P2=yi1; delay(100);}P1=P2=P3=0xff;delay(200);yi0=0x7f,yi1=0xfe;P1=yi0,P3=P2=yi1;for(i=7;i--;i>0){yi0=yi0>>1,yi1=yi1<<1;P1=yi0,P3=P2=yi1;delay(100);} //大循环跟踪流水P1=P2=P3=0xff;for(i=8;i--;i>0){ P1=P1<<1; delay(150);}for(i=8;i--;i>0){ P3=P3<<1; delay(150);}
for(i=8;i--;i>0){ P2=P2>>1; delay(150);} //逆向大循环跟踪流水
P1=P2=P3=0xff;for(i=8;i--;i>0){ P2=P2<<1; delay(150);}for(i=8;i--;i>0){ P3=P3>>1; delay(50);}for(i=8;i--;i>0){ P1=P1>>1; delay(50);}
//全部亮,闪4次P1=P3=P2=0xff;for(i=5;i--;i>0){P1=P2=P3=0x00;delay(100);P1=P2=P3=0xff;delay(100);} //全部亮,只有一个暗的在流水temp0=0x01,temp1=0x00;P1=temp0,P3=P2=temp1;delay(100);for(i=7;i--;i>0){temp0=_crol_(temp0,1);P1=P0=temp0;delay(100);}P1=0x00;temp1=0x01;P3=P2=temp1;delay(100);for(i=7;i--;i>0){temp1=_crol_(temp1,1);P3=P2=temp1;delay(100);} //全部亮,逆向一个暗在流水temp0=0x00,temp1=0x80;P1=P0=temp0,P3=P2=temp1;delay(100);for(i=7;i--;i>0){temp1=_cror_(temp1,1);P3=P2=temp1;delay(100);}P3=P2=0x00;temp0=0x80;P1=P0=temp0;delay(100); for(i=7;i--;i>0){temp0=_cror_(temp0,1);P1=temp0;delay(100);} //花样temp0=0xaa,temp1=0x55;P1=P3=temp0,P2=temp1;delay(500);for(;a--;a>0){for(i=7;i--;i>0){temp0=_crol_(temp0,1),temp1=_cror_(temp1,1);P1=P3=temp0,P2=temp1;delay(500);}
}for(;b--;b>0){temp0=0xee,temp1=0x77;P1=P3=temp0,P2=temp1;delay(300);for(i=7;i--;i>0){temp0=_crol_(temp0,1),temp1=_cror_(temp1,1);P1=P3=temp0,P2=temp1;delay(300);}} //环形逐个亮P1=P2=P3=0xff;for(i=8;i--;i>0){ P2=P2<<1; delay(100);}for(i=8;i--;i>0){ P3=P3>>1; delay(100);}for(i=8;i--;i>0){ P1=P1>>1; delay(100);} //逐个熄灭
for(i=7;i--;i>0){P2=~P2<<1;P2=~P2;delay(100);}P2=0xff;for(i=7;i--;i>0){P3=~P3>>1;P3=~P3;delay(100);}P3=0xff;for(i=7;i--;i>0){P1=~P1>>1;P1=~P1;delay(100);}P1=0xff;
}
//---------------------------------------------------------------------------//void music_play(unsigned char *msc) //音乐 { unsigned char music_freq=32; //音高 unsigned char music_long; //节拍 unsigned char music_data=0; //音符数据 bit music_up=0; //升半音 bit music_break=0; //断奏 temp_TH1=0xff; temp_TL1=0xea; //关输出(输出超声波) TMOD=0x11; //T0:16位定时器、T1:16位定时器 ET0=0; //关T0定时器中断 ET1=1; //开T1定时器中断//--------------------------------------------------------- while (*msc != 0x00) { music_data=*msc & 0x07; music_long=*msc>>4; //--------------------------------------------------------- if (music_long != 0) //是音符 { if (*msc >=128) //需升降调 { if (((*msc) & 0x08) == 0) {if (music_freq >= 16) music_freq -= 16;} //音高下降 else {if (music_freq < 48) music_freq += 16;} //音高上升 } //--------------------------------------------------------- if (music_up==1) //升半音 { temp_TH1=music_frequp_tab[(music_freq + (music_data <<1))]; temp_TL1=music_frequp_tab[(music_freq + (music_data <<1)+1)]; } else //不升半音 { temp_TH1=music_freq_tab[(music_freq + (music_data <<1))]; temp_TL1=music_freq_tab[(music_freq + (music_data <<1)+ 1)]; } //--------------------------------------------------------- if (music_break) //断奏 { music_delay(music_l_tab[music_long&0x07]-1); temp_TH1=0xff; temp_TL1=0xea; music_delay(1); } else //连奏 music_delay(music_l_tab[music_long&0x07]); } //--------------------------------------------------------- else //是功能码 { switch(music_data) { case 0x04: music_up=0; //不升半音 break; case 0x05: music_up=1; //升半音 break; case 0x06: music_break=0; //断奏 break; case 0x07: music_break=1; //连奏 break; default: break; } } //--------------------------------------------------------- msc++; } ET1=0; //播放结束,关T1中断 PIN_MSC=1; //关输出// temp_TH1=0xff; temp_TL1=0xea; //关输出(输出超声波) ET0=1; //开T0定时器中断 TMOD=0x51; //T0:16位定时器、T1:16位计数器}
//---------------------------------------------------------------------------//void main(void) //示例{ // 设置定时器-------------------------------------- TMOD = 0x11; // TMOD: 定时器1:模式1:16位;定时器2:模式1:16 TCON = 0x50; // TCON: 定时器 1运行; 定时器 2运行 // 设置并开启中断: IP = 0x08; //中断优先寄存器IP:|D7_x |x|x |PS|PT1|PX1|PT0|D0_PX0|// IE = 0x88; //中断允许寄存器IE:|D7_EA|x|ET2|ES|ET1|EX1|ET0|D0_EX0|// for (;;) { //在此调用键盘和显示函数// music_play(music_tab1); music_play(music_tab2); music_play(music_tab3); music_play(music_tab4); }}//----
I