//*********************************************************
// Labwindows/cvi8.5 虚拟信号发生器
// szlihongtao
// 2010-06-28
// 参照 <基于Labwindows/CVI的虚拟仪器设计与应用> page108
// 综合有关源代码改写而成
//*********************************************************
/************************************************************************/
/* This program introduces the callback function style of programming */
/* in LabWindows/CVI. The program simply generates 4 kind of waves */
/* and plots them on a graph control. The plotting is triggered by an */
/* Acquire button on the front panel, and program execution is halted */
/* by the Quit button. */
/************************************************************************/
#include <analysis.h>
#include <ansi_c.h>
#include <cvirte.h>
#include <userint.h>
#include "sample2.h"
//*********************************************************
#define pi 3.1415926
//*********************************************************
static int panelHandle;
static int n="200";
static double datapoints[256];
void GenerateWave (int shape);
/************************************************************************/
/* The main function is very simple when you use callbacks. Notice */
/* that you only have to load and display the panels. The two callback */
/* functions, AcquireData (executed when the Acquire button is clicked)*/
/* and Shutdown (executed when the Quit button is clicked) are */
/* installed when you load the panels. Calling RunUserInterface simply*/
/* tells LabWindows/CVI to begin passing all event information from */
/* front panels to the appropriate callback functions. */
/* */
/* Open the callback.uir file to see how the callback functions are */
/* assigned. If you edit the Acquire button in the UIR editor, you */
/* will see the AcquireData function name listed in the Callback */
/* Function control in the dialog box. */
/************************************************************************/
int main (int argc, char *argv[])
{
if (InitCVIRTE (0, argv, 0) == 0) /* Initialize CVI libraries */
return -1; /* out of memory */
if ((panelHandle = LoadPanel (0, "sample2.uir",PANEL)) < 0)
return -1;
DisplayPanel (panelHandle);
RunUserInterface ();
return 0;
}
/*************************************************************************/
/* The AcquireData function is assigned to the Acquire button in the */
/* User Interface Editor. The function is automatically prototyped in */
/* the header file callback.h generated by LabWindows/CVI. This function*/
/* is called by LabWindows/CVI whenever you click on the Acquire button */
/* during program execution. */
/* */
/* Notice that you must decide which event to respond to in the func- */
/* tion. In this case, the datapoints are generated only when a */
/* COMMIT event is sensed (up mouse click). LabWindows/CVI can recog- */
/* and pass many different event types, such as LEFT_CLICK, RIGHT_CLICK,*/
/* LEFT_DOUBLE_CLICK, and so on. */
/*************************************************************************/
int CVICALLBACK AcquireData (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
int trace_color, shape;
double caiyang,frequency;
if (event ==EVENT_COMMIT)
{
GetCtrlVal (panelHandle, PANEL_WFM, &shape); /* 采集波形 */
GenerateWave (shape);
GetCtrlVal (panelHandle, PANEL_COLOR, &trace_color); /* 采集颜色 */
DeleteGraphPlot (panelHandle, PANEL_GRAPH, -1, 1);
PlotY (panelHandle, PANEL_GRAPH, datapoints, n, VAL_DOUBLE,
VAL_THIN_LINE, VAL_EMPTY_SQUARE, VAL_SOLID, 1, trace_color);
}
return 0;
}
//*********************************************************
/* 定义波形函数 */
//*********************************************************
void GenerateWave (int shape)
{
// 定义参数类型
double frequency,valtage,caiyang,phase;
double delay,dt,dutyCycle;
GetCtrlVal (panelHandle, PANEL_PHASE, &phase); /* 采集偏移量(度)*/
GetCtrlVal (panelHandle, PANEL_FRE, &frequency); /* 采集 频率(Hz) */
GetCtrlVal (panelHandle, PANEL_VAL, &valtage); /* 采集 幅值(V) */
GetCtrlVal (panelHandle, PANEL_CAIYANG, &caiyang); /* 采集采样频率 */
if (valtage>5
|| valtage<-5) /* 提示错误信息(幅值应在-5~5之间) */
MessagePopup ("故障提示", "哈哈,电压超出范围了!");
else
{
switch (shape)
{
case 0: /* 三角波 */
TriangleWave (n,valtage,frequency/caiyang,&phase,datapoints);
break;
case 1: /* 正弦波*/
SineWave (n,valtage,frequency/caiyang,&phase,datapoints);
break;
case 2: /*方波 */
dutyCycle=33.0;
SquareWave (n,valtage,frequency/caiyang,&phase,dutyCycle,datapoints);
break;
case 3: /*sincwave */
{
/*定义部分变量参数值 */
delay=1.57; // 产生任意波形的时候要用到这个参数
dt=(2*pi)/100.0;
Sinc(n,valtage,delay,dt,datapoints);
break;
}
default:
break;
}
}
}
//*********************************************************
int CVICALLBACK Shutdown (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
if (event ==EVENT_COMMIT)
QuitUserInterface(0);
return 0;
}
//*********************************************************
//*********************************************************
//*********************************************************
文章评论(0条评论)
登录后参与讨论