function casp_usd;
%Calculation of capacitance-voltage dependence of a
%reverse-biased P-N junction to illustrate the
%SPICE model and parameters
%(Section 2.4 in "Understanding Semiconductor Devices")

%   Sima Dimitrijev, September 27, 1999.
%   Copyright (c) 2000, Oxford University Press.

global conW_usd14; global conEXP_usd14; 
global con_go_usd14; 

%OPEN PARENT FIGURE
Hf=figure('NumberTitle','off','Name',...
   'Section 2.4: SPICE Model and Parameters of P-N Junction Capacitor',...
   'Color',[0.8 0.8 0.8]);
set(gcf,'InvertHardCopy','off');

hold on;
Ha0=gca;
set(Ha0,'Xlim',[0 1],'Ylim',[0 1],'Visible','off',...
   'Position',[0.0 0.0 1.0 1.0]);

%READ THE INTRO TEXT              
introtext ={'UNDERSTANDING SEMICONDUCTOR DEVICES ';...
      'Section 2.4: SPICE Model and Parameters of P-N Junction Capacitor' ;...
      ' ';...
      'Set SPICE parameters to see the effects on the capacitance-voltage curve';...
      'of a reverse-biased P-N junction.';... 
      ' ';... 
      'Experimental data can be entered to illustrate the graphical ';...
      'parameter-measurement technique, and/or compare the data with the model.'};   

Ht_intro=text(0.1,0.55,char(introtext),'Units','normalized');
Hp_cont=uicontrol(Hf,'Units','normalized','Position',[0.1 0.1 0.7 0.1],...
   'Style','pushbutton','String','CLICK HERE TO CONTINUE!','CallBack','con_go_usd14=1;');
con_go_usd14=0;
while con_go_usd14<1; 
   pause(0.2);  
   if ishandle(Hf)==0;
      return; 
   end;
end
con_go_usd14=0;
set(Ht_intro,'Visible','off');
set(Hp_cont,'Visible','off','Enable','off');
drawnow;

conW_usd14=0; conEXP_usd14=0; con_go_usd14=0; 

Hframe1=patch([0.65 0.99 0.99 0.65 0.65],[0.61 0.61 0.99 0.99 0.61],[0.75 0.75 0.75]);
set(Hframe1,'FaceColor',[0.75 0.75 0.75],'EdgeColor',[0 0 0]);
Hframe2=patch([0.49 0.99 0.99 0.49 0.49],[0.11 0.11 0.45 0.45 0.11],[0.75 0.75 0.75]);
set(Hframe2,'FaceColor',[0.75 0.75 0.75],'EdgeColor',[0 0 0]);

hold off;
Ht_txt(1)=text(0.83,0.95,'SPICE MODEL','Color','k','HorizontalAlignment','center',...
   'VerticalAlignment','bottom');
Ht_txt(2)=text(0.83,0.88,'C_{d}=CJO(1+V_{R}/ VJ)^{-M}','Color','k','HorizontalAlignment','center',...
   'VerticalAlignment','bottom');
Ht_txt(3)=text(0.83,0.81,'SPICE PARAMETERS','Color','k','HorizontalAlignment','center',...
   'VerticalAlignment','bottom');

Ht_par(1)=text(0.78,0.75,'CJO=','Color','k','HorizontalAlignment','right',...
   'VerticalAlignment','bottom');
Ht_edit(1)=uicontrol(Hf,'Units','normalized','Position',[0.78 0.75 0.10 0.04],...
   'Style','edit','String','','CallBack','conW_usd14=1;',...
   'BackgroundColor',[1 1 1],'ForegroundColor','b');
Ht_un(1)=text(0.885,0.75,'F ','Color','k','HorizontalAlignment','left',...
   'VerticalAlignment','bottom');
Ht_par(2)=text(0.78,0.69,'M=','Color','k','HorizontalAlignment','right',...
   'VerticalAlignment','bottom');
Ht_edit(2)=uicontrol(Hf,'Units','normalized','Position',[0.78 0.69 0.10 0.04],...
   'Style','edit','String','','CallBack','conW_usd14=2;',...
   'BackgroundColor',[1 1 1],'ForegroundColor','b');
