Digital I/O Demo 2 – Generating digital output

In this demo, we get the number of TTL/digital outs available on the device: nBits = Datapixx('GetDoutNumBits');. To figure out how to talk to one of these nBits TTL, we need to describe this in binary. If nBits is for example 16, we will have access to 16 TTL and each of them are represented with a power of 2. 2^0 is going to be output zero, 2^1 is output one. Keeping up with this example of 16 TTL, we can represent this in binary: 0b0000 0000 0000 0000, where every 0 represents a 2 to a power and a digital out.

To set all of them on, we do Datapixx('SetDoutValues', (2^nBits) - 1);. 2^nBits would be 0b1 0000 0000 0000 0000, if we remove one from that, we get 0b1111 1111 1111 1111, therefore setting all of them on (this example is again for nBits = 16).

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
function DatapixxDoutBasicDemo()
% DatapixxDoutBasicDemo()
%
% Demonstrates the basic functions of the DATAPixx TTL digital outputs.
% Prints the number of TTL outputs in the system,
% then waits for keypresses to:
%   -Set digital output 0 high
%   -Set all digital outputs high
%   -Bring all digital outputs back low
%
% Also see: DatapixxDoutTriggerDemo
%
% History:
%
% Oct 1, 2009  paa     Written
% Oct 29, 2014 dml     Revised

AssertOpenGL;   % We use PTB-3

% Open Datapixx, and stop any schedules which might already be running
Datapixx('Open');
Datapixx('StopAllSchedules');
Datapixx('RegWrRd');    % Synchronize DATAPixx registers to local register cache

% Show how many TTL output bits are in the Datapixx
nBits = Datapixx('GetDoutNumBits');
fprintf('\nDATAPixx has %d TTL output bits\n\n', nBits);

% Bring 1 output high
HitKeyToContinue('\nHit any key to bring digital output bit 0 high:');
Datapixx('SetDoutValues', 1);
Datapixx('RegWrRd');

% Bring all the outputs high
HitKeyToContinue('\nHit any key to bring all the digital outputs high:');
Datapixx('SetDoutValues', (2^nBits) - 1);
Datapixx('RegWrRd');

% Bring all the outputs low
HitKeyToContinue('\nHit any key to bring all the digital outputs low:');
Datapixx('SetDoutValues', 0);
Datapixx('RegWrRd');

% Job done
Datapixx('Close');
fprintf('\n\nDemo completed\n\n');