c OHMICAS       - OHMIc Conversion Analog Single end
c OHMIC heating - control program Single Ended 16 channels
c 25.8.99: volumetric flowrate START/STOP
c Input file
c OHMICAS.PAR
c backgroud-color (integer), nchan-number of channels (<=16)
c a1 b1
c a2 b2
c ...
c a8 b8    linear trend T=a+b.V
c
      character *16 file,label*10,name(16)*(4),labela(16)*(10),
     /          labelt*6,labelv*6,lin*50,ch1*1
      dimension iv(9),volto(16),voltm(16),x(2),y(2),icv(16),a(16),b(16),
     /          vcal(16)
      real *8 to,tn
      integer *2 hour,minute,second,ssecond
      logical cal(16),graf,full,volmes,voltag
      common /scom/iss(50)
      equivalence (iv(1),file),(iv(5),dt),(iv(6),irg),(iv(7),nchan),
     /            (iv(8),incr),(iv(9),iunits),(iss(13),kbd,ch1)
      data file,dt,incr,irg,icv /'os1.out         ',0.5,10,1,
     /10,12,13,14,2,3,15,15, 4,5,1,10,12,13,14,1/
      data name/'T1:C','T2:C','T3:C','T4:C','T5:C','T6:C',
     /          'P:kW','V :l',
     /          'T7:C','T8:C','T9:C','C1:S','C2:S','C3:S','C4:S',
     /          'aux '/
c
c Openings: parametric file, graphical screen, initial scan
c
      open(2,file='ohmicas.par')
      read(2,*)icarea,nchan,(a(i),b(i),i=1,nchan)
      close(2)
      icol=2
      cal=.false.
      full=.true.
      voltag=.true.
      volmes=.false.
      vmax=0
      vsmin=10
      vsmax=-10
      vmaxo=-1.
      ilin=0
      lin='                                                            '
      call scomin(0)
