参考:https://wenku.baidu.com/view/1a3c239cfe0a79563c1ec5da50e2524de418d076.html
我这里用的是普中51-单核-A2开发板,这里只是对参考的删减不必要的代码 6,2,3可以改成136,然后求余得到音高,音长,音调的值 定时器初值表应该可以合并成一个表
#include <REG52.H>
#include <INTRINS.H>
sbit speaker=P1^5;
unsigned char timer0h,timer0l,k,time=1,j=0,flagd=0;
code unsigned char sszymmh[]={ 6,2,3, 5,2,1, 3,2,2, 5,2,2, 1,3,2, 6,2,1, 5,2,1,
6,2,4, 3,2,2, 5,2,1, 6,2,1, 5,2,2, 3,2,2, 1,2,1,
6,1,1, 5,2,1, 3,2,1, 2,2,4, 2,2,3, 3,2,1, 5,2,2,
5,2,1, 6,2,1, 3,2,2, 2,2,2, 1,2,4, 5,2,3, 3,2,1,
2,2,1, 1,2,1, 6,1,1, 1,2,1, 5,1,6, 0,0,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,
};
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()
{
TR0=1;
delay(time);
}
void main(void)
{
unsigned char i=0;
TMOD=1;
EA=1;ET0=1;
while(1)
{
while(time)
{
k=sszymmh[i]+7*sszymmh[i+1]-1;
timer0h=FREQH[k];
timer0l=FREQL[k];
time=sszymmh[i+2];
i=i+3;
song();
}
}
}
简谱:世上只有妈妈好
|