Ht_un(2)=text(0.885,0.69,'- ','Color','k','HorizontalAlignment','left',...
   'VerticalAlignment','bottom');
Ht_par(3)=text(0.78,0.63,'VJ=','Color','k','HorizontalAlignment','right',...
   'VerticalAlignment','bottom');
Ht_edit(3)=uicontrol(Hf,'Units','normalized','Position',[0.78 0.63 0.10 0.04],...
   'Style','edit','String','','CallBack','conW_usd14=3;',...
   'BackgroundColor',[1 1 1],'ForegroundColor','b');
Ht_un(3)=text(0.885,0.63,'V ','Color','k','HorizontalAlignment','left',...
   'VerticalAlignment','bottom');

Ht_par(4)=text(0.78,0.55,'C_{p}=','Color','k','HorizontalAlignment','right',...
   'VerticalAlignment','bottom');
Ht_edit(4)=uicontrol(Hf,'Units','normalized','Position',[0.78 0.55 0.10 0.04],...
   'Style','edit','String','','CallBack','conW_usd14=4;',...
   'BackgroundColor',[0.75 0.75 0.75],'ForegroundColor','k');
Ht_un(4)=text(0.885,0.55,'pF ','Color','k','HorizontalAlignment','left',...
   'VerticalAlignment','bottom');
Ht_par(5)=text(0.78,0.49,'V_{R-max}=','Color','k','HorizontalAlignment','right',...
   'VerticalAlignment','bottom');
Ht_edit(5)=uicontrol(Hf,'Units','normalized','Position',[0.78 0.49 0.10 0.04],...
   'Style','edit','String','6','CallBack','conW_usd14=5;',...
   'BackgroundColor',[0.75 0.75 0.75],'ForegroundColor','k');
Ht_un(5)=text(0.885,0.49,'V ','Color','k','HorizontalAlignment','left',...
   'VerticalAlignment','bottom');

Ht_txt(4)=text(0.74,0.40,'EXPERIMENTAL DATA','Color','k','HorizontalAlignment','center',...
   'VerticalAlignment','bottom');
Ht_txt(5)=text(0.50,0.35,'V_{R}  (V)','Color','k','HorizontalAlignment','left',...
   'VerticalAlignment','bottom');

Ht_exp(1,1)=uicontrol(Hf,'Units','normalized','Position',[0.50 0.3 0.06 0.04],...
   'Style','edit','String','0.0','CallBack','conW_usd14=7;',...
   'BackgroundColor',[0.75 0.75 0.75],'ForegroundColor','r');
Ht_exp(1,2)=uicontrol(Hf,'Units','normalized','Position',[0.57 0.3 0.06 0.04],...
   'Style','edit','String','0.5','CallBack','conW_usd14=8;',...
   'BackgroundColor',[0.75 0.75 0.75],'ForegroundColor','r');
Ht_exp(1,3)=uicontrol(Hf,'Units','normalized','Position',[0.64 0.3 0.06 0.04],...
   'Style','edit','String','1.0','CallBack','conW_usd14=9;',...
   'BackgroundColor',[0.75 0.75 0.75],'ForegroundColor','r');
Ht_exp(1,4)=uicontrol(Hf,'Units','normalized','Position',[0.71 0.3 0.06 0.04],...
   'Style','edit','String','2.0','CallBack','conW_usd14=10;',...
   'BackgroundColor',[0.75 0.75 0.75],'ForegroundColor','r');
Ht_exp(1,5)=uicontrol(Hf,'Units','normalized','Position',[0.78 0.3 0.06 0.04],...
   'Style','edit','String','3.0','CallBack','conW_usd14=11;',...
   'BackgroundColor',[0.75 0.75 0.75],'ForegroundColor','r');
Ht_exp(1,6)=uicontrol(Hf,'Units','normalized','Position',[0.85 0.3 0.06 0.04],...
   'Style','edit','String','4.0','CallBack','conW_usd14=12;',...
   'BackgroundColor',[0.75 0.75 0.75],'ForegroundColor','r');
