VS2015定时运行程序,100ms触发一次

3 篇文章 0 订阅
3 篇文章 0 订阅

 代码如下:

#include <stdio.h>
#include <string.h>
//#include <Mmsystem.h>
#include<stdlib.h>
#include<time.h>
#include<windows.h>

#include "osal.h"
#include "osal_win32.h"
#define USECS_PER_SEC 1000000
#define USECS_PER_MSEC 1000


typedef struct timer
{
	ec_timet stop_time;
} timert;


boolean timer_is_expired(timert *self, uint32 timeout_usec)//是否到达 timert时间,到达返回true
{
	return  0;
}

uint32 x = 1, timeout_usec = 1000000, timeout_msec = 100;
//	timer_is_expired(&act_time, timeout);
struct timeval timeout;
struct timeval stop_time;
int is_not_yet_expired;
struct timeval current_time;
int returndata = 0, pre_returndata = 0;
int excute = 0;


int main(int argc, char *argv[])
{

	timeout.tv_sec = timeout_usec / USECS_PER_SEC;
	timeout.tv_usec = timeout_usec % USECS_PER_SEC;
	osal_gettimeofday(&current_time, 0);
	stop_time = current_time;
	long act_time_usec, act_time_msec, pre_act_time_msec;

	osal_gettimeofday(&current_time, 0);
	act_time_usec = current_time.tv_sec * USECS_PER_SEC + current_time.tv_usec;
	act_time_msec = current_time.tv_sec * USECS_PER_MSEC + current_time.tv_usec / USECS_PER_MSEC;
	pre_act_time_msec = act_time_msec;

	while (x == 1)
	{
		osal_gettimeofday(&current_time, 0);
		act_time_usec = current_time.tv_sec * USECS_PER_SEC + current_time.tv_usec;
		act_time_msec = current_time.tv_sec * USECS_PER_MSEC + current_time.tv_usec/USECS_PER_MSEC;

		if (act_time_msec % timeout_msec == 0 && pre_act_time_msec != act_time_msec)
		{
			printf("act_time.sec = %d \t act_time.msec =  %d\t  act_time.usec = %d \t timeout = %d ms\n", current_time.tv_sec, act_time_msec,current_time.tv_usec, timeout_msec);
			pre_act_time_msec = act_time_msec;
		}
	}
	system("pause");
}

运行结果如下:

act_time.sec = 1706845554        act_time.msec =  1743538400      act_time.usec = 912139         timeout = 100 ms
act_time.sec = 1706845555        act_time.msec =  1743538500      act_time.usec = 12140          timeout = 100 ms
act_time.sec = 1706845555        act_time.msec =  1743538600      act_time.usec = 112139         timeout = 100 ms
act_time.sec = 1706845555        act_time.msec =  1743538700      act_time.usec = 212141         timeout = 100 ms
act_time.sec = 1706845555        act_time.msec =  1743538800      act_time.usec = 312140         timeout = 100 ms
act_time.sec = 1706845555        act_time.msec =  1743538900      act_time.usec = 412140         timeout = 100 ms
act_time.sec = 1706845555        act_time.msec =  1743539000      act_time.usec = 512206         timeout = 100 ms
act_time.sec = 1706845555        act_time.msec =  1743539100      act_time.usec = 612141         timeout = 100 ms
act_time.sec = 1706845555        act_time.msec =  1743539200      act_time.usec = 712142         timeout = 100 ms
act_time.sec = 1706845555        act_time.msec =  1743539300      act_time.usec = 812142         timeout = 100 ms
act_time.sec = 1706845555        act_time.msec =  1743539400      act_time.usec = 912141         timeout = 100 ms
act_time.sec = 1706845556        act_time.msec =  1743539500      act_time.usec = 12173          timeout = 100 ms
act_time.sec = 1706845556        act_time.msec =  1743539600      act_time.usec = 112170         timeout = 100 ms
act_time.sec = 1706845556        act_time.msec =  1743539700      act_time.usec = 212171         timeout = 100 ms
act_time.sec = 1706845556        act_time.msec =  1743539800      act_time.usec = 312142         timeout = 100 ms
act_time.sec = 1706845556        act_time.msec =  1743539900      act_time.usec = 412166         timeout = 100 ms
act_time.sec = 1706845556        act_time.msec =  1743540000      act_time.usec = 512142         timeout = 100 ms
act_time.sec = 1706845556        act_time.msec =  1743540100      act_time.usec = 612144         timeout = 100 ms
act_time.sec = 1706845556        act_time.msec =  1743540200      act_time.usec = 712143         timeout = 100 ms
act_time.sec = 1706845556        act_time.msec =  1743540300      act_time.usec = 812144         timeout = 100 ms
act_time.sec = 1706845556        act_time.msec =  1743540400      act_time.usec = 912145         timeout = 100 ms
act_time.sec = 1706845557        act_time.msec =  1743540500      act_time.usec = 12144          timeout = 100 ms
act_time.sec = 1706845557        act_time.msec =  1743540600      act_time.usec = 112144         timeout = 100 ms
act_time.sec = 1706845557        act_time.msec =  1743540700      act_time.usec = 212145         timeout = 100 ms
act_time.sec = 1706845557        act_time.msec =  1743540800      act_time.usec = 312145         timeout = 100 ms
act_time.sec = 1706845557        act_time.msec =  1743540900      act_time.usec = 412174         timeout = 100 ms
act_time.sec = 1706845557        act_time.msec =  1743541000      act_time.usec = 512146         timeout = 100 ms
act_time.sec = 1706845557        act_time.msec =  1743541100      act_time.usec = 612146         timeout = 100 ms
act_time.sec = 1706845557        act_time.msec =  1743541200      act_time.usec = 712159         timeout = 100 ms
act_time.sec = 1706845557        act_time.msec =  1743541300      act_time.usec = 812174         timeout = 100 ms
act_time.sec = 1706845557        act_time.msec =  1743541400      act_time.usec = 912147         timeout = 100 ms
act_time.sec = 1706845558        act_time.msec =  1743541500      act_time.usec = 12147          timeout = 100 ms
act_time.sec = 1706845558        act_time.msec =  1743541600      act_time.usec = 112147         timeout = 100 ms
act_time.sec = 1706845558        act_time.msec =  1743541700      act_time.usec = 212147         timeout = 100 ms
act_time.sec = 1706845558        act_time.msec =  1743541800      act_time.usec = 312148         timeout = 100 ms
act_time.sec = 1706845558        act_time.msec =  1743541900      act_time.usec = 412148         timeout = 100 ms
act_time.sec = 1706845558        act_time.msec =  1743542000      act_time.usec = 512148         timeout = 100 ms
act_time.sec = 1706845558        act_time.msec =  1743542100      act_time.usec = 612149         timeout = 100 ms
act_time.sec = 1706845558        act_time.msec =  1743542200      act_time.usec = 712147         timeout = 100 ms
act_time.sec = 1706845558        act_time.msec =  1743542300      act_time.usec = 812149         timeout = 100 ms
act_time.sec = 1706845558        act_time.msec =  1743542400      act_time.usec = 912162         timeout = 100 ms
act_time.sec = 1706845559        act_time.msec =  1743542500      act_time.usec = 12150          timeout = 100 ms
act_time.sec = 1706845559        act_time.msec =  1743542600      act_time.usec = 112149         timeout = 100 ms
act_time.sec = 1706845559        act_time.msec =  1743542700      act_time.usec = 212149         timeout = 100 ms
act_time.sec = 1706845559        act_time.msec =  1743542800      act_time.usec = 312150         timeout = 100 ms
act_time.sec = 1706845559        act_time.msec =  1743542900      act_time.usec = 412150         timeout = 100 ms
act_time.sec = 1706845559        act_time.msec =  1743543000      act_time.usec = 512151         timeout = 100 ms
act_time.sec = 1706845559        act_time.msec =  1743543100      act_time.usec = 612151         timeout = 100 ms
act_time.sec = 1706845559        act_time.msec =  1743543200      act_time.usec = 712151         timeout = 100 ms
act_time.sec = 1706845559        act_time.msec =  1743543300      act_time.usec = 812151         timeout = 100 ms
act_time.sec = 1706845559        act_time.msec =  1743543400      act_time.usec = 912151         timeout = 100 ms
act_time.sec = 1706845560        act_time.msec =  1743543500      act_time.usec = 12152          timeout = 100 ms
act_time.sec = 1706845560        act_time.msec =  1743543600      act_time.usec = 112152         timeout = 100 ms
act_time.sec = 1706845560        act_time.msec =  1743543700      act_time.usec = 212180         timeout = 100 ms
act_time.sec = 1706845560        act_time.msec =  1743543800      act_time.usec = 312152         timeout = 100 ms
act_time.sec = 1706845560        act_time.msec =  1743543900      act_time.usec = 412181         timeout = 100 ms
act_time.sec = 1706845560        act_time.msec =  1743544000      act_time.usec = 512153         timeout = 100 ms
act_time.sec = 1706845560        act_time.msec =  1743544100      act_time.usec = 612182         timeout = 100 ms
act_time.sec = 1706845560        act_time.msec =  1743544200      act_time.usec = 712178         timeout = 100 ms

  • 7
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
S7200定时中断程序是指在西门子S7200 PLC中,利用定时器来控制程序的间隔时间并在时间到达时触发中断程序的一种编程方法。 一个简单的S7200定时中断程序实例可以如下: 1. 首先,需要在S7200 PLC上创建一个定时器。假设我们创建了一个周期为100ms定时器T1。 2. 在程序中定义一个计数器count,初始值为0。同时,在程序开始时,需要开启定时器T1。 3. 程序循环中,每次循环检查定时器T1是否到达计时周期。如果到达,将计数器count加1,并在计数器达到一定值时执行相应操作。 4. 在处理计数器操作之前,程序需要先关闭定时器T1,避免连续触发中断程序。 5. 在计数器操作结束后,再次开启定时器T1,使程序继续周期性地运行。 该实例可以用以下代码实现: ```ST VAR count : INT := 0; flag : BOOL := true; // 初始化为true,避免程序一开始就进入中断循环 END_VAR // 开启定时器T1 CALL TIMER(T#100ms, T1); WHILE true DO // 检查定时器T1是否到达计时周期 IF TON(T1.IN) = true AND flag = true THEN // 关闭定时器,并执行计数器操作 flag := false; CTU(count, count, 1); IF count >= 10 THEN // 计数器达到10时,执行相应操作 // ... // 清空计数器 count := 0; END_IF END_IF // 恢复定时器并设置flag为true IF NOT TON(T1.IN) THEN flag := true; CALL TIMER(T#100ms, T1); END_IF END_WHILE ``` 以上的代码是一个简单的S7200定时中断程序实例,可以根据实际需求进行修改和扩展。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值