//PWM输出初始化
//arr:自动重装值
//psc:时钟预分频数
void TIM1_PWM_Init(u16 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_BDTRInitTypeDef TIM_BDTRInitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA|RCC_AHBPeriph_GPIOB, ENABLE); //开启GPIOA、GPIOB时钟
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10; //TIM1_CH1,TIM1_CH2
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource8,GPIO_AF_2);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9,GPIO_AF_2);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10,GPIO_AF_2);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15; //TIM1_CH1N,TIM1_CH2N
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource13,GPIO_AF_2);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource14,GPIO_AF_2);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource15,GPIO_AF_2);
TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值 80K
TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置用来作为TIMx时钟频率除数的预分频值 不分频
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_RepetitionCounter =0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
/* Set the default configuration */
TIM_BDTRInitStruct.TIM_OSSRState = TIM_OSSRState_Enable;//运行模式下关闭状态选择
TIM_BDTRInitStruct.TIM_OSSIState = TIM_OSSIState_Enable;//空闲模式下关闭状态选择
TIM_BDTRInitStruct.TIM_LOCKLevel = TIM_LOCKLevel_OFF;//软件错误锁定配置:锁定关闭无保护
TIM_BDTRInitStruct.TIM_DeadTime = 0x5;//DTG[7:0]死区发生器配置:(死区时间DT) ,注意死区时间要设成100ns左右,否则过大会导致MOS栅极PWM波形异常,太小导致上下臂短路
//TDTS = 125nS(8MHz)
//DTG[7: 5] = 0xx => DT = DTG[7: 0] * Tdtg, Tdtg = TDTS;
//DTG[7: 5] = 10x => DT =(64+DTG[5: 0]) * Tdtg, Tdtg = 2 * TDTS;
//DTG[7: 5] = 110 => DT =(32+DTG[4: 0]) * Tdtg, Tdtg = 8 * TDTS;
//DTG[7: 5] = 111=> DT =(32 + DTG[4: 0]) * Tdtg, Tdtg = 16 * TDTS;
TIM_BDTRInitStruct.TIM_Break = TIM_Break_Disable; //刹车配置:使能刹车
TIM_BDTRInitStruct.TIM_BreakPolarity = TIM_BreakPolarity_High;//刹车输入极性选择:高电平有效
TIM_BDTRInitStruct.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable;//自动输出使能配置:MOE只能软件置1
TIM_BDTRConfig( TIM1, &TIM_BDTRInitStruct); //配置互补输出死区时间
TIM_OCStructInit(&TIM_OCInitStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //选择定时器模式:TIM脉冲宽度调制模式1
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
TIM_OCInitStructure.TIM_OutputNState=TIM_OutputNState_Enable;
TIM_OCInitStructure.TIM_Pulse = 500; //设置待装入捕获比较寄存器的脉冲值
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性:TIM输出比较极性高
TIM_OCInitStructure.TIM_OCNPolarity=TIM_OCNPolarity_High;
TIM_OCInitStructure.TIM_OCIdleState=TIM_OCIdleState_Reset;
TIM_OCInitStructure.TIM_OCNIdleState=TIM_OCNIdleState_Reset;
TIM_OC1Init(TIM1, &TIM_OCInitStructure); //根据TIM_OCInitStruct中指定的参数初始化外设TIMx
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //选择定时器模式:TIM脉冲宽度调制模式1
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性:TIM输出比较极性高
TIM_OCInitStructure.TIM_OCNPolarity=TIM_OCNPolarity_High;
TIM_OC2Init(TIM1, &TIM_OCInitStructure); //根据TIM_OCInitStruct中指定的参数初始化外设TIMx
TIM_OC3Init(TIM1, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable); //CH1预装载使能
TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable); //CH2预装载使能
TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable); //CH2预装载使能
TIM_ARRPreloadConfig(TIM1, ENABLE); //使能TIMx在ARR上的预装载寄存器TIM_CCMR1_IC1F_1
TIM_ITConfig(TIM1, TIM_IT_CC1, ENABLE);
TIM_CtrlPWMOutputs(TIM1,ENABLE);
TIM_Cmd(TIM1, ENABLE); //使能TIM1
}
/*******************************************************************************
* @name : initNVIC_TIM1
* @brief : PWM中断配置
* @param : void
* @retval : void
*******************************************************************************/
void initNVIC_TIM1(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
// Config TIM1_CC_IRQn NVIC config
NVIC_InitStructure.NVIC_IRQChannel = TIM1_CC_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
/*******************************************************************************
* @name : OnTIM1
* @brief : Enable TIM1 使能定时器
* @param : void
* @retval : void
*******************************************************************************/
void OnTIM1(void)
{
TIM_Cmd(TIM1, ENABLE);
}
/*******************************************************************************
* @name : TIM1_CC_IRQHandler
* @brief : TIM1 Compare/Capture IRQHandler 定时器中断处理函数
* @param : void
* @retval : void
*******************************************************************************/
volatile uint32_t TIMER0_Count = 0;
void TIM1_CC_IRQHandler() //43us
{
if (TIM_GetITStatus(TIM1, TIM_IT_CC1) != RESET)
{
// Clear the CC1 flag
TIM_ClearITPendingBit(TIM1, TIM_IT_CC1);
TIMER0_Count ++;
}
}
//PWM初始化配置
void PWM_INIT_ALL(void)
{
initPWM_EN();
//config TIM to PWM output
TIM1_PWM_Init(1999,0);
initNVIC_TIM1();
OnTIM1();
}
//占空比配置
TIM1->CCR1 = w_time_ph_a;
TIM1->CCR2 = w_time_ph_b;
TIM1->CCR3 = w_time_ph_c;