I am using the Analog discovery 2 controlled by a C# Software. At a certain point of the software, I need to check the length of a detected pulse. Before this, I prepare the digital in component of the device.
privateconstuint BUFFER_SIZE =4000;privateconstuint DIGITAL_IN_DIVIDER =1000;privateconstuint CLOCK_FREQUENCY =100000000;publicvoid prepareForPulseDetection(int digitalIoIndex ){
checkDigitalIoIndex( digitalIoIndex );// sampling frequency = 100 MHz / 1000 = 100 kHz (-> 10 us)
dwf.FDwfDigitalInDividerSet( device, DIGITAL_IN_DIVIDER );// just sample DIO pins 0...7
dwf.FDwfDigitalInSampleFormatSet( device,8);
dwf.FDwfDigitalInBufferSizeSet( device,(int)BUFFER_SIZE );
dwf.FDwfDigitalInSampleModeSet( device, dwf.DwfDigitalInSampleModeSimple);// just one acquisition after trigger
dwf.FDwfDigitalInAcquisitionModeSet( device, dwf.acqmodeSingle );// setting triggersource to digital in detector
dwf.FDwfDigitalInTriggerSourceSet( device, dwf.trigsrcDetectorDigitalIn );// take 50 samples before trigger
dwf.FDwfDigitalInTriggerPositionSet( device,(uint)BUFFER_SIZE -50);// disable timeout
dwf.FDwfDigitalInTriggerAutoTimeoutSet( device,0);// set trigger to rising edge on DIO pin 0uint triggeredPin =(uint)0x0001<< digitalIoIndex;// high low rising falling
dwf.FDwfDigitalInTriggerSet( device,0x0000,0x0000, triggeredPin,0x0000);// enable digital in without reconfiguring
dwf.FDwfDigitalInConfigure( device,0,1);}privatevoid checkDigitalIoIndex(int digitalIoIndex ){if(( digitalIoIndex <0)||(7< digitalIoIndex ))thrownewArgumentException("Digital IO index must be in array [0...7]");}/// <returns> pulse time in us </returns>publicuint getDetectedPulseLength(int digitalIoIndex ){
checkDigitalIoIndex( digitalIoIndex );
var data =new byte[BUFFER_SIZE];
dwf.FDwfDigitalInStatusData( device, data,(int)BUFFER_SIZE );
var numberOfHighSamples = getNumberOfHighSamples( data, digitalIoIndex );// 1e6 because of us return
var usDuration =(uint)( DIGITAL_IN_DIVIDER *1e6/ CLOCK_FREQUENCY );
usDuration =(uint)( usDuration * numberOfHighSamples );// reset device, else it is not possible to// recognize if pulse occured
dwf.FDwfDigitalInReset( device );return usDuration;}privateint getNumberOfHighSamples( byte[] data,int digitalIoIndex ){if( allZero( data ))return1;// means pulse was shorter than samplerateelse{int startingIndex = getStartingIndexOfPulse( data, digitalIoIndex );int endingIndex = getEndingIndexOfPulse( data, digitalIoIndex );int difference =( endingIndex - startingIndex +1);return difference +3;// plus 3 because analog discovery return is inaccurate}}privatebool allZero( byte[] data ){for(int i =0; i < data.Length;++i )if( data[i]!=0)returnfalse;returntrue;}privateint getStartingIndexOfPulse( byte[] data,int digitalIoIndex ){int index =0;for(; index < data.Length;++index )if( checkBit( data[index],(byte)digitalIoIndex ))break;return index;}privateint getEndingIndexOfPulse( byte[] data,int digitalIoIndex ){int index = getStartingIndexOfPulse( data, digitalIoIndex )+1;for(; index < data.Length;++index )if(!checkBit( data[index],(byte)digitalIoIndex ))break;return--index;}privatebool checkBit( byte value, byte checkedBit ){
byte shiftedValue =(byte)((int)value >>(int)checkedBit );return(( shiftedValue &0x01)==0x01);}
The pulses are reliably measured. But the number of high samples is always smaller than expected. Most of the time, I expect the measured pulse three microseconds longer than it is.
Is this a known issue? Or is there a mistake in my code?
I am very sure, that the pulses I am sending to the analog discovery are as long as they should be. I checked it with the analog discovery with the waveforms software and with another osciloscope.
Question
aaronBe
Hello Community
I am using the Analog discovery 2 controlled by a C# Software. At a certain point of the software, I need to check the length of a detected pulse. Before this, I prepare the digital in component of the device.
The pulses are reliably measured. But the number of high samples is always smaller than expected. Most of the time, I expect the measured pulse three microseconds longer than it is.
Is this a known issue? Or is there a mistake in my code?
I am very sure, that the pulses I am sending to the analog discovery are as long as they should be. I checked it with the analog discovery with the waveforms software and with another osciloscope.
Link to comment
Share on other sites
4 answers to this question
Recommended Posts
Archived
This topic is now archived and is closed to further replies.