//*****************************************************************************
// Labwindows/cvi8.5 虚拟简易示波器
// szlihongtao
// 2010-07-29
//*****************************************************************************
#include <analysis.h>
#include <ansi_c.h>
#include <cvirte.h>
#include <userint.h>
#include "虚拟简易示波器.h"
//*****************************************************************************
#define NUM_DIV 10 // X轴的分格数
#define SAMPLES_DIV 48 // X轴每格对应显示点数
#define SAMPLES_TOTAL (NUM_DIV*SAMPLES_DIV) // X轴的总的点数
//*****************************************************************************
static int panelHandle;
//*****************************************************************************
int main (int argc, char *argv[])
{
if (InitCVIRTE (0, argv, 0) == 0)
return -1; /* out of memory */
if ((panelHandle = LoadPanel (0, "虚拟简易示波器.uir", PANEL)) < 0)
return -1;
DisplayPanel (panelHandle);
RunUserInterface ();
DiscardPanel (panelHandle);
return 0;
}
//*****************************************************************************
int CVICALLBACK func_exit (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
switch (event)
{
case EVENT_COMMIT:
QuitUserInterface (0);
break;
}
return 0;
}
//*****************************************************************************
int CVICALLBACK func_gen (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
double *arr;
double Amplitude;
int Number_of_Elements;
double Phase_Degrees;
double Number_of_Cycles;
double *Sine_Pattern;
double fx,fs,f;
double gain;
int sec_div;
int temp;
switch (event)
{
case EVENT_COMMIT:
{
Number_of_Elements=SAMPLES_TOTAL;
GetCtrlVal (panelHandle, PANEL_NUMERIC, &Amplitude); // 信号幅度
GetCtrlVal (panelHandle, PANEL_NUMERIC_2, &Phase_Degrees);// 初始相位
GetCtrlVal (panelHandle, PANEL_NUMERIC_3, &fx); // 信号的原始频率
GetCtrlVal (panelHandle, PANEL_RINGDIAL_DIV, &sec_div); // 每格代表的时间
fs=Number_of_Elements*1000000.0/(double)(NUM_DIV*sec_div); // 采样频率
Sine_Pattern=malloc(Number_of_Elements*sizeof(double)); // 申请数组内存
#if 1 // 方法1
f=fx/fs; // Frequency of the resulting sine wave signal in normalized units of cycles/sample.
SineWave (Number_of_Elements, Amplitude,f,&Phase_Degrees,Sine_Pattern);
#else // 方法2
f=Number_of_Elements*fx/fs; // Number_of_Cycles,正弦波每个周期对应的点数
SinePattern (Number_of_Elements,Amplitude,Phase_Degrees,f,Sine_Pattern);
#endif
//---------------------------------------------------------------------------------
if (sec_div>=1000) // 1ms
temp=0;
else if (sec_div>=100)
temp=1;
else
temp=0;
SetCtrlAttribute (panelHandle, PANEL_GRAPH, ATTR_XPRECISION, temp); // 小数点位数
//---------------------------------------------------------------------------------
gain=(double)sec_div/(double)SAMPLES_TOTAL;
if (sec_div>=1000) // 1ms
gain/=100;
else if (sec_div>=100) // 0.1ms
gain/=100;
else
gain*=10;
SetCtrlAttribute (panelHandle, PANEL_GRAPH, ATTR_XAXIS_GAIN,gain); // X轴数字显示的放大倍数
SetCtrlAttribute (panelHandle, PANEL_GRAPH, ATTR_XDIVISIONS, NUM_DIV); // X轴的分格数,总是10格,看起来好看一些
//---------------------------------------------------------------------------------
SetAxisScalingMode(panelHandle,PANEL_GRAPH,VAL_BOTTOM_XAXIS,VAL_MANUAL,0,SAMPLES_TOTAL); // 设置X轴的最小值,最大值
DeleteGraphPlot (panelHandle, PANEL_GRAPH, -1, 1);
PlotY (panelHandle, PANEL_GRAPH, Sine_Pattern,Number_of_Elements, VAL_DOUBLE, VAL_THIN_LINE, VAL_EMPTY_SQUARE, VAL_SOLID, 1, VAL_RED);
free(Sine_Pattern); // 释放内存
break;
}
}
return 0;
}
//*****************************************************************************
//*****************************************************************************
//*****************************************************************************
文章评论(0条评论)
登录后参与讨论