计数器模式
原子的链接:http://www.openedv.com/posts/list/11832.htm
TIM2-TIM5可以由向上计数、向下计数、向上向下双向计数。向上计数模式中,计数器从0计数到自动加载值(TIMx_ARR计数器内容),然后重新从0开始计数并且产生一个计数器溢出事件。在向下模式中,计数器从自动装入的值(TIMx_ARR)开始向下计数到0,然后从自动装入的值重新开始,并产生一个计数器向下溢出事件。而中央对齐模式(向上/向下计数)是计数器从0开始计数到自动装入的值-1,产生一个计数器溢出事件,然后向下计数到1并且产生一个计数器溢出事件;然后再从0开始重新计数
中断时间:自动加载值/(主频/预分频)
先设置TIM3 向上计数 1秒1次中断,再设置TIM4向下计数1/4秒中断一次,在中断函数中打印counter,中断函数中不推荐使用printf
Timer3_Config
1 void Timer3_Config(void) 2 { 3 TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; 4 NVIC_InitTypeDef NVIC_InitStructure; 5 6 /* TIM3 clock enable */ 7 RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); 8 9 /* --------------------------------------------------------------- 10 TIM3 Configuration: Output Compare Timing Mode: 11 TIM2CLK = 36 MHz, Prescaler = 7200, TIM2 counter clock = 7.2 MHz 12 --------------------------------------------------------------- */ 13 14 /* Time base configuration */ 15 //这个就是自动装载的计数值,由于计数是从0开始的,计数10000次后为9999 16 TIM_TimeBaseStructure.TIM_Period = (10000 - 1); 17 // 这个就是预分频系数,当由于为0时表示不分频所以要减1 18 TIM_TimeBaseStructure.TIM_Prescaler = (7200 - 1); 19 // 高级应用本次不涉及。定义在定时器时钟(CK_INT)频率与数字滤波器(ETR,TIx) 20 // 使用的采样频率之间的分频比例 21 TIM_TimeBaseStructure.TIM_ClockDivision = 0; 22 //向上计数 23 TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; 24 //初始化定时器3 25 TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); 26 27 /* Clear TIM3 update pending flag[清除TIM5溢出中断标志] */ 28 TIM_ClearITPendingBit(TIM3, TIM_IT_Update); 29 30 /* TIM IT enable */ //打开溢出中断 31 TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE); 32 33 /* TIM3 enable counter */ 34 TIM_Cmd(TIM3, ENABLE); //计数器使能,开始工作 35 36 37 38 /* Set the Vector Table base address at 0x08000000 */ 39 NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0000); 40 41 /* Enable the TIM5 gloabal Interrupt */ 42 NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; 43 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; 44 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; 45 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; 46 47 NVIC_Init(&NVIC_InitStructure); 48 49 }
Timer4_Config
1 void Timer4_Config(void) 2 { 3 4 TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; 5 NVIC_InitTypeDef NVIC_InitStructure; 6 7 /* TIM3 clock enable */ 8 RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); 9 10 /* --------------------------------------------------------------- 11 TIM3 Configuration: Output Compare Timing Mode: 12 TIM2CLK = 36 MHz, Prescaler = 7200, TIM2 counter clock = 7.2 MHz 13 --------------------------------------------------------------- */ 14 15 /* Time base configuration */ 16 //这个就是自动装载的计数值,由于计数是从0开始的,计数10000次后为9999 除以四,1/4秒中断 17 TIM_TimeBaseStructure.TIM_Period = (10000/4 - 1); 18 // 这个就是预分频系数,当由于为0时表示不分频所以要减1 19 TIM_TimeBaseStructure.TIM_Prescaler = (7200 - 1); 20 // 高级应用本次不涉及。定义在定时器时钟(CK_INT)频率与数字滤波器(ETR,TIx) 21 // 使用的采样频率之间的分频比例 22 TIM_TimeBaseStructure.TIM_ClockDivision = 0; 23 //向上计数 24 TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Down; 25 //初始化定时器4 26 TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); 27 28 /* Clear TIM4 update pending flag[清除TIM4溢出中断标志] */ 29 TIM_ClearITPendingBit(TIM4, TIM_IT_Update); 30 31 /* TIM IT enable */ //打开溢出中断 32 TIM_ITConfig(TIM4, TIM_IT_Update, ENABLE); 33 34 /* TIM4 enable counter */ 35 TIM_Cmd(TIM4, ENABLE); //计数器使能,开始工作 36 37 38 39 /* Set the Vector Table base address at 0x08000000 */ 40 NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0000); 41 42 /* Enable the TIM5 gloabal Interrupt */ 43 NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn; 44 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; 45 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; 46 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; 47 48 NVIC_Init(&NVIC_InitStructure); 49 50 51 }
TIM3_IRQHandler
1 void TIM3_IRQHandler(void) 2 { 3 static u32 counter3 = 0; 4 if(TIM_GetITStatus(TIM3,TIM_IT_Update)!=RESET) 5 { 6 TIM_ClearITPendingBit(TIM3,TIM_IT_Update); 7 } 8 printf("Allen:-->TIM3:%d\n\r", counter3++); 9 }
TIM4_IRQHandler
1 void TIM4_IRQHandler(void) 2 { 3 static u32 counter4 = 0; 4 if(TIM_GetITStatus(TIM4,TIM_IT_Update)!=RESET) 5 { 6 TIM_ClearITPendingBit(TIM4,TIM_IT_Update); 7 } 8 printf("Allen:-->TIM4:%d\n\r", counter4++);; 9 10 }