unit tcn75a;

interface

uses
  i2c;

const
  TCN75ADDR = $90;
  TCN75CONFIG = $01;
  TCN75LIMIT = $03;
  TCN75HYST = $02;
  TCN75TEMP = $00;

type
  i2c_tcn75a = object (i2c_port)
    procedure Initialize(port_addr, s_addr: word);
    function ModeSet(new_mode: byte): boolean;
    function LimitSet(new_limit: word): boolean;
    function HysteresisSet(new_hyst: word): boolean;
    function TemperatureRead(var temp: word): boolean;
  end;

implementation

procedure i2c_tcn75a.Initialize(port_addr, s_addr: word);
begin
  inherited Initialize(port_addr, TCN75ADDR + s_addr shl 1);
end;

function i2c_tcn75a.ModeSet(new_mode: byte): boolean;
begin
  Start;
  ModeSet := false;
  if ByteSent(addr) then
    if ByteSent(TCN75CONFIG + I2C_WRITE) then
      if ByteSent(new_mode) then
        ModeSet := true;
  Stop;
end;

function i2c_tcn75a.LimitSet(new_limit: word): boolean;
begin
  Start;
  LimitSet := false;
  if ByteSent(addr + I2C_WRITE) then
    if ByteSent(TCN75LIMIT) then
      if ByteSent(new_limit shr 8) then
        if ByteSent(new_limit and $ff) then
          LimitSet := true;
  Stop;
end;

function i2c_tcn75a.HysteresisSet(new_hyst: word): boolean;
begin
  Start;
  HysteresisSet := false;
  if ByteSent(addr + I2C_WRITE) then
    if ByteSent(TCN75HYST) then
      if ByteSent(new_hyst shr 8) then
        if ByteSent(new_hyst and $ff) then
          HysteresisSet := true;
  Stop;
end;

function i2c_tcn75a.TemperatureRead(var temp: word): boolean;
var
  msb, lsb: byte;
begin
  Start;
  TemperatureRead := false;
  if ByteSent(i2c_addr + I2C_WRITE) then
    if ByteSent(TCN75TEMP) then begin	{ Select temperature register }
      Stop;
      Start;
      if ByteSent(i2c_addr + I2C_READ) then
        if ByteReceived(msb) then
          if ByteReceived(lsb) then begin
            temp := msb * 256 + lsb;
            TemperatureRead := true;
          end;
    end;
  Stop;
end;

begin
end.
