proteus仿真stm32f103c8定时器定时1秒遇到的问题
void Timer_Init(void) { /*1.开启时钟*/ RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); /*2.选择时基单元时钟*/ TIM_InternalClockConfig(TIM2); /*3.配置时基单元*/ TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure; TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1; TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInitStructure.TIM_Period = 10000 - 1; //ARR, 计时频率=晶振频率/(PSC+1)/(ARR+1) TIM_TimeBaseInitStructure.TIM_Prescaler = 400 - 1; //PSC,预分频器和计数器都有1个数的偏差,所以减一 TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0; TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStructure); /*4.使能更新中断*/ TIM_ClearFlag(TIM2, TIM_FLAG_Update); //解决复位就进入中断+1的状态 TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE); NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); NVIC_InitTypeDef NVIC_InitStructure; NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; NVIC_Init(&NVIC_InitStructure); /*启动定时器*/ TIM_Cmd(TIM2, ENABLE); }
理论上内部72MHz的时钟应该是分频给个7200 - 1 ,,
还没搞懂proteus的stm32是怎么搞的,预分频用的400-1才能实现1S定时, ,估计是程序哪里没有配置倍频?留个棋子后面研究。。
void Timer_Init(void){/*1.开启时钟*/RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);/*2.选择时基单元时钟*/TIM_InternalClockConfig(TIM2);/*3.配置时基单元*/TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;TIM_TimeBaseInitStructure.TIM_Period = 10000 - 1; //ARR, 计时频率=晶振频率/(PSC+1)/(ARR+1)TIM_TimeBaseInitStructure.TIM_Prescaler = 400 - 1; //PSC,预分频器和计数器都有1个数的偏差,所以减一TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStructure);/*4.使能更新中断*/TIM_ClearFlag(TIM2, TIM_FLAG_Update); //解决复位就进入中断+1的状态TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);NVIC_InitTypeDef NVIC_InitStructure;NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;NVIC_Init(&NVIC_InitStructure);/*启动定时器*/TIM_Cmd(TIM2, ENABLE);}