[mw_shl_code=applescript,true]void TIM1_PWM_Init(u16 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_BDTRInitTypeDef TIM_BDTRInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 | RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10; //通道1 2 3
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO口速度为50MHz
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15; //通道1N 2N 3N
GPIO_Init(GPIOB, &GPIO_InitStructure);
TIM_TimeBaseStructure.TIM_Period = arr;
TIM_TimeBaseStructure.TIM_Prescaler =psc;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //选择定时器模式:TIM脉冲宽度调制模式1
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; //比较互补输出使能
TIM_OCInitStructure.TIM_Pulse = 0; //设置翻转值
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性默认高电平
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High; //互补输出极性默认高电平
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set; // 输出通道空闲电平极性配置
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Reset; // 互补输出通道空闲电平极性配置
TIM_OC1Init(TIM1, &TIM_OCInitStructure); //配置到通道1
TIM_OC2Init(TIM1, &TIM_OCInitStructure); //配置到通道2
TIM_OC3Init(TIM1, &TIM_OCInitStructure); //配置到通道3
TIM_CtrlPWMOutputs(TIM1,ENABLE); //主功能为PWM
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable); //CH1预装载使能
TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable); //CH2预装载使能
TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable); //CH3预装载使能
/****************************************************************************
*死区时间计算方法:
*@autor 秋雨梧桐
*@date 2017/8/9
*TIM1的时钟频率为72MHZ
*这些位定义了插入互补输出之间的死区持续时间。假设DT表示其持续时间:
*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;
*例:若TDTS = 125ns(8MHZ),可能的死区时间为:
*0到15875ns,若步长时间为125ns;
*16us到31750ns,若步长时间为250ns;
*32us到63us,若步长时间为1us;
*64us到126us,若步长时间为2us;
*
*综上可知:因为TIM1时钟为72M,所以TDTS = 1/72M = 13.9ns;又因为DTG[7:5]=000;
*DTG[7:0] = 0X0A;所以DT(死区时间) = 10(即0X0A) * 13.9ns(即TDTS) = 139us
****************************************************************************/
TIM_BDTRStructInit(&TIM_BDTRInitStructure); //缺省值(一般为0)必须要有,为了保证寄存器的值不是上电随机数
TIM_BDTRInitStructure.TIM_DeadTime = 0x0A; //死区时间设置
TIM_BDTRConfig(TIM1, &TIM_BDTRInitStructure);
TIM_CtrlPWMOutputs(TIM1, ENABLE); //主输出使能,开启OC和OCN输出!!
TIM_ARRPreloadConfig(TIM1, ENABLE); //使能TIMx在ARR上的重装载寄存器
TIM_Cmd(TIM1, ENABLE); //开启TIM1
}
[/mw_shl_code]
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
关于如何输出六路互补带死区PWM波形,请关注“单片机STM32”公众号,里面有一篇文章“STM32定时器TIM1输出六路带死区互补PWM波形(一)”描述。
一周热门 更多>