100   call gmfmon
      call sclear
      call scwrit(19,8,17,30,'OHMICAS  zitny@99')
      call scwrit(20,8,32,30,'PCL-818 16 Single ended channels')
      call scwrit(21,8,31,30,'Jumpers: JP6-[S.E.] JP7-[+-10V]')
      call scwrit(22,8,64,30,'Ch1-6:T1-T6, Ch7:Pwr, Ch8:Level, Ch9-11:T7
     /-T9, Ch12-15:C1-C4')
      call minpv(55,'[Enter]',7,1,4,
     /'                                                            '//
     /'  File @1a16              result recordings. @7i5 -channels '//
     /'                                                            '//
     /'  Voltage @6i4 [0]-autorange, [>0]-fixed range +-10V        '//
     /'                               1-graph, 2-alphanumeric      '//
     /'                                                            '//
     /'  Time step of sampling  @5f10.2      [s]                   '//
     /'            Record every @8i10        time step             '//
     /'            Record       @9i10        Voltage [1] or C [0]  '// 
     /'                                                            '//
     /'  Recording starts immediately after sending the time step  '//
     /'  or after   [F1].                                          '//
     /'                                                            '//
     /'  Quit recording by pressing the [Esc] key                  '//
     /'                                                            ',
     /14,15,iv)
      if(kbd.eq.13.or.kbd.eq.59)then
          nstep=0
          if(dt.le.0.)dt=0.5
          if(nchan.le.0.or.nchan.gt.16)nchan=16
c
c Output file
c
          open(1,file=file)
20        read(1,*,end=21)
          goto 20
21        backspace 1
          call gettim(hour,minute,second,ssecond)
          if(iunits.ne.0)then
             write(1,22)hour,minute,second,dt,nchan,irg
          else
             write(1,23)hour,minute,second,dt,nchan,irg
          endif
22        format(/' Start at ',i2,':',i2,':',i2,' dt=',f6.2,' [s]  N=',
     /          i2,' fix.range=',i1//
     /'    t [s]   T1[V]   T2[V]   T3[V]   T4[V]   T5[V]   T6[V]    P[V]
     /    V[V]   T7[V]   T8[V]   T9[V]   C1[V]   C2[V]   C3[V]   C4[V] 
     /aux[V] V[liters] dVdt [ml/s]')
23        format(/' Start at ',i2,':',i2,':',i2,' dt=',f6.2,' [s]  N=',
     /          i2,' fix.range=',i1//
     /'    t [s]   T1[C]   T2[C]   T3[C]   T4[C]   T5[C]   T6[C]   P[kW]
     /    V[l]   T7[C]   T8[C]   T9[C]   C1[V]   C2[V]   C3[V]   C4[V] 
     /aux[V] V[liters] dVdt [ml/s]')
          flows=0
          flowma=-999
          flowmi=999
          gdt=600*dt
          gdtmi=30*dt
          gtma=0.
c
c Open graphic 
c Set windows No.1-4 and fill empty boxes by colors
c -------------------------------------------------------
          call gmfin
          call gmfont(2,'h18w9rb   ')
c 1 - main graph
c 2 - values
c 3 - line text (input)
c 4 - keys
          call gmfstf(3,0.081,0.783,0.9,0.945,0.,1.,0.,1.)
          call gmfstf(4,0.18,0.783,0.96,0.995,0.,1.,0.,1.)
          call gmfstf(2,0.8,1.,0.,1.,0.,1.,0.,1.)
          call gmfsw(2)
          call gmfrec(0.,1.,0.,1.,icol)
          call gmfill(0.005,.5,16,icol)
          call gmfstf(2,0.81,0.99,0.004,0.996,0.,1.,0.,10.)
          call gmfsw(2)
          call gmfont(2,'h12w7rb   ') 
          do jch1=1,nchan
             ytext=10.4-jch1*.5
             call gmfrec(0.,1.,ytext-.45,ytext,icol)
             call gmfill(0.5,ytext-.35,icv(jch1),icol)
             labela(jch1)='          '
          enddo
          call gmfrec(0.,1.,0.04,.54,icol)
          call gmfrec(0.,1.,0.54,0.94,icol)
          call gmfrec(0.,1.,0.94,1.34,icol)
          call gmfill(0.005,.2,11,icol)
          call gmfill(0.005,1.2,11,icol)
          call gmftext(0.005,.4,0,7,'time[s]')
          call gmftext(0.005,1.2,0,8,'V[ml/s]:')
          labelv='      '
c------------------------------------------------------
          call time(to)
          do jch1=1,nchan
            if(irg.eq.0)then
                call volta(jch1-1,irout,volto(jch1))
            else
                call volts(jch1-1,0,volto(jch1))
            endif
          enddo
          tini=to
c
c Polling channels at a new time step
c
1         voltm=0
          npoll=0
2         do jch1=1,nchan
            if(irg.eq.0)then
                call volta(jch1-1,irout,voltc)
            else
                call volts(jch1-1,0,voltc)
            endif
            voltm(jch1)=voltm(jch1)+voltc
            vmax=amax1(abs(voltc),vmax)
            vsmax=amax1(voltc,vsmax)
            vsmin=amin1(voltc,vsmin)
          enddo
          npoll=npoll+1
          call time(tn)
          if(tn.lt.to+dt)goto2
          voltm=voltm/npoll
          graf=.false.
          call skbds(kbd)
          if(kbd.ge.59.and.kbd.le.91)then
             jch1=0
             if(kbd.le.66)then
                jch1=kbd-58
             elseif(kbd.ge.84)then
                jch1=kbd-75
             elseif(kbd.eq.67)then
                full=.not.full
                graf=.true.
             elseif(kbd.eq.68)then
                if(volmes)then
c Flowrate ml/s (volume l)
                    flowm=(a(8)+voltm(8)*b(8)-volold)/(tn-told)*1e3
                else
                    volold=a(8)+voltm(8)*b(8)
                    told=tn
                endif
                volmes=.not.volmes
                graf=.true.
             endif
             if(jch1.gt.0)then
                if(cal(jch1))then
                    cal(jch1)=.false.
                    vcal(jch1)=0
                else
                    cal(jch1)=.true.
                    vcal(jch1)=voltm(jch1)
                endif
             endif
          elseif(kbd.eq.27)then
             close(1)
             goto 100
          elseif(kbd.eq.133)then
             voltag=.not.voltag             
             graf=.true.
          elseif(kbd.eq.134)then
             if(ilin.gt.0)write(1,4)lin(1:ilin)
4            format(':',a)
             ilin=0
             graf=.true.
          elseif(kbd.ge.538976288.and.kbd.le.538976378)then
             ilin=min0(ilin+1,50)
             lin(ilin:ilin)=ch1
             call gmfsw(3)
             call gmftext(0.1,.95,0,ilin,lin)
          elseif(kbd.eq.8)then
             ilin=max0(ilin-1,0)
             graf=.true.
          endif        
          voltm=voltm-vcal
c Flowrate approximation FLOW= ml/s
c ----------------------------------------------------
c         volume=(voltm(8)-.513)/0.125
          volume=a(8)+voltm(8)*b(8)
c         call flow(dt,tn,volume,flowm)
c FLOW-end--------------------------------------------
c
          nstep=nstep+1
          if(mod(nstep,incr).eq.0)then
            if(iunits.ne.0)then
               write(1,3)tn-tini,voltm,volume,flowm
            else
               write(1,3)tn-tini,(a(ll)+b(ll)*voltm(ll),ll=1,16),
     /                   volume,flowm
            endif
          endif
3         format(f9.2,17f8.3,e11.3)
          if(irg.le.1)then
             if(tn-tini.ge.gtma.or.graf)then
c
c Draw empty window No.1 - graph
c
                if(.not.graf)then
                   gtmi=gtma
                   gtma=gtmi+gdt
                endif
                if(full)then
                   vrmin=-9.5
                   vrmax=9.5
                else
                   vrmin=vsmin
                   vrmax=vsmax
                endif
                call gmfont(2,'h18w9rb   ')
                call gmfste(1,0.,0.8,0.,1.,gtmi+gdtmi,gtma-gdtmi,
     /                     vrmin,vrmax)
                call gmfsyl('t [s]',5,'[V]',3)
                call gmfaxe(1,87,icarea)
                call gmfsw(4)
                call gmfrec(0.,1.,0.,1.,icol)
                call gmfill(0.5,.5,14,icol)
                call gmfont(2,'h12w7rb   ') 
                call gmftext(0.01,.8,13,4,'[Fi]')
                call gmftext(0.065,.8,0,10,'-calibrate')
                call gmftext(0.23,.8,13,4,'[F9]')
                if(full)then
                   call gmftext(0.3,.8,0,11,'-scale Volt')
                else
                   call gmftext(0.3,.8,0,11,'-full range')
                endif
                call gmftext(0.49,.8,13,5,'[F10]')
                if(volmes)then
                   call gmftext(0.58,.8,0,11,'-STOP flow ')
                else
                   call gmftext(0.58,.8,0,11,'-START flow')
                endif
                call gmftext(0.81,.8,13,5,'[F11]')
                if(voltag)then
                   call gmftext(0.89,.8,0,5,'-Temp')
                else
                   call gmftext(0.89,.8,0,5,'-Volt')
                endif
                call gmfsw(3)
                call gmfrec(0.,1.,0.,1.,icol)
                call gmfill(0.5,.5,14,icol)
                call gmftext(0.01,.75,13,6,'[F12]:')
                call gmfont(2,'h22w12rb  ')
                call gmftext(0.1,.95,0,ilin,lin)
             endif
c
c Measured Voltage
c
             x(1)=to-tini
             x(2)=tn-tini
             do jch1=1,nchan
                y(1)=volto(jch1)
                y(2)=voltm(jch1)
                call gmfsw(1)
                call gmfpl(2,x,y,icv(jch1))
                call gmfsw(2)    
                if(voltag)then
                   if(cal(jch1))then
                      write(label,5)jch1,voltm(jch1)
5                     format(i2,'C:',f6.3)
                   else
                      write(label,25)jch1,voltm(jch1)
25                    format(i2,'c:',f6.2)
                   endif
                else
c
c Temperatures, Power, Level
c
                   write(label,15)name(jch1),a(jch1)+b(jch1)*voltm(jch1)
15                 format(a4,f6.1)
                endif
                if(label.ne.labela(jch1))then
                   ytext=10.4-jch1*.5
                   call gmftext(0.03,ytext,icv(jch1),10,labela(jch1))
                   icolor=0
                   if(icv(jch1).eq.1.or.
     /                icv(jch1).eq.4.or.icv(jch1).eq.5)icolor=15
                   call gmftext(0.03,ytext,icolor,10,label)
                   labela(jch1)=label
                endif
             enddo
c
c Volt-max
c
             if(vmax.gt.vmaxo)then
                call gmfill(0.005,0.7,0,icol)
                write(label,8)vmax
8               format('MAX',f5.1)
                call gmftext(0.03,0.94,16,8,label)
                vmaxo=vmax
             endif
c
c Time
c
             call gmftext(0.4,0.54,11,6,labelt)
             write(labelt,6)tn-tini
6            format(f6.0)
             call gmftext(0.4,0.54,0,6,labelt)
c
c Flowrate
c
             call gmftext(0.4,1.34,11,6,labelv)
             write(labelv,7)flowm
7            format(f6.0)
             call gmftext(0.4,1.34,0,6,labelv)
          else
             write(*,3)tn-tini,voltm
          endif
          volto=voltm
          to=tn
          goto1          
      endif
      end

      subroutine time(t)
      REAL*8 t
      integer *2 hour,minute,second,ssecond
      call gettim(hour,minute,second,ssecond)
      t=hour*3600.D0+minute*60.D0+second+ssecond/100.
      end

      subroutine volta(jch,irange,volt)
c
c Read Volts with the highest possible resolution (range is not specified)
c INPUT : JCH-channel
c OUTPUT: RANGE, VOLT
c
      do irange=0,3
         call inum(jch,irange,ics)
         icsig=ics-2048
         if(iabs(icsig).ge.1024)goto 1
      enddo
      irange=3
1     range=10./2**irange
      volt=2.*range*ics/4096.-range
      end

      subroutine volts(jch,irange,volt)
c
c Read Volts (expecting JP7 selected to +-10V)
c INPUT : JCH-channel, RANGE-(0,1,2,3)
c OUTPUT: VOLT
c
      call inum(jch,irange,ics)
      range=10./2**irange
      volt=2.*range*ics/4096.-range
      end

      subroutine inum(jch,irange,ics)
c
c A/D conversion using PCL-818L data acquisition card
c
c Output:  ICS - read binary number in the range 0-4095
c Input :  JCH - channel number (0-15)
c          IRANGE - 0,1,2,3 (full range,..., 1/8 of full range)
c
c Card setting ibase addres BASE=300H
c
c Registers used
c BASE+0 (R)   D3 D2 D1 D0 C3 C2 C1 C0   (4 bits of data, 4 bits channel)
c BASE+0 (W)   any number                (conversion triggering)
c BASE+1 (R)   D11 .... D4               (8 bits of high data)
c BASE+1 (W)   ..... G1 G0  (range 00-full, 01-1/2, 10-1/4, 11-1/8)
c BASE+2 (W)   CH3...CH0 CL3...CL0       (MUX range)
c BASE+8 (R)   EOC _ MUX INT C3 ... C0   (EOC-1 busy, wait until 0)
c BASE+9 (R)   0 (disable interrupts and DMA)
c
      data ibase,ibase1,ibase2,ibase8,ibase9/768,769,770,776,777/
c disable DMA
      call spout(ibase9,0)
c clear INT
      call spout(ibase8,0)
c set sensitivity range 
      do i=1,10
      call spout(ibase2,17*jch)
c DELAY required!!
      enddo
      call spout(ibase1,irange)
c set MUX for one channel scanning
      do i=1,10
      call spout(ibase2,17*jch)
      enddo
c start conversion
      call spout(ibase,1)
      i=0
c EOC - End Of Conversion
1     call spin(ibase8,ibyte)
      i=i+1
      if(btest(ibyte,7).and.i.le.5000) goto 1
c get data
      call spin(ibase,ibyte)
      ics=ibyte/16
      call spin(ibase1,ibyte)
      ics=ics+ibyte*16
      end

    
