program tkmgraf3;                {unter Verwendung von tspa1}
                                 {Auswertung von Real-Files }
uses  dos,crt,temperat,eingabe,graph,
      mess,
      PL488,
      plhpgl;

const Channel5:TChannelCfg=(Quantity      : Voltage;
                            IntegrationTime: 3;
                            Range          : 1;
                            AutoRange      : On;
                            prog           : normal;
                            measure        : continuous;
                            Delay_After_Set: 100;
                            Kind           : direct);
const Channel6:TChannelCfg=(Quantity      : Voltage;
                            IntegrationTime: 3;
                            Range          : 3;
                            AutoRange      : On;
                            prog           : normal;
                            measure        : single;
                            Delay_After_Set: 100;
                            Kind           : direct);

const ba=$300;mxhi=100;myhi=100;hh1=15;hh2=15;myh=35;mg0=10;mg1=11;mg2=7;

var US,tem1,tem2,ur,Strom,tem1a,tem1b,tem2a,tem2b,u1a,u1b,u2a,u2b : real;
    T1i,T2i,UThi,urri:  string[12];
    b1,b2,b3: char;
    Name,kk,bild1,bild2,bild3,bild4: string;
    tex :Text;
    i,l,anz,imax:integer;
    h,k,nn,nn2 : Integer;
    zeit,ut,tm1,tm2 : array[1..50] of Real;
    f : text;
    z0,z1,z2,z3,z4,z5,time: real;
    time1,time2:longint;
    hun1,hun2,min1,min2,sec1,sec2,h1,h2:word;
    hun1i,hun2i,min1i,min2i,sec1i,sec2i,h1i,h2i:integer;
    ax:real;
    ax_wort:string;

var dm1:TDM;
    dm2:TDM;
    dm3:TDM;
    buf:string;
    hliste:array[1..4] of integer;
    
var graphdriver,graphmode:integer;
    mx,my,mym,xp,yp,x0,y0,j,find,d,nr,find1:integer;
    al0,al1,r,dx,dy,xl,yl,invdifx,xminT,xminU,xmaxT,xmaxU,yminT,ymaxT:real;
    xmin,xmax,x,y,ymin,ymax,difx,dify,invdify,deltax,deltay,yminU,ymaxU:real;
    strxmin,strxmax,stryminT,strymaxT,stryminU,strymaxU,strr,sanz,strtime:string;
    sxmin,sxmax,syminT,symaxT,syminU,symaxU,xr,yr,zx,zy,zxy,zxx,a,zeit2:real;
    symin,symax,rr:real;

    {$m 20000,0,400000}

function korrektur(t_m,zz0,zz1,zz2,zz3,zz4,zz5:real):real;
 var i:integer;
     ergebnis:real;
begin
 ergebnis:=zz0+zz1*t_m+zz2*t_m*t_m+zz3*t_m*t_m*t_m;
 ergebnis:=ergebnis+zz4* t_m * t_m * T_m * t_m;
 ergebnis:=ergebnis+zz5* T_m * t_m * T_m * T_m * T_m;
 korrektur:=ergebnis;
end;
begin
 assign(f,'c:\daten\th_delta.txt');
 reset(f);
 readln(f,z0);
 readln(f,z1);
 readln(f,z2);
 readln(f,z3);
 readln(f,z4);
 readln(f,z5);
 close(f);
 clrscr;
 writeln('Programm zur Widerstandsmessung bei tiefen Temperaturen.');
 write ('Soll die Messung tats„chlich beginnen ?(J/N)');
 if janein then exit;
 port[$301]:=$33;
 rr:=0;
 writeln;
 write('Eingabe des Dateinamen zur Speicherung: ');
 readln(name);
 name:=name + '.tkm';
 assign(tex,name);
 rewrite(tex);
 close(tex);
 b3:='j';
 write('Eingabe des Meástromes in mA (0.05): ');
 readln (Strom);
 writeln('Programmstop mit "s" ,Programmende mit "e" .');
 imax:=0;while (imax<5)or(imax>50)do begin
 write('Eingabe der Meápunkte je Grafik (4<imax<=50):');
 readln(imax);end;

 k:=0;
 initthermo;
 IECInit;
 IECSetHardware(1);

 hliste[1]:=mg0;hliste[2]:=mg1;hliste[3]:=mg2;hliste[4]:=-1;

 dm1.init(mg0);
 dm2.init(mg1);
 dm3.init(mg2);

 dm1.SetChannelCfg(8,Channel5);
 dm2.SetChannelCfg(8,Channel5);
 dm3.SetChannelCfg(10,Channel6);
 port[ba+2]:=$12;
 port[ba+1]:=$48;
 iecget(hliste);
 repeat
  write ('Start der Messung ?(J/N)');
 until not janein;writeln; 
 gettime(h1,min1,sec1,hun1);h1i:=h1;min1i:=min1;sec1i:=sec1;hun1i:=hun1;
