高中生
最后登录1970-1-1
在线时间 小时
注册时间2020-7-9
|
想用TIM1生成CH3N周期为216ms且占空比为0.2%,CH2N周期为300us且占空比为50%,而且让CH2N比CH3N滞后75us,有没有大佬帮我看看错哪了
static void TIM1_Mode_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);
/*--------------------中断优先级NVIC设置-------------------------*/
NVIC_InitStructure.NVIC_IRQChannel = TIM1_CC_IRQn; //TIM1捕获比较中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //先占优先级1级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //从优先级1级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
NVIC_Init(&NVIC_InitStructure); //初始化NVIC寄存器
TIM_ITConfig(TIM1, TIM_IT_CC2 | TIM_IT_CC3, ENABLE); //通道2、通道3中断使能
/*--------------------时基结构体初始化-------------------------*/
TIM_TimeBaseStructure.TIM_Period= 10799; //ARR+1=24=1M*T, T=24us?
TIM_TimeBaseStructure.TIM_Prescaler= 71; //配置1M计数
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //配置死区时间
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
TIM_TimeBaseStructure.TIM_RepetitionCounter=0; //重复计数器的值,没用到不用管
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); //初始化TIMx的时间基数单位
/*--------------------输出比较结构体初始化-------------------*/
TIM_OCInitStructure.TIM_OCMode =TIM_OCMode_Toggle; //配置为Toggle模式
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Disable; //输出使能
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;//互补输出使能
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_Set; //互补输出通道空闲电平极性配置
/*--------------------输出比较通道2-------------------------*/
TIM_OCInitStructure.TIM_Pulse = 300;
TIM_OC2Init(TIM1, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable);
/*--------------------输出比较通道3-------------------------*/
TIM_OCInitStructure.TIM_Pulse = 21600;
TIM_OC3Init(TIM1, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable);
// 使能计数器
TIM_Cmd(TIM1, ENABLE);
// 主输出使能,当使用的是通用定时器时,这句不需要
TIM_CtrlPWMOutputs(TIM1, ENABLE);
}
中断服务函数
void TIM1_CC_IRQHandler(void)
{
if (TIM_GetITStatus(TIM1, TIM_IT_CC2) != RESET)
{
u16 capture = 0;
TIM_ClearITPendingBit(TIM1, TIM_IT_CC2 );
capture = TIM_GetCapture2(TIM1);
TIM_SetCompare2(TIM1, capture );
}
if (TIM_GetITStatus(TIM1, TIM_IT_CC3) != RESET)
{
u16 capture = 0;
TIM_ClearITPendingBit(TIM1, TIM_IT_CC3);
capture = TIM_GetCapture3(TIM1);
if(state==0)
{
TIM_SetCompare3(TIM1, capture + 21600* 0.02 );
state=1;
}
else
{
TIM_SetCompare3(TIM1, capture + 21600 * 0.98 );
state=0;
}
}
}
|
|