- #include //51单片机头文件
- #include "L1602.h"
- #define uchar unsigned char
- #define uint unsigned int
- ***it RS=P2^0; //LCD1602数据/命令选择端(H/L)
- ***it RW=P2^1; //LCD1602读/写选择端(H/L)
- ***it E=P2^2; //LCD1602使能信号端
- ***it BEEP=P2^7; //蜂鸣器接口
- ***it k1=P1^0; //下一首
- ***it k2=P1^1; //上一首
- ***it k3=P1^2; //播放/暂停
- ***it k4=P1^3; //复位
- char count2; //歌曲标志
- typedef unsigned char BYTE;
- typedef unsigned int WORD;
- uchar a,b,c,aa,num;
- uchar num1,num2,t;
- uchar code dis0[]=" Welcome to use ";
- uchar code dis01[]=" Musical Box ";
- uchar code time[]=" TIME 00:00 ";
- uchar code dis1[]=" qiang nian ";
- uchar code dis2[]=" ji mo sha zhou ";
- uchar code dis3[]=" wei yi ";
- uchar code dis4[]="qi kai de sheng ";
- uchar second=00,minute=00;
- void clock_init()
- {
- uchar i,j;
- write_com(0x80);
- for(i=0;i<16;i++)
- {
- write_data(dis0[i]);
- }
- write_com(0x80+0x40);
- for(j=0;j<16;j++)
- {
- write_data(dis01[j]);
- }
- }
- void write_sfm(uchar add,uchar sum)
- {
- uchar shi,ge;
- shi=sum/10;
- ge=sum%10;
- write_com(0x80+add);
- write_data(0x30+shi);
- write_data(0x30+ge);
- }
- void clock_write(uint s,uint m)
- {
- write_sfm(0x49,m);
- write_sfm(0x4b,s);
- }
- //---------------------------简谱---------------------------------------
- //编程规则:字节高位是简谱,低位是持续时间,
- //代表多少个十六分音符
- //1-7代表中央C调,8-E代表高八度,0代表停顿
- //最后的0是结束标志
- uchar code table1[]={ //千年之恋
- 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 table2[]={ //寂寞沙洲冷
- 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
- };
- uchar code table3[]={ //唯一
- 0x33,0x53,0x63,0x53,0x63,0x73,0x63,0x33,0x53,0x03,0x53,0x53,0x53,0x34,0x44,
- 0x52,0x43,0x33,0x22,0x14,0x38,
- 0x40,0x43,0x43,0x43,0x33,0x23,0x13,0x38,0x03,0x13,0x23,0x13,0x33,0x26,0x04,
- 0x33,0x23,0x13,0x2c,0x04,
- 0x33,0x53,0x63,0x53,0x63,0x73,0x63,0x33,0x55,0x03,0x53,0x53,0x53,0x43,0x44,
- 0x52,0x43,0x33,0x23,0x13,0x35,
- 0x04,0x43,0x43,0x43,0x33,0x23,0x13,0x33,0x54,0x83,0x83,0x93,0xa3,0x92,0x81,
- 0x63,0x62,0xa3,0x93,0x83,0x96,0x04,0x04,
- 0xa4,0x94,0x82,0x82,0x82,0x52,0x82,0x92,0xa4,
- 0x72,0x72,0x72,0x52,0x72,0x82,0x94,0x04,0xa2,0xa2,0xb2,0xa2,0x82,0x92,
- 0x01,0xa4,0x94,0x82,0x82,0x82,0x52,0x82,0x92,0xa4,
- 0x72,0x72,0x72,0x52,0x72,0x82,0x92,0xa2,0x84,0x64,0x03,0x51,0xb2,0xa2,0x82,
- 0x61,0x51,
- 0x04,0xa4,0x94,0x82,0x82,0x82,0x82,0x72,0x82,0x52,0x32,0x68,
- 0x02,0xa4,0x94,0x82,0x82,0x82,0x82,0x72,0x82,0x54,0x42,0x66,
- 0xff
- };
- uchar code table4[]={ //旗开得胜
- 0xa2,0x82,0x92,0xa1,0x81,0x03,0x81,0xc1,0xb1,0xa1,0x91,0xa2,0x82,0x92,0xa1,
- 0x91,0x04,0xa2,0x82,0x92,0xa1,0x81,0x03,0x81,0xc1,0xb1,0xa1,0x91,0xa2,0x82,
- 0x92,0xa1,0x91,0x94,0x04,
- 0x01,0xa2,0x91,0xa1,0x91,0x82,0x01,0x62,0x81,0x81,0x61,0x52,0x01,0xa2,0xa1,
- 0xc2,0xc2,0x01,0x92,0x81,0xa1,0x91,0x81,0x61,
- 0x01,0xa2,0x91,0xa1,0x91,0x82,0x01,0x62,0x81,0x81,0x61,0x51,0x31,0x01,0xa2,
- 0xa1,0xc2,0xc1,0x91,0x01,0x92,0x81,0xa1,0x91,0x82,
- 0x01,0xa2,0x91,0xa1,0x91,0x82,0x01,0x62,0x81,0x81,0x61,0x52,0x01,0xa2,0xa1,
- 0xc2,0xc2,0x01,0x92,0x81,0xa1,0x91,0x81,0x61,
- 0x01,0xa2,0x91,0xa1,0x91,0x81,0x61,0x01,0x62,0x81,0x81,0x61,0x51,0x31,0x01,
- 0xa2,0xa1,0xc1,0xa1,0x92,0x01,0x92,0x81,0xa1,0x91,0x82,
- 0x82,0x81,0x81,0xa2,0xc1,0xd1,0x02,0xd1,0xd1,0xd1,0xd1,0xd1,0xd1,0xc1,
- 0xa2,0x91,0x02,0x81,0x81,0x82,0x81,0x81,0xa1,0xa1,0xc1,0xd1,0x02,0xd1,0xd1,
- 0xd1,0xd1,0xd1,0xd1,0xc1,0xa2,0x91,
- 0x03,0xa1,0xa1,0x91,0x81,0x61,0x81,0x82,0x91,0xa1,0x92,0x82,0xb1,0xb2,0xb1,
- 0xa1,0x91,0x92,0xa1,0xa2,0xa1,0x91,0x81,0x82,0x91,0x92,0xa1,0x91,0x81,0x72,
- 0x83,0x81,0x91,0x81,0x72,0x83,0x81,0x91,0x81,0x72,0x83,0x81,0x91,0x81,0x72,
- 0x83,0x81,0x91,0x81,0x72,
- 0x81,0x82,0x91,0xa1,0x91,0x82,0xb1,0xb2,0xb1,0xa1,0x91,0x92,0xa1,0xa2,
- 0xa1,0x91,0x81,0x72,
- 0x83,0x81,0x91,0x81,0x72,0x83,0x81,0x91,0x81,0x72,0x83,0x81,0x91,0x81,0x72,
- 0x83,0x81,0x91,0x81,0x72,0x83,0x81,0x91,0x81,0x72,
- 0xa2,0x82,0x92,0xa1,0x81,0x03,0x81,0xc1,0xb1,0xa1,0x91,0xa2,0x82,0x92,0xa1,
- 0x91,0x04,0xa2,0x82,0x92,0xa1,0x81,0x03,0x81,0xc1,0xb1,0xa1,0x91,0xa2,0x82,
- 0x92,0xa1,0x91,0x93,0x91,0xa4,0x91,0x81,0x61,
- 0x02,0xa1,0x91,0xa1,0x91,0xa1,0x91,0xa1,0x91,0x84,0x04,
- 0xff
- };
- //简谱曲调对应的定时器初值,适合11.0592的晶振
- 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 code yinyue[]={0xff,0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f,0x00,0x00};
- //将音调转化为对应的LED样式
- //延时165MS,即十六分音符子函数
- void delay1(uint z)
- {
- uint x,y;
- for(x=z;x>0;x--)
- for(y=19000;y>0;y--);
- }
- //延时1MS子函数
- void delay2(uint z)
- {
- uint x,y;
- for(x=z;x>0;x--)
- for(y=110;y>0;y--);
- }
- //播放音乐子程序
- void song()
- {
- uint temp;
- uchar aa;
- c=0;
- while(1)
- {
- if(count2==0)
- {
- break;
- }
- if(count2==1)
- {
- temp=table1[c];
- for(t=0;t<16;t++)
- {
- write_data(dis1[t]);
- }
- }
- if(count2==2)
- {
- temp=table2[c];
- for(t=0;t<16;t++)
- {
- write_data(dis2[t]);
- }
- }
- if(count2==3)
- {
- temp=table3[c];
- for(t=0;t<16;t++)
- {
- write_data(dis3[t]);
- }
- }
- if(count2==4)
- {
- temp=table4[c];
- for(t=0;t<16;t++)
- {
- write_data(dis4[t]);
- }
- }
- if(temp==0xff)
- break;
- aa=temp/16;//取数的高四位
- if(aa!=0)
- {
- a=cuzhi[aa*2];
- b=cuzhi[aa*2+1];
- }
- else
- {
- TR1=0;
- BEEP=1;
- }
- delay1(temp%16);//取数的低四位
- TR1=0; //唱完一个音停10MS
- BEEP=1;
- delay2(10);
- TR1=1;
- c++;
- }
- TR1=0;
- BEEP=1;
- }
- //按键扫描子程序
- void key()
- {
- if(k3==0)
- {
- delay2(10);
- if(k3==0)
- {
- num1++;
- if(num1==1)
- {
- TR1=0;
- aa=count2;
- count2=0;
- }
- if(num1==2)
- {
- num=0;
- count2=aa;
- TR1=1;
- }
- while(!k3);
- }
- }
- if(k4==0)
- {
- TR1=0;
- count2=0;
- c=0;
- write_com(0x01);
- delay2(5);
- clock_init(); //?
- }
- }
- //LCD1602液晶写命令子函数
- //void write_com(uchar com)
- //{
- // RS=0;
- // RW=0;
- // E=0;
- // P3=com;
- // E=1;
- // delay2(5);
- // E=0;
- //}
- //LCD1602液晶写数据子函数
- //void write_date(uchar date)
- //{
- // RS=1;
- // RW=0;
- // E=0;
- // P3=date;
- // E=1;
- // delay2(5);
- // E=0;
- //}
- //LCD1602液晶初始化子函数
- //void LCD1602_init()
- //{
- // delay2(15);
- // write_com(0x38);
- // delay2(1);
- // write_com(0x0c);
- // delay2(1);
- // write_com(0x06);
- // delay2(1);
- // write_com(0x01);
- // delay2(1);
- //}
- //主程序
- void main()
- {
- TMOD=0x11;
- TH1=0;
- TL1=0;
- ET1=1;
- TH0=0;
- TL0=0;
- ET0=1;
- TR0=1;
- EA=1;
- EX1=1;
- IT1=1;
- EX0=1;
- IT0=1;
- count2=0;
- init1602();
- clock_init();
- while(1);
- {
- song();
- clock_write(second,minute);
- }
- }
- //定时器0中断子函数
- void int_T0() interrupt 1
- {
- num2++;
- if(num2==5)
- {
- num2=0;
- key();
- }
- }
- //定时器1中断子函数
- void int_T1() interrupt 3
- {
- TH1=(65535-45872)/256;
- TL1=(65535-45872)%256;
- //TH1=a;
- // TL1=b;
- // BEEP=~BEEP;
- num++;
- if(num==20);
- {
- num=0;
- second++;
- }
- if(second==60)
- {
- second=0;
- minute++;
- }
- }
- void wb0() interrupt 0
- {
- TR1=0;
- if(k1==0)
- {
- delay2(10);
- if(k1==0);
- {
- count2++;
- c=0;
- if(count2==1)
- {
- //write_com(0x01);
- delay2(1);
- for(t=0;t<16;t++)
- {
- write_data(dis1[t]);
- }
- }
- if(count2 > 4)
- {
- count2=1;
- c=0;
- TR1=0;
- }
- while(!k1);
- }
- }
- }
- void wb1() interrupt 2
- {
- TR1=0;
- if(k2==0)
- {
- delay2(10);
- if(k2==0);
- {
- count2--;
- c=0;
- if(count2==1)
- {
- //write_com(0x01);
- delay2(1);
- for(t=0;t<16;t++)
- {
- write_data(dis1[t]);
- }
- }
- if(count2 <=0)
- {
- count2=4;
- c=0;
- TR1=1;
- }
- while(!k2);
- }
- }
- }
复制代码
0
|
1个回答
|
|
|