跪求大神指点,刚写的一个51程序,但不能发声。
我的思路:
T0中断作为节拍,T1中断作为音频发生器。
程序如下:
#include <reg52.h>
#include <intrins.h>
//Do~Si
#define uint8 unsigned char;
#define uint16 unsigned int;
sbit SOUND=P2^0;
uint16 code FreLow[7]={262,294,330,349,392,440,494}; //低音音频 表,高8位用0表示
uint16 code FreMid[7]={523,578,659,698,784,880,988}; //中音,高8位用1表示
uint16 code FreHig[7]={1046,1175,1318,1397,1568,1760,1976}; //高音,高8位用2表示
uint8 code MusicLoad[]={
0x11,0x05,
0x12,0x05,
0x13,0x05,
0x11,0x05,
0x11,0x05,
0x12,0x05,
0x13,0x05,
0x11,0x05,
0x13,0x05,
0x14,0x05,
0x15,0x0a,
0x13,0x05,
0x14,0x05,
0x15,0x0a,
0x15,0x05,
0x16,0x05,
0x15,0x05,
0x14,0x05,
0x13,0x05,
0x11,0x05,
0x15,0x05,
0x16,0x05,
0x15,0x05,
0x14,0x05,
0x13,0x05,
0x11,0x05,
0x12,0x05,
0x05,0x05,
0x11,0x0a,
0x12,0x05,
0x05,0x05,
0x11,0x0a,
0x00,0x00
}; //音频播放表格式:音频+节拍为一组
uint8 *pFre; //音频指针
uint8 *pBeat; //节拍指针
uint16 Fre; //暂存音频
uint8 temp; //暂存节拍
void
timer_beat(void)interrupt 1{
//定时器T0,计时50ms一个小周期
TH0=0x3c;
TL0=0xb0;
if(temp==(*pBeat)){
//节拍已经够了,指向下一组
pFre=pFre+2;
pBeat=pBeat+2;
temp=0;
}
//节拍不够,继续打节拍
++temp;
}
void timer_fre(void)interrupt 3{
//音频发生器
TH1=Fre/256; //取高8
TH0=Fre%256;
SOUND=~SOUND; //音频输出
}
void Music_Play(void){
while((*pFre)&&(*pBeat)){
//音频表还没播放完毕
if(((*pFre)&0xf0)==0){
//检测到为低音
Fre=65536-1000000/FreLow[(*pFre)&0x0f]; //取出该音频
}
else if(((*pFre)&0xf0)==1){
//检测到为中音
Fre=65536-1000000/FreMid[(*pFre)&0x0f];
}
else if(((*pFre)&0xf0)==2){
//检测到为高音
Fre=65536-1000000/FreHig[(*pFre)&0x0f];
}
//T1初始化
TH1=Fre/256;
TH1=Fre%256;
//T0初始化
TH0=0x3c;
TL0=0xb0;
//打开中断
EA=1;
ET0=1;
ET1=1;
//打开T0、T1
TR1=1;
TR0=1;
}
}
void main(void){
pFre=MusicLoad; //指向表头
pBeat=MusicLoad+1;
temp=0;
TR0=0;
TR1=0;
while(1){
Music_Play();
}
}
一周热门 更多>