function bjts_usd;
%Calculation of BJT output, transfer and input charactersitics
%from set SPICE parameters.
%(Section 6.4 in "Understanding Semiconductor Devices")

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

global conW_usd16; 
global con_go_usd16; global conLIN_usd16;

%OPEN PARENT FIGURE
Hf=figure('NumberTitle','off','Name','Section 6.4: BJT Calculator (SPICE Model)',...
   'Color',[0.8 0.8 0.8]);
hold on;
Ha0=gca;
set(Ha0,'Xlim',[0 1],'Ylim',[0 1],'Visible','off',...
   'Position',[0.0 0.0 1.0 1.0]);
set(gcf,'InvertHardCopy','off');

%READ THE INTRO TEXT              
introtext ={'UNDERSTANDING SEMICONDUCTOR DEVICES ';...
      'Section 6.4: BJT Calculator (SPICE Model)' ;...
      ' ';...
      'Set SPICE BJT parameters to see the effects on the output, input, ';...
      'and transfer characteristics. '};   

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

conW_usd16=0; con_go_usd16=0; conLIN_usd16=0;

Hframe=patch([0.6 1 1 0.6 0.6],[0.14 0.14 0.95 0.95 0.14],[0.75 0.75 0.75]);
set(Hframe,'FaceColor',[0.75 0.75 0.75],'EdgeColor',[0 0 0]);
hold off;

Ht_txt(1)=text(0.81,0.89,'EBERS-MOLL LEVEL','Color','k','HorizontalAlignment','center',...
   'VerticalAlignment','bottom');
Ht_par(1)=text(0.67,0.84,'IS=','Color','k','HorizontalAlignment','right',...
   'VerticalAlignment','bottom');
Ht_edit(1)=uicontrol(Hf,'Units','normalized','Position',[0.67 0.84 0.10 0.04],...
   'Style','edit','String','1e-16','CallBack','conW_usd16=1;',...
   'BackgroundColor',[1 1 1],'ForegroundColor','r','FontWeight','bold');
Ht_un(1)=text(0.775,0.84,'A','Color','k','HorizontalAlignment','left',...
   'VerticalAlignment','bottom');
Ht_par(2)=text(0.67,0.78,'BF=','Color','k','HorizontalAlignment','right',...
   'VerticalAlignment','bottom');
Ht_edit(2)=uicontrol(Hf,'Units','normalized','Position',[0.67 0.78 0.10 0.04],...
   'Style','edit','String','100','CallBack','conW_usd16=2;',...
   'BackgroundColor',[1 1 1],'ForegroundColor','r','FontWeight','bold');
Ht_un(2)=text(0.775,0.78,' -','Color','k','HorizontalAlignment','left',...
   'VerticalAlignment','bottom');
Ht_par(3)=text(0.87,0.78,'BR=','Color','k','HorizontalAlignment','right',...
   'VerticalAlignment','bottom');
Ht_edit(3)=uicontrol(Hf,'Units','normalized','Position',[0.87 0.78 0.10 0.04],...
   'Style','edit','String','1','CallBack','conW_usd16=3;',...
   'BackgroundColor',[1 1 1],'ForegroundColor','r','FontWeight','bold');
Ht_un(3)=text(0.975,0.78,' -','Color','k','HorizontalAlignment','left',...
   'VerticalAlignment','bottom');
Ht_par(4)=text(0.67,0.72,'VA=','Color','k','HorizontalAlignment','right',...
   'VerticalAlignment','bottom');
Ht_edit(4)=uicontrol(Hf,'Units','normalized','Position',[0.67 0.72 0.10 0.04],...
   'Style','edit','String','','CallBack','conW_usd16=4;',...
   'BackgroundColor',[1 1 1],'ForegroundColor','k');
Ht_un(4)=text(0.775,0.72,'V','Color','k','HorizontalAlignment','left',...
   'VerticalAlignment','bottom');
Ht_par(5)=text(0.87,0.72,'VB=','Color','k','HorizontalAlignment','right',...
   'VerticalAlignment','bottom');
