function capr_usd;
%3D and 2D energy-band and space diagrams of a  
%reverse-baised P-N junction, accompanied with
%the corresponding C-V curve.
%(Section 2.2 in Understanding Semiconductor Devices)

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

global con_vg_usd18; global con_vds_usd18; 
global con_r_usd18; global con_start_usd18; 
global con_close_usd18; global con_br_usd18; 
global con_sl_usd18;

%SET THE DOPING COLOR MAP
a=([1:64].')*0; dopcolmap=[a a a];
dopcolmap(1,1)=1;
dopcolmap(1,2)=1;
dopcolmap(1,3)=1;
for ii=2:10; %the grey part
   dopcolmap(ii,1)=0.78*(10-ii)/8;
   dopcolmap(ii,2)=dopcolmap(ii,1);
   dopcolmap(ii,3)=dopcolmap(ii,1);
end   
for ii=11:27; %the dark green part
   dopcolmap(ii,2)=0.7*(ii-11)/16+0.3;
end   
for ii=28:35; %the light green part  
   dopcolmap(ii,2)=1;
   dopcolmap(ii,1)=(ii-27)/8;
   dopcolmap(ii,3)=(ii-27)/8;
end
for ii=36:39; %The white part in the middle
dopcolmap(ii,1)=1; dopcolmap(ii,2)=1; dopcolmap(ii,3)=1;   
end

for ii=40:47; %the light red part
   dopcolmap(ii,2)=1-(ii-40)/8;
   dopcolmap(ii,3)=1-(ii-40)/8;
   dopcolmap(ii,1)=1;
end   
for ii=48:64; %the dark red part
   dopcolmap(ii,1)=0.7*(64-ii)/16+0.3;
end   

%OPEN PARENT FIGURE
Hf=figure('NumberTitle','off','Name','Section 2.2: P-N Junction Energy Bands and Capacitance',...
   'Color',[0.8 0.8 0.8]);
set(gca,'Visible','off','Xlim',[0 15],'Ylim',[0 7.5],'Position',[0.01 0.15 .75 .825]);
set(Hf,'Colormap',dopcolmap);
set(gcf,'InvertHardCopy','off');

%READ THE INTRO TEXT              
introtext ={'UNDERSTANDING SEMICONDUCTOR DEVICES ';...
      'Section 2.2: P-N Junction Energy Bands and Capacitance' ;...
      ' ';...
      '2D x-y diagram of a P-N junction is related to its energy-band diagram (E-x)';...
      'through a 3D visualization of the energy bands - E(x,y). Demo rotations';...
      'are available to provide direct links between these images.';
      ' ';
      'The colors indicate doping: RED for P type, GREEN for N type, whereas ';...
      'the WHITE area shows the depletion layer (lighter the color, lower the doping.) ';...
      ' ';...
      'The reverse-bias voltage and the doping levels can be changed to observe ';...
      'the effects on the depletion-layer width (hence the capacitance), ';...
      'and the corresponding changes of the energy-band diagram. '}; 

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

con_vg_usd18=0; con_vds_usd18=0; con_r_usd18=0; con_start_usd18=0; 
con_close_usd18=0; con_br_usd18=0; con_sl_usd18=0;

%SETTING THE CONTROL PANEL              
Hframe=uicontrol(Hf,'Units','normalized','Position',[0.77 0.01 0.22 0.98],...
   'Style','frame');
HframeVd=uicontrol(Hf,'Units','normalized','Position',[0.775 0.845 0.21 0.14],...
   'Style','frame','BackgroundColor','y');

%Vr
Ht_vdu=uicontrol(Hf,'Units','normalized','Position',[0.78 0.935 0.20 0.04],...
   'Style','text','String','Vr','BackgroundColor','y','FontWeight','bold');
Hsl_vd=uicontrol(Hf,'Units','normalized','Position',[0.78 0.885 0.20 0.05],...
   'Style','slider','min',0,'max',3,'Value',0,'CallBack','con_vds_usd18=1;');
Ht_vdd=uicontrol(Hf,'Units','normalized','Position',[0.78 0.85 0.20 0.03],...
   'Style','text','String',[num2str(round(get(Hsl_vd,'Value')*10)/10) ' V'],...
   'BackgroundColor','y','ForegroundColor','k');
Vrq=round(get(Hsl_vd,'Value')*10)/10;

%P-type doping
Ht_dpu=uicontrol(Hf,'Units','normalized','Position',[0.775 0.80 0.21 0.04],...
   'Style','text','String','P-TYPE DOPING','BackgroundColor',[0.6 0 0],...
   'ForegroundColor','w');
Hsl_dp=uicontrol(Hf,'Units','normalized','Position',[0.775 0.76 0.21 0.04],...
   'Style','slider','min',log10(2e15),'max',19,'Value',16,'CallBack','con_sl_usd18=1;');
Ht_dpd=uicontrol(Hf,'Units','normalized','Position',[0.775 0.72 0.21 0.04],...
   'Style','text','String',[num2str(round(10^(get(Hsl_dp,'Value')))) ' cm-3'],...
   'BackgroundColor',[0.6 0 0],'ForegroundColor','w');
Na=round(10^(get(Hsl_dp,'Value')));
%N-type doping
Ht_dnu=uicontrol(Hf,'Units','normalized','Position',[0.775 0.68 0.21 0.04],...
   'Style','text','String','N-TYPE DOPING','BackgroundColor',[0 0.5 0],...
   'ForegroundColor','w');
Hsl_dn=uicontrol(Hf,'Units','normalized','Position',[0.775 0.64 0.21 0.04],...
   'Style','slider','min',log10(2e15),'max',19,'Value',17,'CallBack','con_sl_usd18=2;');
Ht_dnd=uicontrol(Hf,'Units','normalized','Position',[0.775 0.60 0.21 0.04],...
   'Style','text','String',[num2str(round(10^(get(Hsl_dn,'Value')))) ' cm-3'],...
   'BackgroundColor',[0 .5 0],'ForegroundColor','w');
Nd=round(10^(get(Hsl_dn,'Value')));

%3D
%The radiobutton
%2D
Hr(2)=uicontrol(Hf,'Units','normalized','Position',[0.78 0.54 0.20 0.03],...
   'Style','radio','String','2D x - y diagram','CallBack','con_r_usd18=2;','Value',1,...
   'ForegroundColor','b','FontWeight','bold');
Hr(3)=uicontrol(Hf,'Units','normalized','Position',[0.78 0.49 0.20 0.03],...
   'Style','radio','String','2D E - x diagram','CallBack','con_r_usd18=3;','Value',0,...
   'ForegroundColor','b','FontWeight','bold');
Hr(1)=uicontrol(Hf,'Units','normalized','Position',[0.78 0.44 0.20 0.03],...
   'Style','radio','String','3D','CallBack','con_r_usd18=1;','Value',0,...
   'ForegroundColor','b','FontWeight','bold');
%Azimut
Hsl_az=uicontrol(Hf,'Units','normalized','Position',[0.78 0.405 0.20 0.03],...
   'Style','slider','min',-45,'max',0,'Value',0,'CallBack','con_r_usd18=4;');
Ht_azd=uicontrol(Hf,'Units','normalized','Position',[0.78 0.375 0.20 0.03],...
   'Style','text','String',['Azimuth=' num2str(round(get(Hsl_az,'Value'))) ' deg.']);
Az=round(get(Hsl_az,'Value'));
%Elevation
Hsl_el=uicontrol(Hf,'Units','normalized','Position',[0.78 0.34 0.20 0.03],...
   'Style','slider','min',1,'max',90,'Value',90,'CallBack','con_r_usd18=5;');
Ht_eld=uicontrol(Hf,'Units','normalized','Position',[0.78 0.31 0.20 0.03],...
   'Style','text','String',['Elevation=' num2str(round(get(Hsl_el,'Value'))) ' deg.']);
El=round(get(Hsl_el,'Value'));

%Demo rotations
Ht_start=uicontrol(Hf,'Units','normalized','Position',[0.78 0.255 0.20 0.03],...
   'Style','text','String','DEMO  ROTATIONS');
Hb_start1=uicontrol(Hf,'Units','normalized','Position',[0.78 0.20 0.20 0.05],...
   'Style','pushbutton','String','x-y -> 3D','CallBack','con_start_usd18=1;');
Hb_start2=uicontrol(Hf,'Units','normalized','Position',[0.78 0.14 0.20 0.05],...
   'Style','pushbutton','String','3D->E-x','CallBack','con_start_usd18=2;');

%CLOSE
Hb_close=uicontrol(Hf,'Units','normalized','Position',[0.78 0.02 0.20 0.10],...
   'Style','pushbutton','String','CLOSE','CallBack','con_close_usd18=1;');
%Brightness
Ht_bru=uicontrol(Hf,'Units','normalized','Position',[0.58 0.495 0.18 0.03],...
   'Style','text','String','brightness','BackgroundColor',[0.8 0.8 0.8]);
Hsl_br=uicontrol(Hf,'Units','normalized','Position',[0.58 0.46 0.18 0.03],...
   'Style','slider','min',13,'max',15,'Value',14.5,'CallBack','con_br_usd18=1;');
bright=get(Hsl_br,'Value');

%CONSTANTS
Vt=0.02586;
ni=1.02e10;
%epsox=3.9*8.85e-12;
epssi=11.8*8.85e-12;
q=1.6e-19;
Eg=1.12;
z2_max=log10(2e21); z2_min=log10(1e14);

%THE GRID
Xmax=1; Xmin=-1;
Ymin=0; Ymax=1; 
dx=(Xmax-Xmin)/200;
[xs,ys]=meshgrid(Xmin:dx:Xmax,Ymax:-Ymax/2:0);
[num_y num_x]=size(xs);

%INITIAL VALUES
%Diagram
phifn=-Vt*log(Nd/ni);
phifp=Vt*log(Na/ni);
Vbi=phifp-phifn;
Vrq=0;
wn=sqrt(2*epssi*(Vbi+Vrq)/(q*Nd*1e6*(1+Nd/Na)))*1e6;
wp=sqrt(2*epssi*(Vbi+Vrq)/(q*Na*1e6*(1+Na/Nd)))*1e6;
phin=Vbi+Vrq-(0.5*q*Nd*1e6/epssi)*1e-12*(xs+wn).^2;
phip=(0.5*q*Na*1e6/epssi)*1e-12*(xs-wp).^2;
Ec=-(Vbi+Vrq)*(xs<=-wn)-phin.*(xs>-wn).*(xs<0)-...
   phip.*(xs>=0).*(xs<wp)+phifp+Eg/2;
Ev=Ec-Eg;
Efn=-Vrq;
%Calculate the concentration
Ncarr=-ni*exp((Eg/2-Ec+Efn)/Vt).*(xs<0)+ni*exp((Eg/2+Ev)/Vt).*(xs>=0);
Ncarr=(abs(Ncarr)<2e10).*1e10+(abs(Ncarr)>=2e10).*Ncarr;

%C-V characteristics
Cmaxd=1e-3;
Vrmax=4;
Vr=[0:Vrmax/100:Vrmax];
wnvar=sqrt(2*epssi*(Vbi+Vr)/(q*Nd*1e6*(1+Nd/Na)));
wpvar=sqrt(2*epssi*(Vbi+Vr)/(q*Na*1e6*(1+Na/Nd)));
Cvar=epssi./(wnvar+wpvar);
Cq=epssi/((wn+wp)*1e-6);

%SET THE GRAPH
Cmax=max(Cvar);
if Cmax>Cmaxd;
   Ndig=10^floor(log10(abs(Cmax)));
   Cmax=Ndig*1*ceil((Cmax/Ndig)/1);
else 
   Cmax=Cmaxd;
end
Ha_cvr=axes('Xlim',[0 Vrmax],'Ylim',[0 Cmax],'Position',[0.45 0.1 0.30 0.27],...
   'Color',[0.8 0.8 0.8],'FontAngle','italic');
hold on;
xlabel('V_{R} (V)'); ylabel('C (F/m^{2})');
Hp_cvr=plot(Vr,Cvar,'-k','LineWidth',2);
set(Hp_cvr,'Color',[0.55 0.55 0.55]);
Hp_cqvrq=plot(0,Cq,'.y','MarkerSize',15);
hold off;

%2D GRAPH
z2=sign(Ncarr).*log10(abs(Ncarr));
   if ishandle(Hf)==0;
      return; 
   end;
Ha2=axes('Position',[0.05 0.15 0.65 0.8]);
Hsv=surface(xs,ys,(Ec-Eg),'EdgeColor','none','Visible','on');
hold on;
Hsc=surface(xs,ys,Ec,'EdgeColor','none','Visible','on');
set(Ha2,'Xlim',[Xmin Xmax],'Ylim',[0 Ymax],'Zlim',[-4.1 1.5],...
   'Color',[0.8 0.8 0.8],'FontAngle','italic','CLim',[1 64],...
   'View',[Az El],'Visible','off');
%ylabel('Lateral Distance (nm)'); xlabel('Depth (nm)');
set(Hsv,'CData',((abs(z2)>bright).*((abs(z2)-bright)/(19.1-bright)*9+1)));
set(Hsc,'CData',((abs(z2)>bright).*(z2-sign(z2).*bright)*26.5/(z2_max-bright)+37.5));
Hp_efn=plot3([Xmin -wn],[0 0],[Efn Efn],'-.y');
Hp_efp=plot3([wp Xmax],[0 0],[0 0],'-.y');
Ht_lab(1)=text('String','E_{C}','Position',[Xmax 0 (Eg/2+phifp)],'Color','r',...
   'HorizontalAlignment','left','VerticalAlignment','bottom');
Ht_lab(2)=text('String','E_{F}','Position',[Xmax 0 0],'Color','y',...
   'HorizontalAlignment','left','VerticalAlignment','middle');
Ht_lab(3)=text('String','E_{V}','Position',[Xmax 0 -(Eg/2-phifp)],'Color','k',...
   'HorizontalAlignment','left','VerticalAlignment','top');
      scf=2;
      wdf=0.8/scf;
      btf=0.95-wdf;
      set(Ha2,'Position',[0.05 btf 0.65 wdf],'View',[Az El]);
      set([Hp_efn Hp_efp],'Visible','off');
      set([Ht_lab(1) Ht_lab(2) Ht_lab(3)],'Visible','off');
Ecn=-(Vbi+Vrq)+phifp+Eg/2;      
Ht_lab(4)=text('String','N type','Position',[(Xmin-0.5*(Xmin+wn)) Ymax/2 (Ecn+0.2)],...
     'HorizontalAlignment','center','VerticalAlignment','bottom',...
     'Color',[1 1 1],'FontWeight','bold','FontAngle','Italic','FontSize',12);
Ecp=phifp+Eg/2;      
Ht_lab(5)=text('String','P type','Position',[(wp+0.5*(Xmax-wp)) Ymax/2 (Ecp+0.2)],...
     'HorizontalAlignment','center','VerticalAlignment','bottom',...
     'Color',[1 1 1],'FontWeight','bold','FontAngle','Italic','FontSize',12);
hold off;

%%%%%%%%%%%%%%%%%%%%%%
%%%THE MAIN LOOP
%%%%%%%%%%%%%%%%%%%%%%%
while con_close_usd18<1;
   
%BRIGHTNESS
%%%%%%%%%%%%%%%%%%%%%%%%
if con_br_usd18>0;
   bright=get(Hsl_br,'Value');
      if ishandle(Hf)==0;
         return; 
      end;
set(Hsv,'CData',((abs(z2)>bright).*((abs(z2)-bright)/(19.1-bright)*9+1)));
set(Hsc,'CData',((abs(z2)>bright).*(z2-sign(z2).*bright)*26.5/(z2_max-bright)+37.5));
con_br_usd18=0;
end; %con_br_usd18>0;

 
%VOLTAGE IS CHANGED   
%%%%%%%%%%%%%%%%%%%%%%%%   
if con_vds_usd18>0;   
   Vrq=round(get(Hsl_vd,'Value')*10)/10;
set(Ht_vdd,'String',[num2str(Vrq) ' V']);
wn=sqrt(2*epssi*(Vbi+Vrq)/(q*Nd*1e6*(1+Nd/Na)))*1e6;
wp=sqrt(2*epssi*(Vbi+Vrq)/(q*Na*1e6*(1+Na/Nd)))*1e6;
phin=Vbi+Vrq-(0.5*q*Nd*1e6/epssi)*1e-12*(xs+wn).^2;
phip=(0.5*q*Na*1e6/epssi)*1e-12*(xs-wp).^2;
Ec=-(Vbi+Vrq)*(xs<=-wn)-phin.*(xs>-wn).*(xs<0)-...
   phip.*(xs>=0).*(xs<wp)+phifp+Eg/2;
Ev=Ec-Eg;
Efn=-Vrq;
%Calculate the concentration
Ncarr=-ni*exp((Eg/2-Ec+Efn)/Vt).*(xs<0)+ni*exp((Eg/2+Ev)/Vt).*(xs>=0);
Ncarr=(abs(Ncarr)<2e10).*1e10+(abs(Ncarr)>=2e10).*Ncarr;
z2=sign(Ncarr).*log10(abs(Ncarr));
   if ishandle(Hf)==0;
      return; 
   end;
set(Hsv,'ZData',(Ec-Eg),...
   'CData',((abs(z2)>bright).*((abs(z2)-bright)/(19.1-bright)*9+1)));
set(Hsc,'ZData',Ec,...
   'CData',((abs(z2)>bright).*(z2-sign(z2).*bright)*26.5/(z2_max-bright)+37.5));
set(Hp_efn,'XData',[Xmin -wn],'ZData',[Efn Efn]);
set(Hp_efp,'XData',[wp Xmax]);
Ecn=-(Vbi+Vrq)+phifp+Eg/2;      
set(Ht_lab(4),'Position',[(Xmin-0.5*(Xmin+wn)) Ymax/2 (Ecn+0.2)]);
Ecp=phifp+Eg/2;      
set(Ht_lab(5),'Position',[(wp+0.5*(Xmax-wp)) Ymax/2 (Ecp+0.2)]);


Cq=epssi/((wn+wp)*1e-6);
set(Hp_cqvrq,'XData',Vrq,'YData',Cq);
con_vds_usd18=0;
end; %if con_vg_usd18>0

%DOPING IS CHANGED
%%%%%%%%%%%%%%%%%%%%%%%%%
if con_sl_usd18>0;
if con_sl_usd18>1;   
   Nd=10^(get(Hsl_dn,'Value'));
   set(Ht_dnd,'String',[num2str(round(10^(get(Hsl_dn,'Value')))) ' cm-3']);
   phifn=-Vt*log(Nd/ni);
else
   Na=10^(get(Hsl_dp,'Value'));
   set(Ht_dpd,'String',[num2str(round(10^(get(Hsl_dp,'Value')))) ' cm-3']);
   phifp=Vt*log(Na/ni);
end

Vbi=phifp-phifn;
wn=sqrt(2*epssi*(Vbi+Vrq)/(q*Nd*1e6*(1+Nd/Na)))*1e6;
wp=sqrt(2*epssi*(Vbi+Vrq)/(q*Na*1e6*(1+Na/Nd)))*1e6;
phin=Vbi+Vrq-(0.5*q*Nd*1e6/epssi)*1e-12*(xs+wn).^2;
phip=(0.5*q*Na*1e6/epssi)*1e-12*(xs-wp).^2;
Ec=-(Vbi+Vrq)*(xs<=-wn)-phin.*(xs>-wn).*(xs<0)-...
   phip.*(xs>=0).*(xs<wp)+phifp+Eg/2;
Ev=Ec-Eg;
Efn=-Vrq;
%Calculate the concentration
Ncarr=-ni*exp((Eg/2-Ec+Efn)/Vt).*(xs<0)+ni*exp((Eg/2+Ev)/Vt).*(xs>=0);
Ncarr=(abs(Ncarr)<2e10).*1e10+(abs(Ncarr)>=2e10).*Ncarr;
z2=sign(Ncarr).*log10(abs(Ncarr));
   if ishandle(Hf)==0;
      return; 
   end;
set(Hsv,'ZData',(Ec-Eg),...
   'CData',((abs(z2)>bright).*((abs(z2)-bright)/(19.1-bright)*9+1)));
set(Hsc,'ZData',Ec,...
   'CData',((abs(z2)>bright).*(z2-sign(z2).*bright)*26.5/(z2_max-bright)+37.5));
set(Hp_efn,'XData',[Xmin -wn],'ZData',[Efn Efn]);
set(Hp_efp,'XData',[wp Xmax]);
set(Ht_lab(1),'Position',[Xmax 0 (Eg/2+phifp)]);   
%set(Ht_lab(2),'Position',[Xmax 0 0]);
set(Ht_lab(3),'Position',[Xmax 0 -(Eg/2-phifp)]);

Ecn=-(Vbi+Vrq)+phifp+Eg/2;      
set(Ht_lab(4),'Position',[(Xmin-0.5*(Xmin+wn)) Ymax/2 (Ecn+0.2)]);
Ecp=phifp+Eg/2;      
set(Ht_lab(5),'Position',[(wp+0.5*(Xmax-wp)) Ymax/2 (Ecp+0.2)]);

wnvar=sqrt(2*epssi*(Vbi+Vr)/(q*Nd*1e6*(1+Nd/Na)));
wpvar=sqrt(2*epssi*(Vbi+Vr)/(q*Na*1e6*(1+Na/Nd)));
Cvar=epssi./(wnvar+wpvar);
Cq=epssi/((wn+wp)*1e-6);
Cmax=max(Cvar);
if Cmax>Cmaxd;
   Ndig=10^floor(log10(abs(Cmax)));
   Cmax=Ndig*1*ceil((Cmax/Ndig)/1);
else 
   Cmax=Cmaxd;
end
set(Ha_cvr,'Ylim',[0 Cmax]);
set(Hp_cvr,'YData',Cvar);
set(Hp_cqvrq,'XData',Vrq,'YData',Cq);

con_sl_usd18=0;
end; %if con_sl_usd18>0;

%3D/2D SELECTIONS
%%%%%%%%%%%%%%%%%%%%
if con_r_usd18>0;
   set(Hr,'Value',0);
   csw=con_r_usd18;
   switch csw;
   case 1;   
      if ishandle(Hf)==0;
         return; 
      end;
      set(Hr(1),'Value',1);
      scf=1;
      wdf=0.8/scf;
      btf=0.95-wdf;
      Az=-20;
      El=20;
      set(Hsl_el,'Value',El);
      set(Hsl_az,'Value',Az);
      set(Ht_eld,'String',['Elevation=' num2str(round(get(Hsl_el,'Value'))) ' deg.']);
      set(Ht_azd,'String',['Azimuth=' num2str(round(get(Hsl_az,'Value'))) ' deg.']);
      set(Ha2,'Position',[0.05 btf 0.65 wdf],'View',[Az El]);
      set([Hp_efn Hp_efp],'Visible','on');
      set([Ht_lab(1) Ht_lab(2) Ht_lab(3)],'Visible','on');

   case 2;   
      if ishandle(Hf)==0;
         return; 
      end;
      set(Hr(2),'Value',1);
      scf=2;
      wdf=0.8/scf;
      btf=0.95-wdf;
      Az=0; 
      El=90;
      set(Hsl_el,'Value',El);
      set(Hsl_az,'Value',Az);
      set(Ht_eld,'String',['Elevation=' num2str(round(get(Hsl_el,'Value'))) ' deg.']);
      set(Ht_azd,'String',['Azimuth=' num2str(round(get(Hsl_az,'Value'))) ' deg.']);
      set(Ha2,'Position',[0.05 btf 0.65 wdf],'View',[Az El]);
      set([Hp_efn Hp_efp],'Visible','off');
      set([Ht_lab(1) Ht_lab(2) Ht_lab(3)],'Visible','off');

   case 3;
      if ishandle(Hf)==0;
         return; 
      end;
      set(Hr(3),'Value',1);
      scf=1;
      wdf=0.8/scf;
      btf=0.95-wdf;
      Az=0; 
      El=1;
      set(Hsl_el,'Value',El);
      set(Hsl_az,'Value',Az);
      set(Ht_eld,'String',['Elevation=' num2str(round(get(Hsl_el,'Value'))) ' deg.']);
      set(Ht_azd,'String',['Azimuth=' num2str(round(get(Hsl_az,'Value'))) ' deg.']);
      set(Ha2,'Position',[0.05 btf 0.65 wdf],'View',[Az El]);
      set([Hp_efn Hp_efp],'Visible','on');
      set([Ht_lab(1) Ht_lab(2) Ht_lab(3)],'Visible','on');
      
   case 4;
      if ishandle(Hf)==0;
         return; 
      end;
      set(Hr(1),'Value',1);
      Az=round(get(Hsl_az,'Value'));
      set(Ha2,'View',[Az El]);
      set(Ht_azd,'String',['Azimuth=' num2str(round(get(Hsl_az,'Value'))) ' deg.']);
   
   case 5;
      El=round(get(Hsl_el,'Value'));
      if ishandle(Hf)==0;
         return; 
      end;
      set(Hr(1),'Value',1);
      if El>20;
         scf=1+(El-20)/70;
         wdf=0.8/scf;
         btf=0.95-wdf;
         set(Ha2,'Position',[0.05 btf 0.65 wdf],'View',[Az El]);
      else;  
         scf=1;
         wdf=0.8/scf;
         btf=0.95-wdf;
         set(Ha2,'Position',[0.05 btf 0.65 wdf],'View',[Az El]);
      end;   
      set(Ht_eld,'String',['Elevation=' num2str(round(get(Hsl_el,'Value'))) ' deg.']);
      if El>85;
         set([Hp_efn Hp_efp],'Visible','off');
         set([Ht_lab(1) Ht_lab(2) Ht_lab(3)],'Visible','off');
      else
         set([Hp_efn Hp_efp],'Visible','on');
         set([Ht_lab(1) Ht_lab(2) Ht_lab(3)],'Visible','on');
      end   
   end
con_r_usd18=0;
end; %con_r_usd18>0;

%DEMO ROTATIONS
%%%%%%%%%%%%%%%%%
if con_start_usd18>0;
   dsw=con_start_usd18;
   set(Hb_close,'Enable','off');
   set(Hsl_br,'Enable','off');
   set([Hsl_el Hsl_az],'Enable','off');
   set([Ht_azd Ht_eld],'Visible','off');
   set(Hr,'Value',0,'Enable','off');
   set([Hb_start1 Hb_start2],'Enable','off');
   set([Hsl_vd Hsl_dp Hsl_dn],'Enable','off');
   switch dsw;
   case 1; %x-y -> 3D  
      Nstps=10;
      dAz=20/Nstps;
      dEl=(90-20)/Nstps;
      Az=0;
      El=90;  
      for i=1:(Nstps+1)
         Az=-(i-1)*dAz;
         El=90-(i-1)*dEl;
         scf=1+(El-20)/70;
         wdf=0.8/scf;
         btf=0.95-wdf;
         if ishandle(Hf)==0;
            return; 
         end;
         set(Ha2,'Position',[0.05 btf 0.65 wdf],'View',[Az El]);
         if El>85;
            set([Hp_efn Hp_efp],'Visible','off');
            set([Ht_lab(1) Ht_lab(2) Ht_lab(3)],'Visible','off');
         else
            set([Hp_efn Hp_efp],'Visible','on');
            set([Ht_lab(1) Ht_lab(2) Ht_lab(3)],'Visible','on');
         end   
         drawnow;
         tic;
         while toc<0.3;
            pause(0.1);
         end 
         if ishandle(Hf)==0;
            return; 
         end;
      end   
         set(Hr,'Value',0);
         set(Hr(1),'Value',1);
         set(Ht_eld,'String',['Elevation=' num2str(round(get(Hsl_el,'Value'))) ' deg.'],'Visible','on');
         set(Ht_azd,'String',['Azimuth=' num2str(round(get(Hsl_az,'Value'))) ' deg.'],'Visible','on');
         
  case 2; %3D -> E-x
      Nstps=10;
      dAz=20/Nstps;
      dEl=(20-1)/Nstps;
      Az=-20;
      El=20;
      set([Hsl_el Hsl_az],'Enable','off');
      set([Ht_azd Ht_eld],'Visible','off');
      set(Hr,'Value',0,'Enable','off');
      set([Hb_start1 Hb_start2],'Enable','off');
      scf=1;
      wdf=0.8/scf;
      btf=0.95-wdf;
      set(Ha2,'Position',[0.05 btf 0.65 wdf]);
      set([Hp_efn Hp_efp],'Visible','on');
      set([Ht_lab(1) Ht_lab(2) Ht_lab(3)],'Visible','on');
      for i=1:(Nstps+1)
         Az=(i-1)*dAz-20;
         El=-(i-1)*dEl+20;
         if ishandle(Hf)==0;
            return; 
         end;
         set(Ha2,'View',[Az El]);
         drawnow;
         tic;
         while toc<0.3;
            pause(0.1);
         end 
         if ishandle(Hf)==0;
            return; 
         end;
      end   
         set(Hr,'Value',0);
         set(Hr(3),'Value',1);
         set(Ht_eld,'String',['Elevation=' num2str(round(get(Hsl_el,'Value'))) ' deg.'],'Visible','on');
         set(Ht_azd,'String',['Azimuth=' num2str(round(get(Hsl_az,'Value'))) ' deg.'],'Visible','on');
    end;
    set(Hr,'Enable','on');
    set(Hb_close,'Enable','on');
    set(Hsl_br,'Enable','on');
    set(Hsl_el,'Value',El,'Enable','on');
    set(Hsl_az,'Value',Az,'Enable','on');
    set([Hb_start1 Hb_start2],'Enable','on');
    set([Hsl_vd Hsl_dp Hsl_dn],'Enable','on');
  
con_start_usd18=0;
end;   


if ishandle(Hf)==0;
   return; 
end;
pause(1);
if ishandle(Hf)==0;
   return; 
end;

end; %while of main loop
close;