delay(1000);
 nn:=10;nn2:=0;
b1:='a';anz:=0;
while b1<>'e' do begin
    inc(nn2);if nn2=11 then nn2:=1;
    u1a:=dm1.GetMeasure(8);
    u2a:=-dm2.GetMeasure(8);
     ur:=dm3.GetMeasure(10);

  iecget(hliste);
  gettime(h2,min2,sec2,hun2);h2i:=h2;min2i:=min2;sec2i:=sec2;hun2i:=hun2;
  time:=3600*(h2i-h1i)+60*(min2i-min1i)+(sec2i-sec1i)+0.01*(hun2i-hun1i);
  str(time:8:1,strtime);
  h1i:=h2i;min1i:=min2i;sec1i:=sec2i;hun1i:=hun2i;

  thermo((u1a*1000000),tem1);
  thermo((u2a*1000000),tem2);

  ur:=(ur)*1000/strom;
  ur:=abs(ur); rr:=ur;

  us:=ur;
  str(tem1:6:3,t1i);
  str(tem2:6:3,t2i);
  str(us:8:1,uthi);
  str(ur:10:3,urri);
  if (tem1<>tem2) then ax:=us/(tem1-tem2) else ax:=0;
  str(ax:5:1,ax_wort);
  if b1='s'then begin readln;b1:='a';end;
if ((nn2=3) or (nn2=8)) then  closegraph;
  append(tex);
  writeln(tex,anz);
  writeln(tex,tem1);
  writeln(tex,tem2);
  writeln(tex,0);
  writeln(tex,time);
  writeln(tex,rr);
  close(tex);
  {+++++++++++++++++++++++++++++++++++++++}
if anz=0 then begin
  for i:=1 to imax do begin
  zeit[i]:=0;
  if tem1=tem2 then tem1:=tem1+0.1;
  tm1[i]:=tem1;
  tm2[i]:=tem2;
  ut[i]:=us;
  end;
end;
zeit2:=zeit[2];
for i:=1 to imax-1 do begin
    zeit[i]:=zeit[i+1]-zeit2;
    tm1[i]:=tm1[i+1];
    tm2[i]:=tm2[i+1];
    ut[i]:=ut[i+1];
    end;
zeit[imax]:=zeit[imax-1]+time;
tm1[imax]:=tem1;
tm2[imax]:=tem2;
ut[imax]:=us;
yminT:=tm1[1];ymaxT:=tm1[1];
for i:=1 to imax do begin
if tm1[i]<yminT then yminT:=tm1[i];
if tm1[i]>ymaxT then ymaxT:=tm1[i];
if tm2[i]<yminT then yminT:=tm2[i];
if tm2[i]>ymaxT then ymaxT:=tm2[i];end;
ymaxT:=ymaxT+0.1;yminT:=yminT-0.1;
yminU:=ut[1];ymaxU:=ut[1];
for i:=1 to imax do begin
if ut[i]<yminU then yminU:=ut[i];
if ut[i]>ymaxU then ymaxU:=ut[i];
end;
ymaxU:=ymaxU+0.1*abs(ymaxU);yminU:=yminU-0.1*abs(yminU);
xmin:=-1;xmax:=zeit[imax]+1;
{++++++++++++++++++++++++++}

