通过systick,产生多个软定时器
200个100ms
46个10ms
4个1ms
定义
/* 常规100ms,共200点 */
static volatile u16 *pT_100msSetValue,*pT_100msCurrentValue;
void T_100MS(void)
{
u16 timer_count;
//1. 200个100ms软定时轮询
for(timer_count =0;timer_count<200;timer_count++)
{
//2. 目标时间、当前时间
pT_100msSetValue = plc_16BitBuf + 0x0900 + timer_count;
pT_100msCurrentValue =plc_16BitBuf + 0x0800 + timer_count;
//3. 软定时器是否开启
if(PLC_BIT_TEST(0x1600 + timer_count))
{
//4. 到达预定值
if( (*pT_100msCurrentValue) < (*pT_100msSetValue) )
{
//5. 将结果写入对应地址
u16 ret = PLC_BIT_TEST(0x600 + timer_count);
if(!ret)
*pT_100msCurrentValue +=1; // 计时
}
}
}
}
/* 常规10ms,共46点 */
void T_10MS(void)
{
for(timer_count =200;timer_count<246;timer_count++)...
}
/* 积算1ms,共4点 */
void T_1MS(void)
{
for(timer_count =246;timer_count<250;timer_count++)
}
调用
//1ms系统中断
void SysTick_ISR(void)
{
all_clock++;
T_1MS();
if(all_clock>99) // m8011 10MS m8012 100MS m8013 1SEC m8014 1minute
{
all_clock=0;
PLC_BIT_ON(M8012);
}
if((all_clock%10)==7) // 7可以是0-9任意值,需要避免各种任务在同一时间执行
{
T_10MS();
PLC_BIT_OFF(M8011);
}
if(all_clock==50) //50可以是0-99任意值,需要避免各种任务在同一时间执行
{
T_100MS();
PLC_BIT_OFF(M8012);
}
}
使能
static volatile u16 *pT_DisSetValue,*pT_DisCurrentValue;
void timer_disble(u16 timer_number)
{
pT_DisSetValue =plc_16BitBuf+0x0900+timer_number; // 计数器设定地址
pT_DisCurrentValue =plc_16BitBuf+0x0800+timer_number; // 计数器当前值地址
//
PLC_BIT_OFF(0x600+timer_number); //溢出线圈
PLC_BIT_OFF(0x1600+timer_number); //使能线圈
PLC_BIT_OFF(0x3800+timer_number); //复位线圈
// 传人记,T246~T249 4点1ms积算定时器;T250~T255 6点100ms积算定时器;
if(timer_number < 246) // 非积算定时器
{
*pT_DisSetValue=0;
*pT_DisCurrentValue=0;
}
}
//开启定时器
static
volatile u16 *pSetValue,*pCurrentValue;
void timer_enable(u16 timer_number)
{
pSetValue=plc_16BitBuf + 0x0900 + timer_number; //计数器设定值地址
pCurrentValue=plc_16BitBuf+0x0800+timer_number; // 计数器当前值地址
if(*pCurrentValue < *pSetValue)
{
if(PLC_BIT_TEST(0x600+timer_number)) // 判断触点是否闭合
{
PLC_BIT_ON(0x3800+timer_number); //复位线圈
}
else
{
PLC_BIT_OFF(0x600+timer_number); //值小于设定值时OFF
if(PLC_BIT_TEST(0x3800 + timer_number))
{
*pCurrentValue=0;
PLC_BIT_OFF(0x3800 + timer_number);
}
}
}
else
{
PLC_BIT_ON(0x600+timer_number); //值到达设定值时ON
}
}
使用
....