function disp_usd;
%Calculation of diode I-V and C-V charactersitics
%from set SPICE parameters.
%(Section 3.2 in "Understanding Semiconductor Devices")

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

global conW_usd15; 
global con_go_usd15; global conLIN_usd15;

%OPEN PARENT FIGURE
Hf=figure('NumberTitle','off','Name','Section 3.2: Diode Calculator (SPICE Model)',...
   '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 3.2: Diode Calculator (SPICE Model)' ;...
      ' ';...
      'Set SPICE diode parameters and/or temperature to see the effects on ';...
      'the current-voltage and capacitance-voltage characteristics. ';...
      'As a particular exercise, it is very useful to investigate the ';
      'relationship between the Is parameter and the turn-on voltage. '};   

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_usd15=1;');
con_go_usd15=0;
while con_go_usd15<1; 
   pause(0.2);  
   if ishandle(Hf)==0;
      return; 
   end;
end
con_go_usd15=0;
set(Ht_intro,'Visible','off');
set(Hp_cont,'Visible','off','Enable','off');
drawnow;

conW_usd15=0; con_go_usd15=0; conLIN_usd15=0;

Hframe=patch([0.70 1 1 0.70 0.70],[0.1 0.1 0.99 0.99 0.1],[0.75 0.75 0.75]);
set(Hframe,'FaceColor',[0.75 0.75 0.75],'EdgeColor',[0 0 0]);
Hframe2=patch([0.70 1 1 0.70 0.70],[0.1 0.1 0.17 0.17 0.1],[0.75 0.75 0.75]);
set(Hframe2,'FaceColor',[1 1 0.4],'EdgeColor',[0 0 0]);
hold off;

Ht_txt(1)=text(0.85,0.95,'STATIC PARAMETERS','Color','k','HorizontalAlignment','center',...
   'VerticalAlignment','bottom');
Ht_par(1)=text(0.82,0.90,'IS=','Color','k','HorizontalAlignment','right',...
   'VerticalAlignment','bottom');
Ht_edit(1)=uicontrol(Hf,'Units','normalized','Position',[0.82 0.90 0.10 0.04],...
   'Style','edit','String','1e-14','CallBack','conW_usd15=1;',...
   'BackgroundColor',[1 1 1],'ForegroundColor','r','FontWeight','bold');
Ht_un(1)=text(0.925,0.90,'A','Color','k','HorizontalAlignment','left',...
   'VerticalAlignment','bottom');
Ht_par(2)=text(0.82,0.84,'N=','Color','k','HorizontalAlignment','right',...
   'VerticalAlignment','bottom');
Ht_edit(2)=uicontrol(Hf,'Units','normalized','Position',[0.82 0.84 0.10 0.04],...
   'Style','edit','String','1','CallBack','conW_usd15=2;',...
   'BackgroundColor',[1 1 1],'ForegroundColor','r','FontWeight','bold');
Ht_un(2)=text(0.925,0.84,' -','Color','k','HorizontalAlignment','left',...
   'VerticalAlignment','bottom');
Ht_par(3)=text(0.82,0.78,'RS=','Color','k','HorizontalAlignment','right',...
   'VerticalAlignment','bottom');
Ht_edit(3)=uicontrol(Hf,'Units','normalized','Position',[0.82 0.78 0.10 0.04],...
   'Style','edit','String','0','CallBack','conW_usd15=3;',...
   'BackgroundColor',[1 1 1],'ForegroundColor','r','FontWeight','bold');
Ht_un(3)=text(0.925,0.78,' \Omega','Color','k','HorizontalAlignment','left',...
   'VerticalAlignment','bottom');
Ht_par(4)=text(0.82,0.72,'BV=','Color','k','HorizontalAlignment','right',...
   'VerticalAlignment','bottom');
Ht_edit(4)=uicontrol(Hf,'Units','normalized','Position',[0.82 0.72 0.10 0.04],...
   'Style','edit','String',' ','CallBack','conW_usd15=4;',...
   'BackgroundColor',[1 1 1],'ForegroundColor','k','FontWeight','normal');