if ((nn2=3) or (nn2=8)) then begin
nr:=9;
graphdriver:=detect;
initgraph(graphdriver,graphmode,'c:\sprachen\tp\bgi');
mx:=getmaxx;my:=getmaxy;mx:=mx-round(mxhi/2);
mym:=round(myh*(my-myhi)/100)+myhi;
for i:=mxhi to mx do begin putpixel(i,myhi,1);
                           putpixel(i,mym,1);
                           putpixel(i,my,1);end;
for i:=myhi to my do begin putpixel(mxhi,i,1);
                           putpixel(mx,i,1);end;

dx:=(xmax-xmin);
if dx>0 then begin
r:=1;
    if dx>=100 then begin
           while dx>=100 do begin
                            dx:=dx/10;
                            r:=r*10;
                            end;                           {Ermittlung des}
                     end;                                  {X-Rasters}
    while dx<10 do begin
                   dx:=dx*10;
                   r:=r/10;
                   end;
    if dx>50 then deltax:=10*r
    else if dx>20 then deltax:=5*r
    else deltax:=2*r;
    invdifx:=(mx-mxhi)/(xmax-xmin);
    find:=0;
    x:=int(xmin/deltax)*deltax;
    for j:=-1 to 12 do begin                               {Zeichnen des}
    xr:=x+j*deltax;                                        {X-Rasters und}
    xl:=mxhi+(-xmin+x+j*deltax)*invdifx;                   {Ermittlung der}
    x0:=round(xl);                                         {X-Skale}
    if (x0>=mxhi) and (x0<=mx) then begin
    if find=0 then begin sxmin:=xr;find:=1;end;
    sxmax:=xr;
    for i:=myhi to mym do begin
    putpixel(x0,i,8);
    end;
    for i:=mym to my do begin
    putpixel(x0,i,8);
    end;
    end;
    end;
x0:=round(mxhi-xmin*invdifx);
if (x0>=mxhi) and(x0<=mx) then begin
    for i:=myhi to my do begin
    putpixel(x0,i,4);
    end;
    end;
end;
{+++++++++++++++++++++++++++++}
for find1:=1 to 2 do begin
if nr=9 then begin ymax:=ymaxU;ymin:=yminU;end;
if nr=10 then begin ymax:=ymaxT;ymin:=yminT;end;          
xl:=mxhi+(-xmin+x+j*deltax)*invdifx;
dy:=ymax-ymin;
if dy>0 then begin
r:=1;
    if dy>=100 then begin
           while dy>=100 do begin
                            dy:=dy/10;
                            r:=r*10;
                            end;                           {Ermittlung des}
                     end;                                  {Y-Rasters}
    while dy<10 do begin
                   dy:=dy*10;
                   r:=r/10;
                   end;
    if dy>50 then deltay:=10*r
    else if dy>20 then deltay:=5*r
    else deltay:=2*r;
    y:=int(ymax/deltay)*deltay;
if nr=9 then invdify:=(mym-myhi)/(ymax-ymin);
if nr=10 then invdify:=(my-mym)/(ymax-ymin);
    find:=0;
    for j:=-1 to 12 do begin
    yr:=y-j*deltay;
 if nr=9 then yl:=myhi+(ymax-y+j*deltay)*invdify;
 if nr=10 then yl:=mym+(ymax-y+j*deltay)*invdify;
    y0:=round(yl);

if((nr=9)and(y0>=myhi)and(y0<=mym)) or ((nr=10)and(y0>=mym)and(y0<=my))
                               then begin                  {Zeichnen des}
    if find=0 then begin symax:=yr;find:=1;end;            {Y-Rasters und}
    symin:=yr;                                             {Ermittlung der}
 if nr=9 then begin syminU:=symin;symaxU:=symax;end;
 if nr=10 then begin syminT:=symin;symaxT:=symax;end;
    for i:=mxhi to mx do begin                             {Y-Skale}
    putpixel(i,y0,8);
    end;
    end;
    end;
