Program Examples

This program continuously measures analog channel 2 and sends the results to the RS232-port.

program analog_to_rs232;

const channel=2;

var result : byte;

begin
  repeat
    analog(channel,result);
    write(rs232,result);
  until 0=1;
end.

This program shows how to use a PCF8574 (Philips) I2C 8-bit I/O expander using I2C commands. It contains procedures to write data to the ports and read the status of the ports.

program ioexpander;

var buffer : byte;

procedure read_io(var data : byte);

begin
  { read i/o to porti using I2C }
  write(i2c, start); { repeated start }
  write(i2c, 79);    { address + 1 }
  read(i2c, data);   { read from all 8 i/o-lines }
  write(i2c, stop);  { stop }
end;

procedure write_io(data : byte);
begin
  { write porti to i/o using I2C }
  write(i2c, start); { start }
  write(i2c, 78);    { address }
  write(i2c, data);  { write to all 8 i/o-lines }
  write(i2c, stop);  { stop }
end;

begin
  { If LEDs are attached to the I/O-lines, a pattern is shown. }
  buffer:=1;
  repeat
    write_io(buffer);
    buffer:=buffer*2;
    delay(250);
  until 0=1; { endless loop }
end.

This program counts the pulses on counter-input and displays the result on a LCD-module. Counts till 99,999,999 (expandable), maximum frequency is approximately 25 kHz.

program counting;

var hundreds, tenthousands, millions, units : byte;

procedure interrupt;
begin
  write(counter,156); { 100 pulses before interrupt occurs } 
  inc(hundreds);         
  if hundreds>99 then 
    begin
      clr(hundreds);
      inc(tenthousands);
      if tenthousands>99 then
        begin
          clr(tenthousands);
          inc(millions);
          if millions>99 then millions:=0;
        end;
    end;
end;

procedure init;
begin
  hundreds:=0;
  tenthousands:=0;
  millions:=0;
  write(counter,156);   { Give counter preset of 156 }
  write(lcd,255);       { Clear LCD-display }
end;

procedure write2lcd;
var dec1, dec2 : byte;
begin
  gotoxy(4,0);
  dec1:=millions div 10;
  dec2:=millions mod 10;
  dec1:=dec1+48;
  dec2:=dec2+48;
  write(lcd, dec1, dec2);
  dec1:=tenthousands div 10;
  dec2:=tenthousands mod 10;
  dec1:=dec1+48;
  dec2:=dec2+48;
  write(lcd, dec1, dec2);
  dec1:=hundreds div 10;
  dec2:=hundreds mod 10;
  dec1:=dec1+48;
  dec2:=dec2+48;
  write(lcd, dec1, dec2);
  read(counter, units);
  units:=units-156;
  dec1:=units div 10;
  dec2:=units mod 10;
  dec1:=dec1+48;
  dec2:=dec2+48;
  write(lcd, dec1, dec2);
end;

begin
  init;
  repeat
    write2lcd;
  until 0=1;
end.

This program creates a stopwatch using a clock-module attached to the expansion bus, and buttons on d2 ( = start ) and d3 ( = stop ) to GND.

program stopwatch;

var hours, minutes, seconds, hundreds : byte;

procedure init;
begin
  write(lcd,255); { clear LCD }
end;

procedure write_time;
var dec1, dec2 : byte;
begin
  dec1:=hours DIV 10;
  dec2:=hours MOD 10; 
  dec1:=dec1+48;
  dec2:=dec2+48;
  gotoxy(2,2);
  write(lcd,dec1,dec2,':');
  dec1:=minutes DIV 10;
  dec2:=minutes MOD 10; 
  dec1:=dec1+48;
  dec2:=dec2+48;
  write(lcd,dec1,dec2,':');
  dec1:=seconds DIV 10;
  dec2:=seconds MOD 10; 
  dec1:=dec1+48;
  dec2:=dec2+48;
  write(lcd,dec1,dec2,'.');
  dec1:=hundreds DIV 10;
  dec2:=hundreds MOD 10; 
  dec1:=dec1+48;
  dec2:=dec2+48;
  write(lcd,dec1,dec2);
end;

{ Main program }
begin
  init;
  repeat
    if not(d2) then settime(0,0,0,0);
    if not(d3) then 
      begin 
        gettime(hours,minutes,seconds,hundreds);
        write_time;
      end;
  until 0=1;
end.