Ht_edit(5)=uicontrol(Hf,'Units','normalized','Position',[0.87 0.72 0.10 0.04],...
   'Style','edit','String','','CallBack','conW_usd16=5;',...
   'BackgroundColor',[1 1 1],'ForegroundColor','k');
Ht_un(5)=text(0.975,0.72,'V','Color','k','HorizontalAlignment','left',...
   'VerticalAlignment','bottom');


Ht_txt(2)=text(0.81,0.65,'GUMMEL-POON LEVEL','Color','k','HorizontalAlignment','center',...
   'VerticalAlignment','bottom');
Ht_par(6)=text(0.67,0.59,'IKF=','Color','k','HorizontalAlignment','right',...
   'VerticalAlignment','bottom');
Ht_edit(6)=uicontrol(Hf,'Units','normalized','Position',[0.67 0.59 0.10 0.04],...
   'Style','edit','String','','CallBack','conW_usd16=6;',...
   'BackgroundColor',[1 1 1],'ForegroundColor','k');
Ht_un(6)=text(0.775,0.59,'A','Color','k','HorizontalAlignment','left',...
   'VerticalAlignment','bottom');
Ht_par(7)=text(0.87,0.59,'IKR=','Color','k','HorizontalAlignment','right',...
   'VerticalAlignment','bottom');
Ht_edit(7)=uicontrol(Hf,'Units','normalized','Position',[0.87 0.59 0.10 0.04],...
   'Style','edit','String','','CallBack','conW_usd16=7;',...
   'BackgroundColor',[1 1 1],'ForegroundColor','k');
Ht_un(7)=text(0.975,0.59,'A','Color','k','HorizontalAlignment','left',...
   'VerticalAlignment','bottom');
Ht_par(8)=text(0.67,0.53,'ISE=','Color','k','HorizontalAlignment','right',...
   'VerticalAlignment','bottom');
Ht_edit(8)=uicontrol(Hf,'Units','normalized','Position',[0.67 0.53 0.10 0.04],...
   'Style','edit','String','','CallBack','conW_usd16=8;',...
   'BackgroundColor',[1 1 1],'ForegroundColor','k');
Ht_un(8)=text(0.775,0.53,'A','Color','k','HorizontalAlignment','left',...
   'VerticalAlignment','bottom');
Ht_par(9)=text(0.87,0.53,'ISC=','Color','k','HorizontalAlignment','right',...
   'VerticalAlignment','bottom');
Ht_edit(9)=uicontrol(Hf,'Units','normalized','Position',[0.87 0.53 0.10 0.04],...
   'Style','edit','String','','CallBack','conW_usd16=9;',...
   'BackgroundColor',[1 1 1],'ForegroundColor','k');
Ht_un(9)=text(0.975,0.53,'A','Color','k','HorizontalAlignment','left',...
   'VerticalAlignment','bottom');
Ht_par(10)=text(0.67,0.47,'NE=','Color','k','HorizontalAlignment','right',...
   'VerticalAlignment','bottom');
Ht_edit(10)=uicontrol(Hf,'Units','normalized','Position',[0.67 0.47 0.10 0.04],...
   'Style','edit','String','1','CallBack','conW_usd16=10;',...
   'BackgroundColor',[1 1 1],'ForegroundColor','r');
Ht_un(10)=text(0.775,0.47,' -','Color','k','HorizontalAlignment','left',...
   'VerticalAlignment','bottom');
Ht_par(11)=text(0.87,0.47,'NC=','Color','k','HorizontalAlignment','right',...
   'VerticalAlignment','bottom');
Ht_edit(11)=uicontrol(Hf,'Units','normalized','Position',[0.87 0.47 0.10 0.04],...
   'Style','edit','String','1','CallBack','conW_usd16=11;',...
   'BackgroundColor',[1 1 1],'ForegroundColor','r');
Ht_un(11)=text(0.975,0.47,' -','Color','k','HorizontalAlignment','left',...
   'VerticalAlignment','bottom');

Ht_txt(3)=text(0.81,0.37,'PARASITIC RESISTANCES','Color','k','HorizontalAlignment','center',...
   'VerticalAlignment','bottom');