Ht_exp(1,7)=uicontrol(Hf,'Units','normalized','Position',[0.92 0.3 0.06 0.04],...
   'Style','edit','String','5.0','CallBack','conW_usd14=13;',...
   'BackgroundColor',[0.75 0.75 0.75],'ForegroundColor','r');

Ht_txt(6)=text(0.50,0.23,'C_{meas}  (pF)','Color','k','HorizontalAlignment','left',...
   'VerticalAlignment','bottom');

Ht_exp(2,1)=uicontrol(Hf,'Units','normalized','Position',[0.50 0.18 0.06 0.04],...
   'Style','edit','String','4.70','CallBack','conW_usd14=14;',...
   'BackgroundColor',[0.75 0.75 0.75],'ForegroundColor','r');
Ht_exp(2,2)=uicontrol(Hf,'Units','normalized','Position',[0.57 0.18 0.06 0.04],...
   'Style','edit','String','4.05','CallBack','conW_usd14=15;',...
   'BackgroundColor',[0.75 0.75 0.75],'ForegroundColor','r');
Ht_exp(2,3)=uicontrol(Hf,'Units','normalized','Position',[0.64 0.18 0.06 0.04],...
   'Style','edit','String','3.68','CallBack','conW_usd14=16;',...
   'BackgroundColor',[0.75 0.75 0.75],'ForegroundColor','r');
Ht_exp(2,4)=uicontrol(Hf,'Units','normalized','Position',[0.71 0.18 0.06 0.04],...
   'Style','edit','String','3.25','CallBack','conW_usd14=17;',...
   'BackgroundColor',[0.75 0.75 0.75],'ForegroundColor','r');
Ht_exp(2,5)=uicontrol(Hf,'Units','normalized','Position',[0.78 0.18 0.06 0.04],...
   'Style','edit','String','3.00','CallBack','conW_usd14=18;',...
   'BackgroundColor',[0.75 0.75 0.75],'ForegroundColor','r');
Ht_exp(2,6)=uicontrol(Hf,'Units','normalized','Position',[0.85 0.18 0.06 0.04],...
   'Style','edit','String','2.83','CallBack','conW_usd14=19;',...
   'BackgroundColor',[0.75 0.75 0.75],'ForegroundColor','r');
Ht_exp(2,7)=uicontrol(Hf,'Units','normalized','Position',[0.92 0.18 0.06 0.04],...
   'Style','edit','String','2.70','CallBack','conW_usd14=20;',...
   'BackgroundColor',[0.75 0.75 0.75],'ForegroundColor','r');

Hp_cont=uicontrol(Hf,'Units','normalized','Position',[0.5 0.12 0.48 0.04],...
   'Style','pushbutton','String','ENTER','CallBack','conW_usd14=6;');

%CLOSE
Hp_cont=uicontrol(Hf,'Units','normalized','Position',[0.65 0.01 0.34 0.08],...
   'Style','pushbutton','String','CLOSE','CallBack','con_go_usd14=1;');


%%%%%%INITIAL VALUES%%%%%%%%%%%%%%%%%
Cjo=0;
M=0;
VJ=str2num(get(Ht_edit(3),'String'));
Cp=0;
Vmax=str2num(get(Ht_edit(5),'String'));
Vrexp=str2num(''); Cdexp=str2num('');
ii=0;
for i=1:7;
   ii=ii+1;
   anum1=str2num(get(Ht_exp(1,i),'String'));
   anum2=str2num(get(Ht_exp(2,i),'String'));
   if ((isempty(anum1)==1)+(isempty(anum2)==1)); 
      ii=ii-1;
   else
      Vrexp(ii)=anum1;
      Cdexp(ii)=anum2;
      Cdexp0(ii)=Cdexp(ii)-Cp;
   end   
end   
Cdmin=min(Cdexp0);
Cdmax=max(Cdexp0);
Vrexplog=log10(1+Vrexp);
Vrlogmax=max(Vrexplog); 
if Vrlogmax<0.1; Vrlogmax=0.1; end 
Ndig=10^floor(log10(Vrlogmax+0.1));  
Vmaxloggr=Ndig*0.1*ceil((Vrlogmax/Ndig)/0.1);


