配置STM32CubeMX如下
生成的Keil代码的创建启动定时器如下
/* Create the timer(s) */ /* definition and creation of myTimer01 */ osTimerDef(myTimer01, Callback01); myTimer01Handle = osTimerCreate(osTimer(myTimer01), osTimerPeriodic, NULL); /* USER CODE BEGIN RTOS_TIMERS */ /* start timers, add new ones, ... */ osTimerStart(myTimer01Handle,1000);//1秒钟启动一次定时 /* USER CODE END RTOS_TIMERS */
定时器的回调函数如下
/* Callback01 function */ void Callback01(void const * argument) { /* USER CODE BEGIN Callback01 */ static int32_t count = 0; printf( "定时器计数:%d.\r\n",count++); /* USER CODE END Callback01 */ }
运行结果如下,每隔一秒打印一次