|
|
#include "reg51.h"
sbit P1_3=P1^3;
unsigned char code TABLE[]=
{
0x82,0x01,0x81,0x94,0x84,0xB4,0xA4,0x04,
0x82,0x01,0x81,0x94,0x84,0xC4,0xB4,0x04,
0x82,0x01,0x81,0xF4,0xD4,0xB4,0xA4,0x94,
0xE2,0x01,0xE1,0xD4,0xB4,0xC4,0xB4,0x04,
0x82,0x01,0x81,0x94,0x84,0xB4,0xA4,0x04,
0x82,0x01,0x81,0x94,0x84,0xC4,0xB4,0x04,
0x82,0x01,0x81,0xF4,0xD4,0xB4,0xA4,0x94,
0xE2,0x01,0xE1,0xD4,0xB4,0xC4,0xB4,0x04,0
};//延时和取音阶下标
unsigned int code TABLE1[]=
{
64260,64400,64524,64580,64684,64777,64820,
64898,64968,65030,65058,65110,65157,65178,65217
}; //音阶
unsigned char th0_temp,tl0_temp;
void SING(unsigned char hi);
void DELAY(unsigned char lo);
main()
{
unsigned char i,hi,lo,coute;
TMOD= 0X01;
ET0 = 1;
EA = 1;
TR0 = 1;
while(1)
{
P1_3= 1;
coute= 0;//中断初始化
while(TABLE[coute]!=0)
{
i=TABLE[coute];
coute++;
lo=i&0x0f;
hi=(i&0xf0)>>4;
if(hi>0)
SING(hi);
else
TR0=0;
DELAY(lo);
}
}
}
void SING(unsigned char hi)
{
th0_temp=(TABLE1[hi-1]/256);
TH0=th0_temp;
tl0_temp=(TABLE1[hi-1]%256);
TL0=tl0_temp;
TR0=1;
}
void DELAY(unsigned char lo)
{
unsigned char temp1,temp2;
do
{
for(temp1=0;temp1<150;temp1++)
for(temp2=0;temp2<200;temp2++);
}
while(lo--);
}
void INTT0() interrupt 1//定时器0中断
{
TH0=th0_temp;
TL0=tl0_temp;
P1_3=~P1_3;
} |
|
|
|
|