- //程序如下:
- #include <reg51.h>
- #define uchar unsigned char
- #define uint unsigned int
- sbit key1=P3^2;//按key1可切换花样
- sbit key2=P3^3;//按key2可切换歌曲
- sbit fm=P3^7;//蜂鸣器连续的IO口
- uchar code huayang1[]={0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe,
- 0xfd,0xfb,0xf7,0xef,0xdf,0xbf};//花样1
- uchar code huayang2[]={0x7f,0xfe,0xbf,0xfd,0xdf,0xfb,0xef,0xf7,
- 0xef,0xfb,0xdf,0xfd,0xbf,0xfe};//花样2
- uchar code huayang3[]={0x7f,0x3f,0x1f,0x0f,0x07,0x03,0x01,0x0,
- 0x80,0xc0,0xe0,0xf0,0xf8,0xfc,0xfe,0xff};//花样3
- char code huayang4[]={0x55,0xaa,0xcc,0x33,0x99,0x66,0x0f,0xf0};
- uchar count1;//花样标志
- uchar count2;//歌曲标志
- uchar timeh,timel,i;
- //---------------------------简谱---------------------------------------
- //编程规则:字节高位是简谱,低位是持续时间,
- //代表多少个十六分音符
- //1-7代表中央C调,8-E代表高八度,0代表停顿
- //最后的0是结束标志
- uchar code qnzl[]={ //千年之恋
- 0x12,0x22,0x34,0x84,0x74,0x54,0x38,0x42,0x32,0x22,0x42,0x34,0x84,0x72,0x82,0x94,0xA8,0x08,
- //前奏
- 0x32,0x31,0x21,0x32,0x52,0x32,0x31,0x21,0x32,0x62,
- //竹林的灯火 到过的沙漠
- 0x32,0x31,0x21,0x32,0x82,0x71,0x81,0x71,0x51,0x32,0x22,
- //七色的国度 不断飘逸风中
- 0x32,0x31,0x21,0x32,0x52,0x32,0x31,0x21,0x32,0x62,
- //有一种神秘 灰色的旋涡
- 0x32,0x31,0x21,0x32,0x83,0x82,0x71,0x72,0x02,
- //将我卷入了迷雾中
- 0x63,0xA1,0xA2,0x62,0x92,0x82,0x52,
- //看不清的双手
- 0x31,0x51,0x63,0x51,0x63,0x51,0x63,0x51,0x62,0x82,0x7C,0x02,
- //一朵花传来谁经过的温柔
- 0x61,0x71,0x82,0x71,0x62,0xA2,0x71,0x76,
- //穿越千年的伤痛
- 0x61,0x71,0x82,0x71,0x62,0x52,0x31,0x36,
- //只为求一个结果
- 0x61,0x71,0x82,0x71,0x62,0xA3,0x73,0x62,0x53,
- //你留下的轮廓 指引我
- 0x42,0x63,0x83,0x83,0x91,0x91,
- //黑夜中不寂寞
- 0x61,0x71,0x82,0x71,0x62,0x0A2,0x71,0x76,
- //穿越千年的哀愁
- 0x61,0x71,0x82,0x71,0x62,0x52,0x31,0x36,
- //是你在尽头等我
- 0x61,0x71,0x82,0x71,0x62,0xA3,0x73,0x62,0x53,
- //最美丽的感动 会值得
- 0x42,0x82,0x88,0x02,0x74,0x93,0x89,0xff//结束标志
- //用一生守候
- };
- uchar code jmszl[]={ //寂寞沙洲冷
- 0x12,0x12,0x22,0x32,0x31,0x22,0x21,0x22,
- //自你走后心憔悴
- 0x21,0x31,0x51,0x52,0x31,0x52,0x61,0x15,0x14,
- //白色油桐风中纷飞
- 0x51,0x52,0x31,0x52,0x62,0x13,0x11,0x13,0x32,0x28,0x08,0x28,
- //落花似人有情 这个季节
- 0x31,0x32,0x31,0x32,0x11,0x21,0x51,0x52,0x51,0x52,
- //河畔的风放肆拼命地吹
- 0x51,0x51,0x31,0x32,0x31,0x32,0x81,0x72,0x63,
- //不断拨弄离人的眼泪
- 0x62,0x71,0x81,0x72,0x61,0x61,0x52,0x31,0x21,0x32,0x51,0x54,
- //那样浓烈的爱再也无法给
- 0x22,0x12,0x11,0x12,0x11,0x12,0x12,0x14,0x26,0x32,0x26,
- //伤感一夜一夜
- 0x32,0x61,0x51,0x51,0x31,0x31,0x21,0x31,0x51,0x61,0x51,0x31,0x51,
- //当记忆的线缠绕过往支离破碎
- 0x02,0x32,0x81,0x81,0x81,0x81,0x62,0x52,0x34,
- //是慌乱占据了心扉
- 0x31,0x81,0x81,0x81,0x61,0x91,0x82,
- //有花儿伴着蝴蝶
- 0x51,0x51,0x51,0x51,0x31,0x61,0x53,
- //孤雁可以双飞
- 0x21,0x11,0x21,0x11,0x22,0x11,0x21,0x26,
- //夜深人静独徘徊
- 0x32,0x61,0x51,0x51,0x31,0x31,0x21,0x31,0x51,0x61,0x51,0x31,0x51,0x52,
- //当幸福恋人寄来红色分享喜悦
- 0x31,0x31,0x81,0x81,0x81,0x61,0x91,0x81,0x61,0x31,0x56,
- //闭上双眼难过头也不敢回
- 0x32,0x32,0x81,0x81,0x81,0x81,0x91,0x81,0x61,0x81,0x61,0x51,0x31,0x51,0x34,
- //仍然捡尽寒枝不肯安歇微带着后悔
- 0x21,0x31,0x51,0x31,0x21,0x11,0x61,0x21,0x16,
- //寂寞沙洲我该思念谁
- 0xff};
- //----------------------------简谱音调对应的定时器初值---------------------------
- //适合11.0592M的晶振
- uchar code cuzhi[]={
- 0xff,0xff,//占位
- 0xFC,0x8E,//中央C调1-7
- 0xFC,0xED,
- 0xFD,0x43,
- 0xFD,0x6A,
- 0xFD,0xB3,
- 0xFD,0xF3,
- 0xFE,0x2D,
- 0xFE,0x47, //高八度1-7
- 0xFE,0x76,
- 0xFE,0xA1,
- 0xFE,0xC7,
- 0xFE,0xD9,
- 0xFE,0xF9,
- 0xFF,0x16
- };
- uchar yinyue[]={0xff,0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f,0x0,0x0};
- //将音调转化为对应的LED样式
- void delay1(uint z); //延时1MS
- void delay(uint z); //延时165MS,即十六分音符
- void song();
- void beep();//蜂鸣器叫一声
- main()
- { uchar x;
- count1=0;//流水灯无花样
- count2=1;//唱第一首歌
- EA=1;//开总中断
- EX0=1;//开外部中断0
- IT0=1;//外部中断0下降沿触发方式
- EX1=1;//开外部中断1
- IT1=1;//外部中断1下降沿触发方式
- TMOD=0x01;//定时器0工作在方式1
- TH0=0;
- TL0=0;
- ET0=1;
-
- while(1)
- {
- if(count1!=0)
- {
- switch(count1)
- {
- case 1:
- for(x=0;x<14;x++)
- {
- P1=huayang1[x];
- beep();
- delay1(300);
- if(count1!=1)
- break;
- }
- break;
- case 2:
- for(x=0;x<14;x++)
- {
- P1=huayang2[x];
- beep();
- delay1(300);
- if(count1!=2)
- break;
- }
- break;
- case 3:
- for(x=0;x<16;x++)
- {
- P1=huayang3[x];
- beep();
- delay1(300);
- if(count1!=3)
- break;
- }
- break;
- case 4:
- for(x=0;x<8;x++)
- {
- P1=huayang4[x];
- beep();
- delay1(300);
- if(count1!=4)
- break;
- }
- break;
- }
- }
- else
- {
- song();
- delay1(1000);
- }
- }
-
- }
- void int0() interrupt 0
- {
- EA=0;//关总中断
- delay1(1);//去抖
- if(key1==0)
- {
- count2=0;//不让蜂鸣器唱歌
- TR0=0;
- count1++;
- if(count1==5)
- count1=1;
- }
- EA=1;//开总中断
- }
- void int1() interrupt 2
- {
- EA=0;//关总中断
- delay1(1);//去抖
- if(key2==0)
- {
- count1=0;//流水灯无花样
- TR0=1;
- i=0;//从头开始唱
- count2++;
- if(count2==3)
- count2=1;
- }
- EA=1;//开总中断
- }
- void timer0() interrupt 1 //用于产生各种音调
- {
- TH0=timeh;
- TL0=timel;
- fm=~fm;
- }
- void song()
- {
- uint temp,duan;
- uchar jp;//jp是简谱
- i=0;
- while(1)
- { if(count2==0)
- {
- break;
- }
- if(count2==1) //选曲
- temp=qnzl[i];
- if(count2==2)
- temp=jmszl[i];
- if(temp==0xff)
- break;
-
- jp=temp/16; //取数的高4位
- duan=1;
- P1=yinyue[jp];
- duan=0;
- if(jp!=0)
- {
- timeh=cuzhi[jp*2];
- timel=cuzhi[jp*2+1];
- }
- else
- {
- TR0=0;
- fm=1;//关蜂鸣器
- }
- delay(temp%16); //取数的低4位
- TR0=0; //唱完一个音停10MS
- fm=1;
- delay1(10);
- TR0=1;
- i++;
- }
- TR0=0;
- fm=1;
-
- }
- void delay(uint z) //延时165MS,即十六分音符
- { uint x,y;
- for(x=z;x>0;x--)
- for(y=19000;y>0;y--);
- }
- void delay1(uint z) //延时1MS
- { uint x,y;
- for(x=z;x>0;x--)
- for(y=112;y>0;y--);
- }
- void beep() //蜂鸣器叫一声
- { uchar i;
- for(i=0;i<50;i++)
- { fm=~fm;
- delay1(1);
- }
- fm=1;
- }
复制代码 |