Hello all,
I am attempting to read back the Digital IO pins on the AD3, for tracking the ticks of a rotary encoder, but even with no pins coming in high, im getting non 0 values for my pins(when reading from SDK only, Waveforms app does not show any pins high)
I assume that I am doing something wrong on the read and have attached my test below.
Dwf.DigitalInReset(_deviceManager.GetDevice()!.Id.Value);
int leadingPinIndex = 0;
int trailingPinIndex = 1;
var id = _deviceManager.GetDevice()!.Id;
var hzSys = Dwf.DigitalInInternalClockInfo(id.Value);
// sample rate to 1MHz
var divider = (uint)(hzSys / 1e6);
Dwf.DigitalInDividerSet(id.Value, divider);
// 16bit WORD format
Dwf.DigitalInSampleFormatSet(id.Value, 16);
Dwf.DigitalInAcquisitionModeSet(id.Value, ACQMODE.Single);
// get the maximum buffer size
var cSamples = Dwf.DigitalInBufferSizeInfo(id.Value);
// default buffer size is set to maximum
Dwf.DigitalInBufferSizeSet(id.Value, cSamples);
// set trigger position to the middle
Dwf.DigitalInTriggerPositionSet(id.Value, (uint)(cSamples / 2));
// trigger on any pin transition
Dwf.DigitalInTriggerSourceSet(id.Value, DigitalTriggerSource.DetectorDigitalIn.MapToWaveFormsSdkType());
//_digitalInService.DigitalInTriggerAutoTimeoutSet(id, 10.0);
//var triggerBits = SetBits(0, 1);
uint triggerBits = 0xFFFF;
Dwf.DigitalInTriggerSet(id.Value, triggerBits, triggerBits, triggerBits, triggerBits);
Dwf.DigitalInTriggerAutoTimeoutSet(id.Value, 5);
// start
Dwf.DigitalInConfigure(id.Value, false, true);
bool previousLeading = false;
bool previousTrailing = false;
int count = 0;
while (count < 5)
{
STATE sts;
do
{
sts = Dwf.DigitalInStatus(id.Value, false);
} while (sts != STATE.Done);
var rgwSamples = Dwf.DigitalInStatusData(id.Value, cSamples);
for (int i = 0; i < rgwSamples.Length; i += 2)
{
// Ensure there's a pair of bytes to process
if (i + 1 < rgwSamples.Length)
{
var byte1 = rgwSamples[i]; // Even byte - pins [0-7]
var byte2 = rgwSamples[i + 1]; // Odd byte - pins [8-15]
var byte1BoolArray = ByteToBoolArray(byte1);
var byte2BoolArray = ByteToBoolArray(byte2);
bool leadingPin = leadingPinIndex < 8 ? byte1BoolArray[leadingPinIndex] : byte2BoolArray[leadingPinIndex - 8];
bool trailingPin = trailingPinIndex < 8 ? byte1BoolArray[trailingPinIndex] : byte2BoolArray[trailingPinIndex - 8];
if (leadingPin != previousLeading || trailingPin != previousTrailing)
{
//This will yield return in actual application
//yield return (leadingPin, trailingPin);
previousLeading = leadingPin;
previousTrailing = trailingPin;
}
}
}
count++;
}
If I provide a True to the DigitalInStatus call, rgwSamples is [3,0,3,0,3,0,3,....]
If I provide a False to the DigitalInStatus call, rgwSamples is [80,1,145,144,229,1,0,0....] and then the series repeats.
My expectation would be that if I have no high digital pins, my entire rgwSamples would be 0s.
Since I have a 16 bit format and 16 pins, its my assumption that the even elements in rgwSamples would be for pins 0-7 and the odd elements would be for pins 1-15. Is that a misunderstanding?
Thanks in advice for any advice/help!