unit Unit1;
interface
uses
Winapi.Windows,
Winapi.Messages, System.SysUtils, System.Variants, System.Classes,
Vcl.Graphics,
Vcl.Controls,
Vcl.Forms,
Vcl.Dialogs , i2cusb,
Vcl.StdCtrls,
Vcl.ComCtrls;
const
cTimeoutInMs = 1000;
cTimeoutInit = 5000;
type
TArray =
array[0..7]
of Byte;
TArray11 =
array[0..10]
of Byte;
TByteArr =
array of byte;
TForm1 =
class(TForm)
Button1: TButton;
StatusBar1: TStatusBar;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
private
{ Déclarations privées }
public
end;
function start_i2c ( address : Integer ):ShortInt;
function stop_i2c():ShortInt;
function slave_port( address: TArray): ShortInt;
function ACK_Slave():ShortInt;
function ACK_Master():ShortInt;
Function temperature_LM75_lesen():TArray;
function Integer_To_Byte(str:
String):TByteArr;
function write( Wert : TArray ):ShortInt;
function read():TArray;
function No_ACK_Master():ShortInt;
var
Form1: TForm1;
ic : Ti2cUsb;
Temperatur1 : Byte;
Temperatur2 : Byte;
Wert : Double ;
Byte1, Byte2 :Byte;
a : Double;
write_add_temp : TArray = (1,0,0,1,0,0,0,0);
read_add_temp : TArray = (1,0,0,1,0,0,0,1);
write_add_pca : TArray = (0,1,0,0,1,1,1,0);
read_add_pca : TArray = (0,1,0,0,1,1,1,1);
init_temp : TArray = (0,0,0,0,0,0,0,0);
pointer_Byte : TArray = (0,0,1,1,0,0,0,1);
implementation
{$R *.dfm}
function slave_port( address : TArray): ShortInt;
var i:ShortInt;
begin
for i := 0
to 7
do
begin
if address[i] = 1
then
begin
ic.wr_byte_port(1);
//SDA = 1 SCL = 0
ic.wr_byte_port(0);
//SDA = 1 SCL = 1
end
else
begin
ic.wr_byte_port(3);
//SDA = 0 SCL = 0
ic.wr_byte_port(2);
//SDA = 0 SCL = 1
end;
end;
Result := 0;
end;
function start_i2c ( address : Integer ):ShortInt;
begin
ic.wr_byte_port(0);
//SDA = 1 ; SCL = 1
ic.wr_byte_port(2);
//SDA = 0 ; SCL = 1
if address = 144
then
begin
slave_port(write_add_temp);
end
else if address = 145
then
begin
slave_port(read_add_temp);
end
else if address = 78
then
begin
slave_port(write_add_pca);
end
else if address = 79
then
begin
slave_port(read_add_pca);
end;
Result := 0;
end;
function ACK_Slave():ShortInt;
begin
ic.wr_byte_port(1);
//SDA = 1 SCL = 0
ic.wr_byte_port(0);
//SDA = 1 SCL = 1
ic.wr_byte_port(3);
//SDA = 0 SCL = 0 // 3
Result := 0;
end;
function ACK_Master():ShortInt;
begin
ic.wr_byte_port(3);
//SDA = 0 SCL = 0
ic.wr_byte_port(2);
//SDA = 0 SCL = 1
ic.wr_byte_port(3);
//SDA = 0 SCL = 0
Result := 0;
end;
function No_ACK_Master():ShortInt;
begin
ic.wr_byte_port(1);
//SDA = 1 SCL = 0
ic.wr_byte_port(0);
//SDA = 1 SCL = 1
ic.wr_byte_port(3);
//SDA = 0 SCL = 0
Result := 0;
end;
function stop_i2c():ShortInt;
begin
ic.wr_byte_port(3);
//SDA = 0 SCL = 0
ic.wr_byte_port(0);
//SDA = 1 ; SCL = 1
Result := 0;
end;
Function temperature_LM75_lesen():TArray;
//Liest einen Wert (1 Byte) vom LM75 Temperatursensor
var
i:ShortInt;
value:Byte;
arr:TArray;
begin
for i := 7
downto 0
do
begin
ic.wr_byte_port(1);
ic.wr_byte_port(0);
ic.rd_byte_port(value);
arr[i] := value;
ShowMessage(value.ToString);
end;
Result := arr;
end;
function Integer_To_Byte(str:
String):TByteArr;
var
i: integer;
begin
SetLength(Result, Length(str));
for i := 0
to length(str)-1
do
Result[i] := ord(str[i+1]) -48;
end;
function write( Wert : TArray ):ShortInt;
var
myByte : TArray;
i : ShortInt;
begin
myByte := Wert;
for i := 7
downto 0
do
begin
if myByte[i] = 1
then
begin
ic.wr_byte_port(1);
//SDA = 1 SCL = 0
ic.wr_byte_port(0);
//SDA = 1 SCL = 1
end
else
begin
ic.wr_byte_port(3);
//SDA = 0 SCL = 0
ic.wr_byte_port(2);
//SDA = 0 SCL = 1
end;
end;
Result := 0;
end;
function read():TArray;
var
i:ShortInt;
value:Byte;
arr : TArray;
begin
for i := 7
downto 0
do
begin
ic.wr_byte_port(1);
ic.wr_byte_port(0);
ic.rd_byte_port(value);
arr[i] := value;
end;
Result := arr;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
val1 : TArray;
val2 : TArray;
temp : TArray11;
Value : double ;
I: Integer;
begin
//Register Temp configurate
ic := Ti2cUsb.Create;
ic.Init(3);
start_i2c(78);
ACK_Slave;
write(init_temp);
ACK_Slave;
stop_i2c;
//Read Temp register with preset Pointer (2 Byte Data)
start_i2c(79);
ACK_Slave;
val1 := temperature_LM75_lesen;
ACK_Master;
val2 := temperature_LM75_lesen;
No_ACK_Master;
stop_i2c;
for I := 0
to 10
do
begin
if I<3
then
begin
temp[I] := val2[I];
end
else
begin
temp[I] := val1[I];
end;
end;
end;
End.