Ht_un(4)=text(0.925,0.72,'V','Color','k','HorizontalAlignment','left',...
   'VerticalAlignment','bottom');

Ht_txt(2)=text(0.85,0.65,'DYNAMIC PARAMETERS','Color','k','HorizontalAlignment','center',...
   'VerticalAlignment','bottom');
Ht_par(5)=text(0.82,0.59,'Cjo=','Color','k','HorizontalAlignment','right',...
   'VerticalAlignment','bottom');
Ht_edit(5)=uicontrol(Hf,'Units','normalized','Position',[0.82 0.59 0.10 0.04],...
   'Style','edit','String','','CallBack','conW_usd15=5;',...
   'BackgroundColor',[0.6 1 0.6],'ForegroundColor','k');
Ht_un(5)=text(0.925,0.59,'F','Color','k','HorizontalAlignment','left',...
   'VerticalAlignment','bottom');
Ht_par(6)=text(0.82,0.53,'VJ=','Color','k','HorizontalAlignment','right',...
   'VerticalAlignment','bottom');
Ht_edit(6)=uicontrol(Hf,'Units','normalized','Position',[0.82 0.53 0.10 0.04],...
   'Style','edit','String','','CallBack','conW_usd15=6;',...
   'BackgroundColor',[0.6 1 0.6],'ForegroundColor','k');
Ht_un(6)=text(0.925,0.53,'V','Color','k','HorizontalAlignment','left',...
   'VerticalAlignment','bottom');
Ht_par(7)=text(0.82,0.47,'M=','Color','k','HorizontalAlignment','right',...
   'VerticalAlignment','bottom');
Ht_edit(7)=uicontrol(Hf,'Units','normalized','Position',[0.82 0.47 0.10 0.04],...
   'Style','edit','String','','CallBack','conW_usd15=7;',...
   'BackgroundColor',[0.6 1 0.6],'ForegroundColor','k');
Ht_un(7)=text(0.925,0.47,' -','Color','k','HorizontalAlignment','left',...
   'VerticalAlignment','bottom');
Ht_par(8)=text(0.82,0.41,'TT=','Color','k','HorizontalAlignment','right',...
   'VerticalAlignment','bottom');
Ht_edit(8)=uicontrol(Hf,'Units','normalized','Position',[0.82 0.41 0.10 0.04],...
   'Style','edit','String','','CallBack','conW_usd15=8;',...
   'BackgroundColor',[0.6 1 1],'ForegroundColor','k');
Ht_un(8)=text(0.925,0.41,'s','Color','k','HorizontalAlignment','left',...
   'VerticalAlignment','bottom');

Ht_txt(3)=text(0.85,0.34,'TEMPERATURE-RELATED','Color','k','HorizontalAlignment','center',...
   'VerticalAlignment','bottom');
Ht_txt(3)=text(0.85,0.30,'PARAMETERS','Color','k','HorizontalAlignment','center',...
   'VerticalAlignment','bottom');

Ht_par(9)=text(0.82,0.24,'EG=','Color','k','HorizontalAlignment','right',...
   'VerticalAlignment','bottom');
Ht_edit(9)=uicontrol(Hf,'Units','normalized','Position',[0.82 0.24 0.10 0.04],...
   'Style','edit','String','1.12','CallBack','conW_usd15=9;',...
   'BackgroundColor',[1 1 0.4],'ForegroundColor','r');
Ht_un(9)=text(0.925,0.24,'eV','Color','k','HorizontalAlignment','left',...
   'VerticalAlignment','bottom');
Ht_par(10)=text(0.82,0.18,'XTI=','Color','k','HorizontalAlignment','right',...
   'VerticalAlignment','bottom');
Ht_edit(10)=uicontrol(Hf,'Units','normalized','Position',[0.82 0.18 0.10 0.04],...
   'Style','edit','String','3','CallBack','conW_usd15=10;',...
   'BackgroundColor',[1 1 0.4],'ForegroundColor','r');
Ht_un(10)=text(0.925,0.18,' -','Color','k','HorizontalAlignment','left',...
   'VerticalAlignment','bottom');

