TIM2~TIM4我都用了,现在还需要用一个计时器,但是只要我在定时器初始化加上TIM1的初始化,还没写中断函数,其他程序就什么都运行不了了,想请问大佬们是为什么呢 #include "stm32f10x.h" // Device headervoid Timer_Init(void){ //超声波的定时器 RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); //启用TIM4时钟 TIM_InternalClockConfig(TIM4); //设置TIM4使用内部时钟,其实就算不写也默认用内部时钟 TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure4; //定义结构体,配置定时器 TIM_TimeBaseInitStructure4.TIM_ClockDivision = TIM_CKD_DIV1; //设置1分频(不分频) TIM_TimeBaseInitStructure4.TIM_CounterMode = TIM_CounterMode_Up; //设置计数模式为向上计数 TIM_TimeBaseInitStructure4.TIM_Period = 10 - 1; //ARR 自动重装器的值 (0~65535) //计数器溢出频率CK_CNT_OV= TIM_TimeBaseInitStructure4.TIM_Prescaler = 72 - 1; //PSC 预分频器的值 (0~65535) //=CK_PSC/(PSC+1)/(ARR+1) TIM_TimeBaseInitStructure4.TIM_RepetitionCounter = 0; //重复计数器(高级定时器才有) TIM_TimeBaseInit(TIM4, &TIM_TimeBaseInitStructure4); //时基单元配置 TIM_ClearFlag(TIM4, TIM_FLAG_Update); //清除更新中断标志位 TIM_ITConfig(TIM4, TIM_IT_Update, ENABLE); //开启更新中断,开启了更新中断到NVIC的通路 NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置中断优先级分组 NVIC_InitTypeDef NVIC_InitStructure4; //定义结构体,配置中断优先级 NVIC_InitStructure4.NVIC_IRQChannel = TIM4_IRQn; //指定中断通道(定时器在NVIC里的通道 NVIC_InitStructure4.NVIC_IRQChannelCmd = ENABLE; //中断使能 NVIC_InitStructure4.NVIC_IRQChannelPreemptionPriority = 2; //抢占优先级 NVIC_InitStructure4.NVIC_IRQChannelSubPriority = 1; //响应优先级 NVIC_Init(&NVIC_InitStructure4); TIM_Cmd(TIM4, ENABLE); //开启定时器 //人体红外的定时器 RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);//开启APB1总线上的APB1的时钟 TIM_InternalClockConfig(TIM3);//调用使用内部时钟,其实就算不写也默认用内部时钟 TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure3; TIM_TimeBaseInitStructure3.TIM_ClockDivision = TIM_CKD_DIV1;//1分频 TIM_TimeBaseInitStructure3.TIM_CounterMode = TIM_CounterMode_Up;//向上计数模式 TIM_TimeBaseInitStructure3.TIM_Period = 10000-1;//ARR 自动重装器的值 (0~65535) //计数器溢出频率CK_CNT_OV= TIM_TimeBaseInitStructure3.TIM_Prescaler = 7200-1;//PSC 预分频器的值 (0~65535) //=CK_PSC/(PSC+1)/(ARR+1) TIM_TimeBaseInitStructure3.TIM_RepetitionCounter = 0;//重复计数器的值 TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStructure3); //时基单元配置 TIM_ClearFlag(TIM3,TIM_FLAG_Update);//避免刚初始化完就立刻进入中断 TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);//开启了更新中断到NVIC的通路 NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); NVIC_InitTypeDef NVIC_InitStructure3; NVIC_InitStructure3.NVIC_IRQChannel = TIM3_IRQn;//定时器2在NVIC里的通道 NVIC_InitStructure3.NVIC_IRQChannelCmd = ENABLE; NVIC_InitStructure3.NVIC_IRQChannelPreemptionPriority = 1;//抢占优先级 NVIC_InitStructure3.NVIC_IRQChannelSubPriority = 2;//响应优先级 NVIC_Init(&NVIC_InitStructure3); TIM_Cmd(TIM3 , ENABLE);//启动定时器}/* //称重的定时器 RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);//开启APB2总线上的APB1的时钟 TIM_InternalClockConfig(TIM1); //设置TIM1使用内部时钟,其实就算不写也默认用内部时钟 TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure1; //定义结构体,配置定时器 TIM_TimeBaseInitStructure1.TIM_ClockDivision = TIM_CKD_DIV1; //设置1分频(不分频) TIM_TimeBaseInitStructure1.TIM_CounterMode = TIM_CounterMode_Up;//设置计数模式为向上计数 TIM_TimeBaseInitStructure1.TIM_Period = 10000-1; //ARR 自动重装器的值 (0~65535) //计数器溢出频率CK_CNT_OV= TIM_TimeBaseInitStructure1.TIM_Prescaler = 7200-1; //PSC 预分频器的值 (0~65535) //=CK_PSC/(PSC+1)/(ARR+1) TIM_TimeBaseInitStructure1.TIM_RepetitionCounter = 0; //重复计数器(高级定时器才有) TIM_TimeBaseInit(TIM1, &TIM_TimeBaseInitStructure1); //时基单元配置 TIM_ClearFlag(TIM1, TIM_FLAG_Update); //清除更新中断标志位 TIM_ITConfig(TIM1, TIM_IT_Update | TIM_IT_Trigger, ENABLE); //开启更新中断,开启了更新中断到NVIC的通路 NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); NVIC_InitTypeDef NVIC_InitStructure1; NVIC_InitStructure1.NVIC_IRQChannel = TIM1_UP_IRQn;//定时器2在NVIC里的通道 NVIC_InitStructure1.NVIC_IRQChannelCmd = ENABLE; NVIC_InitStructure1.NVIC_IRQChannelPreemptionPriority = 3;//抢占优先级 NVIC_InitStructure1.NVIC_IRQChannelSubPriority = 3;//响应优先级 NVIC_Init(&NVIC_InitStructure1); TIM_Cmd(TIM1 , ENABLE);//启动定时器}*///中断函数模板/*void TIM4_IRQHandler(void) //更新中断函数//当定时器产生更新中断时,函数自动被执行{ if (TIM_GetITStatus(TIM4, TIM_IT_Update) == SET) //获取TIM4定时器的更新中断标志位//如果(获取中断标志位==SET),那么可以执行相应地用户代码 { TIM_ClearITPendingBit(TIM4, TIM_IT_Update); //清除更新中断标志位 }}*/
|