在使用STM32F103的高级定时器TIM8时,遇到一个问题:初始化的互补PWM波形输出时,同时初始化3对互补通道,CH1,CH1N,CH2,CH2N,CH3,CH3N,使用时,需要将CH1=1,CH1N=0,CH2=0,CH2N=1,到这里都可以实现,但是下面的CH3=1,CH3N=1,不会实现,同时为0倒是会,请问怎么去配置呢?
初始化代码如下:voidTIM8_Configuration(void) //72M{ TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDefTIM_OCInitStructure; TIM_BDTRInitTypeDefTIM_BDTRInitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8,ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE); TIM_TimeBaseStructure.TIM_Period=99; TIM_TimeBaseStructure.TIM_Prescaler=71; TIM_TimeBaseStructure.TIM_ClockDivision =TIM_CKD_DIV1 ; TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_CenterAligned1; TIM_TimeBaseInit(TIM8,&TIM_TimeBaseStructure); //PWM互补信号输出PC6,PA7 TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1;//PWM输出模式 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_Low; TIM_OCInitStructure.TIM_OCNPolarity=TIM_OCNPolarity_Low; TIM_OCInitStructure.TIM_OCIdleState=TIM_OCIdleState_Set;//死区后输出状态 TIM_OCInitStructure.TIM_OCNIdleState=TIM_OCNIdleState_Reset; TIM_OC1Init(TIM8,&TIM_OCInitStructure); TIM_OC1PreloadConfig(TIM8,TIM_OCPreload_Enable);//PWM输出口PB13 TIM_OC1FastConfig(TIM8,TIM_OCFast_Enable); //PWM互补信号输出PC7,PB0 TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1; 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_Low; TIM_OCInitStructure.TIM_OCNPolarity=TIM_OCNPolarity_Low; TIM_OCInitStructure.TIM_OCIdleState=TIM_OCIdleState_Set;//死区后输出状态 TIM_OCInitStructure.TIM_OCNIdleState=TIM_OCNIdleState_Reset; TIM_OC2Init(TIM8,&TIM_OCInitStructure); TIM_OC2PreloadConfig(TIM8,TIM_OCPreload_Enable);//PWM输出口PB14 TIM_OC2FastConfig(TIM8,TIM_OCFast_Enable); //PWM互补信号输出PC8,PB1 TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1; 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_Low; TIM_OCInitStructure.TIM_OCNPolarity=TIM_OCNPolarity_Low; TIM_OCInitStructure.TIM_OCIdleState=TIM_OCIdleState_Set;//死区后输出状态 TIM_OCInitStructure.TIM_OCNIdleState=TIM_OCNIdleState_Reset; TIM_OC3Init(TIM8,&TIM_OCInitStructure); TIM_OC3PreloadConfig(TIM8,TIM_OCPreload_Enable);//PWM输出口PB14 TIM_OC3FastConfig(TIM8,TIM_OCFast_Enable); //死区设置 TIM_BDTRInitStructure.TIM_OSSRState =TIM_OSSRState_Disable;//使能 TIM_BDTRInitStructure.TIM_OSSIState =TIM_OSSIState_Disable;//使能 TIM_BDTRInitStructure.TIM_LOCKLevel =TIM_LOCKLevel_OFF;//不锁任何位 TIM_BDTRInitStructure.TIM_DeadTime =0xCF;//死区设置为5us TIM_BDTRInitStructure.TIM_Break =TIM_Break_Disable;//使能TIM刹车输入 TIM_BDTRInitStructure.TIM_BreakPolarity=TIM_BreakPolarity_High;//TIM刹车输入管脚为高 TIM_BDTRInitStructure.TIM_AutomaticOutput=TIM_AutomaticOutput_Enable;//自动输出功能使能 TIM_BDTRConfig(TIM8,&TIM_BDTRInitStructure); //TIM_OCInitStructure.TIM_OCNIdleState=TIM_OCIdleState_Reset; TIM_ARRPreloadConfig(TIM8,ENABLE); //TIM_CCPreloadControl(TIM8,ENABLE); TIM_CtrlPWMOutputs(TIM8,ENABLE); TIM_Cmd(TIM8,ENABLE);}主函数如下:intmain(void){ RCC_Configuration();//时钟配置 TIM8_Configuration(); //TIM8初始化 while(1) { TIM8->CCR1=60; //TIM_CCxCmd(TIM8,TIM_Channel_1,TIM_CCx_Disable); //TIM_CCxNCmd(TIM8,TIM_Channel_1,TIM_CCxN_Disable); //TIM_SetCompare2(TIM8,60); //TIM8->CCR2=60; //TIM8->CCR3=60; //TIM_CCxCmd(TIM8,TIM_Channel_2,TIM_CCx_Enable); TIM_CCxNCmd(TIM8,TIM_Channel_2,TIM_CCxN_Enable); TIM_ForcedOC2Config(TIM8,TIM_ForcedAction_InActive); }}此时使用的是通道CH1和CH2两组互补波形,(示波器是4通道),波形如下图所示:CH1对应TIM8-CH1,CH2对应TIM8-CH1N,CH3对应TIM8-CH2,CH4对应TIM8-CH2N。从图上可以看到,TIM8-CH2和TIM8-CH2N没有同时指高,请问怎么设置?
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
原子哥帮我看看怎么改,我改改,再把结果说一下哇,还试过不停在while中初始化,但是那样,初始化有影响
* 定时器1产生2路互补的PWM波(频率=pfreq / (psc+1))
* channel1 ,channel2 -->A.8, A.9,A.10
channel1N,channel2N-->B.13, B.14 B.15(互补)
* TimerPeriod --> 自动重装载周期值
* ChannelxPulse --> 占空周期值
******************************************************/
/*******************************************************
* 定时器1产生2路互补的PWM波(频率=pfreq / (psc+1))
* channel1 ,channel2 -->A.8, A.9,A.10
channel1N,channel2N-->B.13, B.14 B.15(互补)
* TimerPeriod --> 自动重装载周期值
* ChannelxPulse --> 占空周期值
******************************************************/
void TIM1_PWM_Init(unsigned int pfreq,unsigned int psc) //pfreq为不分频时的PWM频率,psc为预分频值
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure; //输出通道配置
TIM_BDTRInitTypeDef TIM_BDTRInitStructure; //死区和刹车配置
TimerPeriod = (SystemCoreClock / pfreq) - 1; //自动重装载周期值
/* ChannelxPulse = DutyCycle * (TIM1_Period - 1) / 100 */
Channel1Pulse = (u16)((u32)(50 * (TimerPeriod - 1)) / 100 ); //占空比50%
Channel2Pulse = (u16)((u32)(50 * (TimerPeriod - 1)) / 100 ); //占空比50%
Channel3Pulse = (u16)((u32)(50 * (TimerPeriod - 1)) / 100 ); //占空比50%
/* 使能TIM1,GPIOA,GPIOB,GPIOE */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOE, ENABLE);
/*channel1 ,channel2 -->A.8,A.9,A.10*
*channel1N,channel2N-->B.13,B.14,B.15*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* 初始化TIM1 */
TIM_TimeBaseStructure.TIM_Period = TimerPeriod; //设置重装载周期值
TIM_TimeBaseStructure.TIM_Prescaler = psc; //设置预分频值
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //时钟分频因子,仅与输入捕获有关(定时器与滤波器的频率比)
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//TIM向上计数模式
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0; //重复溢出中断
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); //初始化定时器基本配置
/* Channel_1 TIM_OCMode_PWM1模式 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //在向上计数时,一旦TIMx_CNT<TIMx_CCR1时通道1为有效电平
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; //PWM互补输出使能
TIM_OCInitStructure.TIM_Pulse = Channel1Pulse; //占空比 = TIM_Pulse/TIM_Period;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //有效电平为高电平
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High; //互补PWM极性
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set; //输出空闲状态
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Reset; //PWM互补输出空闲状态
TIM_OC1Init(TIM1, &TIM_OCInitStructure); //根据指定的参数初始化外设TIM1 OC1
/* Channel_2 TIM_OCMode_PWM1模式 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //在向上计数时,一旦TIMx_CNT>TIMx_CCR1时通道1为有效电平
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; //PWM互补输出使能
TIM_OCInitStructure.TIM_Pulse = Channel2Pulse; //占空比 = TIM_Pulse/TIM_Period;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //有效电平为高电平
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High; //互补PWM极性
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set; //输出空闲状态
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Reset; //PWM互补输出空闲状态
TIM_OC2Init(TIM1, &TIM_OCInitStructure); //根据指定的参数初始化外设TIM1 OC2
/* Channel_3 TIM_OCMode_PWM1模式 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //在向上计数时,一旦TIMx_CNT>TIMx_CCR1时通道1为有效电平
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; //PWM互补输出使能
TIM_OCInitStructure.TIM_Pulse = Channel3Pulse; //占空比 = TIM_Pulse/TIM_Period;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //有效电平为高电平
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High; //互补PWM极性
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set; //输出空闲状态
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Reset; //PWM互补输出空闲状态
TIM_OC3Init(TIM1, &TIM_OCInitStructure); //根据指定的参数初始化外设TIM1 OC3
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable); //使能TIM3在CCR1上的预装载寄存器
TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable); //使能TIM3在CCR2上的预装载寄存器
TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable); //使能TIM3在CCR2上的预装载寄存器
/*死区和刹车功能配置*/
TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable;
TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable;
TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_1;
TIM_BDTRInitStructure.TIM_DeadTime = 0x2F; //设置TIM1_BDTR的DTG[7:0]
TIM_BDTRInitStructure.TIM_Break = TIM_Break_Disable;
TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_High;
TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable;
TIM_BDTRConfig(TIM1, &TIM_BDTRInitStructure);
TIM_Cmd(TIM1, ENABLE); //使能TIM1
TIM_CtrlPWMOutputs(TIM1,ENABLE); //PWM输出使能
}
/*******************************************************
* 定时器8产生2路互补的PWM波(频率=pfreq / (psc+1))
* channel1 ,channel2 -->C.6, C.7,C.8
channel1N,channel2N-->A.7, B.0,B.1 (互补)
* TimerPeriod --> 自动重装载周期值
* ChannelxPulse --> 占空周期值
******************************************************/
void TIM8_PWM_Init(unsigned int pfreq ,unsigned int psc) //pfreq为不分频时的PWM频率,psc为预分频值
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure; //输出通道配置
TIM_BDTRInitTypeDef TIM_BDTRInitStructure; //死区和刹车配置
TimerPeriod1 = (SystemCoreClock / pfreq) - 1; //自动重装载周期值
/* ChannelxPulse = DutyCycle * (TIM8_Period - 1) / 100 */
Channe8_1Pulse = (unsigned int)((unsigned short int)(50 * (TimerPeriod - 1)) / 100 ); //占空比50%
Channe8_2Pulse = (unsigned int)((unsigned short int)(50 * (TimerPeriod - 1)) / 100 ); //占空比50%
Channe8_3Pulse = (unsigned int)((unsigned short int)(50 * (TimerPeriod - 1)) / 100 ); //占空比50%
/* 使能TIM1,GPIOA,GPIOB,GPIOE */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOC, ENABLE);
/* channel1 ,channel2 -->C.6, C.7,C.8
channel1N,channel2N-->A.7, B.0,B.1 (互补)*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* 初始化TIM1 */
TIM_TimeBaseStructure.TIM_Period = TimerPeriod1; //设置重装载周期值
TIM_TimeBaseStructure.TIM_Prescaler = psc; //设置预分频值
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //时钟分频因子,仅与输入捕获有关(定时器与滤波器的频率比)
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//TIM向上计数模式
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0; //重复溢出中断
TIM_TimeBaseInit(TIM8, &TIM_TimeBaseStructure); //初始化定时器基本配置
/* Channel_1 TIM_OCMode_PWM1模式 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //在向上计数时,一旦TIMx_CNT<TIMx_CCR1时通道1为有效电平
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; //PWM互补输出使能
TIM_OCInitStructure.TIM_Pulse = Channe8_1Pulse; //占空比 = TIM_Pulse/TIM_Period;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //有效电平为高电平
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High; //互补PWM极性
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set; //输出空闲状态
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Reset; //PWM互补输出空闲状态
TIM_OC1Init(TIM8, &TIM_OCInitStructure); //根据指定的参数初始化外设TIM1 OC1
/* Channel_2 TIM_OCMode_PWM1模式 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //在向上计数时,一旦TIMx_CNT>TIMx_CCR1时通道1为有效电平
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; //PWM互补输出使能
TIM_OCInitStructure.TIM_Pulse = Channe8_2Pulse; //占空比 = TIM_Pulse/TIM_Period;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //有效电平为高电平
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High; //互补PWM极性
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set; //输出空闲状态
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Reset; //PWM互补输出空闲状态
TIM_OC2Init(TIM8, &TIM_OCInitStructure); //根据指定的参数初始化外设TIM1 OC2
/* Channel_3 TIM_OCMode_PWM1模式 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //在向上计数时,一旦TIMx_CNT>TIMx_CCR1时通道1为有效电平
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; //PWM互补输出使能
TIM_OCInitStructure.TIM_Pulse = Channe8_3Pulse; //占空比 = TIM_Pulse/TIM_Period;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //有效电平为高电平
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High; //互补PWM极性
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set; //输出空闲状态
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Reset; //PWM互补输出空闲状态
TIM_OC3Init(TIM8, &TIM_OCInitStructure); //根据指定的参数初始化外设TIM1 OC3
TIM_OC1PreloadConfig(TIM8, TIM_OCPreload_Enable); //使能TIM2在CCR1上的预装载寄存器
TIM_OC2PreloadConfig(TIM8, TIM_OCPreload_Enable); //使能TIM2在CCR2上的预装载寄存器
TIM_OC3PreloadConfig(TIM8, TIM_OCPreload_Enable); //使能TIM2在CCR2上的预装载寄存器
/*死区和刹车功能配置*/
TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable;
TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable;
TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_1;
TIM_BDTRInitStructure.TIM_DeadTime = 0x2F; //设置TIM1_BDTR的DTG[7:0]
TIM_BDTRInitStructure.TIM_Break = TIM_Break_Disable;
TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_High;
TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable;
TIM_BDTRConfig(TIM8, &TIM_BDTRInitStructure);
TIM_Cmd(TIM8, ENABLE); //使能TIM1
TIM_CtrlPWMOutputs(TIM8,ENABLE); //PWM输出使能
}
一周热门 更多>