有熟悉NI采集卡的坛友么?如何让外触发一次AD就采集一次?
如果是自己做板用单片机是很简单的事情,但是用上采集卡涉及到硬件软件之间来回协调,就有些费解了。用法很直观:NI USB-6210采集卡(AD速度250kps/s),计数器接口外接编码器,信号下降沿计数。
编码器转速是变化的,信号频率最大为25kHz,在每个脉冲到来的时候触发一次AD采集,采集完等下一个脉冲。
看了NI的范例,然后用手头有的低端6008采集卡试了下,外触发只能用一次,也就是说让AD采集启动后,就不能再次触发了。
6210贵一点,好像可以重复触发,但暂时没收到,不过看代码觉得就25kHz这个触发频率有些难实现。
范例是 收到触发信号--->回调函数处理数据--->然后继续等待触发信号--->回调函数处理数据
/*********************************************************************
*
* ANSI C Example program:
* ContAcq-IntClk-DigStart-Retrig.c
*
* Example Category:
* AI
*
* Description:
* This example demonstrates how to acquire finite amounts of data
* on each digital trigger.
*
* Instructions for Running:
* 1. Select the physical channel to correspond to where your
* signal is input on the DAQ device.
* 2. Enter the minimum and maximum voltage range.
* Note: For better accuracy try to match the input range to the
* expected voltage level of the measured signal.
* 3. Select the number of points to acquire per channel.
* 4. Specify the rate of the sample clock.
* 5. Select a source for the digital edge start trigger.
* 6. Select the edge, rising or falling, on which to trigger.
*
* Steps:
* 1. Create a task.
* 2. Create an analog input voltage channel.
* 3. Setup the timing for the acquisition. In this example, the
* Sample Clock is generated internally by the board. Rate
* specifies the rate of the internal clock. The timing is set
* to take a finite number of samples. The number of samples is
* specified by the Samples per Channel control. For a
* retriggerable acquisition, the Samples per Channel defines
* the number of samples to acquire for each trigger.
* 4. Set the parameters for a digital edge start trigger.
* 5. Set the Trigger Attribute to set the operation for
* retriggerable.
* 6. Call the Start function to start the acquistion.
* 7. Read the data in the EveryNCallback function until the stop
* button is pressed or an error occurs.
* 8. Call the Clear Task function to clear the task.
* 9. Display an error if any.
*
* I/O Connections Overview:
* Make sure your signal input terminal matches the Physical
* Channel I/O control. Also, make sure your analog trigger
* terminal matches the Trigger Source Control. For further
* connection information, refer to your hardware reference manual.
*
*********************************************************************/
#include <stdio.h>
#include <NIDAQmx.h>
#define DAQmxErrChk(functionCall) if( DAQmxFailed(error=(functionCall)) ) goto Error; else
int32 CVICALLBACK EveryNCallback(TaskHandle taskHandle, int32 everyNsamplesEventType, uInt32 nSamples, void *callbackData);
int32 CVICALLBACK DoneCallback(TaskHandle taskHandle, int32 status, void *callbackData);
int main(void)
{
int32 error=0;
TaskHandletaskHandle=0;
char errBuff={'\0'};
/*********************************************/
// DAQmx Configure Code
/*********************************************/
DAQmxErrChk (DAQmxCreateTask("",&taskHandle));
DAQmxErrChk (DAQmxCreateAIVoltageChan(taskHandle,"Dev1/ai0","",DAQmx_Val_Cfg_Default,-10.0,10.0,DAQmx_Val_Volts,NULL));
DAQmxErrChk (DAQmxCfgSampClkTiming(taskHandle,"",10000.0,DAQmx_Val_Rising,DAQmx_Val_FiniteSamps,1000));
DAQmxErrChk (DAQmxCfgDigEdgeStartTrig(taskHandle,"/Dev1/PFI0",DAQmx_Val_Falling));
DAQmxErrChk (DAQmxSetStartTrigRetriggerable(taskHandle,1));
DAQmxErrChk (DAQmxRegisterEveryNSamplesEvent(taskHandle,DAQmx_Val_Acquired_Into_Buffer,1000,0,EveryNCallback,NULL));
DAQmxErrChk (DAQmxRegisterDoneEvent(taskHandle,0,DoneCallback,NULL));
/*********************************************/
// DAQmx Start Code
/*********************************************/
DAQmxErrChk (DAQmxStartTask(taskHandle));
printf("Acquiring samples continuously. Press Enter to interrupt\n");
getchar();
Error:
if( DAQmxFailed(error) )
DAQmxGetExtendedErrorInfo(errBuff,2048);
if( taskHandle!=0 ) {
/*********************************************/
// DAQmx Stop Code
/*********************************************/
DAQmxStopTask(taskHandle);
DAQmxClearTask(taskHandle);
}
if( DAQmxFailed(error) )
printf("DAQmx Error: %s\n",errBuff);
printf("End of program, press Enter key to quit\n");
getchar();
return 0;
}
int32 CVICALLBACK EveryNCallback(TaskHandle taskHandle, int32 everyNsamplesEventType, uInt32 nSamples, void *callbackData)
{
int32 error=0;
char errBuff={'\0'};
static inttotalRead=0;
int32 read=0;
float64 data;
/*********************************************/
// DAQmx Read Code
/*********************************************/
DAQmxErrChk (DAQmxReadAnalogF64(taskHandle,1000,10.0,DAQmx_Val_GroupByScanNumber,data,1000,&read,NULL));
if( read>0 ) {
printf("Acquired %d samples. Total %d\r",read,totalRead+=read);
fflush(stdout);
}
Error:
if( DAQmxFailed(error) ) {
DAQmxGetExtendedErrorInfo(errBuff,2048);
/*********************************************/
// DAQmx Stop Code
/*********************************************/
DAQmxStopTask(taskHandle);
DAQmxClearTask(taskHandle);
printf("DAQmx Error: %s\n",errBuff);
}
return 0;
}
int32 CVICALLBACK DoneCallback(TaskHandle taskHandle, int32 status, void *callbackData)
{
int32 error=0;
char errBuff={'\0'};
// Check to see if an error stopped the task.
DAQmxErrChk (status);
Error:
if( DAQmxFailed(error) ) {
DAQmxGetExtendedErrorInfo(errBuff,2048);
DAQmxClearTask(taskHandle);
printf("DAQmx Error: %s\n",errBuff);
}
return 0;
}
用labview,上手很快的。 CVI也很方便,没折腾过NI USB-6210采集卡,不懂,帮顶。
页:
[1]