You are on page 1of 4

%Input

vinp=0:0.00005:1;
residue_array=zeros(1,size(vinp,2));
v_out_array=zeros(1,size(vinp,2));
iter=0;
for vinput=0:0.00005:1
iter=iter+1;
%vinput=.001;
%Reference
refp=1;
refm=0;
if (vinput>refp || vinput<refm)
disp("Warning!!! Input Voltage out of range!!!")
end
vin=vinput/(refp-refm)-refm;
%CDAC Caps
n= (0:1:9);
Cn=[1,2.^n];
Ctot=sum(Cn);
%Errors and Parasitics
Ctp=0;%Top parasitic cap
Flash_offset=[0.01 0.01 -0.01 -0.01 -0.01 -0.01 0.01]; %LSB first
%Flash_offset=zeros(1,15);
SAR_comp_offset=0; %note - no additional offset correction part present,offset
considered at positive terminal
SAR_bit_error=[0 0 0 0 0 0 0]; %this error accumulates after each bit
conversion
Ctot=Ctot+Ctp;
%Flash Bits
Flash_comp_ideal=(0.125:0.125:0.875);%LSB first
Flash_comp_actual=Flash_comp_ideal+Flash_offset;
thermo_code=zeros(1,7);
j=0;
for i=1:7
if (vin>Flash_comp_actual(i)) %!!!Check for >=
thermo_code(i)=1;
j=j+1;
end
end
vbin=decimalToBinaryVector(j,3);
resid = vin;
if(j~=0)
resid = vin-Flash_comp_ideal(j);
end
%SAR Conversion
sar_red=zeros(1,2); %Redundant Bits
sar_bit=zeros(1,7); %Output bits
Csar=flip(Cn(2:8)); %Capacitors switched in SAR
%sar_resid=resid; %residue in SAR
%Csum=0;
%Here resid is treated as input voltage to SAR
for i=1:2
sar_bit(i)=1;
Csum=sum(sar_bit.*Csar); %sum of capacitors connected to REFP
resid=resid+SAR_bit_error(i); %effective Vin seen by SAR with error
sar_resid=resid-Csum/Ctot; %SAR residue after switching
if sar_resid <= SAR_comp_offset
sar_bit(i)=0; %bit becomes zero if above Comp+
end
end
%Redundant Check
if sar_bit(2)==1
resid=resid-32/Ctot;
Csum=sum(sar_bit.*Csar);
sar_resid=resid-Csum/Ctot;
if sar_resid <= SAR_comp_offset
sar_red(1)=0;
resid=resid+32/Ctot;
else
sar_red(1)=1;
end
else
Csum=sum(sar_bit.*Csar);
sar_resid=resid-Csum/Ctot;
if sar_resid < SAR_comp_offset
sar_red(1)=0;
resid=resid+32/Ctot;
else
sar_red(1)=1;
end
end
%SAR Conversion
for i=3:7
sar_bit(i)=1;
Csum=sum(sar_bit.*Csar);
resid=resid+SAR_bit_error(i);
sar_resid=resid-Csum/Ctot;
if sar_resid<=SAR_comp_offset
sar_bit(i)=0; %bit becomes zero if above Comp+
end
end
%Redundant Check
if sar_bit(7)==1
resid=resid-1/Ctot;
Csum=sum(sar_bit.*Csar);
sar_resid=resid-Csum/Ctot;
if sar_resid<=SAR_comp_offset
sar_red(2)=0;
resid=resid+1/Ctot;
else
sar_red(2)=1;
end
else
Csum=sum(sar_bit.*Csar);
sar_resid=resid-Csum/Ctot;
if sar_resid<SAR_comp_offset
sar_red(2)=0;
resid=resid+1/Ctot;
else
sar_red(2)=1;
end
end

%Final Modifications in MO
mo=[vbin,sar_bit];
mo_dec=binaryVectorToDecimal(mo);
if sar_bit(2)==1 && sar_red(1)==1
mo_dec=mo_dec+32;
end
if sar_bit(2)==0 && sar_red(1)==0
mo_dec=mo_dec-32;
end
if sar_bit(7)==1 && sar_red(2)==1
mo_dec=mo_dec+1;
end
if sar_bit(7)==0 && sar_red(2)==0
mo_dec=mo_dec-1;
end
%Residue/Quantization Plot
if(mo_dec~=0)
bin_out=decimalToBinaryVector(mo_dec,10);
else
bin_out=zeros(1,10);
end
v_bin_out=mo_dec/Ctot;
v_out=(refp-refm)*v_bin_out+refm;
%bin_act=decimalToBinaryVector((vinput*1024-mod(vinput*1024,1)),10);
final_residue=Ctot*(vinput-v_out);
%v_out_array=[v_out_array,mo_dec];
%residue_array=[residue_array,final_residue];
v_out_array(iter)=mo_dec;
residue_array(iter)=final_residue;
end

% %Final Modifications in MO(NO MODIFICATION)


% mo=[vbin,sar_bit];
% mo_dec=binaryVectorToDecimal(mo);
% if sar_bit(2)==1 && sar_red(1)==1
% mo_dec=mo_dec+0;
% end
% if sar_bit(2)==0 && sar_red(1)==0
% mo_dec=mo_dec-0;
% end
% if sar_bit(7)==1 && sar_red(2)==1
% mo_dec=mo_dec+0;
% end
% if sar_bit(7)==0 && sar_red(2)==0
% mo_dec=mo_dec-0;
% end
% %Residue/Quantization Plot
% if(mo_dec~=0)
% bin_out=decimalToBinaryVector(mo_dec,10);
% else
% bin_out=zeros(1,10);
% end
% v_bin_out=mo_dec/Ctot;
% v_out=(refp-refm)*v_bin_out+refm;
% %bin_act=decimalToBinaryVector((vinput*1024-mod(vinput*1024,1)),10);
% final_residue=Ctot*(vinput-v_out);
% %v_out_array=[v_out_array,mo_dec];
% %residue_array=[residue_array,final_residue];
% v_out_array(iter)=mo_dec;
% residue_array(iter)=final_residue;
% end

figure
plot(vinp,residue_array,"r-","LineWidth",1.5);
hold on
grid on
title("Residue Plot")
xlabel("Input(V)")
ylabel("Residue(LSB)")
figure
plot(vinp,v_out_array,"b-","LineWidth",1);
grid on
hold on
title("Output Code vs. Input")
xlabel("Input(V)")
ylabel("Output Code(LSB)")
%vinput=[0,vinput,1];
%v_out_array=[0,v_out_array,4095];
figure
s=inldnl(vinp,v_out_array,[0,1],"ADC");
plot(s.Codes,s.INL,"g-","Linewidth",1);
grid on
hold on
xlim([0,1024])
title("INL Plot")
xlabel("Output Code (LSB)")
ylabel("INL(LSB)")
figure
plot(s.Codes, s.DNL,"b-","Linewidth",1);
grid on
hold on
xlim([0,1024])
title("DNL Plot")
xlabel("Output Code (LSB)")
ylabel("DNL(LSB)")

You might also like