Ht_par(12)=text(0.67,0.31,'RE=','Color','k','HorizontalAlignment','right',...
   'VerticalAlignment','bottom');
Ht_edit(12)=uicontrol(Hf,'Units','normalized','Position',[0.67 0.31 0.10 0.04],...
   'Style','edit','String','','CallBack','conW_usd16=12;',...
   'BackgroundColor',[1 1 1],'ForegroundColor','k');
Ht_un(12)=text(0.775,0.31,'\Omega','Color','k','HorizontalAlignment','left',...
   'VerticalAlignment','bottom');
Ht_par(13)=text(0.67,0.25,'RB=','Color','k','HorizontalAlignment','right',...
   'VerticalAlignment','bottom');
Ht_edit(13)=uicontrol(Hf,'Units','normalized','Position',[0.67 0.25 0.10 0.04],...
   'Style','edit','String','','CallBack','conW_usd16=13;',...
   'BackgroundColor',[1 1 1],'ForegroundColor','k');
Ht_un(13)=text(0.775,0.25,'\Omega','Color','k','HorizontalAlignment','left',...
   'VerticalAlignment','bottom');
Ht_par(14)=text(0.67,0.19,'RC=','Color','k','HorizontalAlignment','right',...
   'VerticalAlignment','bottom');
Ht_edit(14)=uicontrol(Hf,'Units','normalized','Position',[0.67 0.19 0.10 0.04],...
   'Style','edit','String','','CallBack','conW_usd16=14;',...
   'BackgroundColor',[1 1 1],'ForegroundColor','k');
Ht_un(14)=text(0.775,0.19,'\Omega','Color','k','HorizontalAlignment','left',...
   'VerticalAlignment','bottom');


%CLOSE
Hp_cont=uicontrol(Hf,'Units','normalized','Position',[0.61 0.03 0.38 0.09],...
   'Style','pushbutton','String','CLOSE','CallBack','con_go_usd16=1;');


%%%%%%%%%%%%%%%%%%%%%%%
%%%CONSTANTS%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%

Vcemax=10;
Ibmax=100e-6;
IS=1e-16;
BF=100;
BR=1;
Vt=8.62e-5*(27+273.15);

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

%Ic-Vce
Vce=[0:Vcemax/100:Vcemax];
Vcepl=Vce;
Ic=Vce*0;
Ic_old=Ic;
Ha_icvce=axes('Xlim',[0 Vcemax],'Ylim',[0 10],'Position',[0.10 0.60 0.40 0.35],...
   'Color',[0.8 0.8 0.8],'FontAngle','italic','XGrid','on','YGrid','on','Box','on');
hold on;
xlabel('V_{CE} (V)'); ylabel('I_{C} (mA)');
for i=1:6;
   Hp_icvceold(i)=plot(Vce,Ic_old*1000,'-k','LineWidth',1);
end   
set(Hp_icvceold,'Color',[0.5 0.5 1]);
for i=1:6;
   Hp_icvce(i)=plot(Vce,Ic,'-b','LineWidth',2);
end   
hold off;

%Ib-Vbe
Ibminlog=log(Ibmax/(1e4*BF));
Ibmaxlog=log(Ibmax);
Ib=exp([Ibminlog:(Ibmaxlog-Ibminlog)/100:Ibmaxlog]);
Vbe=Vt*log(BF*Ib/IS+1);
Vbe_old=Vbe;
Vbepl=Vbe;
Vbepl_old=Vbe;
Ha_ivbe=axes('Xlim',[0.2 1],'Ylim',[BF*Ibmax/1e6 BF*Ibmax],...
   'Position',[0.10 0.10 0.35 0.35],...
   'Color',[0.8 0.8 0.8],'FontAngle','italic','XGrid','on','YGrid','on','Box','on',...
   'XAxisLocation','bottom','YAxisLocation','left','YScale','log');