%%%%%%%%%%%%%%%%%%%%%
%%%SET THE GRAPHS%%%%
%%%%%%%%%%%%%%%%%%%%%
Vr=[0:Vmax/100:Vmax];

%Cd-Vr
Cd=Vr*0-1;
Ha_cdvr=axes('Xlim',[0 Vmax],'Ylim',[0 5],'Position',[0.10 0.60 0.48 0.35],...
   'Color',[0.8 0.8 0.8],'FontAngle','italic','XGrid','on','YGrid','on','YScale','lin','Box','on');
hold on;
xlabel('REVERSE-BIAS VOLTAGE, V_{R} (V)','Color','k'); 
ylabel('C_{d}=C_{meas}-C_{p}   (pF)','Color','k');

Hp_lin_exp=plot(Vrexp,Cdexp0,'.r','MarkerSize',15);
Hp_lin_old=plot(Vr,Cd,'-b','LineWidth',1);
set(Hp_lin_old,'Color',[0.5 0.5 0.7]);
Hp_lin=plot(Vr,Cd,'-b','LineWidth',2);
hold off;

%log Cd - log Vr
Cdlog_min=0; Cdlog_max=1;
Vmaxloggr=1;
Vrlog=Vr;
Cdlog=-1+Vrlog;
Ha_log=axes('Xlim',[0 Vmaxloggr],'Ylim',[Cdlog_min Cdlog_max],'Position',[0.10 0.09 0.33 0.37],...
   'Color',[0.8 0.8 0.8],'FontAngle','italic','XGrid','on','YGrid','on','YScale','lin','Box','on');
hold on;
xlabel('log(1+V_{R} /VJ)','Color','k'); 
ylabel('log(C_{meas}-C_{p} ) (C in pF)','Color','k');

Hp_log_exp=plot(Vrexp,Cdexp0,'.r','MarkerSize',15,'Visible','off');
Hp_log_old=plot(Vrlog,Cdlog,'-b','LineWidth',1);
set(Hp_log_old,'Color',[0.5 0.5 0.7]);
Hp_log=plot(Vrlog,Cdlog,'-b','LineWidth',2);
hold off;


conW_usd14=0; conEXP_usd14=0; con_go_usd14=0; 
firstgo=1; linlg=1;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%MAIN LOOP %%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
while con_go_usd14<1;
   
switch conW_usd14;
case 1;
   Cjo=str2num(get(Ht_edit(1),'String'));
   if isempty(Cjo)==1; 
      Cjo=0;
      set(Ht_edit(1),'String','');
   end
   if Cjo<0;
      Cjo=0;
      set(Ht_edit(1),'String','');
   end
case 2;
   M=str2num(get(Ht_edit(2),'String'));
   if isempty(M)==1; 
      M=0;
      set(Ht_edit(2),'String','');
   end
   if M<0;
      M=0;
      set(Ht_edit(2),'String','');
   end
   if M>1;
      M=1;
      set(Ht_edit(2),'String','1');
   end
case 3;
     VJ=str2num(get(Ht_edit(3),'String'));
   if isempty(VJ)==1; 
      set(Ht_edit(3),'String','');
   else
      if VJ<=0;
      VJ=str2num('');
      set(Ht_edit(3),'String','');
      end
      if VJ>10;
      VJ=10;
      set(Ht_edit(3),'String','10');
      end
   end
case 4;
   Cp=str2num(get(Ht_edit(4),'String'));
   if isempty(Cp)==1; 
      Cp=0;
      set(Ht_edit(4),'String','');
   end
   if Cp<0;
      Cp=0;
      set(Ht_edit(4),'String','');
   end
   if length(Cdexp>0);
      Cdexp0=Cdexp-Cp;
   end   
