Labwindows cvi2017函数信号发生器完整代码
时间: 2023-06-27 12:05:38 浏览: 195
以下是一个简单的 LabWindows/CVI 2017 函数信号发生器的完整代码示例:
```
#include <cvirte.h>
#include <userint.h>
#include <utility.h>
#include <ansi_c.h>
#include <analysis.h>
#define PI 3.14159265358979323846
static int panelHandle;
double sineWave(double amplitude, double frequency, double time)
{
return amplitude * sin(2.0 * PI * frequency * time);
}
double squareWave(double amplitude, double frequency, double time)
{
double period = 1.0 / frequency;
double halfPeriod = period / 2.0;
double t = fmod(time, period);
if (t < halfPeriod)
return amplitude;
else
return -amplitude;
}
double triangleWave(double amplitude, double frequency, double time)
{
double period = 1.0 / frequency;
double t = fmod(time, period);
double slope = amplitude / (period / 2.0);
if (t < period / 2.0)
return slope * t;
else
return slope * (period - t) - amplitude;
}
int CVICALLBACK panelCallback(int panel, int event, void *callbackData,
int eventData1, int eventData2)
{
switch (event)
{
case EVENT_CLOSE:
QuitUserInterface(0);
break;
}
return 0;
}
int main (int argc, char *argv[])
{
double (*waveFunc)(double, double, double) = sineWave; // 默认使用正弦波
double amplitude = 1.0;
double frequency = 1.0;
double sampleRate = 1000.0;
double duration = 1.0;
double timeStep = 1.0 / sampleRate;
double *data;
int numSamples;
int i;
if (InitCVIRTE (0, argv, 0) == 0)
return -1;
panelHandle = LoadPanel (0, "panel.uir", PANEL);
SetPanelAttribute(panelHandle, ATTR_CALLBACK_DATA, NULL);
InstallCtrlCallback(panelHandle, PANEL_WAVE_TYPE, waveTypeCallback, &waveFunc);
InstallCtrlCallback(panelHandle, PANEL_AMPLITUDE, amplitudeCallback, &litude);
InstallCtrlCallback(panelHandle, PANEL_FREQUENCY, frequencyCallback, &frequency);
InstallCtrlCallback(panelHandle, PANEL_SAMPLE_RATE, sampleRateCallback, &sampleRate);
InstallCtrlCallback(panelHandle, PANEL_DURATION, durationCallback, &duration);
InstallCtrlCallback(panelHandle, PANEL_GENERATE_BUTTON, generateButtonCallback, &data);
SetCtrlVal(panelHandle, PANEL_WAVE_TYPE, 0);
SetCtrlVal(panelHandle, PANEL_AMPLITUDE, amplitude);
SetCtrlVal(panelHandle, PANEL_FREQUENCY, frequency);
SetCtrlVal(panelHandle, PANEL_SAMPLE_RATE, sampleRate);
SetCtrlVal(panelHandle, PANEL_DURATION, duration);
DisplayPanel(panelHandle);
RunUserInterface();
FreeWaveform(data);
DiscardPanel(panelHandle);
CloseCVIRTE();
return 0;
}
int CVICALLBACK waveTypeCallback(int panel, int control, int event, void *callbackData,
int eventData1, int eventData2)
{
double (*waveFunc)(double, double, double) = (double (*)(double, double, double))callbackData;
switch (event)
{
case EVENT_COMMIT:
switch (GetCtrlVal(panelHandle, PANEL_WAVE_TYPE))
{
case 0:
*waveFunc = sineWave;
break;
case 1:
*waveFunc = squareWave;
break;
case 2:
*waveFunc = triangleWave;
break;
}
break;
}
return 0;
}
int CVICALLBACK amplitudeCallback(int panel, int control, int event, void *callbackData,
int eventData1, int eventData2)
{
double *amplitude = (double *)callbackData;
switch (event)
{
case EVENT_COMMIT:
GetCtrlVal(panelHandle, PANEL_AMPLITUDE, amplitude);
break;
}
return 0;
}
int CVICALLBACK frequencyCallback(int panel, int control, int event, void *callbackData,
int eventData1, int eventData2)
{
double *frequency = (double *)callbackData;
switch (event)
{
case EVENT_COMMIT:
GetCtrlVal(panelHandle, PANEL_FREQUENCY, frequency);
break;
}
return 0;
}
int CVICALLBACK sampleRateCallback(int panel, int control, int event, void *callbackData,
int eventData1, int eventData2)
{
double *sampleRate = (double *)callbackData;
switch (event)
{
case EVENT_COMMIT:
GetCtrlVal(panelHandle, PANEL_SAMPLE_RATE, sampleRate);
break;
}
return 0;
}
int CVICALLBACK durationCallback(int panel, int control, int event, void *callbackData,
int eventData1, int eventData2)
{
double *duration = (double *)callbackData;
switch (event)
{
case EVENT_COMMIT:
GetCtrlVal(panelHandle, PANEL_DURATION, duration);
break;
}
return 0;
}
int CVICALLBACK generateButtonCallback(int panel, int control, int event, void *callbackData,
int eventData1, int eventData2)
{
double *data = (double *)callbackData;
double amplitude;
double frequency;
double sampleRate;
double duration;
double timeStep;
int numSamples;
int i;
switch (event)
{
case EVENT_COMMIT:
GetCtrlVal(panelHandle, PANEL_AMPLITUDE, &litude);
GetCtrlVal(panelHandle, PANEL_FREQUENCY, &frequency);
GetCtrlVal(panelHandle, PANEL_SAMPLE_RATE, &sampleRate);
GetCtrlVal(panelHandle, PANEL_DURATION, &duration);
timeStep = 1.0 / sampleRate;
numSamples = (int)(duration * sampleRate);
*data = (double *)MallocWaveform(sizeof(double) * numSamples);
for (i = 0; i < numSamples; i++)
{
(*data)[i] = (*waveFunc)(amplitude, frequency, i * timeStep);
}
PlotY(panelHandle, PANEL_GRAPH, *data, numSamples, VAL_DOUBLE, VAL_THIN_LINE, VAL_EMPTY_SQUARE, VAL_SOLID, 1, VAL_BLUE);
break;
}
return 0;
}
```
这个程序使用了三个不同的波形函数:正弦波,方波和三角波。您可以使用“波形类型”下拉列表框选择要生成的波形类型,然后设置振幅,频率,采样率和持续时间。单击“生成”按钮将生成波形数据,并将其绘制在图形窗口中。
阅读全文