hold on;
xlabel('V_{BE} (V)'); 
ylabel('I_{C} and I_{B} (mA)');
Hp_ibvbeold=plot(Vbe_old,Ib,'-k','LineWidth',1,'Visible','off');
set(Hp_ibvbeold,'Color',[0.3 0.8 0.3]);
Hp_ibvbe=plot(Vbe,Ib,'-g','LineWidth',2,'Visible','off');
set(Hp_ibvbe,'Color',[0 0.8 0]);

%Ic-Vbe
Ic=BF*Ib;
Hp_icvbeold=plot(Vbe_old,Ic,'-k','LineWidth',1,'Visible','off');
set(Hp_icvbeold,'Color',[0.5 0.5 1]);
Hp_icvbe=plot(Vbe,Ic,'-b','LineWidth',2,'Visible','off');
hold off;

%The max/min voltages/currents
axes(Ha0);
Ht_txt(4)=text(0.57,0.43,'I_{C}-V_{BE}','Color','b','HorizontalAlignment','right');
Ht_txt(5)=text(0.57,0.38,'I_{B}-V_{BE}','Color',[0 0.8 0],'HorizontalAlignment','right');
Ht_txt(6)=text(0.57,0.33,'V_{CE}=','Color','k','HorizontalAlignment','right');
Ht_edit(15)=uicontrol(Hf,'Units','normalized','Position',[0.49 0.255 0.06 0.04],...
   'Style','edit','String','5','CallBack','conW_usd16=15;',...
   'BackgroundColor',[0.8 0.8 0.8],'ForegroundColor','k');
Ht_un(15)=text(0.57,0.255,'V','Color','k','HorizontalAlignment','right',...
   'VerticalAlignment','bottom');

Ht_txt(6)=text(0.46,0.48,'I_{B-max}=','Color','k','HorizontalAlignment','right',...
   'VerticalAlignment','bottom');
Ht_edit(16)=uicontrol(Hf,'Units','normalized','Position',[0.46 0.48 0.07 0.04],...
   'Style','edit','String','80','CallBack','conW_usd16=16;',...
   'BackgroundColor',[0.8 0.8 0.8],'ForegroundColor','k');
Ht_un(16)=text(0.57,0.48,'\muA','Color','k','HorizontalAlignment','right',...
   'VerticalAlignment','bottom');

Ht_edit(17)=uicontrol(Hf,'Units','normalized','Position',[0.46 0.555 0.07 0.04],...
   'Style','edit','String','10','CallBack','conW_usd16=17;',...
   'BackgroundColor',[0.8 0.8 0.8],'ForegroundColor','k');

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

%%%%%%%%%%%%%%%%%%%%%%%
%THE INITIAL PARAMETERS
%%%%%%%%%%%%%%%%%%%%%%%
IS=str2num(get(Ht_edit(1),'String'));
BF=str2num(get(Ht_edit(2),'String'));
BR=str2num(get(Ht_edit(3),'String'));
VA=str2num(get(Ht_edit(4),'String')); %Empty
VB=str2num(get(Ht_edit(5),'String')); %Empty
IKF=str2num(get(Ht_edit(6),'String')); %Empty
   %if isempty(Cjo)==1; Cjo=0; end
IKR=str2num(get(Ht_edit(7),'String')); %Empty 
%    if isempty(M)==1; M=0; end
ISE=str2num(get(Ht_edit(8),'String'));
    if isempty(ISE)==1; ISE=0; end
ISC=str2num(get(Ht_edit(9),'String'));    
    if isempty(ISC)==1; ISC=0; end
NE=str2num(get(Ht_edit(10),'String'));
NC=str2num(get(Ht_edit(11),'String'));
RE=str2num(get(Ht_edit(12),'String'));
    if isempty(RE)==1; RE=0; end
RB=str2num(get(Ht_edit(13),'String'));
    if isempty(RB)==1; RB=0; end
RC=str2num(get(Ht_edit(14),'String'));
    if isempty(RC)==1; RC=0; end
Vceq=str2num(get(Ht_edit(15),'String'));
Ibmax=str2num(get(Ht_edit(16),'String'))*1e-6;
Vcemax=str2num(get(Ht_edit(17),'String'));


conW_usd16=21; conLIN_usd16=0; con_go_usd16=0;
firstgo=1; linlg=1;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%MAIN LOOP %%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
while con_go_usd16<1;
   
