这是STM32的定时器程序
单片机源程序如下:
- #include "SoftTimer.h"
- static Timer_typedef TimerList[10];
- void TIMER_TimerInitialisation(void)
- {
- u8 i = 0;
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
-
- TIM_DeInit(TIM2);
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
-
- TIM_TimeBaseStructure.TIM_Period = 2; // 最大计数值,根据设定的分频,time=1即为0.5ms
- TIM_TimeBaseStructure.TIM_Prescaler = 36000-1; // 分频36000
- TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; // 时钟分割
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; // 计数方向向上计数
- TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
- TIM_SetAutoreload(TIM2, 2);
- TIM_ARRPreloadConfig(TIM2, ENABLE);
- TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
- TIM_Cmd(TIM2, ENABLE);
-
- for(i = 0; i < 10; i++)
- {
- TimerList[i].Timeoutcnt = 1000001;
- TimerList[i].Timeout = 1000001;
- TimerList[i].Timeoutfuc = (void*)0;
- TimerList[i].Parameter = (void*)0;
- }
- }
- void TIMER_TimerStart(u8 TimerIdent, u32 TimeOut, void (*Timeoutfuc)(void* parameter), void* parameter, u8 flag)
- {
- if(TimerIdent > 9)
- {
- return;
- }
-
- __disable_irq();
-
- TimerList[TimerIdent].Timeoutcnt = TimeOut;
- TimerList[TimerIdent].Timeout = TimeOut;
- TimerList[TimerIdent].Timeoutfuc = Timeoutfuc;
- TimerList[TimerIdent].Parameter = parameter;
- TimerList[TimerIdent].Timerflag = flag;
-
- __enable_irq();
- }
- void TIMER_Execute(void)
- {
- u8 i = 0;
- for(i = 0; i < 10; i++)
- {
- if((TimerList[i].Timeoutcnt != 0) && (TimerList[i].Timeoutcnt <= 1000000))
- {
- TimerList[i].Timeoutcnt--;
- if(TimerList[i].Timeoutcnt == 0)
- {
- if(TimerList[i].Timerflag != TIMER_PERIOD)
- {
- TimerList[i].Timeoutcnt = 1000001;
- }
- else
- {
- TimerList[i].Timeoutcnt = TimerList[i].Timeout;
- }
- TimerList[i].Timeoutfuc(TimerList[i].Parameter);
- }
- }
- }
- }
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
进阶七 软件定时器.zip
(1.53 KB, 下载次数: 31)
|