Ht_par(11)=text(0.86,0.11,'Temperature=','Color','k','HorizontalAlignment','right',...
   'VerticalAlignment','bottom');
Ht_edit(11)=uicontrol(Hf,'Units','normalized','Position',[0.86 0.11 0.10 0.04],...
   'Style','edit','String','27','CallBack','conW_usd15=11;',...
   'BackgroundColor',[1 1 0.4],'ForegroundColor','r');
Ht_un(11)=text(0.965,0.11,'^{\circ}C','Color','k','HorizontalAlignment','left',...
   'VerticalAlignment','bottom');

%CLOSE
Hp_cont=uicontrol(Hf,'Units','normalized','Position',[0.71 0.01 0.28 0.07],...
   'Style','pushbutton','String','CLOSE','CallBack','con_go_usd15=1;');


%%%%%%%%%%%%%%%%%%%%%%%
%%%CONSTANTS%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%
Vfmax=1.4;
Ifmax=0.01;
Vrmin=-5;
Irmin=-2e-14;
Cmax=5;
kb=8.62e-5;
Tnom=27+273.15;
T=Tnom;
Vt=kb*T;
FC=0.5;

%%%%%%%%%%%%%%%%%%%%%
%%%SET THE GRAPHS%%%%
%%%%%%%%%%%%%%%%%%%%%

%If-Vf
If=[0:Ifmax/100:Ifmax];
Vf=If*0;
Vf_old=Vf;
Ha_ifvf=axes('Xlim',[1e-3 Vfmax],'Ylim',[1e-55 Ifmax],'Position',[0.40 0.60 0.25 0.35],...
   'Color',[0.8 0.8 0.8],'FontAngle','italic','XGrid','on','YGrid','on','Box','on');
hold on;
xlabel('V_{D} (V)'); ylabel('I_{D} (A)');
Hp_ifvfold=plot(Vf_old,If*1000,'-k','LineWidth',1);
set(Hp_ifvfold,'Color',[0.5 0.5 1]);
Hp_ifvf=plot(Vf,If,'-b','LineWidth',2);
hold off;

%Ir-Vr
Vr=[Vrmin:abs(Vrmin/100):0];
Vrpl=Vr;
Ir=Vr*0;
Ir_old=Ir;
Ha_irvr=axes('Xlim',[Vrmin -1e-3],'Ylim',[Irmin -1e-55],...
   'Position',[0.05 0.40 0.35 0.20],...
   'Color',[0.8 0.8 0.8],'FontAngle','italic','XGrid','on','YGrid','on','Box','on',...
   'XAxisLocation','top','YAxisLocation','right');
hold on;
xlabel('V_{D} (V)'); %ylabel('I_{D} (mA)');
Hp_irvrold=plot(Vr,Ir_old,'-k','LineWidth',1);
set(Hp_irvrold,'Color',[0.5 0.5 1]);
Hp_irvr=plot(Vr,Ir,'-b','LineWidth',2);
hold off;

%C-V
Vcv=[Vr Vf]*0; Vcv_old=Vcv;
Ccv=Vcv*0; Ccv_old=Ccv;
Ha_cv=axes('Xlim',[Vrmin Vfmax],'Ylim',[0 Cmax],'Position',[0.15 0.10 0.45 0.26],...
   'Color',[0.8 0.8 0.8],'FontAngle','italic','XGrid','on','YGrid','on','Box','on');
hold on;
xlabel('V_{D} (V)'); ylabel('C_{d}+C_{s}  (pF)');
Hp_cvold=plot(Vcv_old,Ccv_old,'-k','LineWidth',1);
set(Hp_cvold,'Color',[0.5 0.5 1]);

Hp_cv=plot(Vcv,Ccv,'-b','LineWidth',2);
hold off;

%The max/min voltages/currents
%Vrmin
axes(Ha0);
Ht_par(12)=text(0.125,0.90,'V_{D-min}=','Color','k','HorizontalAlignment','right',...
   'VerticalAlignment','bottom');