if conLIN_usd16>0;
   set(Hr_lin,'Value',0);
   if conLIN_usd16==2; %lin
      linlg=0;
      set(Hr_lin(2),'Value',1);
   else %log
      linlg=1;
      set(Hr_lin(1),'Value',1);
   end   
%firstgo=1;         
%conLIN_usd16=0;   
end;   %conLIN_usd16>0;
   
switch conW_usd16;   
   
case 1; %IS
   IS=str2num(get(Ht_edit(1),'String'));
   set(Ht_edit(1),'ForegroundColor','k');
   if isempty(IS)==1; 
      IS=1e-16; 
      set(Ht_edit(1),'String','1e-16','ForegroundColor','r');
   end
   if IS<=1e-50;
      IS=1e-16; 
      set(Ht_edit(1),'String','1e-16','ForegroundColor','r');
   end
   
case 2; %BF   
   BF=str2num(get(Ht_edit(2),'String'));
   set(Ht_edit(2),'ForegroundColor','k');
   if isempty(BF)==1; 
      BF=100; 
      set(Ht_edit(2),'String','100','ForegroundColor','r');
   end
   if BF<=0;
      BF=100; 
      set(Ht_edit(2),'String','100','ForegroundColor','r');
   end
   
case 3; %BR   
   BR=str2num(get(Ht_edit(3),'String'));
   set(Ht_edit(3),'ForegroundColor','k');
   if isempty(BR)==1; 
      BR=1; 
      set(Ht_edit(3),'String','1','ForegroundColor','r');
   end
   if BR<=0;
      BR=1; 
      set(Ht_edit(3),'String','1','ForegroundColor','r');
   end
   
case 4; %VA   
   VA=str2num(get(Ht_edit(4),'String'));
   if isempty(VA)==1; 
      set(Ht_edit(4),'String','');
   else
   if VA<=0;
      VA=str2num(''); 
      set(Ht_edit(4),'String','');
   end
   end
   
case 5; %VB   
   VB=str2num(get(Ht_edit(5),'String'));
   if isempty(VB)==1; 
      set(Ht_edit(5),'String','');
   else
   if VB<=0;
      VB=str2num(''); 
      set(Ht_edit(5),'String','');
   end
   end
   
case 6; %IKF   
   IKF=str2num(get(Ht_edit(6),'String'));
   if isempty(IKF)==1; 
      set(Ht_edit(6),'String','');
   else
   if IKF<=0;
      IKF=str2num(''); 
      set(Ht_edit(6),'String','');
   end
   end

case 7; %IKR   
   IKR=str2num(get(Ht_edit(7),'String'));
   if isempty(IKR)==1; 
      set(Ht_edit(7),'String','');
   else
   if IKR<=0;
      IKR=str2num(''); 
      set(Ht_edit(7),'String','');
   end
   end
   
case 8; %ISE   
   ISE=str2num(get(Ht_edit(8),'String'));
   if isempty(ISE)==1; 
      ISE=0;
      set(Ht_edit(8),'String','');
   end
   if ISE<=0;
      ISE=0; 
      set(Ht_edit(8),'String','');
   end
   
case 9; %ISC   
   ISC=str2num(get(Ht_edit(9),'String'));
   if isempty(ISC)==1; 
      ISC=0;
      set(Ht_edit(9),'String','');
   end
   if ISC<=0;
      ISC=0; 
      set(Ht_edit(9),'String','');
   end
 
case 10; %NE   
   NE=str2num(get(Ht_edit(10),'String'));
   set(Ht_edit(10),'ForegroundColor','k');
   if isempty(NE)==1; 
      NE=1; 
      set(Ht_edit(10),'String','1','ForegroundColor','r');
   end
   if NE<=0;
      NE=1; 
      set(Ht_edit(10),'String','1','ForegroundColor','r');
   end
   if NE>10;
      NE=1; 
      set(Ht_edit(10),'String','1','ForegroundColor','r');
   end
   
