资源描述:
《虚拟仪器实验指导二》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、实验二1.任务编写一个函数发生器,要求:能够通过用户界面选择产生正弦波、三角波、锯齿波、方波等函数波形用曲线图控件绘制用户选择的波形。2.实验步骤步骤1:根据要实现的功能,设计如图2-1所示的用户界面,其具体控件属性设置如表2-1所示。图2-1最终用户界面表2-1主要控件的基本属性列表控件类型ConstantName属性设置面板PanelPANEL命令按钮CommandButtonQUITBUTTONCallbackFunctionQuitCallbackLable__Quit曲线图控件GraphGRAPH1ControlModeIndicat
2、orDataModeRetainLable波形显示下拉列表控件RingWAVEFORMCallbackFunctionWaveFormChooseCBLabel波形选择Label/ValuePairsLabel无Value0Label正弦波Value1Label三角波Value2Label锯齿波Value3Label方波Value4步骤2:生成代码框架。选择Code→Generate→AllCode,生成代码框架,接着添加自定义函数show_wave进行显示。/*下拉列表控件“波形选择”对应的回调函数*/intCVICALLBACKWaveFo
3、rmChooseCB(intpanel,intcontrol,intevent,void*callbackData,inteventData1,inteventData2){switch(event){caseEVENT_COMMIT:show_wave();break;}return0;}然后,在函数show_wave内添加如下代码。voidshow_wave(void){intPOINTS=200;intwaveForm;/*波形选择变量*/doubleample=5.0;/*幅度*/doublephase=0.0,waveData[POI
4、NTS];/*相位和波形数据变量*/doublesampleRate=10000.0,freq=100.0;/*采样率和频率变量*/GetCtrlVal(panelHandle,PANEL_WAVEFORM,&waveForm);switch(waveForm){case0:/*无波形*/DeleteGraphPlot(panelHandle,PANEL_GRAPH1,-1,VAL_IMMEDIATE_DRAW);/*清除所画曲线*/break;case1:/*正弦波*/SineWave(POINTS,ample,freq/sampleRate
5、,&phase,waveData);break;case2:/*三角波*/TriangleWave(POINTS,ample,freq/sampleRate,&phase,waveData);break;case3:/*锯齿波*/SawtoothWave(POINTS,ample,freq/sampleRate,&phase,waveData);break;case4:/*方波*/SquareWave(POINTS,ample,freq/sampleRate,&phase,50.0,waveData);break;}if(waveForm)/*
6、先判断是否选择了波形*/{DeleteGraphPlot(panelHandle,PANEL_GRAPH1,-1,VAL_IMMEDIATE_DRAW);PlotWaveform(panelHandle,PANEL_GRAPH1,waveData,POINTS,VAL_DOUBLE,1.0,0.0,0.0,1.0/sampleRate,VAL_THIN_LINE,VAL_EMPTY_SQUARE,VAL_SOLID,1,VAL_RED);/*画出波形*/}}代码中的函数意义及调用方法如下(1)函数DeleteGraphPlot清除所画曲线,调用
7、路径为:Library→UerInterfaceLibrary→Controls/Graphs/StripCharts….→GraphsandStirpCharts→DeleteGraphPlot其参数设置见图2-2。(2)函数SineWave,TriangleWave,SawtoothWave,SquareWave分别为正弦波,三角波,锯齿波和方波生成函数。它们都在Library→AdvancedAnalysisLibrary→SignalGeneration目录下获得,其参数设置如图2-3~2-6所示。(3)函数PlotWaveform,画
8、曲线图函数,调用路径为:Library→UerInterfaceLibrary→Controls/Graphs/StripCharts….→Graph