1.间隔x秒执行1次
if(Get_RunTime - led_time > 150)//间隔1.5秒,
{
led_time = Get_RunTime;
LED = !LED;
//需要定时执行的函数
}
2.间隔x秒执行1次
if (IS_TIMEOUT_10MS(eTimLed, 80))//间隔0.8秒,
{
LED = !LED;
//需要定时执行的函数
}
3.次数,开时间,关时间(控制LED或BEEP)
SetLed(0xff, 100, 100);//连续,间隔1秒,
SetLed(5, 5, 300);//3次,开50毫秒,关3秒,
bsp.c 文件
static u16 timer_on = 0,timer_off = 0;
static u16 nCnt = 0;
static u8 nNum = 0;
//次数,开时间,关时间(单位10ms),其中num=0xff为连续
void SetLed(u8 num,u16 on_time,u16 off_time)
{
nNum = num;
timer_on = on_time;
timer_off = off_time;
SetLedPower(0);
nCnt = 0;
}
void Led_10mS_Conut(void)//定期器10ms调度任务
{
if(nCnt)
{
nCnt--;
if(nCnt == timer_off)
SetLedPower(0);
}
else
{
if(nNum == 0XFF)
{
SetLedPower(1);
nCnt = timer_on + timer_off;
}
else if(nNum)
{
SetLedPower(1);
nNum --;
nCnt = timer_on + timer_off;
}
}
}
void SetLedPower(u8 high)//接口
{
if(high)
LED = 1;
else
LED = 0;
}
BspTime2.h 文件
#ifndef _BSPTIME2_h_
#define _BSPTIME2_h_
#include <stm32f10x.h>
enum {
eTimLed,
eTimNrf,
eTim3,
eTim4,
eTimMax,
};
//如果index对应的时间超时,清零并返回1,未超时返回0
#define IS_TIMEOUT_10MS(index, count) ((g_Tim2Array[(u16)(index)] >= (count))?
((g_Tim2Array[(u16)(index)] = 0) == 0): 0)
extern volatile int g_Tim2Array[(u16)eTimMax];
void BspTim2Init(void);
u32 Get_RunTime(void);
#endif
BspTime2.c 文件
volatile int g_Tim2Array[(u16)eTimMax] = {0};
volatile u32 sys_time=0;
//通用定时器初始化,
void BspTim2Init(void)
{
NVIC_InitTypeDef NvicInitdef;
TIM_TimeBaseInitTypeDef timbase;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
TIM_DeInit(TIM2);
timbase.TIM_CounterMode = TIM_CounterMode_Up;
timbase.TIM_ClockDivision = TIM_CKD_DIV1;
timbase.TIM_Period = 10000 - 1;//1000=1ms;10000=10ms
timbase.TIM_Prescaler = 72 - 1;// 72分频
TIM_TimeBaseInit(TIM2, &timbase);
NvicInitdef.NVIC_IRQChannel = TIM2_IRQn;
NvicInitdef.NVIC_IRQChannelPreemptionPriority = 3;//最低优先级
NvicInitdef.NVIC_IRQChannelSubPriority = 3;
NvicInitdef.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NvicInitdef);
TIM_SetCounter(TIM2, 0);
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
TIM_Cmd(TIM2, ENABLE);
}
//tim2中断函数(10毫秒)
void TIM2_IRQHandler(void)
{
u16 i = 0;
if( SET == TIM_GetITStatus(TIM2,TIM_IT_Update) )
{
TIM_ClearITPendingBit( TIM2, TIM_IT_Update);
Led_10mS_Conut();
sys_time++;
if(Timerx)
{
Timerx--;
}
for (i = 0; i < (u16)eTimMax; i++)
{
g_Tim2Array[i]++;
}
}
}
u32 Get_RunTime(void)
{
return sys_time;
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>