case 11; %NC   
   NC=str2num(get(Ht_edit(11),'String'));
   set(Ht_edit(11),'ForegroundColor','k');
   if isempty(NC)==1; 
      NC=1; 
      set(Ht_edit(11),'String','1','ForegroundColor','r');
   end
   if NC<=0;
      NC=1; 
      set(Ht_edit(11),'String','1','ForegroundColor','r');
   end
   if NC>10;
      NC=1; 
      set(Ht_edit(11),'String','1','ForegroundColor','r');
   end

case 12; %RE   
   RE=str2num(get(Ht_edit(12),'String'));
   if isempty(RE)==1; 
      RE=0;
      set(Ht_edit(12),'String','');
   end
   if RE<0;
      RE=0; 
      set(Ht_edit(12),'String','');
   end
   
case 13; %RB   
   RB=str2num(get(Ht_edit(13),'String'));
   if isempty(RB)==1; 
      RB=0;
      set(Ht_edit(13),'String','');
   end
   if RB<0;
      RB=0; 
      set(Ht_edit(13),'String','');
   end
   
case 14; %RC   
   RC=str2num(get(Ht_edit(14),'String'));
   if isempty(RC)==1; 
      RC=0;
      set(Ht_edit(14),'String','');
   end
   if RC<0;
      RC=0; 
      set(Ht_edit(14),'String','');
   end
   
case 15; %Vceq;   
   Vceq=str2num(get(Ht_edit(15),'String'));
   if isempty(Vceq)==1; 
      Vceq=5;
      set(Ht_edit(15),'String','5');
   end
   if Vceq<0;
      Vceq=5; 
      set(Ht_edit(15),'String','5');
   end
   
case 16; %Ibmax   
   Ibmax=str2num(get(Ht_edit(16),'String'))*1e-6;
   if isempty(Ibmax)==1; 
      Ibmax=80e-6;
      set(Ht_edit(16),'String','80');
   end
   if Ibmax<0;
      Ibmax=80e-6;
      set(Ht_edit(16),'String','80');
   end
   firstgo=1;
   
case 17; %Vcemax   
   Vcemax=str2num(get(Ht_edit(17),'String'));
   if isempty(Vcemax)==1; 
      Vcemax=10;
      set(Ht_edit(17),'String','10');
   end
   if Vcemax<0;
      Vcemax=10; 
      set(Ht_edit(17),'String','10');
   end
   firstgo=1;
   
   
end; %switch   
   
   %drawnow;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% THE MODEL %%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if ((conW_usd16>0)+(firstgo>0)); %Do the recalculations
   
if firstgo<1; 
   Ic_old=Ic;
   Vceplold=Vcepl;
   Ibtrold=Ibtr;  
   Ictrold=Ictr;
   Vbeplold=Vbepl;
end   

%Output characteristics
%Generate Vbe step voltages
Vbes(1)=0;
Vbest=0;
Ibb=0;
for i=2:6;
   while Ibb<((i-1)*Ibmax/5);
      Vbest=Vbest+0.001;
      lbes=exp(Vbest/Vt)-1;
      lbels=exp(Vbest/(NE*Vt))-1;
      lbcs=exp((Vbest-Vcemax)/Vt)-1;
      lbcls=exp((Vbest-Vcemax)/(NC*Vt))-1;
      Ibb=IS*lbes/BF+ISE*lbels+IS*lbcs/BR+ISC*lbcls;
   end
   Vbes(i)=Vbest;
end   

