1、 再放置接收信号频率增益和幅度增益控件,设置为图5和图6。图5 接收信号频率增益图6接收信号幅度增益2. 用代码生成器生成程序框架。选择CodeGenerateAll Code,在如图2面中选择QuitCallk项后确定。图7 生成程序框架3. 插入库函数。将鼠标放在需要插入库函数的地方,点击library菜单来插入。在LibraryAdvanced Analysis有各种函数,选中任意函数会弹出设置面板,需要设置具体的参数。设置完成后选择CodeInsert Function Call即完成函数插入。4. 完成了程序框架的填写后对每个函数具体按照示波器的各个按钮的功能来写代码。此时要特别注意
2、回调函数的用法,可以在函数上点击右键,选择Browse Identifier,如图3,来查看函数的定义与用法举例,还可以在右边的库中查找到其他有类似用法的函数,如图4,方便你的调用。 图8 查看函数定义 图9 库函数 另外,还可以直接右键,选择Find UI Object来直接查看该函数对应的用户面板上的控件。5. 再完成了程序的编写后就需要进行程序的调试了。点击菜单Run Debug*.exe或点击快捷栏内绿色三角图标就可以运行程序。未开启示波器时显示如图5.电源开关未按下时电源指示灯为绿色,开启电源开关后电源指示灯为红色。图10 示波器面板 此时,点击电源开关,在选择好输入信号形式,输入信
3、号幅度和频率以及输入信号通道,接受信号通道,曲线滚动模式及频率增益和幅度增益后,就可以显示需要的波形了。六、关键代码及注释#include /头文件ansi_c.hcvirte.h /*做扩展编译时需要 */userint.h#include 综合示波器.h#define PI 3.1416 /宏命令定义#define TRUE 1#define FALSE 0#define MAX_POINTS 51200struct CHANNELDATA int waveform; double frequency; double scope; int pointnum; double dataMAX_
4、POINTS; /定义通道数据结构体 Channel1,Channel2;static int handle; /定义参数int channel=0,channel_signal=0;double channel_scope_gain=1.0,channel_frequency_gain=1;int chartmode=1; /继续int power=0; /关闭/静态整形波形static double dataMAX_POINTS*2;void GenerateData(void) ;int main (int argc, char *argv)/主函数 if (InitCVIRTE (0,
5、 argv, 0) = 0) /* 做扩展编译时需要 */ return -1; /* 读出内存 */ if (handle = LoadPanel (0, 综合示波器.uir, PANEL) 0) DisplayPanel (handle); Channel1.frequency=1;/通道1初始频率 Channel1.scope=1.0;/通道1初始幅度 Channel1.pointnum=512;/通道1初始采样点数 Channel1.waveform=1;/正弦函数 Channel2.frequency=1; Channel2.scope=1.0; Channel2.pointnum=
6、512; Channel2.waveform=1; RunUserInterface (); return 0;/函数参数设置函数int CVICALLBACK DataFunc (int panel, int control, int event, void *callbackData, int eventData1, int eventData2) double signal_scope,signal_frequency; int waveform; switch (control) case PANEL_SCOPEGAIN: if (event = EVENT_VAL_CHANGED)
7、GetCtrlVal(handle, PANEL_SCOPEGAIN, &channel_scope_gain); break; case PANEL_FREQGAIN: GetCtrlVal(handle,PANEL_FREQGAIN,&channel_frequency_gain); case PANEL_SCOPE: if (event = EVENT_COMMIT) GetCtrlVal (handle, PANEL_SCOPE, &signal_scope); if(channel_signal=0) Channel1.scope=signal_scope; else Channel
8、2.scope=signal_scope; break; case PANEL_FREQUENCY: GetCtrlVal (handle, PANEL_FREQUENCY, &signal_frequency); Channel1.frequency=signal_frequency; Channel2.frequency=signal_frequency; case PANEL_WAVEFORM: if (event = EVENT_COMMIT) GetCtrlVal (handle, PANEL_WAVEFORM, &waveform); if(channel_signal=0) Ch
9、annel1.waveform=waveform; Channel2.waveform=waveform; return (0);/曲线滚动模式选择int CVICALLBACK ChartMode (int panel, int control, int event, int Mode; switch (event) case EVENT_COMMIT: GetCtrlVal (handle, PANEL_CHART_MODE, &chartmode); switch(chartmode) case 0: Mode=VAL_SWEEP; break; case 1: Mode= VAL_CO
10、NTINUOUS; case 2: Mode= VAL_BLOCK; SetCtrlAttribute (handle, PANEL_CHART, ATTR_SCROLL_MODE, Mode); break;/退出示波器int CVICALLBACK Quit (int panel, int control, int event, QuitUserInterface (0);/通道选择int CVICALLBACK SelectChannel (int panel, int control, int event, GetCtrlVal (handle, PANEL_CHANNEL, &cha
11、nnel);/定时函数int CVICALLBACK Timer (int panel, int control, int event, int i; case EVENT_TIMER_TICK: GenerateData(); if(channel=0) PlotStripChart(handle,PANEL_CHART,Channel1.data, Channel1.pointnum, 0, 0, VAL_DOUBLE); if(channel=1) PlotStripChart(handle,PANEL_CHART,Channel2.data, Channel2.pointnum, 0,
12、 0, VAL_DOUBLE); if(channel=2) PlotStripChart(handle,PANEL_CHART, data, Channel1.pointnum*2, 0, 0, VAL_DOUBLE);/电源开关int CVICALLBACK Power (int panel, int control, int event, GetCtrlVal (handle, PANEL_POWER, &power); SetCtrlVal (handle, PANEL_LED, power); SetCtrlAttribute (handle, PANEL_TIMER, ATTR_E
13、NABLED, power); if(power=0) ClearStripChart (handle, PANEL_CHART);/输入信号通道选择int CVICALLBACK ChannelSignal (int panel, int control, int event, GetCtrlVal (handle, PANEL_CHANNELSIGNAL, &channel_signal); if(channel_signal=0) SetCtrlVal (handle, PANEL_FREQUENCY, Channel1.frequency); SetCtrlVal (handle, P
14、ANEL_SCOPE, Channel1.scope); SetCtrlVal(handle,PANEL_WAVEFORM,Channel1.waveform) ; else SetCtrlVal (handle, PANEL_FREQUENCY, Channel2.frequency); SetCtrlVal (handle, PANEL_SCOPE, Channel2.scope); SetCtrlVal(handle,PANEL_WAVEFORM,Channel2.waveform);/显示波形通道选择int CVICALLBACK Channel (int panel, int con
15、trol, int event, SetCtrlAttribute (handle, PANEL_CHART, ATTR_NUM_TRACES, 1); SetTraceAttribute (handle,PANEL_CHART,1 ,ATTR_TRACE_COLOR, VAL_GREEN); SetTraceAttribute (handle, PANEL_CHART,1, ATTR_TRACE_COLOR, VAL_RED); SetCtrlAttribute (handle, PANEL_CHART, ATTR_NUM_TRACES, 2); SetTraceAttribute (han
16、dle, PANEL_CHART,1,ATTR_TRACE_COLOR, VAL_GREEN); SetTraceAttribute (handle, PANEL_CHART,2,ATTR_TRACE_COLOR, VAL_RED); /记数点函数void CalculatePointNum() if(channel=0) Channel1.pointnum=1024/Channel1.frequency/channel_frequency_gain ; else if(channel=1) Channel2.pointnum=1024/Channel2.frequency/channel_f
17、requency_gain ; else /设置同样的点 Channel2.pointnum=1024/Channel1.frequency/channel_frequency_gain ;/参数设置函数void GenerateData() CalculatePointNum(); switch (Channel1.waveform) case 0: for (i=0; iChannel1.pointnum/2; i+) Channel1.datai =Channel1.scope/channel_scope_gain ; for (i=Channel1.pointnum/2;Channel
18、1.pointnum; Channel1.datai =-Channel1.scope/channel_scope_gain ; case 1: Channel1.datai = sin (i*2.0*PI/Channel1.pointnum) * Channel1.scope/channel_scope_gain ; case 2: for (i=0; Channel1.datai = (rand()/23767.0) - 0.5) * Channel1.scope/channel_scope_gain ; switch (Channel2.waveform) Channel2.pointn
19、um/2; Channel2.datai =Channel2.scope/channel_scope_gain ; for (i=Channel2.pointnum/2;Channel2.pointnum; Channel2.datai =-Channel2.scope/channel_scope_gain ; Channel2.datai = sin (i*2.0*PI/Channel2.pointnum) * Channel2.scope/channel_scope_gain ; Channel2.datai = (rand()/23767.0) - 0.5) * Channel2.sco
20、pe/channel_scope_gain ; if(channel=2) for(i=0;ii+) datai*2=Channel1.datai; data1+i*2=Channel2.datai; /输入信号频率调整int CVICALLBACK FREQUENCY (int panel, int control, int event, GetCtrlVal(handle,PANEL_FREQUENCY,FREQUENCY);七、实验结果 图11 面板界面 图12 正弦波 图13 方波 图14 噪声八、心得虚拟仪器(Virtual Instrument-VI)是虚拟现实在仪器仪表领域中的一
21、个重要应用,目前已在国际上悄然兴起。虚拟仪器是以多媒体计算机作为基础,使用图形界面编程技术,模拟实际仪器的面板、功能和操作,从而生成完成各种任务的专用仪器。虚拟仪器的出现是是测量仪器领域的一个突破,从根本上更新了测量仪器的概念。虚拟仪器技术不仅可以简化仪器系统结构,而且能有效的降低生产成本和缩短开发时间。示波器是时域测量最常用的仪器。在做课程实验之前,我对LabWindows知之甚少,只学习过C语言。通过几次的实验,逐渐对其有了一些了解,通过边学习边练习,我很快掌握了部分要领,也使我对LabWindows有了更深刻的了解。看到结课作业时,我感觉很头疼,设计内容比实验时多了很多,对照着设计样例,一步一步的进行设计修改,我们不单单要显示出被测波形,还要通过界面,对波形的幅值及周期进行调整,使波形整体感觉更加完善。我深刻的体会到,边学习边实践是一种很好的学习方法,它不仅使我对知识点的理解更深,也是我真真体味到学习新知识的乐趣。九、参考文献1 薛源,基于LabWindows/CVI平台的虚拟仪器的设计与开发.东北大学,2005,12-27.2 刘君华,虚拟程序编程语言LabWindows_CVI编程.北京大学出版社,2009,1-1.
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1