Ht_edit(12)=uicontrol(Hf,'Units','normalized','Position',[0.125 0.90 0.08 0.04],...
   'Style','edit','String','-5','CallBack','conW_usd15=12;',...
   'BackgroundColor',[0.8 0.8 0.8],'ForegroundColor','k');
Ht_un(12)=text(0.215,0.90,'V','Color','k','HorizontalAlignment','left',...
   'VerticalAlignment','bottom');
Ht_par(13)=text(0.125,0.84,'I_{D-min}=','Color','k','HorizontalAlignment','right',...
   'VerticalAlignment','bottom');
Ht_edit(13)=uicontrol(Hf,'Units','normalized','Position',[0.125 0.84 0.08 0.04],...
   'Style','edit','String','','CallBack','conW_usd15=13;',...
   'BackgroundColor',[0.8 0.8 0.8],'ForegroundColor','k');
Ht_un(13)=text(0.215,0.84,'A','Color','k','HorizontalAlignment','left',...
   'VerticalAlignment','bottom');
Ht_par(14)=text(0.125,0.78,'I_{D-max}=','Color','k','HorizontalAlignment','right',...
   'VerticalAlignment','bottom');
Ht_edit(14)=uicontrol(Hf,'Units','normalized','Position',[0.125 0.78 0.08 0.04],...
   'Style','edit','String','0.01','CallBack','conW_usd15=14;',...
   'BackgroundColor',[0.8 0.8 0.8],'ForegroundColor','k');
Ht_un(14)=text(0.215,0.78,'A','Color','k','HorizontalAlignment','left',...
   'VerticalAlignment','bottom');

%LIN/LOG buttons
Hr_lin(1)=uicontrol(Hf,'Units','normalized','BackgroundColor',[0.8 0.8 0.8],...
   'Position',[0.57 0.47 0.08 0.03],'ForegroundColor','k',...
   'Style','radio','String','LIN','CallBack','conLIN_usd15=1;','Value',1);
Hr_lin(2)=uicontrol(Hf,'Units','normalized','BackgroundColor',[0.8 0.8 0.8],...
   'Position',[0.57 0.44 0.08 0.03],'ForegroundColor','k',...
   'Style','radio','String','LOG','CallBack','conLIN_usd15=2;','Value',0);

%%%%%%%%%%%%%%%%%%%%%%%
%THE INITIAL PARAMETERS
%%%%%%%%%%%%%%%%%%%%%%%
IS=str2num(get(Ht_edit(1),'String'));
N=str2num(get(Ht_edit(2),'String'));
RS=str2num(get(Ht_edit(3),'String'));
BV=str2num(get(Ht_edit(4),'String')); %Empty
Cjo=str2num(get(Ht_edit(5),'String'));
   if isempty(Cjo)==1; Cjo=0; end
VJ=str2num(get(Ht_edit(6),'String')); %Empty
M=str2num(get(Ht_edit(7),'String'));  
    if isempty(M)==1; M=0; end
TT=str2num(get(Ht_edit(8),'String'));
    if isempty(TT)==1; TT=0; end
EG=str2num(get(Ht_edit(9),'String'));    
XTI=str2num(get(Ht_edit(10),'String'));
T=str2num(get(Ht_edit(11),'String'))+273.15;
Vrmin=str2num(get(Ht_edit(12),'String'));
Ifmin=str2num(get(Ht_edit(13),'String'));
Ifmax=str2num(get(Ht_edit(14),'String'));


conW_usd15=21; conLIN_usd15=0; con_go_usd15=0;
firstgo=1; linlg=1;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%MAIN LOOP %%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
while con_go_usd15<1;
   
if conLIN_usd15>0;
   set(Hr_lin,'Value',0);
   if conLIN_usd15==2; %log
      linlg=0;
      set(Hr_lin(2),'Value',1);
      Ifminlog=log(Ifmax*1e-4);
      Ifmaxlog=log(Ifmax);
      If=exp([Ifminlog:(Ifmaxlog-Ifminlog)/100:Ifmaxlog]);
      Vf=If*0;
      Vf_old=Vf;
   else %linear
      linlg=1;
      set(Hr_lin(1),'Value',1);
      If=[0:Ifmax/100:Ifmax];
      Vf=If*0;
      Vf_old=Vf;
   end   