Vce=0;
Vce=[0:Vcemax/100:Vcemax];
Ic=0*Vce;
Ic(1,:)=0*Vce;
Vcepl(1,:)=Vce;
%Ic_old=Ic;
for i=2:6;
   lbes=exp(Vbes(i)/Vt)-1;
   lbels=exp(Vbes(i)/(NE*Vt))-1;
   lbc=exp((Vbes(i)-Vce)/Vt)-1;
   lbcl=exp((Vbes(i)-Vce)/(NC*Vt))-1;
   if (isempty(IKF)==1).*(isempty(IKR)==1);
      q2=0*Vce;
   else
      if (isempty(IKR)==1);
         q2=IS*lbes/IKF;
      else
         if (isempty(IKF)==1);
            q2=IS*lbc/IKR;
         else
            q2=IS*lbes/IKF+IS*lbc/IKR;
         end   
      end
   end
   if (isempty(VA)==1).*(isempty(VB)==1);
      q1=0*Vce+1;
   else
      if (isempty(VB)==1);
         q1=1./(1-(Vbes(i)-Vce)/VA);
      else
         if (isempty(VA)==1);
            q1=1./(1-(Vbes(i)-Vce)/VB);
         else
            q1=1./(1-(Vbes(i)-Vce)/VA-(Vbes(i)-Vce)/VB);
         end   
      end
   end
   qb=0.5*q1.*(1+sqrt(1+4*q2));
   
   Ic(i,:)=IS*(lbes-lbc)./qb-IS*lbc/BR-ISC*lbcl;
   Ie=IS*(lbes-lbc)/qb+IS*lbes/BF+ISE*lbels;
   Vcepl(i,:)=Vce+RC*Ic(i,:)+RE*Ie;
end   %for i=2:6

%Transfer/input characteristics
%Find Vbemin and Vbemax and set Vbetr;
Vbest=0;
Ibb=0;
if linlg==1; %LOG graph
   Ibmin=Ibmax/1e4;
while Ibb<Ibmin;
   Vbest=Vbest+0.001;   
   lbes=exp(Vbest/Vt)-1;
   lbels=exp(Vbest/(NE*Vt))-1;
   lbcs=exp((Vbest-Vceq)/Vt)-1;
   lbcls=exp((Vbest-Vceq)/(NC*Vt))-1;
   Ibb=IS*lbes/BF+ISE*lbels+IS*lbcs/BR+ISC*lbcls;
end
Vbemin=Vbest;
while Ibb<Ibmax;
   Vbest=Vbest+0.001;   
   lbes=exp(Vbest/Vt)-1;
   lbels=exp(Vbest/(NE*Vt))-1;
   lbcs=exp((Vbest-Vceq)/Vt)-1;
   lbcls=exp((Vbest-Vceq)/(NC*Vt))-1;
   Ibb=IS*lbes/BF+ISE*lbels+IS*lbcs/BR+ISC*lbcls;
end
Vbemax=Vbest;
if Vbemax<=(Vbemin+0.05); Vbemax=Vbemin+0.05; end
Vbetr=[Vbemin:((Vbemax-Vbemin)/100):Vbemax];
else %LIN graph
   Vbemin=0.2;
   while Ibb<Ibmax;
   Vbest=Vbest+0.001;   
   lbes=exp(Vbest/Vt)-1;
   lbels=exp(Vbest/(NE*Vt))-1;
   lbcs=exp((Vbest-Vceq)/Vt)-1;
   lbcls=exp((Vbest-Vceq)/(NC*Vt))-1;
   Ibb=IS*lbes/BF+ISE*lbels+IS*lbcs/BR+ISC*lbcls;
   end
   Vbemax=Vbest;
   if Vbemax<0.3; Vbemax=0.3; end;
   Vbeminlog=log(Vbemin);
   Vbemaxlog=log(Vbemax);
   Vbetr=exp([Vbeminlog:((Vbemaxlog-Vbeminlog)/100):Vbemaxlog]);
end   
Vbepl=Vbetr;
Ibtr=Vbetr*0;
Ictr=Vbetr*0;

lbe=exp(Vbetr/Vt)-1;
lbel=exp(Vbetr/(NE*Vt))-1;
lbc=exp((Vbetr-Vceq)/Vt)-1;
lbcl=exp((Vbetr-Vceq)/(NC*Vt))-1;

