//用于数码管计数
TIM_DeInit( TIM2 ); //将TIM寄存器重置为缺省值
TIM_InternalClockConfig( TIM2 ); //SMS =000采用内部时钟给TIM2提供时钟源( TIMxCLK( 72MHz ) -> CK_INT -> CK_PSC -> CK_CNT )
TIM_TimeBaseStructure.TIM_Period=2000; //2000*1/1000=1S
TIM_TimeBaseStructure.TIM_Prescaler=35999; //35999+1/72M=1/2000;
TIM_TimeBaseStructure.TIM_ClockDivision=0;
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2,&TIM_TimeBaseStructure);
TIM_ClearFlag( TIM2, TIM_FLAG_Update ); //清除溢出中断标志位
TIM_ARRPreloadConfig( TIM2, DISABLE ); //禁止ARR预装载缓冲器
TIM_ITConfig( TIM2,TIM_IT_Update,ENABLE );// 打开更新事件中断
TIM_Cmd(TIM2,ENABLE); //使能TIM2
//数码管扫描
TIM_DeInit( TIM6 ); //将TIM6寄存器重置为缺省值
TIM_InternalClockConfig( TIM6 );
TIM_TimeBaseStructure.TIM_Period=10; //扫描周期5ms
TIM_TimeBaseStructure.TIM_Prescaler=35999;
TIM_TimeBaseStructure.TIM_ClockDivision=0;
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM6,&TIM_TimeBaseStructure);
TIM_ClearFlag( TIM6, TIM_FLAG_Update ); //清除溢出中断标志位
TIM_ARRPreloadConfig( TIM6, DISABLE ); //禁止ARR预装载缓冲器
TIM_ITConfig( TIM6,TIM_IT_Update,ENABLE );// 打开更新事件中断
TIM_Cmd(TIM6,ENABLE); //使能TIM6
//温度采集
TIM_DeInit(TIM5);
TIM_TimeBaseStructure.TIM_Period=300; //150ms
TIM_TimeBaseStructure.TIM_Prescaler=35999;
TIM_TimeBaseStructure.TIM_ClockDivision=0;
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM5,&TIM_TimeBaseStructure);
TIM_ClearFlag(TIM5, TIM_FLAG_Update );
TIM_ITConfig(TIM5,TIM_IT_Update,ENABLE );
TIM_Cmd(TIM5,ENABLE);
除此以外我PWM3用了ch2和ch4控制电机转速。
我试了下,定时器4和定时器5无论哪个使用,我用delay_ms时候,都会使原有的时间变短许多。
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
void NVIC_Cfg()
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig( NVIC_PriorityGroup_2 ); //选择中断分组2
//选择TIM2的中断通道 计数
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //抢断式中断优先级设置为1
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //响应式中断优先级设置为0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能中断
NVIC_Init( &NVIC_InitStructure ); //将配置应用到NVIC中
//选择TIM6的中断通道 数码管
NVIC_InitStructure.NVIC_IRQChannel = TIM6_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //抢断式中断优先级设置为1
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //响应式中断优先级设置为0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能中断
NVIC_Init( &NVIC_InitStructure ); //将配置应用到NVIC中
//选择TIM7的中断通道 按键
NVIC_InitStructure.NVIC_IRQChannel = TIM7_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init( &NVIC_InitStructure );
//PWM
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init( &NVIC_InitStructure );
//温度
NVIC_InitStructure.NVIC_IRQChannel =TIM5_IRQn ;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init( &NVIC_InitStructure );
}
中断服务函数应该是没问题的。我就多加了个传递ds18b20温度数据。只要加了TIM5和TIM4就会出现延时不正确
滴答定时器没写中断服务函数
我现在只能换成软件延时来解决
一周热门 更多>