基于STM32的WAV音频格式播放器 (3)(转)
- UID
- 1029342
- 性别
- 男
|
基于STM32的WAV音频格式播放器 (3)(转)
定时器初始化:
void Timerx_Init(u16 arr,u16 psc)
{
NVIC_InitTypeDefNVIC_InitStructure;
RCC->APB1ENR|=1<<1;//TIM3时钟使能
TIM3->ARR=arr; //设定计数器自动重装值
TIM3->PSC=psc; //预分频器7200,得到10KHz的计数时钟
TIM3->DIER|=1<<0; //允许更新中断
TIM3->DIER|=1<<6; //允许触发中断
TIM3->CR1|=0x01; //使能定时器3
//优先级设置
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitStructure.NVIC_IRQChannel= TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority= 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority= 3;
NVIC_InitStructure.NVIC_IRQChannelCmd =ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
DAC初始化:
#include "dac.h"
extern u16 digital;
void MyDAC_Init(void)//DAC channel1Configuration
{
unsigned int tmpreg1=0,tmpreg2=0;
RCC->APB2ENR|=1<<2;//使能PORTA时钟
RCC->APB1ENR|=RCC_APB1Periph_DAC;//使能DAC时钟
GPIOA->CRL&=0XFFF0FFFF;
GPIOA->CRL|=0X00040000;//PA4浮空输入
tmpreg1=DAC->CR;//Get the DAC CR value
tmpreg1&=~(CR_CLEAR_Mask<<DAC_Channel_1);
tmpreg2=(DAC_Trigger_Software|DAC_WaveGeneration_None|DAC_LFSRUnmask_Bits8_0|DAC_OutputBuffer_Enable);
tmpreg1|=tmpreg2<<DAC_Channel_1;
DAC->CR=tmpreg1;//Write to DAC CR
DAC->CR|=CR_EN_Set<<DAC_Channel_1;//DAC通道1使能,PA4自动连接到DAC
DAC1_SetData(0x000);
#if0
tmpreg1=DAC->CR;//Get the DAC CRvalue
tmpreg1&=~(CR_CLEAR_Mask<<DAC_Channel_2); tmpreg2=(DAC_Trigger_Software|DAC_WaveGeneration_None|DAC_LFSRUnmask_Bits8_0|DAC_OutputBuffer_Enable);
tmpreg1|=tmpreg2<<DAC_Channel_2;
DAC->CR=tmpreg1;
DAC->CR|=CR_EN_Set<<DAC_Channel_2;
DAC2_SetData(0x000);
#endif
}
void DAC1_SetData(u16 data)
{
DAC->DHR12R1=data;//通道1的12位右对齐数据
DAC->SWTRIGR|=0x01;//软件启动转换
}
void DAC2_SetData(u16 data)
{
DAC->DHR12R2=data;//通道2的12位右对齐数据
DAC->SWTRIGR|=0x02;//软件启动转换
}
定时器中断服务程序:
void TIM3_IRQHandler(void)
{
u16temp;
if(TIM3->SR&0X0001)//溢出中断
{
if(CHanalnum==1)//单声道
{
if(Bitnum==8)//8位精度
{
DAC->DHR12R1=wav_buf[DApc]*10/volume;
DAC->DHR12R2=wav_buf[DApc]*10/volume;
DAC->SWTRIGR|=0x01;
DApc++;
}
elseif(Bitnum==16)
{
temp=(((u8)(wav_buf[DApc+1]-0x80)<<4)|(wav_buf[DApc]>>4))*10/volume;
DAC->DHR12L1=temp;
DAC->DHR12L2=temp;
DAC->SWTRIGR|=0x01;
DApc+=2;
}
}
elseif(CHanalnum==2)
{
if(Bitnum==8)
{
DAC->DHR12R1=wav_buf[DApc]*10/volume;
DApc++;
DAC->DHR12R2=wav_buf[DApc]*10/volume;
DApc++;
DAC->SWTRIGR|=0x01;
}
elseif(Bitnum==16)
{ DAC->DHR12L1=(((u8)(wav_buf[DApc+1]-0x80)<<4)|(wav_buf[DApc]>>4))*10/volume; DApc+=2; DAC->DHR12L2=(((u8)(wav_buf[DApc+1]-0x80)<<4)|(wav_buf[DApc]>>4))*10/volume;
DApc+=2;
DAC->SWTRIGR|=0x01;
}
}
if(DApc==16384)
{
DApc=0;
DACdone=1;
}
}
TIM3->SR&=~(1<<0);
} |
|
|
|
|
|