Ibtr=IS*lbe/BF+ISE*lbel+IS*lbc/BR+ISC*lbcl;

   if (isempty(IKF)==1).*(isempty(IKR)==1);
      q2=0*Vbetr;
   else
      if (isempty(IKR)==1);
         q2=IS*lbe/IKF;
      else
         if (isempty(IKF)==1);
            q2=IS*lbc/IKR;
         else
            q2=IS*lbe/IKF+IS*lbc/IKR;
         end   
      end
   end
   if (isempty(VA)==1).*(isempty(VB)==1);
      q1=0*Vbetr+1;
   else
      if (isempty(VB)==1);
         q1=1./(1-(Vbetr-Vceq)/VA);
      else
         if (isempty(VA)==1);
            q1=1./(1-(Vbetr-Vceq)/VB);
         else
            q1=1./(1-(Vbetr-Vceq)/VA-(Vbetr-Vceq)/VB);
         end   
      end
   end
   qb=0.5*q1.*(1+sqrt(1+4*q2));
   
Ictr=IS*(lbe-lbc)./qb-IS*lbc/BR-ISC*lbcl;
Ietr=IS*(lbe-lbc)./qb+IS*lbe/BF+ISE*lbel;
%Ietr=Ibtr+Ictr;
Vbepl=Vbetr+RB*Ibtr+RE*Ietr;

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

%%%%%%%%%%%%%%%%%%%%%
%%%RESET THE GRAPHS %
%%%%%%%%%%%%%%%%%%%%%
if ((conW_usd16>0)+(firstgo>0)+(conLIN_usd16>0)); %Do the recalculations

   if firstgo>0;
   Ic_old=Ic;
   Vceplold=Vcepl;
   Ibtrold=Ibtr;  
   Ictrold=Ictr;
   Vbeplold=Vbepl;
   firstgo=0;
   end   

axes(Ha_icvce);
Icmax=max(1000*Ic(6,:));
if Icmax<=0; Icmax=10; end;
Ndig=10^floor(log10(Icmax));
Icmaxgr=Ndig*1*ceil((Icmax/Ndig)/1);
set(Ha_icvce,'Xlim',[0 Vcemax],'Ylim',[0 Icmaxgr]);   
for i=1:6;
set(Hp_icvceold(i),'XData',Vceplold(i,:),'YData',Ic_old(i,:)*1000,'Visible','on');   
end
for i=1:6;
set(Hp_icvce(i),'XData',Vcepl(i,:),'YData',Ic(i,:)*1000,'Visible','on');   
end

axes(Ha_ivbe);
Ndig=10^floor(log10(abs(Vbemin)));
Vbemingr=Ndig*1*floor((Vbemin/Ndig)/1);
Vbeplmax=max(Vbepl);
Ndig=10^floor(log10(abs(Vbeplmax)));
Vbemaxgr=Ndig*1*ceil((Vbeplmax/Ndig)/1);

Ictrmax=max(Ictr*1000);
Ibtrmax=max(Ibtr*1000);
Itrmax=max([Ictrmax Ibtrmax]);
Ndig=10^floor(log10(Itrmax));
if linlg>0; %LOG graph  
   Itrmaxgr=Ndig*10*ceil((Icmax/Ndig)/10);
   Itrmin=min([Ibtr Ictr])*1000;
   Ndig=10^floor(log10(Itrmin));
   Ibmingr=Ndig*10*floor((Itrmin/Ndig)/10);
   set(Ha_ivbe,'Xlim',[Vbemingr Vbemaxgr],'Ylim',[Ibmingr Itrmaxgr],'YScale','log');
else %LIN graph
   Ibmingr=0;
   Itrmaxgr=Ndig*1*ceil((Icmax/Ndig)/1);
   set(Ha_ivbe,'Xlim',[Vbemingr Vbemaxgr],'Ylim',[Ibmingr Itrmaxgr],'YScale','lin');
end 

set(Hp_ibvbeold,'XData',Vbeplold,'YData',Ibtrold*1000,'Visible','on');
set(Hp_ibvbe,'XData',Vbepl,'YData',Ibtr*1000,'Visible','on');
set(Hp_icvbeold,'XData',Vbeplold,'YData',Ictrold*1000,'Visible','on');
set(Hp_icvbe,'XData',Vbepl,'YData',Ictr*1000,'Visible','on');


conW_usd16=0; conLIN_usd16=0; 

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

end; %if ((conW_usd16>0)+(firstgo>0)+(conLIN_usd16>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_usd16<1;
close;
