stm32定时器能同时两个怎么配置?怎么配置优先级?
2个回答
展开全部
void timer_init(void)//初始化time1使能计数器
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
//TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
PrescalerValue = (uint16_t) (SystemCoreClock /TIM2_rate) - 1; //设定的是定时器的频率,要设定pwm的频率为50hz要乘以对
//应的arr值
TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue; //TIM_Prescaler 设置了用来作为TIMx时钟频率除数的预分频值
TIM_TimeBaseStructure.TIM_Period = PWM_ARR; //设置了在下一个更新事件装入活动的自动重装载寄存器周期的值
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//向上计数模式
//TIM_BaseInitStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_ClearFlag(TIM2, TIM_FLAG_Update);//清中断,以免一启用中断后立即产生中断
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);//使能TIM1中断源
TIM_ARRPreloadConfig(TIM2, ENABLE);
TIM_Cmd(TIM2, ENABLE);
}
void time_NVIC(void)//time中断初始化
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel=TIM2_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
//TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
PrescalerValue = (uint16_t) (SystemCoreClock /TIM2_rate) - 1; //设定的是定时器的频率,要设定pwm的频率为50hz要乘以对
//应的arr值
TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue; //TIM_Prescaler 设置了用来作为TIMx时钟频率除数的预分频值
TIM_TimeBaseStructure.TIM_Period = PWM_ARR; //设置了在下一个更新事件装入活动的自动重装载寄存器周期的值
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//向上计数模式
//TIM_BaseInitStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_ClearFlag(TIM2, TIM_FLAG_Update);//清中断,以免一启用中断后立即产生中断
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);//使能TIM1中断源
TIM_ARRPreloadConfig(TIM2, ENABLE);
TIM_Cmd(TIM2, ENABLE);
}
void time_NVIC(void)//time中断初始化
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel=TIM2_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询