Home    Impressum / Datenschutz    Shop    Download    Links     Blog  

Ansteuerung von I2C-Slaves mit dem I2C-Koppler in Delphi

Quellcode zur Ansteuerung von I2C-Baugruppen und Temperatursensor LM75

 Zitat:

Hallo,

Bevor ich mich dem Modem zuwende, wollte ich Ihnen zunächst noch eine Beispielanwendung in Delphi schicken.

Diese bildet Ihr Excel-Programm nach und beinhaltet eine Schicht zur Ansteuerung der Port-DLL((portinc.pas), eine Adaptionsschicht für Ihr serielles Interface (horter_seriell.pas), eine allgemeiner Schicht zur Bedienung des I2c-busses(i2c_base.pas) sowie ein AddOn für den LM75(lm75inc.pas). All das wird in dem Beispielprogramm angewendet.

Viele Grüße Thomas D.

unit portinc;
{
Delphi Includes for PORT.DLL written by B.Kainka/Elektor
Description is here: http://www.b-kainka.de/referenz.txt
Download: http://www.b-kainka.de/port.zip
(C)2006 Thomas Dreßler
}

interface

type DWORD=LONGWORD;
Procedure DELAY(i:WORD); stdcall; external 'PORT.DLL';
Procedure TIMEINIT; stdcall; external'PORT.DLL';
Function TIMEREAD: DWORD; stdcall; external 'PORT.DLL';
Procedure DELAYUS(i:DWORD); stdcall; external 'PORT.DLL';
Procedure TIMEINITUS; stdcall; external 'PORT.DLL';
Function TIMEREADUS: DWORD; stdcall; external 'PORT.DLL';
Procedure OUTPORT(PortAddr:Word; Data:byte); stdcall; external 'PORT.DLL';
Function INPORT(PortAddr:Word):Byte;stdcall; external 'PORT.DLL';
Function OPENCOM(S:PCHAR):Integer;stdcall; external 'PORT.DLL';
Procedure CLOSECOM; stdcall; external 'PORT.DLL';
Function READBYTE:Integer;stdcall; external 'PORT.DLL';
Procedure SENDBYTE(d:WORD);stdcall; external 'PORT.DLL';
Procedure DTR(d:WORD);stdcall; external 'PORT.DLL';
Procedure RTS(d:WORD);stdcall; external 'PORT.DLL';
Procedure TXD(d:WORD);stdcall; external 'PORT.DLL';
Function CTS:Integer;stdcall; external 'PORT.DLL';
Function DSR:Integer;stdcall; external 'PORT.DLL';
Function RI:Integer;stdcall; external 'PORT.DLL';
Function DCD:Integer;stdcall; external 'PORT.DLL';
Procedure TIMEOUT(Wert: WORD); stdcall; external 'PORT.DLL';
Procedure REALTIME(d:BOOLEAN);stdcall; external 'PORT.DLL';
Function SOUNDSETRATE(Rate:DWORD):DWORD; stdcall; external 'PORT.DLL';
Function SOUNDGETRATE:DWORD; stdcall; external 'PORT.DLL';
Function SOUNDBUSY:Boolean; stdcall; external 'PORT.DLL';
Function SOUNDIS:Boolean; stdcall; external 'PORT.DLL';
Procedure SOUNDIN(Puffer:Pchar;Size:DWORD); stdcall; external 'PORT.DLL';
Procedure SOUNDOUT(Puffer:Pchar;Size:DWORD); stdcall; external 'PORT.DLL';
Function SOUNDGETBYTES:DWORD; stdcall; external 'PORT.DLL';
Function SOUNDSETBYTES(B:DWORD):DWORD; stdcall; external 'PORT.DLL';
Procedure SOUNDCAPIN; stdcall; external 'PORT.DLL';
Procedure SOUNDCAPOUT; stdcall; external 'PORT.DLL';
Function JOYX:DWORD;stdcall; external 'PORT.DLL';
Function JOYY:DWORD;stdcall; external 'PORT.DLL';
Function JOYZ:DWORD;stdcall; external 'PORT.DLL';
Function JOYR:DWORD;stdcall; external 'PORT.DLL';
Function JOYBUTTON:DWORD;stdcall; external 'PORT.DLL';
implementation
End.

unit horter_seriell;
{
I2C Hardware interface for Horter & Kalb I2C-Interfaces
based on Port.DLL from B.Kainka
(C) 2006 Thomas Dreßler
}

interface
FunctionPortOpen(Port:String):Boolean;
Procedure PortClose;
Procedure SCL(State:Boolean);
Procedure SDA(State:Boolean);
FunctionGetSDA:Boolean;
FunctionGetINT:Boolean;
FunctionGetSCL:Boolean;
FunctionDeviceCheck:Boolean;
FunctionDeviceOpen:Boolean;
implementation
uses portinc;
const Parameter=':1200,N,8,1';
high = true;
low = false;
var PortState:Boolean;
SCLState:Boolean;

// --------------------------
// Hardware specific parts

// --------------------------

Procedure SCL(state:Boolean);
//SCL auf der RTS-Leitung am COM-Port
var b:word;
Begin
ifstate then b:=1 <font color="#0000AD">else</font> b:=0;
RTS(b);
SCLState:=state;
End;

Procedure SDA(state:Boolean);
//Die Daten werden über DTR am COM-Port geschrieben
var b:word;
Begin
ifstate then b:=1 <font color="#0000AD">else</font> b:=0;
DTR(b);
End;

FunctionGetSCL():Boolean;
//faked
Begin
Result :=SCLState;
End;

FunctionGetSDA():Boolean;
//Die SDA-Leitung wird über DSR am COM-Port wieder zurückgelesen
Begin
Result := (DSR()=1);
End;

FunctionGetINT:Boolean;
//Die INT-Leitung der Eingabekarten wird über CTS am COM-Port überwacht
Begin
Result := (CTS()=1);
End;
// check openflag
FunctionDeviceCheck:Boolean;
Begin
SDA(high);
SCL(high);
if(GetSDA) then Result:=true <font color="#0000AD">else</font> Result:=false;
End;

// check openflag
FunctionDeviceOpen:Boolean;
Begin
Result:=PortState;
End;

// Schnittstelle öffnen und Leitungen initialiseren
FunctionPortOpen(Port:String):Boolean;
var p:string;
Begin
Result := false;
Portstate:=false;
p:=Port+Parameter;
if(OPENCOM(PCHAR(p))<>0) then
Begin
ifDeviceCheck() then PortState:=true;
Result := PortState;
End;
End;

// Schnittstelle freigeben
Procedure PortClose;
Begin
CLOSECOM();
PortState:=false;
End;

End.

unit i2c_base;
{
i2c Base Library for Horter & Kalb I2C-interfaces
based on Port.DLL from B.Kainka and
Demo Program I2C-LM75-Test.XLS from Horter & Kalb
(C) 2006 Thomas Dreßler
}

interface
FunctionI2Copen(Port:String):Boolean;
Procedure I2CClose;
Procedure I2CStart;
Procedure I2CStop;
Procedure I2CInit;
FunctionI2Cout(b:Byte):Boolean;
FunctionI2Cin:Byte;
Procedure I2CAck;
Procedure I2CNoAck;

implementation
//Hardware Unit: Horter Serial PC Interface
uses horter_seriell;

Const
high = true;
low = false;

// ---------------
// I2C-Protocoll
//----------------

// Schnittstelle oeffnen und testen

FunctionI2Copen(Port:String):Boolean;
Begin
Result:=PortOpen(Port);
End;

Procedure I2CInit;
Begin
//Ruhezustand SDA=high und SCL=high
SDA (high);
SCL (high);
End;

// Sendet ein Startbit zum Slave
Procedure I2CStart;
Begin
// Wenn CLK high ist, wechselt SDA von high nach low
SDA(high);
SCL(high);
SDA(low);
SCL(low);
End;

// Sendet ein Stopbit zum Slave
Procedure I2CStop;
Begin
// Wenn CLK high ist, wechselt SDA von low nach high
SDA(low);
SCL(high);
SDA(high);
End;

Procedure I2CAck;
Begin
//ACKNOWLEDGE => Byte empfangen weitere Daten senden
SDA (low);
SCL (high);
SCL (low);
SDA (high);
End;


Procedure I2CNoAck;
Begin
//NO ACKNOWLEDGE => Byte empfangen - Keine weiteren Daten senden
SDA (high);
SCL (high);
SCL (low);
End;

// Schnittstelle ´schliessen
Procedure I2Cclose;
Begin
//i2cNoack;
//I2CStop;
PortClose;
End;

// Sendet ein Byte zum Slave
// b1 ist das Byte, welches zum Slave gesendet wird

FunctionI2Cout(b:Byte):Boolean;
Var i:Integer; Bit:Byte;
Begin
Result := false;
Bit := $80;
For i := 1 to 8 do Begin
if(b and Bit) = Bit then
SDA(high)
Else
SDA(low);

SCL(high);
SCL(low);
//SDA(high);
Bit := Bit div 2;
End;
{Acknowledge empfangen ->
SDA high setzen - wird vom Slave heruntergezogen
9. Impuls}

SDA(high);
SCL(high);
//Slave antwortet mit neg. Flanke
Result := not GetSDA;
SCL(low);
SDA(high);
End;

// Empfängt ein Byte vom Slave
// Ack ist das zum Slave gesendete Acknowledge Bit.
// Ist bei einigen Bausteinen notwendig um einen
// Autoincrement Modus zu beenden.

FunctionI2Cin:Byte;
Var i:Integer; b1:Byte;
Begin
Result := 0;
SDA(high);
b1 := $00;
For i := 1 to 8 do Begin
b1 := b1 * 2;
SCL(high);
ifGetSDA = high then b1 := b1 or $01;
SCL(low);
End;
Result := b1;
End;
end.

 

Delphi-Quellcode Beispiel
delphi_I2C-seriell-Test.zip (10 kB)
Delphi Quellcode mit einem Beispiel wie die port.dll in Delphi eingebunden und die Funktionen aufgerufen werden können.