% Ouput voltage N-bit DAC with optimal resistors N = 4; Vc = 3.3; Vmax = 0.7; ZL = 75; b = 0:(N-1); k = 0:(2^N-1); % Optimal resistor values for linear ouput curve, you can change them to % obtain other output curves. % r = ZL*(2^N - 1)*(Vc/Vmax - 1) * 2.^(-b) + 0 r(1) = 3.9e3; r(2) = 2.2e3; r(3) = 1.0e3; r(4) = 470; % r(5) = r(5)*1.8; out = zeros(1, length(k)); for i=1:length(k) % dec2binvec is in Data Acquisition Toolbox, but it's very easy to % write it if you don't have this toolbox installed. a = dec2binvec(k(i), N); out(i) = DAC_out(Vc, ZL, r, a); end hold on hb = plot(k, out, '-db', 'DisplayName', 'REAL '); hg = plot(k, k*Vmax/(2^N-1), '-sg', 'DisplayName', 'IDEAL'); legend('show'); title (sprintf('DAC N=%i-bit output voltage, V_c=%.2f, V_{max}=%.2f, Z_L=%i', N, Vc, Vmax, ZL)); ylabel('Vo (V)'); xlabel('Number to convert'); grid