"); //-->
//学习板唱歌程序(两只老鼠)
#include<reg51.h>
#define u_char unsigned char
#define u_int unsigned int
sbit spk=P3^4;
u_int m,i;
u_int rti;
u_int l;
//音频表:高位,低位
u_char code toneth[14]={0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,0xff,0xff,0xff,0xff,0xff,0x0f,0xff};
u_char code tonetl[14]={0x22,0x56,0x85,0x9a,0xc1,0xe3,0x03,0x10,0x2b,0x42,0x4c,0x60,0x71,0x81};
//乐谱1
u_char code song[]="123112313455345556543256543215111511";
u_char code length[]="444444444444444422224422224422224444444444";
//定时器0中断服务程序
void timer0(void)interrupt 1 using 1
{
spk=!spk;
TH0=toneth[rti];
TL0=tonetl[rti];
}
//定时器中断服务程序
void timer1(void)interrupt 3 using 3
{
TH1=0x0c;
TL1=0xdc;
m++;
}
//音符到音频换程序
playc(char ch)
{
int ti;
switch(ch)
{
case'q':ti=0;break;
case'w':ti=1;break;
case'e':ti=2;break;
case'r':ti=3;break;
case't':ti=4;break;
case'y':ti=5;break;
case'u':ti=6;break;
case'1':ti=7;break;
case'2':ti=8;break;
case'3':ti=9;break;
case'4':ti=10;break;
case'5':ti=11;break;
case'6':ti=12;break;
case'7':ti=13;break;
case' ':ti=50;break;
default:ti=50;break;
}
if (ti==50)
return(100);
return(ti);
}
//主程序
void main(void)
{
m=0;
TMOD=0x11;
spk=0;
TH0=toneth[0];
TL0=tonetl[0];
TH1=0x0c;
TL1=0Xdc;
IP=0x08;
EA=1;
ET0=1;
ET1=1;
TR0=1;
TR1=1;
for (i=0;i<37;i++)
{
rti=playc(song);
l=length-0x30;
do {}while (m<l);
m=0;
}
TR0=0;
TR1=0;
spk=0;
}
*博客内容为网友个人发布,仅代表博主个人观点,如有侵权请联系工作人员删除。