firstgo=1;         
conLIN_usd15=0;   
end;   %conLIN_usd15>0;
   
switch conW_usd15;   
   
case 1; %IS
   IS=str2num(get(Ht_edit(1),'String'));
   set(Ht_edit(1),'ForegroundColor','k');
   if isempty(IS)==1; 
      IS=1e-14; 
      set(Ht_edit(1),'String','1e-14','ForegroundColor','r');
   end
   if IS<=1e-50;
      IS=1e-14; 
      set(Ht_edit(1),'String','1e-14','ForegroundColor','r');
   end
   
case 2; %N   
   N=str2num(get(Ht_edit(2),'String'));
   set(Ht_edit(2),'ForegroundColor','k');
   if isempty(N)==1; 
      N=1; 
      set(Ht_edit(2),'String','1','ForegroundColor','r');
   end
   if N<=0;
      N=1; 
      set(Ht_edit(2),'String','1','ForegroundColor','r');
   end
   
case 3;    
   RS=str2num(get(Ht_edit(3),'String'));
   set(Ht_edit(3),'ForegroundColor','k');
   if isempty(RS)==1; 
      RS=0; 
      set(Ht_edit(3),'String','0','ForegroundColor','r');
   end
   if RS<0;
      RS=0; 
      set(Ht_edit(3),'String','0','ForegroundColor','r');
   end
   
case 4;    
   BV=str2num(get(Ht_edit(4),'String'));
   if isempty(BV)==1;
      set(Ht_edit(4),'String','');
   else   
   if BV<=0;
      BV=str2num(''); 
      set(Ht_edit(4),'String','');
   end
   end

case 5;    
   Cjo=str2num(get(Ht_edit(5),'String'));
   if isempty(Cjo)==1; 
      Cjo=0; 
      set(Ht_edit(5),'String','');
   end
   if Cjo<0;
      Cjo=0; 
      set(Ht_edit(5),'String','');
   end;    
   
case 6;    
   VJ=str2num(get(Ht_edit(6),'String'));
   if isempty(VJ)==1; 
      set(Ht_edit(6),'String','');
   else
   if VJ<0;
      VJ=str2num(''); 
      set(Ht_edit(6),'String','');
   end
   if VJ>10;
      VJ=str2num(''); 
      set(Ht_edit(6),'String','');
   end
   end

case 7;   
   M=str2num(get(Ht_edit(7),'String'));
   if isempty(M)==1; 
      M=0; 
      set(Ht_edit(7),'String','');
   end
   if M<0;
      M=0; 
      set(Ht_edit(7),'String','');
   end
   if M>10;
      M=0;
      set(Ht_edit(7),'String','');
   end
   
case 8;   
   TT=str2num(get(Ht_edit(8),'String'));
   if isempty(TT)==1; 
      TT=0; 
      set(Ht_edit(8),'String','');
   end
   if TT<0;
      TT=0; 
      set(Ht_edit(8),'String','');
   end
   
case 9;   
   EG=str2num(get(Ht_edit(9),'String'));
   set(Ht_edit(9),'ForegroundColor','k');
   if isempty(EG)==1; 
      EG=1.12; 
      set(Ht_edit(9),'String','1.12','ForegroundColor','r');
   end
   if EG<0;
      EG=1.12; 
      set(Ht_edit(9),'String','1.12','ForegroundColor','r');
   end
   if EG>10;
      EG=1.12; 
      set(Ht_edit(9),'String','1.12','ForegroundColor','r');
   end
   
case 10;   
   XTI=str2num(get(Ht_edit(10),'String'));
   set(Ht_edit(10),'ForegroundColor','k');
   if isempty(XTI)==1; 
      XTI=3; 
      set(Ht_edit(10),'String','3','ForegroundColor','r');
   end
   if XTI<0;
      XTI=3; 
      set(Ht_edit(10),'String','3','ForegroundColor','r');
   end
   if EG>10;
      XTI=3; 
      set(Ht_edit(10),'String','3','ForegroundColor','r');
   end
   
