功能描述:将外部引脚的输入时钟作为定时器的基准时钟。外部方波信号通过PA5引脚输入作为TIM2的外部时钟,定时器在外部时钟模式2下实现计数计时功能。
- main.c文件内容如下
#include "stm32f4xx.h" // Device header #include "OLED.h" #include "Timer.h" uint8_t cnt = 0; int main(void) { Timer_Init(); OLED_Init(); OLED_Clear(); //清屏OLED OLED_Refresh(); //刷新OLED while(1) { OLED_Printf(0,0,8,"cnt = %d",cnt); //在OLED上显示cnt OLED_Refresh(); } } //TIM2中断服务函数 void TIM2_IRQHandler(void) { if(TIM_GetITStatus(TIM2,TIM_IT_Update) == SET) //判断相应的中断挂起标志位是否置SET { cnt++; TIM_ClearITPendingBit(TIM2,TIM_IT_Update); //清除上述中断挂起标志位 } }
- Timer.c文件内容如下
#include "Timer.h" /** * @摘要 初始化TIM * @参数 无 * @返回值 无 * @说明