其实主要要理解的内容也就不到50行 连音的地方,就将音长(节拍)相加, 0调的音就停止定时器 发音采用音高,音长,音调组合成8bit数据节省空间,缺点是没有超低音和超高音以及高音的音长仅有四种的问题
#include <REG52.H>
#include <INTRINS.H>
sbit speaker=P1^5;
unsigned char timer0h,timer0l,time=1;
code unsigned char gyz[]={
122,27,121,26,122,27,121,26,122,27,121,26,120,
143,140,110,111,112,111,143,130,111,112,111,112,113,36,111,36,111,36,111,122,121,47,140,
143,140,111,112,111,143,140,111,112,111,112,113,36,111,36,111,36,111,123,122,47,140,
16,111,136,116,116,115,126,116,115,116,115,116,115,143,140,16,111,136,116,116,115,116,115,137,117,117,116,127,126,143,
120,113,115,113,132,113,132,113,132,113,115,113,115,113,132,113,132,113,142,120,111,112,123,26,121,123,132,113,112,121,46,140,
116,117,211,212,117,211,221,211,117,211,212,117,211,221,211,212,213,212,213,212,223,213,212,223,225,223,
116,117,211,212,117,211,221,211,117,211,212,117,211,221,211,212,213,212,213,212,223,213,212,223,225,223,225,
233,215,233,215,213,215,216,213,225,225,233,215,233,215,
213,215,216,213,225,215,215,213,222,222,211,223,232,222,221,146,140,215,215,213,222,222,211,233,222,222,221,146,140,140,
116,115,136,115,116,115,116,115,126,116,115,116,115,116,115,143,140,116,115,136,115,116,115,116,115,137,117,117,116,117,116,143,140,
113,115,113,132,113,132,113,132,113,115,113,115,113,132,113,132,113,142,120,111,112,123,126,221,223,232,213,212,231,166,120,
116,117,211,212,117,211,221,211,117,211,212,117,211,221,211,212,213,212,213,212,223,213,212,223,225,223,
116,117,211,212,117,211,221,211,117,211,212,117,211,221,211,212,213,212,213,212,223,213,212,223,225,223,225,
233,215,233,215,213,215,216,213,225,225,233,215,233,215,
213,215,216,213,225,215,215,213,222,222,211,223,232,222,221,146,140,215,215,213,222,222,211,233,222,222,221,146,140,140,
26,121,123,147,127,117,127,186,120,26,121,123,147,127,117,127,186,120,
116,117,211,212,117,211,221,211,117,211,212,117,211,221,211,212,213,212,213,212,223,213,212,223,225,223,
116,117,211,212,117,211,221,211,117,211,212,117,211,221,211,212,213,212,213,212,223,213,212,223,225,223,225,
233,215,233,215,213,215,216,213,225,225,233,215,233,215,
213,215,216,213,225,215,215,213,222,222,211,223,232,222,221,146,140,215,215,213,222,222,211,233,222,222,221,146,140,140,
0
};
code unsigned char FREQH[]={
0xF2,0xF3,0xF5,0xF5,0xF6,0xF7,0xF8,
0xF9,0xF9,0xFA,0xFA,0xFB,0xFB,0xFC,
0xFC,0xFC,0xFD,0xFD,0xFD,0xFD,0xFE,
0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFF,
} ;
code unsigned char FREQL[]={
0x42,0xC1,0x17,0xB6,0xD0,0xD1,0xB6,
0x21,0xE1,0x8C,0xD8,0x68,0xE9,0x5B,
0x8F,0xEE,0x44,0x6B,0xB4,0xF4,0x2D,
0x47,0x77,0xA2,0xB6,0xDA,0xFA,0x16,
};
unsigned int code FreTab[28] = {
3517,3134,2792,2633,2351,2094,1865,
1758,1566,1395,1319,1175,1046,932 ,
880 ,785 ,699 ,660 ,587 ,523 ,466 ,
440 ,392 ,349 ,329 ,293 ,261 ,233 ,
};
void t0int() interrupt 1
{
speaker=!speaker;
TH0=timer0h;
TL0=timer0l;
}
void delay(unsigned char t)
{
unsigned long t2;
for(;t>0;t--)
{
for(t2=0;t2<5000;t2++){;}
}
TR0=0;
}
void song()
{
unsigned char k=0;
unsigned long i=0;
while(time)
{
if(gyz[i]%10==0)
{
TR0=0;
}
else
{
k=gyz[i]%10+7*(gyz[i]/100+1)-1;
timer0h=FREQH[k];
timer0l=FREQL[k];
TR0=1;
}
time=gyz[i]/10%10;
i=i+1;
delay(time);
}
}
void main(void)
{
TMOD=1;
EA=1;ET0=1;
while(1)
{
song();
}
}
|