单片机音频

作者在 2019-12-15 14:31:59 发布以下内容
#include <REGX52.H>
sbit speaker=P2^1;

unsigned char timer0h,timer0l,time;
//定义每个音阶的长度的数组 《世上只有妈妈好》
code unsigned char song_mother[]=
{
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,1 ,2,2,1,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  
} ;
  //音阶高8位表
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 
} ; 
	 //低音阶8位表
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 delay(unsigned char t )
{
unsigned char t1;
unsigned long t2;

for( t1=0; t1 < t; t1++ )
 for ( t2=0; t2<8000; t2++) ;
 
 TR0=0;
}

void t0int() interrupt 1
{
TR0=0;
speaker=~speaker;
TH0=timer0h;
TL0=timer0l;
TR0=1;
}

void song()
{
TH0=timer0h;
TL0=timer0l;
TR0=1;
delay(time);

}

void main()
{
unsigned char k,i;
TMOD=1;//置TO定时工作模式1
EA=1;
ET0=1;  //IE=0X82
while(1){
i=0;
	while(i<100)
	{
	   k= song_mother[i]+7*song_mother[i+1]-1;
	   timer0h=	 freqh[k];
	   timer0l=freql[k];
	   time=song_mother[i+2];
	   i=i+3;
	   song();

}
}
}





在原码低音频上加一些,丰富了低音段,使乐曲音准一些了。有点像卖老鼠药的广告音乐了。
				
单片机 | 阅读 1951 次
文章评论,共0条
游客请输入验证码
浏览239354次
最新评论