case 11;   
   T=str2num(get(Ht_edit(11),'String'));
   set(Ht_edit(11),'ForegroundColor','k');
   if isempty(T)==1; 
      T=27+273.15; 
      set(Ht_edit(11),'String','27','ForegroundColor','r');
   else
      T=T+273.15;
   end
   if T<(-250+273.15);
      T=(-250+273.15); 
      set(Ht_edit(11),'String','-250','ForegroundColor','r');
   end
   if T>2273.15;
      T=27; 
      set(Ht_edit(11),'String','27','ForegroundColor','r');
   end
   
case 12;   
   Vrmin=str2num(get(Ht_edit(12),'String'));
   if isempty(Vrmin)==1; 
      Vrmin=-5; 
      set(Ht_edit(12),'String','-5');
   end
   if ((Vrmin<1e-2)*(Vrmin>-1e-2));
      Vrmin=-5; 
      set(Ht_edit(12),'String','-5');
   end
   if Vrmin>1e-3;
      Vrmin=-Vrmin; 
      set(Ht_edit(12),'String',num2str(Vrmin));
   end
   Vr=[Vrmin:abs(Vrmin/100):0];
   Vrpl=Vr;
   Ir=Vr*0;
   Ir_old=Ir;

case 13;   
   Irmin=str2num(get(Ht_edit(13),'String'));
   if isempty(Irmin)==1; 
      set(Ht_edit(13),'String','');
   else   
   if Irmin==0;
      Irmin=str2num(''); 
      set(Ht_edit(13),'String','');
   end
   if Irmin>0;
      Irmin=-Irmin; 
      set(Ht_edit(13),'String',num2str(Irmin));
   end
   end

case 14;   
   Ifmax=str2num(get(Ht_edit(14),'String'));
   if isempty(Ifmax)==1; 
      Ifmax=0.01; 
      set(Ht_edit(14),'String','0.01');
   end
   if Ifmax<1e-50;
      Ifmax=0.01; 
      set(Ht_edit(14),'String','0.01');
   end
   
   if linlg>0;
   If=[0:Ifmax/100:Ifmax];
   Vf=If*0;
   Vf_old=Vf;
   else
   Ifminlog=log(Ifmax*1e-4);
   Ifmaxlog=log(Ifmax);
   If=exp([Ifminlog:(Ifmaxlog-Ifminlog)/100:Ifmaxlog]);
   Vf=If*0;
   Vf_old=Vf;
   end
   firstgo=1;
   
end; %switch   
   
   %drawnow;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% THE MODEL %%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if ((conW_usd15>0)+(firstgo>0)); %Do the recalculations
   
 if firstgo<1; 
 Vf_old=Vf;
 If_old=If;  
 Ir_old=Ir;
 Vr_old=Vr;
 Vr_oldpl=Vrpl;
 Vcv_old=Vcv;
 Ccv_old=Ccv;
 end   
 
   Vt=kb*T;
   IST=IS*(T/Tnom)^(XTI/N)*exp(-(EG/Vt)*(1-T/Tnom));
   if isempty(VJ)==0;
   Vbi=VJ*T/Tnom-2*Vt*log(T/Tnom)^1.5;
   Cdo=Cjo*(1+M*(400e-6*(T-Tnom)-(Vbi-VJ)/VJ));
   else
      Vbi=str2num('');
      Cdo=Cjo;
   end   
   
%I-V
Vf=N*Vt*log(If/IST+1)+If*RS;

Ir1=IST*(exp(Vr/(N*Vt))-1);
if isempty(BV)==1;
   Ir=Ir1; 
   Vrpl=Vr+RS*Ir;
else
   BV0=-(BV+Vr)/Vt;
   BV0=(BV0<100).*BV0+(BV0>=100).*BV0;
   Ir2=-IST*(exp(BV0)-1+BV/Vt);
   Irmins=str2num(get(Ht_edit(13),'String'));
   if isempty(Irmins)==1;
      Ir2=Ir2.*(Ir2>-1)+(Ir2<=-1).*(1e-10);
   else
      Ir2=Ir2.*(Ir2>Irmins)+(Ir2<=Irmin).*(1e-10);
   end   
   Ir=Ir1.*(Vr>-BV)+Ir2.*(Vr<=-BV)-1e-50;
   Vrpl=Vr+RS*Ir;
