void MCU_TimerPwmInit(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Pin =GPIO_Pin_7;
GPIO_Init( GPIOC, &GPIO_InitStructure);
GPIO_PinAFConfig(TONE_AFCONFIG);
//初始化TIM1寄存器
TIM_DeInit(TIM1);
/*分频和周期计算公式:
Prescaler=(TIMxCLK/TIMxcounterclock)-1;
Period=(TIMxcounterclock/TIM3outputclock)-1TIMxcounterclock为你所需要的TXM的定时器时钟
*/
/* DMA clock enable */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2 , ENABLE);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannel = DMA2_Stream6_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
DMA_DeInit(DMA2_Stream6);
DMA_InitStructure.DMA_Channel = DMA_Channel_6;
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)(TIM1_CCR3_ADDRESS);
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)Buff;
DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;
DMA_InitStructure.DMA_BufferSize = bufSize;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
DMA_InitStructure.DMA_MemoryDataSize = DMA_PeripheralDataSize_HalfWord;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_Init(DMA2_Stream6, &DMA_InitStructure);
/* TIM1 compare DMA Request enable */
TIM_DMACmd(TIM1, TIM_DMA_CC3, ENABLE);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitStructure.NVIC_IRQChannel = DMA2_Stream2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
DMA_DeInit(DMA2_Stream2);
DMA_InitStructure.DMA_Channel = DMA_Channel_6;
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)(TIM1_CCR2_ADDRESS);
DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)Buff;
DMA_InitStructure.DMA_BufferSize = bufSize;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
DMA_InitStructure.DMA_MemoryDataSize = DMA_PeripheralDataSize_HalfWord;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_Init(DMA2_Stream2, &DMA_InitStructure);
/* TIM1 compare DMA Request enable */
TIM_DMACmd(TIM1, TIM_DMA_CC2, ENABLE);
TIM_TimeBaseStructure.TIM_Period = timeCnt; //查数据手册可知,TIM1与TIM8为16位自动装载
//TIM_TimeBaseStructure.TIM_Period = 4098; //cdcss 134.4Hz,PWM 40992Hz
/*在system_stm32f4xx.c中设置的APB2Prescaler=2,可知*APB1时钟为168M/2*2,因为如果APB1分频不为1,则定时时钟x2*/
TIM_TimeBaseStructure.TIM_Prescaler = 0; //分频为168MHz
//TIM_TimeBaseStructure.TIM_RepetitionCounter = 10; //重复2此输出PWM
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0x0;
TIM_TimeBaseInit(TIM1,&TIM_TimeBaseStructure);
/*填入缺省值,其中后几项只对TIM1和TIM8有效*/
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //PWM1为正常占空比模式
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //输出允许
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; //互补输出允许
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Reset;
TIM_OCInitStructure.TIM_Pulse = 0; //输入通道1CCR1(占空比数值)
TIM_OC2Init(TIM1,&TIM_OCInitStructure);
TIM_OC3Init(TIM1,&TIM_OCInitStructure);
/*CCR自动装载默认也是打开的*/
TIM_OC2PreloadConfig(TIM1,TIM_OCPreload_Enable);
TIM_OC3PreloadConfig(TIM1,TIM_OCPreload_Enable);
/*ARR自动装载默认是打开的,可以不设置*/
TIM_ARRPreloadConfig(TIM1,ENABLE);
/*清除中断标志*/
TIM_ClearFlag(TIM1,TIM_FLAG_Update);
/*产生一个更新事件用于装载预装值*/
// TIM_ITConfig(TIM1,TIM_IT_Update,ENABLE);
/*开启TIM1嵌套中断向量*/
// NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_TIM10_IRQn ;
// NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x01;
// NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0F;
// NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
//初始化嵌套中断向量表
// NVIC_Init(&NVIC_InitStructure);
//TIM_Cmd(TIM1,ENABLE);
TIM_CtrlPWMOutputs(TIM1,DISABLE);
TIM_Cmd(TIM1,DISABLE);
}
//PWM启动
void MCU_PWMStart(void)
{
ToneStartFlag = TRUE;
DMA_ITConfig(DMA2_Stream2, DMA_IT_TC, ENABLE);
DMA_Cmd(DMA2_Stream2, ENABLE);
/*使能TIM的PWM输出,TIM1与TIM8有效*/
TIM_CtrlPWMOutputs(TIM1,ENABLE);
/*使能TIM定时器*/
TIM_Cmd(TIM1,ENABLE);
}
//PWM停止
void MCU_PWMStop(void)
{
ToneStartFlag = FALSE;
DMA_ITConfig(DMA2_Stream2, DMA_IT_TC, DISABLE);
DMA_Cmd(DMA2_Stream2, DISABLE);
/*除能TIM的PWM输出,TIM1与TIM8有效*/
TIM_CtrlPWMOutputs(TIM1,DISABLE);
/*除能TIM定时器*/
TIM_Cmd(TIM1,DISABLE);
}