PLC中的软定时器

通过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
	}
}


使用

....
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值