问题如题啊,TIM3库代码,用在STM32F103C8上,TIM2,TIM3都好用,改为TIM4就出问题了,进入不了中断,求解呢?
代码如下:
void TIM3_NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void TIM3_Configuration(unsigned char timedata)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3 , ENABLE);
TIM_DeInit(TIM3);
TIM_TimeBaseStructure.TIM_Period=10000/timedata;//(10000-timedata);//5*timedata; /* 自动重装载寄存器周期的值(计数值)介于0x0000到0xffff之间 */
/* 累计 TIM_Period个频率后产生一个更新或者中断 */
TIM_TimeBaseStructure.TIM_Prescaler= (72 - 1); /* 时钟预分频数 72M/72 介于0x0000到0xffff之间 */
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1; /* 采样分频 */
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; /* 向上计数模式 */
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
TIM_ClearFlag(TIM3, TIM_FLAG_Update); /* 清除溢出中断标志 */
TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);
TIM_Cmd(TIM3, ENABLE); /* 开启时钟 */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3 , DISABLE); /*先关闭等待使用*/
}
中断函数:
void TIM3_IRQHandler(void)
{ if ( TIM_GetITStatus(TIM3 , TIM_IT_Update) != RESET )
{
TIM_ClearITPendingBit(TIM3 , TIM_FLAG_Update);
tim3_factor++;
GPIO_ResetBits(GPIOB,GPIO_Pin_0 |GPIO_Pin_1);
}
if(tim3_factor==100) tim3_factor=0; }
Main中:
TIM3_NVIC_Configuration();
TIM3_Configuration(50);
START_TIME3;
TIM2,TIM3都好用,改为TIM4就出问题了,TIM4不工作,求解~
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
void Timerx_Init(u16 arr,u16 psc)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); //时钟使能
TIM_TimeBaseStructure.TIM_Period = 5000; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值 计数到5000为500ms
TIM_TimeBaseStructure.TIM_Prescaler =(7200-1); //设置用来作为TIMx时钟频率除数的预分频值 10Khz的计数频率
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
TIM_ITConfig( //使能或者失能指定的TIM中断
TIM4, //TIM2
TIM_IT_Update | //TIM 中断源
TIM_IT_Trigger, //TIM 触发中断源
ENABLE //使能
);
NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn; //TIM3中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //先占优先级0级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //从优先级3级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
NVIC_Init(&NVIC_InitStructure); //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器
TIM_Cmd(TIM4, ENABLE); //使能TIMx外设
}
TIM4_IRQHandler
---------------------------------
改了
---------------------------------
那可以仿真看下寄存器状态了.
一周热门 更多>