contiki的5种定时器理解

一,概述

Contiki的定时器包含一个硬件时钟模型,五个定时器模型,五种timer简述如下:
timer  --be used to check if a time period has passed[1]
stimer --be used to check if a time period has passed[1]
ctimer --Active timer, calls a functionwhen it expires, Used by Rime[2]
etimer --Active timer, sends an event whenit expires[2]
rtimer --Real-time timer, calls a functionat an exact time[2]
注:
(1) timer与stimer区别在于the resolution of time:timers use system clock ticks while stimers use seconds to allow much longer time periods[1]。
(2) Unlike the other timers, the timer and stimer libraries can be safely used from interrupts which makes them especially useful in low level drivers.

二,硬件时钟模型

    硬件时钟模型是clock.c与clock.h文件:

1,core/sys/clock.h

    该文件定义了需要实现的接口

2,clock.c

    该文件对应于不同的硬件,需要自己实现,功能在clock.h里都定义了

三,定时器模块

1,timer

    该模块以clock的tick作为时间精度,设定定时器,等待时间到达以后触发,需要轮询调用;

2,stimer

    该模块以second作为时间精度,设定定时器,等待时间到达以后触发,需要轮询调用;

3,ctimer

    该模块调用etimer,触发以后调用回调函数;

4,etimer

    该模块调用timer,当定时器超时触发以后以事件的方式通知绑定定时器的线程;

5,rtimer

    该模块直接调用硬件平台,设定一个硬件实时定时器,触发以后调用一个定时器回调;

四,总结

contiki的定时器种类太多,在初期会让人有一种分不清的感觉。都是以timer(硬件定时器)为基础拓展成软件的定时器方式,有的以事件方式、有的以回调方式,有的通过粗精度来保证软件的低消耗,可以通过功能的划分来选择不同的定时器。
在ClassWizard中响应ID为~Dlg中的WM_TIMER消息。 使用SetTimer(nIDEvent,time,NULL)来建立一个定时器,关闭定时器用KillTimer(nIDEvent)函数。 然后可以响应ON_WM_TIMER消息来响应一个定时器完成一次记时后的程序。 响应方式如下: void CTimeDlg::OnTimer(UINT nIDEvent) { if(nIDEvent==1000)//间隔为5秒 { //处理事件 } elseif(nIDEvent==1001)//间隔为10秒 { //处理事件 } CDialog::OnTimer(nIDEvent); } 以下是给出一个串口通信定时检查接收数据的部分代码 void CMyDlg::OnOpenCom() { // TODO: Add your control notification handler code here if( f_open_com==true ) { f_open_com = false; GetDlgItem(IDC_OPEN_COM)->SetWindowText("打开通信端口"); CloseHandle(hComm); KillTimer(1000); /// 关闭定时器 return ; } SetTimer(1000, 1000, NULL); ///nIDEvent==1000,time=5000ms const char *ComNo; DCB dcb; string temp("COM1"); ComNo = temp.c_str(); hComm = CreateFile( ComNo , GENERIC_READ|GENERIC_WRITE , 0 , NULL , OPEN_EXISTING , 0 , 0); if( hComm==INVALID_HANDLE_VALUE ) /// 如果端口未打开 { MessageBox("打开通信端口出错!" , "Comm Error" , MB_OK); return ; } /// 将dcb地址传入,以取得通信参数 GetCommState(hComm,&dcb); /// 得知目前通信状态 dcb.BaudRate = CBR_9600; dcb.ByteSize = 8; /// 字节为8 dcb.Parity = NOPARITY; /// Parity为None dcb.StopBits = ONESTOPBIT; /// 1个停止位 if( !SetCommState( hComm , &dcb)){ MessageBox("通信端口设置出错!" , "Set Error" , MB_OK ); CloseHandle(hComm); return; } GetDlgItem(IDC_OPEN_COM)->SetWindowText("关闭通信端口"); f_open_com = true; } void CMyDlg::OnTimer(UINT nIDEvent) { // TODO: Add your message handler code here and/or call default char inbuff[1024]; DWORD nBytesRead , dwError; COMSTAT cs; /// 取得状态 ClearCommError( hComm , &dwError , &cs); /// 数据是否大于所准备的缓冲区 if( cs.cbInQue > sizeof(inbuff) ) { PurgeComm(hComm , PURGE_RXCLEAR ); /// 清除通信端口数据 return ; } ReadFile(hComm , inbuff , cs.cbInQue , &nBytesRead , NULL ); //接收通信端口的数据 inbuff[cs.cbInQue] = '\0'; MessageBox("打开通信端口出错!" , "Comm Error" , MB_OK); m_Receive.Format("%s",inbuff); UpdateData(false); CDialog::OnTimer(nIDEvent); } 李杨: for(int i=0; ;i++ ) { ... Sleep(5); if(i>...) {AfxMessageBox("错误XXX"); return;} }//跳出后记得停止一些机器动作
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值