Einzelnen Beitrag anzeigen

yacdrak

Registriert seit: 22. Dez 2020
7 Beiträge
 
#1

I2C BUS LM75A Temperatur sensor Problem

  Alt 10. Apr 2021, 11:40
Moin zusammen,
Ich versuche Temperatur aus der LM75A Sensor zu lesen. Ich bekomme immer das gleiche Problem : Ausgabe 253. könnte jemand mir helfen bitte ? danke.
Anhang finden Sie mein Quellcode.
Delphi-Quellcode:
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 Integer;
  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_device():ShortInt;
  function No_ACK():ShortInt;
 Function temperature_LM75_lesen():TArray;
 function Integer_To_Byte(str: String):TByteArr;
 function write( Wert : TArray ):ShortInt;
 function read():TArray;


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,1,0);
  read_add_temp : TArray = (1,0,0,1,0,0,1,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,0,0,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 = 146 then
   begin
     slave_port(write_add_temp);
   end
   else if address = 147 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 No_ACK():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 ACK_device():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 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;


       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;// Integer_To_Byte(Wert);

   for i := 0 to 7 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;
 Value : double ;
 begin
 //Register Temp configurate
 ic := Ti2cUsb.Create;
 ic.Init(10);
 start_i2c(146);
 ACK_device;
 write(pointer_Byte);
 //ACK_device;
 //write(init_temp);
 No_ACK;
 stop_i2c;
 //Read Temp register with preset Pointer (2 Byte Data)
 start_i2c(147);
 ACK_device;
 val1 := temperature_LM75_lesen;
 ACK_device;
 val2 := temperature_LM75_lesen;
 No_ACK;
 {
  If (val1 And 128) = 0 Then
    begin
        Value := val1              //Temperatur Vorkomma >= 0°C
    end
    else
    begin
        Value := val1 - 255;        //Temperatur Vorkomma < 0°C
    end;
    If ((val2 And 128) <> 0) Then
        begin
        Value := Value + 0.5;
        end;
   }


 stop_i2c();

      ShowMessage(val1[0].ToString);
      ShowMessage(val2[1].ToString);
 end;

End.
Miniaturansicht angehängter Grafiken
capture-1.jpg   capture-2.jpg  
Angehängte Dateien
Dateityp: txt delphi i2c test.txt (4,9 KB, 9x aufgerufen)
  Mit Zitat antworten Zitat