end   

%C-V
Vcv=[Vr Vf];
Ccv=Vcv*0;
if isempty(VJ)==1;
   Cd=Cdo+Vcv*0;
else
   Cd1=Cdo*(1-Vcv/Vbi).^(-M);
   F2=(1-FC)^(1+M);
   F3=1-FC*(1+M);
   Cd2=Cdo*(F3+M*(Vcv-RS*[Ir If])/Vbi)/F2;
   Cd=Cd1.*(Vcv<FC*Vbi)+Cd2.*(Vcv>=FC*Vbi);
end   
Cs=[Vr*0 TT*If/(N*Vt)];
Ccv=(Cd+Cs)*1e12;

%%%%%%%%%%%%%%%%%%%%%
%%%RESET THE GRAPHS %
%%%%%%%%%%%%%%%%%%%%%

   if firstgo>0;
      Vf_old=Vf;
      If_old=If;
      Ir_old=Ir;
      Vr_old=Vr;
      Vr_oldpl=Vrpl;
      Vcv_old=Vcv;
      Ccv_old=Ccv;
      firstgo=0;
   end   
   
if linlg>0;   
   Irmin=str2num(get(Ht_edit(13),'String'));
   if isempty(Irmin)==1;   
      Irmin=1.1*min(Ir.*(Ir<=0)+1*(Ir>0));
      Ndig=10^floor(log10(abs(Irmin)));
      Irmin=Ndig*1*floor((Irmin/Ndig)/1);
   end
   set(Ha_ifvf,'Xlim',[1e-3 Vfmax],'Ylim',[1e-50 Ifmax],'YScale','lin');
   if Irmin>=0; Irmin=-0.01; end
   set(Ha_irvr,'Xlim',[Vrmin -1e-3],'Ylim',[Irmin -1e-3*abs(Irmin)],'YScale','lin');
else
      Irmin=str2num(get(Ht_edit(13),'String'));
   if isempty(Irmin)==1;   
      Irmin=1.1*min(Ir.*(Ir<=0)+1*(Ir>0));
      Ndig=10^floor(log10(abs(Irmin)));
      Irmin=Ndig*10*floor((Irmin/Ndig)/10);
   end
   if Irmin>=0; Irmin=-0.01; end
   set(Ha_ifvf,'Xlim',[1e-3 Vfmax],'Ylim',[Ifmax/1e4 Ifmax],'YScale','log');
   set(Ha_irvr,'Xlim',[Vrmin -1e-3],'Ylim',[Irmin Irmin*1e-3],'YScale','log');
end 
  
axes(Ha_ifvf);
set(Hp_ifvfold,'XData',Vf_old,'YData',If_old);   
set(Hp_ifvf,'XData',Vf,'YData',If);   

axes(Ha_irvr);
set(Hp_irvrold,'XData',(Vr_oldpl),'YData',((Ir_old<=0).*Ir_old-1e50.*(Ir_old>0)));   
set(Hp_irvr,'XData',(Vrpl),'YData',((Ir<=0).*Ir-1e50.*(Ir>0)));   

axes(Ha_cv);
Cmax=max(1.1*Ccv);
if Cmax<=0;
   Cmaxgr=5;
else   
Ndig=10^floor(log10(Cmax));
Cmaxgr=Ndig*1*ceil((Cmax/Ndig)/1);
end
set(Ha_cv,'Xlim',[Vrmin Vfmax],'Ylim',[0 Cmaxgr]);

set(Hp_cvold,'XData',Vcv_old,'YData',Ccv_old);   
set(Hp_cv,'XData',Vcv,'YData',Ccv);   

conW_usd15=0; conLIN_usd15=0;

if ishandle(Hf)==0;
   return;
end   
drawnow;

end; %if ((conW_usd15>0)+(firstgo>0));

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

end; %while con_go_usd15<1;
close;
