home *** CD-ROM | disk | FTP | other *** search
/ Power Programming / powerprogramming1994.iso / progtool / microcrn / issue_37.arc / REAL-37.FIG < prev    next >
Text File  |  1987-08-05  |  3KB  |  82 lines

  1. { control AC via parallel port A or B }
  2.  
  3. type
  4.     switch_positions = (OFF,ON);
  5.     switch_rec = record
  6.                    name : string[15];
  7.                    x_location, y_location : integer; {screen coords}
  8.                  end;
  9.  
  10. const
  11.      port_a = $220;  { Port locations in the PIO-12 8255 chip. }
  12.      port_b = $221;  { Use $378 for LPT1 or $278 for LPT2.     }
  13.      port_c = $222;  { The stupid compiler can't add constants.}
  14.      control = $223; { To configure the 8255.                  }
  15.  
  16.      switches : array[0..7] of switch_rec = ( { easier than initializing }
  17.        (name:'Doghouse'; x_location:1; y_location: 1 ),
  18.        (name:'Floodlight 1'; x_location:40; y_location: 1 ),
  19.        (name:'Floodlight 2'; x_location: 1 ; y_location: 6 ),
  20.        (name:'Garage'; x_location:40; y_location: 6 ),
  21.        (name:'Spotlight'; x_location: 1; y_location: 11 ),
  22.        (name:'Radio'; x_location: 40 ; y_location: 11 ),
  23.        (name:'Alarm'; x_location: 1 ; y_location: 16 ),
  24.        (name:'Lock'; x_location: 40 ; y_location: 16 )
  25.      );
  26.      HOME_X = 14;  { Keep cursor here during program execution }
  27.      HOME_Y = 20;
  28.  
  29. var
  30.    switch_state : array [0..7] of switch_positions;
  31.    i : integer;
  32.    switch_byte : byte;  { value sent to the output port }
  33.    CH : char;
  34.  
  35. procedure display_switch_state(switch : integer);
  36. { Show new switch state on monitor }
  37. begin
  38.   with switches[i] do begin
  39.      gotoXY(x_location,y_location);
  40.      write(switch,' : ',name);
  41.      gotoXY(x_location + 20, y_location);
  42.      if switch_state[switch] = on then write('ON ')
  43.      else write('OFF');
  44.   end;
  45. end;
  46.  
  47. procedure invert_switch(switch : integer);
  48. { flip a switch from off to on or on to off }
  49. begin
  50.      { first flip the bit and output the result }
  51.      switch_byte := switch_byte xor (1 shl switch);
  52.      port[port_b] := switch_byte;  { see "Port Array" in the manual }
  53.      { now invert the display information }
  54.      if switch_state[switch] = on then
  55.         switch_state[switch] := off else
  56.         switch_state[switch] := on;
  57. end;
  58.  
  59. begin
  60.      { Initialize the 8255.  Not necessary for LPT1 or LPT2   }
  61.      port[control] := $99;   { Port B is output, A & C inputs }
  62.      { Initialize everything else }
  63.      switch_byte := 0;
  64.      ClrScr;
  65.      for i := 0 to 7 do begin
  66.          switch_state[i] := off;
  67.          display_switch_state(i);
  68.      end;
  69.      GotoXY(HOME_X,HOME_Y);
  70.      Write('Press number to change switch; ESC to quit');
  71.      CH := ' ';
  72.      while CH <> chr(27) do begin
  73.            GotoXY(HOME_X -1,HOME_Y);
  74.            read(KBD,CH);
  75.            i := ord(CH) - ord('0');
  76.            if i in [0..7] then begin
  77.               invert_switch(i);
  78.               display_switch_state(i);
  79.            end;
  80.      end;
  81. end.
  82.