Hello,
I am using the LabJack U3 for an experimental paradigm with the help of a a third-party matlab script (labjack.m).
I can send digital output signals and also managed to receive the digital input on one channel (FIO4) thanks to information previously posted on this forum:
https://labjack.com/forums/u3/seeking-advice-how-use-u3-measure-pedal-st...
Now I need one more digital input channel. If I am correct, then FIO4 is the only channel that is configured as digital input. I would be grateful if you could provide advice (using matlab) how to configure one extra channel (for instance FIO5) to serve as input channel. Using labjack.m’s setDIODirection function didn’t do the job.
Also: I do not have access to the LJControlPanel because I am running Ubuntu
Thanks
Nic
Looking at source, a call like this should work and set only FIO5 to an input (first parameter is the directions, second is the masks):
setDIODirection([0, 0, 0], [32, 0, 0])
For testing, connect GND to FIO5, read from FIO5 and confirm it is 0 (low). If there is no connection to FIO5, the floating state is 1 (high).
If that doesn't resolve the issue, what errors are you getting and what code of yours is not working?
Thanks for the feedback. I now realize that I needed to change the settings for the counter (which was initialized for IO4 and not IO5; see cmd(9) below);
function setCounter(obj)
if obj.silentMode == false && obj.vHandle == 1
cmd=zeros(12,1);
cmd(2) = 248; %Command byte. Feedback command is 0xF8 in hex.
cmd(3) = 3; %Number of Data Words. For ConfigIO it is 0x03
cmd(4) = 11; %Extended command number. For ConfigIO it is 0x0B in hex.
cmd(7) = 1; %WriteMask. TimerCounterConfig is bit 0.
cmd(9) = 69; %68 for IO4 and 69 for IO5.
cmd(11) = 15; %FIOAnalog.
cmd = obj.checksum(cmd,'extended'); %Calculate and set Checksum8 and Checksum16
obj.outp = obj.rawWrite(cmd); %Send the 12-byte/vector command
[obj.inp, res] = obj.rawRead2(12); %This will read a 12-byte response (vector "res" with 12 elements).
%%%obj.inp(:) = obj.rawRead(res,12);
save('res.mat','res')
end
end
Unfortunately my problems are not solved, because I was hoping to read digital information simultaneously from both FIO4 and FIO5; my first thought was to use different counters (setCounter1 and setCounter2; one for each channel) but that does not work because they overwrite each other; in addition, the function checkCounter does not discriminate between FIO4 and FIO5; I also tried to create another instance (lj2 = labJack(‘verbose’,true) which was not helpful either because the second instance is in silent mode. Any advice?
Do you want to read digital I/O on FIO4 and FIO5 (input-low/high), or have Counters on those lines? When enabling counters, you need to do so in one ConfigIO command-response. Currently with "cmd(9) = 69;", that enables 1 timer, enables counter 0 and sets the pin offset to 4 (FIO4).
If you want to enable both counters (counter 0 and counter 1) starting at FIO4 (counter 0 on FIO4, counter 1 on FIO5), and set FIO0-3 as analog lines and FIO4-FIO5 (setCounter doesn't set the WriteMask correctly in original code), the bytes would look like (using bitsll for shifting bits but you can also use the decimal value):
cmd(7) = 1 + bitsll(1, 3); % = d9. WriteMask. TimerCounterConfig is bit 0. FIOAnalog is bit 3.
%TimerCounterConfig: Bits 4-7 are TimerCounterPinOffset,
%bit 3 is enable counter1, bit 2 is enable counter0,
%bit 0-1 is # timers enabled.
pinOffset = 4; %4 = FIO4
cmd(9) = bitsll(pinOffset, 4) + bitsll(1, 3) + bitsll(1, 2); % = d76
cmd(11) = 15; %FIOAnalog. FIO0-FIO3 are analog inputs. FIO4-FIO7 are digital I/O.
As for checkCounter, that only reads Counter 0. You can modify it to read both:
cmd=zeros(12,1); %Command is 11-bytes, so need to add extra padding byte for 12-bytes
cmd(2) = 248; %Command byte. Feedback command is 0xF8 in hex.
cmd(3) = (length(cmd)-6)/2; %0.5 + Number of Data Words (IOTypes and Data)
%IOTypes and Data
cmd(8) = 54; %IOType: Counter0 = 54
cmd(9) = 0; %Reset. Set to 1 and the count will reset after the reading is performed.
cmd(10) = 55; %IOType: Counter1 = 55
cmd(11) = 0; %Reset. Set to 1 and the count will reset after the reading is performed.
cmd = obj.checksum(cmd,'extended'); %Calculate and set Checksum8 and Checksum16
obj.outp = obj.rawWrite(cmd); %Send the 12-byte/vector command
[obj.inp, res] = obj.rawRead2(18); %This will read a 18-byte response (1 byte is padding).
%Counter 0 value is at indexes 10-13 and Counter 1 value is at indexes 14-17.
Note that the above command-response is the Feedback function. Also, code is untested.
I modified my script to enable both counters and it works perfect! Thank you so much!