case 5;
   Vmax=str2num(get(Ht_edit(5),'String'));
   if isempty(Vmax)==1; 
      Vmax=6;
      set(Ht_edit(5),'String','6');
   end
   if Vmax<=0;
      Vmax=6;
      set(Ht_edit(5),'String','6');
   end
   Vr=[0:Vmax/100:Vmax];
   if isempty(VJ)==0;
      Vrlog=log10(1+Vr/VJ);
   else
      Vrlog=log10(1+Vr);
   end
   firstgo=1;
end; %switch conW_usd14   

if (conW_usd14>6)*(conW_usd14<14); %Exp. voltage is changed
   ii=conW_usd14-6;
   anum1=str2num(get(Ht_exp(1,ii),'String'));
   if isempty(anum1)==1; 
      set(Ht_exp(1,ii),'String','');
      anum1=0;
   end   
   if anum1<0;
      set(Ht_exp(1,ii),'String','');
   end   
end   
if (conW_usd14>13); %Exp. capacitance is changed
   ii=conW_usd14-13;
   anum1=str2num(get(Ht_exp(1,ii),'String'));
   if isempty(anum1)==1; 
      set(Ht_exp(1,ii),'String','');
      anum1=0;
   end   
   if anum1<0;
      set(Ht_exp(1,ii),'String','');
   end   
end   

   
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%RECALCULATE THE CURVES%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if (conW_usd14>0)*(conW_usd14<7);   
   if firstgo<1;
      Cd_old=Cd;
      Cdlog_old=Cdlog;
   end   
   
   %Renew the exp. data
   Vrexp=str2num(''); Cdexp=str2num(''); Cdexp0=str2num('');
   ii=0;
   for i=1:7;
      ii=ii+1;
      anum1=str2num(get(Ht_exp(1,i),'String'));
      if isempty(anum1)==1; 
         set(Ht_exp(1,i),'String',''); 
      else
         if anum1<0; set(Ht_exp(1,i),'String',''); end
      end
      anum2=str2num(get(Ht_exp(2,i),'String'));
      if isempty(anum2)==1; 
         set(Ht_exp(2,i),'String',''); 
      else
         if anum2<0; set(Ht_exp(2,i),'String',''); end
      end
      
      if ((isempty(anum1)==1)+(isempty(anum2)==1)); 
         ii=ii-1;
      else
         Vrexp(ii)=anum1;
         Cdexp(ii)=anum2;
         Cdexp0(ii)=Cdexp(ii)-Cp;
      end   
   end   
   
  %Renew Vrlog 
  if isempty(VJ)==0;
      Vrlog=log10(1+Vr/VJ);
   else
      Vrlog=log10(1+Vr);
   end

   
   %Set Vmaxloggr and Vmaxgr
   if length(Cdexp0)>0;
      if isempty(VJ)==1;
         Vrexplog=log10(1+Vrexp);
      else   
         Vrexplog=log10(1+Vrexp./VJ);
      end   
      Vrlogmax=max(Vrexplog);   
      if Vrlogmax<0.1; Vrlogmax=0.1; end
      Ndig=10^floor(log10(Vrlogmax+0.1));  
      Vmaxloggr=Ndig*0.1*ceil((Vrlogmax/Ndig)/0.1);
   else
      Vrlogmax=max(Vrlog);   
      if Vrlogmax<0.1; Vrlogmax=0.1; end
      Ndig=10^floor(log10(Vrlogmax+0.1));  
      Vmaxloggr=Ndig*0.1*ceil((Vrlogmax/Ndig)/0.1);
   end
   Ndig=10^floor(log10(Vmax));
   Vmaxgr=Ndig*1*ceil((Vmax/Ndig)/1);
   
   if isempty(VJ)==1;
      Cd=(Vr*0+1)*Cjo*1e12;   
      if Cd>0;
         Cdlog=log10(Cd);
      else
         Cdlog=-1+Cd*0;
      end   
   else
      Cd=1e12*Cjo*(1+Vr/VJ).^(-M);
      if Cd>0;
         Cdlog=log10(Cd);
      else
         Cdlog=-1+Cd*0;
      end   
   end   
   
