Hi, I was thinking of using a K8055 board for a project that requires a counter, but I’m having some trouble with the counter function.
It seems to lag, the value is not updated immediately as soon as it gets a pulse. I first tested this by displaying the counter value on screen and manually pressing the input button for counter 1. It did seem to lag there but no way to be sure with the human factor involved. So to test it in a better way, I connected digital output 1 to input 1, and send pulses using software.
My loop looks something like this:
[code]int sendCounter = 0;
SetCounterDebounceTime(1, 0);
ResetCounter(1);
while(!kbhit())
{
// Send a pulse by switching the output off and on again
WriteAllDigital(0);
WriteAllDigital(1);
sendCounter++;
int readCounter = ReadCounter(1);
printf("%d %d\n", sendCounter, readCounter);
Sleep(1000/10); // Send 10 pulses per second
}
Sleep(500);
printf("%d %d\n", sendCounter, ReadCounter(1));[/code]
If everything were as I expected it to be, then readCounter should equal sendCounter. It doesn’t though and it always lags behind a couple of pulses (depending at which rate at which I send pulses). Eventually it will catch up, the last line of code always shows equal values as long as the delay before it is long enough.
By putting a delay between the writes and the reads, I found that in this case the counter lags about 260 milliseconds. Any delay smaller than that and the values will differ.
The weirdness doesn’t end there… If I make the code wait for the counters to be synchronized by continuously reading the input counter without delay, it only takes 2 reads or 12 msec to synchronize. But if I add a small delay in that synchronizing loop, it needs more reads? For whatever reason, it seems that the larger the delay, the more reads it needs to synchronize? A delay of 10 msec between reads causes it to need 28 reads before it’s synchronized. With a delay of 0 msec it only needs 2.
int readCounter = ReadCounter(1);
while(readCounter!=sendCounter)
{
numReads++;
readCounter = ReadCounter(1);
Sleep(0); // Increasing delay time here makes it need more reads before the value is correct??
}
All this seems to apply to inputs in general, not just the counter function.
I’m not sure what to make of all this, the safest bet for getting somewhat accurate readings seems to be to just hammer the board with reads, but that’s hardly optimal, I should not have to do multiple reads to get the correct value.
Could this be a bug in the DLL (k8055d.dll version 4.0.0.0)?
Any help would be appreciated.
Thanks