原创 Labwindows/cvi8.5 同步/异步定时器的使用2

2010-6-16 19:09 4989 2 5 分类: 软件与OS


//*********************************************************
// Labwindows/cvi8.5 同步/异步定时器的使用2
// szlihongtao
// 2010-06-16
// 参照 王建新的<Labwindows/cvi测试技术及工程应用> page312
// 稍有改写
//*********************************************************
#include <utility.h> 
#include <cvirte.h>  
#include <userint.h>
#include "toolbox.h"
#include "asynctmr.h"
#include "asy.h"
//*********************************************************  
#define TB 0.02   // 定时器的间隔周期


//设置异步定时器的callbackData参数
static void *asyncCBData = "这是一个异步定时器示例!5555555555"; 
//设置同步定时器的callbackData参数
static void *syncCBData = "这是一个同步定时器示例!4444444444";    
       
static int timerid;     //异步定时器句柄
static int panelHandle;
int CVICALLBACK asynCB (int reserved, int timerId, int event, void *callbackData, int eventData1, int eventData2);
//*********************************************************
int main (int argc, char *argv[])
{
 if (InitCVIRTE (0, argv, 0) == 0)
  return -1;     /* out of memory */
 if ((panelHandle = LoadPanel (0, "asy.uir", PANEL)) < 0)
  return -1;
 
 timerid = NewAsyncTimer (TB, -1, 0, asynCB, asyncCBData);   //创建一个异步定时器  
 DisplayPanel (panelHandle);
 RunUserInterface ();
  
 DiscardAsyncTimer (timerid);   //释放异步定时器  
 DiscardPanel (panelHandle);
 return 0;
}         
//*********************************************************
int CVICALLBACK start (int panel, int control, int event,
  void *callbackData, int eventData1, int eventData2)
{
 int mode;
 double ddd;
 
 switch (event)
 {
  case EVENT_COMMIT:
  {
   GetCtrlVal (panelHandle, PANEL_BINARYSWITCH, &mode);
   if (mode)     //设置异步定时器的ENABLED属性  
   {  
    SetAsyncTimerAttribute (timerid, ASYNC_ATTR_ENABLED, 1);
    SetAsyncTimerAttribute (timerid, ASYNC_ATTR_INTERVAL, TB);
    SetCtrlAttribute (panelHandle, PANEL_TIMER, ATTR_ENABLED, 0);
   }
   else      //传递同步定时器的callbackData参数  
   {  
    SetAsyncTimerAttribute (timerid, ASYNC_ATTR_ENABLED, 0);
    SetCtrlAttribute (panelHandle, PANEL_TIMER, ATTR_ENABLED, 1);
    SetCtrlAttribute (panelHandle, PANEL_TIMER, ATTR_INTERVAL, TB);    
   }
   
   //设置输入模式,与设置控件的dimmed属性类似
   SetInputMode (panelHandle, PANEL_CMD_START, 0);
   break;
  }
 }
 return 0;
}
//*********************************************************
int CVICALLBACK QuitCallback (int panel, int control, int event,
  void *callbackData, int eventData1, int eventData2)
{
 switch (event)
 {
  case EVENT_COMMIT:
   QuitUserInterface (0);
   break;
 }
 return 0;
}
//*********************************************************
int CVICALLBACK stop (int panel, int control, int event,
  void *callbackData, int eventData1, int eventData2)
{
 switch (event)
 {
  case EVENT_COMMIT:
  {
   SetCtrlAttribute (panelHandle, PANEL_CMD_START, ATTR_DIMMED, 0);// 允许再次启动 
   SetAsyncTimerAttribute (timerid, ASYNC_ATTR_ENABLED, 0);
   SetCtrlAttribute (panelHandle, PANEL_TIMER, ATTR_ENABLED, 0);  
   break;
  }
 }
 return 0;
}
//*********************************************************
int CVICALLBACK timer (int panel, int control, int event,
  void *callbackData, int eventData1, int eventData2)
{
 double yarray[2];
 
 switch (event)
 {
  case EVENT_TIMER_TICK:    
  { 
   yarray[0] = Random (0, 100);  //产生0-100之间的随机数      ;
   PlotStripChart (panelHandle, PANEL_STRIPCHART, yarray, 1, 0, 0, VAL_DOUBLE);
  
   SetCtrlVal (panelHandle, PANEL_TEXTMSG,syncCBData);
   break;
  } 
 }
 return 0;
}
//*********************************************************
int CVICALLBACK asynCB (int reserved, int timerId, int event,
  void *callbackData, int eventData1, int eventData2)
{
 double yarray[2];


 switch (event)
 {
  case EVENT_TIMER_TICK:
  {     
   yarray[0] = Random (0, 100);   
   PlotStripChart (panelHandle, PANEL_STRIPCHART, yarray, 1, 0, 0, VAL_DOUBLE);
 
   SetCtrlVal (panelHandle, PANEL_TEXTMSG, (char *)callbackData);
     break;
  }
 }
 return 0;
}
//*********************************************************
//*********************************************************
//*********************************************************

文章评论3条评论)

登录后参与讨论

1353645794_757575543 2015-8-19 19:16

谢谢

xjhujunwen_133076670 2014-10-4 20:21

很有用,谢谢

120905487_665251635 2014-4-18 15:58

很有用,谢谢
相关推荐阅读
sz_lihongtao 2011-04-20 21:32
32bit无符号数快速开平方根
//*******************************************************************************// 32bit无符号数开平方根// ...
sz_lihongtao 2011-04-20 12:12
STM32学习日志(24)----使用dsp库的FFT函数测相位
attachment download/**  ****************************************************************************...
sz_lihongtao 2011-04-19 14:57
STM32学习日志(23)----使用dsp库的FFT函数.rar
attachment download/**  ****************************************************************************...
sz_lihongtao 2011-04-19 10:39
stm32 dsp lib V2.0
attachment downloadattachment download...
sz_lihongtao 2011-04-19 10:37
STM32学习日志(22)----使用DMA功能自动更新PWM的输出
attachment download/*******************************************************************************编...
sz_lihongtao 2010-09-08 21:59
Labwindows/cvi8.5学习日志(56)----任意波形发生器
//******************************************************************************// Labwindows/cvi8.5...
广告
我要评论
3
2
1
2
3
4
5
6
7
8
9
0
广告
关闭 热点推荐上一条 /2 下一条