TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_BDTRInitTypeDef TIM_BDTRInitStructure ;
/* PWM信号电平跳变值 */
u16 CCR1_Val = hBuffCH1[3];
u16 CCR4_Val = hBuffCH4[3];
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = 999; //当定时器从0计数到9999,即为1000次,为一个定时周期
TIM_TimeBaseStructure.TIM_Prescaler = 71; //设置预分频:不预分频,即为72MHz
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1 ; //设置时钟分频系数:不分频
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_CenterAligned3; //上顶点与下低点 触发比较中断标志位
TIM_TimeBaseStructure.TIM_RepetitionCounter = 1 ;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
/* PWM1 Mode configuration: Channel1 */
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 = CCR1_Val; //设置跳变值,当计数器计数到这个值时,电平发生跳变
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low ;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset ;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
TIM_OC1Init(TIM1, &TIM_OCInitStructure); //使能通道1
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //配置为PWM模式1
TIM_OCInitStructure.TIM_Pulse = 400; //设置通道4的电平跳变值,输出另外一个占空比的PWM
TIM_OC2Init(TIM1, &TIM_OCInitStructure); //使能通道4
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //配置为PWM模式1
TIM_OCInitStructure.TIM_Pulse = 200; //设置通道4的电平跳变值,输出另外一个占空比的PWM
TIM_OC3Init(TIM1, &TIM_OCInitStructure); //使能通道4
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //配置为PWM模式1
TIM_OCInitStructure.TIM_Pulse = CCR4_Val; //设置通道4的电平跳变值,输出另外一个占空比的PWM
TIM_OC4Init(TIM1, &TIM_OCInitStructure); //使能通道4
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Disable);
TIM_OC4PreloadConfig(TIM1, TIM_OCPreload_Disable);
/* Automatic Output enable, Break, dead time and lock configuration*/
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 = 20;
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_ITConfig(TIM1,TIM_IT_Update,ENABLE);
PWMNVIC_Init();
}
void PWMNVIC_Init(void)
{
NVIC_InitTypeDef NVIC_InitStructure ;
NVIC_InitStructure.NVIC_IRQChannel= TIM1_UP_IRQn; //更新中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority= 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority= 2;
NVIC_InitStructure.NVIC_IRQChannelCmd= ENABLE;
NVIC_Init( &NVIC_InitStructure );
}
it.c函数
void TIM1_UP_IRQHandler(void)
{
if(TIM_GetFlagStatus(TIM1, TIM_FLAG_Update) == SET)
{
TIM_ClearFlag(TIM1,TIM_FLAG_Update);
}
}
-----------------
我想实现的是PWM计数器在底部的时候触发一中断,实际是顶部触发一次中断,底部触发一次中断,即更新就触发一次中断。我设置了TIM_TimeBaseStructure.TIM_RepetitionCounter = 1,可能不只是这个问题,所以不懂,请教下
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>