%Reset the graphs   
if firstgo==1;
   Cd_old=Cd;
   Cdlog_old=Cdlog;
   firstgo=0;
end 

if Cjo<=0;
   set(Hp_lin_old,'Visible','off');
   set(Hp_lin,'Visible','off');
   set(Hp_log_old,'Visible','off');
   set(Hp_log,'Visible','off');
   if length(Cdexp0)>0;
      Cdmax=max(Cdexp0);
      Cdmin=min(Cdexp0);
   end

else   
   if length(Cdexp0)>0;
      Cdmax1=max(1.1*Cd);
      Cdmax2=max(Cdexp0);
      if Cdmax2<=0;
         Cdexp0=str2num('');
         Cdmax=max(1.1*Cd);
      else   
         Cdmax=max([Cdmax1 Cdmax2]);
      end   
   else   
      Cdmax=max(1.1*Cd);
   end   
Ndig=10^floor(log10(Cdmax));
Cdmaxgr=Ndig*1*ceil((Cdmax/Ndig)/1);
   axes(Ha_cdvr);
   set(Ha_cdvr,'Xlim',[0 Vmax],'Ylim',[0 Cdmaxgr]);
   set(Hp_lin_old,'XData',Vr,'YData',Cd_old,'Visible','on');
   set(Hp_lin,'XData',Vr,'YData',Cd,'Visible','on');
   

if length(Cdexp0)>0;
   Cdmin1=min(Cd);
   Cdmin2=min(Cdexp0);
   if Cdmin2<=0;
      Cdexp0=str2num('');
      Cdmin=min(Cd);
   else      
      Cdmin=min([Cdmin1 Cdmin2]);
   end   
else   
   Cdmin=min(Cd);   
end   
Ndig=10^floor(log10(Cdmin));
Cdmingr=Ndig*1*floor((Cdmin/Ndig)/1);
axes(Ha_log);
   if Cdmingr==Cdmaxgr; Cdmaxgr=1.1*Cdmingr; end
   set(Ha_log,'Xlim',[0 Vmaxloggr],'Ylim',[log10(Cdmingr) log10(Cdmaxgr)]);
   set(Hp_log_old,'XData',Vrlog,'YData',Cdlog_old,'Visible','on');
   set(Hp_log,'XData',Vrlog,'YData',Cdlog,'Visible','on');
end

%Recalculate and set exper graphs

   if ((length(Cdexp0)<1)+(Cdmin<=0));
      set(Hp_lin_exp,'Visible','off');
   else
      if Cjo<=0;
      Ndig=10^floor(log10(Cdmax));
      Cdmaxgr=Ndig*1*ceil((Cdmax/Ndig)/1);
      axes(Ha_cdvr);
      set(Ha_cdvr,'Xlim',[0 Vmax],'Ylim',[0 Cdmaxgr]);
      end
      set(Hp_lin_exp,'XData',Vrexp,'YData',Cdexp0,'Visible','on');
   end  


   Cdexplog=0;
   if ((isempty(VJ)==1)+(Cdmin<=0)+(length(Cdexp0)<1));
      set(Hp_log_exp,'Visible','off');
   else
      Cdexplog=log10(Cdexp0);
      if Cjo<=0;
      Ndig=10^floor(log10(Cdmin));
      Cdmingr=Ndig*1*floor((Cdmin/Ndig)/1);
      axes(Ha_log);
      if Cdmingr==Cdmaxgr; Cdmaxgr=1.1*Cdmingr; end
      set(Ha_log,'Xlim',[0 Vmaxloggr],'Ylim',[log10(Cdmingr) log10(Cdmaxgr)]);
      end
      set(Hp_log_exp,'XData',Vrexplog,'YData',Cdexplog,'Visible','on');
   end   
   
if ishandle(Hf)==0;
   return;
end   
drawnow;

end;  %(conW_usd14>0)*(usdW_14<6);   

conW_usd14=0;
if ishandle(Hf)==0;
   return;
end   
tic;
while toc<0.5;
   pause(0.1);
end   
if ishandle(Hf)==0;
   return;
end   

end; %while con_go_usd14<1;
close;

