function [vo] = DAC_out(Vc, ZL, r, a) % Vc voltage for "1", for example 3.3V % ZL load resistance % r = [r_0 r_1 r_2 r_3 ... r_{N-1}] DAC resistors % a = [a_0 a_1 ... a_{N-1} a_{N-2}] binary code to convert in analog voltage N = length(r); if N ~= length(a) error('r and a vectors must have the same lenght'); end req = zeros(1, N); out = zeros(1, N); % Calculate de equivalent resistor of the all resistors in parelell except the current resistor % for example, for a_0 is % req(1) = r_1*r_2*...*r_{N-1}*ZL / (r_2*r_3...*r_{N-1}*ZL + r_1*r_3*r_4...*r_{N-1}*ZL + ... + % r_1*r_2*...*r_{N-2}*ZL + r_1*r_2*...*r_{N-1}) for i=1:N rt = [r ZL]; rt(i) = []; req(i) = prod(rt)/sumprod(rt); out(i) = Vc * req(i)/(req(i) + r(i)); end % The network is linear vo = 0; for i=1:N if a(i) == 1 vo = vo + out(i); end end function [a] = sumprod(r) % Calculates the sum of products of the elements of r excludinf j-th % element of r in each iteration. j = 1, 2, ... N a = 0; for j=1:length(r) p = 1; for i=1:length(r) if i ~= j p = p * r(i); end end a = a + p; end