if nr=9 then begin
y0:=round(myhi+ymax*invdify);
if (y0>=myhi) and (y0<=mym) then begin
    for i:=mxhi to mx do begin
    putpixel(i,y0,4);
    end; 
    end;end;
end;nr:=nr+1;end;nr:=nr-2;

for find:=1 to 3 do begin
if (nr=9) or (nr=10) or (nr=11) then begin
for i:=1 to imax do begin
    x:=zeit[i];
    if nr=9 then begin
    ymax:=ymaxU;ymin:=yminU;invdify:=(mym-myhi)/(ymax-ymin);
    end;
    if (nr=10) or (nr=11) then begin
    ymax:=ymaxT;ymin:=yminT;invdify:=(my-mym)/(ymax-ymin);
    end;
if nr=9 then y:=ut[i];                                  { Zeichnen der }
if nr=10 then y:=tm1[i];
if nr=11 then y:=tm2[i];
    x:=mxhi+(x-xmin)*invdifx;xp:=round(x);                { Meáwerte }
if nr=9 then y:=myhi+(ymax-y)*invdify;yp:=round(y);
if nr>9 then y:=mym+(ymax-y)*invdify;yp:=round(y);
if i=1 then putpixel(xp,yp,15);
if i<>1 then lineto(xp,yp); circle(xp,yp,2);
moveto(xp,yp);
end;end;
nr:=nr+1;end;
nr:=nr-3;
str(rr:8:2,strr);str((anz+1):6,sanz);
str(sxmin:8:2,strxmin);
str(sxmax:8:2,strxmax);
str(syminT:8:2,stryminT);
str(symaxT:8:2,strymaxT);
str(syminU/1000:8:2,stryminU);
str(symaxU/1000:8:2,strymaxU);
 settextstyle(1,horizdir,1);
settextjustify(lefttext,lefttext);
outtextxy(mxhi,myhi-hh1,strxmin);
settextjustify(righttext,lefttext);                    { Beschriftung des}
outtextxy(mx,myhi-hh1,strxmax);                         {  X-Y-Rasters    }
{ settextstyle(0,vertdir,1); }
settextjustify(righttext,righttext);
outtextxy(mxhi-hh1,mym+hh2,strymaxT);
outtextxy(mxhi-hh1,myhi,strymaxU);
settextjustify(righttext,lefttext);
outtextxy(mxhi-hh1,my,stryminT);
outtextxy(mxhi-hh1,mym-hh2,stryminU);


 settextstyle(1,horizdir,1);                            { Beschriftung der }
settextjustify(centertext,lefttext);                    { Grafik           }
outtextxy(round((mxhi+mx)/2),myhi-hh2,'t/s');
{ settextstyle(1,vertdir,1);}       
settextjustify(righttext,centertext);
outtextxy(mxhi-hh2,round((myhi+mym)/2),'R/kOhm');
outtextxy(mxhi-hh2,round((my+mym)/2),'T/K');
settextjustify(lefttext,centertext);
if tem1>=tem2 then begin
   outtextxy(mx+hh1,mym+2*hh2,'T1');outtextxy(mx+hh1,my-2*hh2,'T2');end;
if tem1<tem2 then begin
   outtextxy(mx+hh1,mym+2*hh2,'T2');outtextxy(mx+hh1,my-2*hh2,'T1');end;
{outtextxy(mx+hh1,round((myhi+mym)/2),'Uth');}
outtextxy(15,15,'R/Ohm: ');outtextxy(round(mx/2+{115}130),15,'N = ');
outtextxy(125,15,strr);outtextxy(round(mx/2+155),15,sanz);
outtextxy(round((mxhi+mx)/2-75),15,'dt/s:');
outtextxy(round((mxhi+mx)/2-45),15,strtime);
end;
if keypressed then b1:=readkey;
anz:=anz+1;
end;closegraph;
 port[ba+1]:=$44;
 dm1.done;
 dm2.done;
 dm3.done;
end.