STM32 播放wav声音很嘈杂,杂音很多。代码如下,求各位大神看看是哪里的问题
void WAV_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
DAC_InitTypeDef DAC_InitType;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE );
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_SetBits(GPIOD,GPIO_Pin_0);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_PinRemapConfig(GPIO_Remap_PD01, ENABLE);
DAC_DeInit();//
DAC_InitType.DAC_Trigger=DAC_Trigger_None; //不使用触发功能 TEN1=0
DAC_InitType.DAC_WaveGeneration=DAC_WaveGeneration_Noise;//不使用波形发生
DAC_InitType.DAC_LFSRUnmask_TriangleAmplitude=DAC_LFSRUnmask_Bit0;//屏蔽、幅值设置
DAC_InitType.DAC_OutputBuffer=DAC_OutputBuffer_Disable ; //DAC1输出缓存关闭 BOFF1=1
DAC_Init(DAC_Channel_1,&DAC_InitType); //初始化DAC通道1
DAC_Cmd(DAC_Channel_1, ENABLE); //使能DAC1
DAC_SetChannel1Data(DAC_Align_12b_R, 01);
}
void DAC_data_Conven(u16 data)
{
DAC_SetChannel1Data(DAC_Align_12b_R, data);
DAC_SoftwareTriggerCmd(DAC_Channel_1,ENABLE);
}
void TIM2_Init(u16 arr,u16 psc)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); //时钟使能
TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值
TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置用来作为TIMx时钟频率除数的预分频值
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); //根据TIM_Int_Init指定的参数初始化TIMx的时间基数单位
TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE ); //使能指定的TIM2中断,允许更新中断
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn; //TIM2中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //先占优先级0级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //从优先级3级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
NVIC_SetVectorTable ( NVIC_VectTab_FLASH , 0x0);
NVIC_Init(&NVIC_InitStructure); //初始化NVIC寄存器
TIM_Cmd(TIM2, ENABLE); //使能TIMx
}
void TIM2_IRQHandler(void)
{
static uint16_t ti = 0;
u16 temp;
if(TIM_GetITStatus(TIM2, TIM_IT_Update) !=RESET)
{
TIM_ClearITPendingBit(TIM2,TIM_IT_Update);
DAC_data_Conven(wav_decode_buf[ti++]);
if(ti>=1024) {ti=0;DAC_DONE=1;}
}
}
u8 WAV_Analyze(u8* pbuf)
{
if(Check_Ifo(pbuf,(u8*)"RIFF"))return 1;//RIFF
wavinfo.wavlen=Get_num(pbuf+4,4);
if(Check_Ifo(pbuf+8,(u8*)"WAVE"))return 2;//WAVE
if(Check_Ifo(pbuf+12,(u8*)"fmt "))return 3;//fmt????
wavinfo.formart=Get_num(pbuf+20,2);//????
wavinfo.CHnum=Get_num(pbuf+22,2);//???
CHANALNUM=wavinfo.CHnum;
wavinfo.SampleRate=Get_num(pbuf+24,4);//???
wavinfo.speed=Get_num(pbuf+28,4);//??????
wavinfo.ajust=Get_num(pbuf+32,2);//??????
wavinfo.SampleBits=Get_num(pbuf+34,2);//??????
BITNUM=wavinfo.SampleBits;
wavinfo.DATAlen=Get_num(pbuf+44,4);//????
return 0;
}
u8 Check_Ifo(u8* pbuf1,u8* pbuf2)
{
u8 i;
for(i=0;i<4;i++)if(pbuf1[i]!=pbuf2[i])return 1;//
return 0;
}
u32 Get_num(u8* pbuf,u8 len)
{
u32 num;
if(len==2)num=(pbuf[1]<<8)|pbuf[0];
else if(len==4)num=(pbuf[3]<<24)|(pbuf[2]<<16)|(pbuf[1]<<8)|pbuf[0];
return num;
}
/*ADPCM ???????*/
const signed char index_adjust[8] =
{-1,-1,-1,-1,2,4,6,8};
/**/
const uint16_t step_table[89] =
{
7,8,9,10,11,12,13,14,16,17,19,21,23,25,28,31,34,37,41,45,
50,55,60,66,73,80,88,97,107,118,130,143,157,173,190,209,230,253,279,307,337,371,
408,449,494,544,598,658,724,796,876,963,1060,1166,1282,1411,1552,1707,1878,2047,
2272,2499,2749,3024,3327,3660,4026,4428,4871,5358,5894,6484,7132,7845,8630,9493,
10442,11487,12635,13899,15289,16818,18500,20350,22385,24623,27086,29794,32767
};
void adpcm_encode( u16* sour, u8 *dis,u16 size)
{
}
void adpcm_decode( u8 *sour, u16 *dis,u16 size)
{
static u8 index =0;
static int16_t cur_sample = 0;
int16_t delta;
uint16_t tex;
u8 sb,tem; //sb????
unsigned char code = 0;
u8 i;
size/=2;
while (size--)
{
code = tem = *sour++;
for(i = 0;i < 2;i ++ ) //?????????????
{
code=(code & 0x0f); // ??????
if ((code & 8) != 0) //??????
{
sb=1 ;
}
else
{
sb=0;
}
code&=7; // ?????
delta = (step_table[index]*code)/4+step_table[index]/8; // ??????
if (sb==1) delta=-delta; //??
cur_sample+=delta; //???????
if (cur_sample>2047) cur_sample = 2047;
else if (cur_sample<-2047) cur_sample = (-2047);
*dis++ = (cur_sample+2047)&0xfff;
index+=index_adjust[code];
if (index<0) index=0;
if (index>59) index=59;
code = (tem>>4);
}
}
}
u8 PlayWav(void)
{
u16 i,times;
wav_addr = 0x002695D5;
sFLASH_ReadBuffer(wav_buf,wav_addr,46);
wav_addr+=46;
if(WAV_Analyze(wav_buf)) return 0;
DAC_DONE=0x00;
TIM2_Init(1000000/wavinfo.SampleRate,8);
times=(wavinfo.DATAlen>>9);
for(i=0;i<times;i++)
{
while(!DAC_DONE);
DAC_DONE=0;
sFLASH_ReadBuffer(wav_buf,wav_addr,512);
wav_addr+=512;
adpcm_decode(wav_buf,wav_decode_buf,512);
}
TIM_ITConfig(TIM2,TIM_IT_Update,DISABLE);
return 0;
}
一周热门 更多>