#include <reg51.h>
sbit K1=P2^0;
sbit K2=P2^1;
sbit K3=P2^2;
sbit K4=P2^3;
sbit K5=P2^4;
sbit K6=P2^5;
sbit K7=P2^6;
sbit K8=P2^7;
sbit K9=P1^5;
sbit K10=P1^6;
sbit bell=P1^7;
sbit RS = P1^0;
sbit RW = P1^1;
sbit E = P1^2;
void init();
void wcom(unsigned char i);
void wdata(unsigned char i);
void ifnotbusy(void);
unsigned char code music_H[]={0xfc,0xfc,0xfd,0xfd,0xfd,0xfd,0xfe,0xfe, 0xfa,0xfb,0xfb,0xfb,0xfa, 0xf9,0xfa,0xfb, 0xfb,0xfa,0xfb,0xfb,0xfa,0xf9,0xf9,0xfa,0xf9,0xfb,0xfa,0xfa,0xf9,0xf9,0xfb,0xf9,0xf9,0xfb,0xf9, 0xf9,0xf9,0xfa, 0xf9,0xf9,0xfa, 0xfb,0xfb,0xfb,0xfb,0xfb,0xf9,0xfa};
unsigned char code music_L[]={0x8f,0xef,0x45,0x6c,0xb4,0xf4,0x2e,0x3d, 0x8c,0x68,0xe9,0x68,0xbc, 0xe1,0x8c,0x68, 0x68,0x8c,0x68,0x68,0x8c,0xe1,0x21,0x8c,0xe1,0x68,0xd8,0x8c,0x21,0xe1,0x68,0x21,0xe1,0x68,0x21, 0x21,0x21,0x8c, 0x21,0x21,0x8c, 0xe9,0xe9,0xe9,0xe9,0x68,0x21,0x8c};
unsigned char code Time[]={500,500,500,500,500,500,500,500,500,500,500,500,500,250,250,500,500,800,500,500,500,500,250,250,500,500,800,500,500,1000,500,500,1000,500,500,500,500,500,500,500,500,500,500,500,500,500};
int i;
void fy();
void delay( int k);
main()
{
TMOD=0x01; //选择方式1
TH0=music_H[i]; // 高八位
TL0=music_L[i]; //低八位
//TR0=1; //启动计时
while(1){
if(K1==0)
{delay(10);
if(K1==0)
{
i=0;
fy();
init();
wcom(0x80);
wdata(0x31);
}
}
if(K2==0)
{delay(10);
if(K2==0)
{
i=1;
fy();
init();
wcom(0x80);
wdata(0x32);
}
}
if(K3==0)
{delay(10);
if(K3==0)
{
i=2;
fy();
init();
wcom(0x80);
wdata(0x33);
}
}
if(K4==0)
{delay(10);
if(K4==0)
{ i=3;
fy();
init();
wcom(0x80);
wdata(0x34);
}
}
if(K5==0)
{delay(10);
if(K5==0)
{ i=4;
fy();
init();
wcom(0x80);
wdata(0x35);
}
}
if(K6==0)
{delay(10);
if(K6==0)
{ i=5;
fy();
init();
wcom(0x80);
wdata(0x36);
}
}
if(K7==0)
{delay(10);
if(K7==0)
{ i=6;
fy();
init();
wcom(0x80);
wdata(0x37);
}
}
if(K8==0)
{delay(10);
if(K8==0)
{ i=7;
fy();
init();
wcom(0x80);
wdata(0x38);
}
}
if(K9==0)
{delay(10);
if(K9==0)
{ i=8;
wcom(0x80);
wdata('l');
wcom(0x81);
wdata('z');
wcom(0x82);
wdata('l');
wcom(0x83);
wdata('h');
while(1)
{
fy();
i++;
if(i>=25)
break;
}
}
}
if(K10==0)
{delay(10);
if(K10==0)
{ i=40;
wcom(0x80);
wdata('h');
wcom(0x81);
wdata('l');
wcom(0x82);
wdata('w');
while(1)
{
fy();
i++;
if(i>=53)
break;
}
}
}
}
}
void fy()
{ int j=0;
TR0=1;
while(j<Time[i]) //Time[i]music_Time[i]
{ if(TF0==1) //定时/计数器溢出标志位 TF0=1 表示溢出
{ TF0=0;
TH0=music_H[i];
TL0=music_L[i];
bell=!bell;
j++;
}
}
TR0=0;
}
void delay(int k)
{
int b,d;
for (d=0;d<k;d++)
{
for(b=0;b<250;b++);
}
}
void init()
{
wcom(0x01);
wcom(0x38);
wcom(0x06);
wcom(0x0c);
}
void ifnotbusy()
{ P0=0xff;
RS=0;
RW=1;
E=0;
E=1;
while(P0&0x80)
{
E=0;E=1;
}
}
void wcom(unsigned char j)
{ifnotbusy();
E=0;
RS=0;
RW=0;
P0=j;
E=1;
E=0;
}
void wdata(unsigned char j)
{ifnotbusy();
E=0;
RS=1;
RW=0;
P0=j;
E=1;
E=0;
} |