⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 i2cdllfuncunit.pas

📁 Delphi源码,实现I2C接口,非常适合硬件开发人员
💻 PAS
📖 第 1 页 / 共 2 页
字号:
procedure Read_External_Device(DeviceName: String; DeviceIndex, ExternalDeviceIndex: Integer; ClockDivisor: Dword;
                               DevAddress: Integer; bBlockRead: Boolean; var DisplayLines: TStringList);
var passed : boolean;
ExternalDevice : string;
BlockDataRead: ReadBlockDataBuff;
ftStatus: FTC_STATUS;
LocationID: Dword;
begin
  if (FT2232CI2CDll = True) then
  begin
    LocationID := LocationIDs[DeviceIndex];
    ftStatus := FT2232CI2C.OpenEx(DeviceName, LocationID, @ftHandle);

    if (ftStatus = FTC_SUCCESS) then
    begin
      ftStatus := FT2232CI2C.InitDevice(ftHandle, ClockDivisor);

      if (ftStatus = FTC_SUCCESS) then
      begin
        case ExternalDeviceIndex of
          I2C24C02ChipIndex: ftStatus := Read_24C02_Device(DevAddress, bBlockRead, DisplayLines);
          I2CM24C64ChipIndex: ftStatus := Read_M24C64_Device(DevAddress, bBlockRead, DisplayLines);
          I2CDS1621ChipIndex: ftStatus := Read_DS1621_Device(DevAddress, DisplayLines);
          I2CM41T81ChipIndex: ftStatus := Read_M41T81_Device(DevAddress, bBlockRead, DisplayLines);
        end;
      end
      else
        DisplayFT2232CI2CDllError('Read_24C02', 'InitDevice', ftStatus);

      if (ftHandle <> 0) then
      begin
        FT2232CI2C.Close(ftHandle);

        ftHandle := 0;
      end;
    end
    else
      DisplayFT2232CI2CDllError('Read_24C02', 'OpenEx', ftStatus);
  end;
end;

function  Program_M24C64_Device(DevAddress: Integer; bErase, bPageWrite: Boolean; ByteValue: String; var DisplayLines: TStringList): FTC_STATUS;
var
  ftStatus: FTC_STATUS;
  LocationAddress: Dword;
  ControlDevAddress, ControlLocAddress0, ControlLocAddress1: Byte;
  WriteControlBuffer: WriteControlByteBuffer;
  WriteDataBuffer: WriteDataByteBuffer;
  WriteDataBufferIndex: Dword;
  ByteValueStr, HexByteValueStr: string;
  PageWriteData: FtcPageWriteData;
begin
  LocationAddress := 256; //0;

  //passed := Write_Location(j,DevAddress,j);
  // set up device address
  ControlDevAddress := (DevAddress shl 1);
  ControlDevAddress := (ControlDevAddress or $A0);
  ControlDevAddress := (ControlDevAddress and $FE);

  repeat
    // shift down by 8 bits
    ControlLocAddress1 := ((LocationAddress SHR 8) and $FF);
    ControlLocAddress0 := (LocationAddress and $FF);

    WriteControlBuffer[0] := ControlDevAddress;
    WriteControlBuffer[1] := ControlLocAddress1;
    WriteControlBuffer[2] := ControlLocAddress0;

    if (not bErase) then
      ByteValueStr := ByteValue
    else
      ByteValueStr := 'FF';

    if (ByteValueStr = '') then
      WriteDataBuffer[0] := ControlLocAddress0
    else
    begin
      HexByteValueStr := '$';
      HexByteValueStr := HexByteValueStr + ByteValueStr;

      WriteDataBuffer[0] := StrToInt(HexByteValueStr);
    end;

    if (not bPageWrite) then
      ftStatus := FT2232CI2C.Write(fthandle, @WriteControlBuffer, 3, True, 20, True,
                                   BYTE_WRITE_TYPE, @WriteDataBuffer, 1, True, 20,
                                   @PageWriteData)
    else
    begin
      PageWriteData.dwNumPages := NumM24C64Pages;
      PageWriteData.dwNumBytesPerPage := NumM24C64BytesPerPage;

      for WriteDataBufferIndex := 1 to ((NumM24C64Pages * NumM24C64BytesPerPage) - 1) do
        WriteDataBuffer[WriteDataBufferIndex] := WriteDataBuffer[0];

      ftStatus := FT2232CI2C.Write(fthandle, @WriteControlBuffer, 3, True, 20, True,
                                   PAGE_WRITE_TYPE, @WriteDataBuffer, (NumM24C64Pages * NumM24C64BytesPerPage),
                                   True, 20, @PageWriteData);
    end;

    if (ftStatus = FTC_SUCCESS) then
    begin
      if (not bErase) then
        DisplayLines.Add('Loc Addr : ' + HexWrdToStr(LocationAddress) + ' Data : ' + HexWrdToStr(WriteDataBuffer[0]) );
    end
    else
      DisplayFT2232CI2CDllError('Program_M24C64_Device', 'Write', ftStatus);

    if (not bPageWrite) then
      Inc(LocationAddress)
    else
      LocationAddress := LocationAddress + (NumM24C64Pages * NumM24C64BytesPerPage);
  until ((LocationAddress >= MaxI2CM24C64ChipSizeInBytes) or (ftStatus <> FTC_SUCCESS));
  
  Result := ftStatus;
end;

function  Program_24C02_Device(DevAddress: Integer; bErase, bPageWrite: Boolean; ByteValue: String; var DisplayLines: TStringList): FTC_STATUS;
var
  ftStatus: FTC_STATUS;
  LocationAddress: Dword;
  ControlDevAddress, ControlLocAddress: Byte;
  WriteControlBuffer: WriteControlByteBuffer;
  WriteDataBuffer: WriteDataByteBuffer;
  ByteValueStr, HexByteValueStr: string;
  PageWriteData: FtcPageWriteData;
begin
  LocationAddress := 0;

  //passed := Write_Location(j,DevAddress,j);
  // set up device address
  ControlDevAddress := (DevAddress shl 1);
  ControlDevAddress := (ControlDevAddress or $A0);
  ControlDevAddress := (ControlDevAddress and $FE);

  repeat
    ControlLocAddress := (LocationAddress and $FF);

    WriteControlBuffer[0] := ControlDevAddress;
    WriteControlBuffer[1] := ControlLocAddress;

    if (not bErase) then
      ByteValueStr := ByteValue
    else
      ByteValueStr := 'FF';

    if (ByteValueStr = '') then
      WriteDataBuffer[0] := ControlLocAddress
    else
    begin
      HexByteValueStr := '$';
      HexByteValueStr := HexByteValueStr + ByteValueStr;

      WriteDataBuffer[0] := StrToInt(HexByteValueStr);
    end;

    if (not bPageWrite) then
      ftStatus := FT2232CI2C.Write(fthandle, @WriteControlBuffer, 2, True, 20, True,
                                   BYTE_WRITE_TYPE, @WriteDataBuffer, 1, True, 20,
                                   @PageWriteData)
    else
    begin
      PageWriteData.dwNumPages := Num24C02Pages;
      PageWriteData.dwNumBytesPerPage := Num24C02BytesPerPage;

      WriteDataBuffer[1] := WriteDataBuffer[0];

      ftStatus := FT2232CI2C.Write(fthandle, @WriteControlBuffer, 2, True, 20, True,
                                   PAGE_WRITE_TYPE, @WriteDataBuffer, (Num24C02Pages * Num24C02BytesPerPage),
                                   True, 20, @PageWriteData);
    end;

    if (ftStatus = FTC_SUCCESS) then
    begin
      if (not bErase) then
        DisplayLines.Add('Loc Addr : ' + HexWrdToStr(LocationAddress) + ' Data : ' + HexWrdToStr(WriteDataBuffer[0]) );
    end
    else
      DisplayFT2232CI2CDllError('Program_24C02_Device', 'Write', ftStatus);

    if (not bPageWrite) then
      Inc(LocationAddress)    
    else
      LocationAddress := LocationAddress + (Num24C02Pages * Num24C02BytesPerPage);
  until ((LocationAddress >= MaxI2C24C02ChipSizeInBytes) or (ftStatus <> FTC_SUCCESS));

  Result := ftStatus;
end;

function  Program_DS1621_Device(DevAddress: Integer; var DisplayLines: TStringList): FTC_STATUS;
var
  ftStatus: FTC_STATUS;
  LocationAddress: Dword;
  ControlDevAddress: Byte;
  WriteControlBuffer: WriteControlByteBuffer;
  WriteDataBuffer: WriteDataByteBuffer;
  ByteValueStr, HexByteValueStr: string;
  PageWriteData: FtcPageWriteData;
begin
  LocationAddress := 0;

  //passed := Write_Location(j,DevAddress,j);
  // set up device address
  ControlDevAddress := (DevAddress shl 1);
  ControlDevAddress := (ControlDevAddress or $90);
  ControlDevAddress := (ControlDevAddress and $FE);

  WriteControlBuffer[0] := ControlDevAddress;
  WriteControlBuffer[1] := $AC;  // Access config command
  WriteControlBuffer[2] := $02;  // sets output polarity active high, continuous conversion

  ftStatus := FT2232CI2C.Write(fthandle, @WriteControlBuffer, 3, True, 20, False,
                               NO_WRITE_TYPE, @WriteDataBuffer, 0,
                               True, 20, @PageWriteData);

  if (ftStatus = FTC_SUCCESS) then
  begin
    WriteControlBuffer[1] := $A1;  // Set max high temperature value command
    WriteControlBuffer[2] := $28;  // set +40C high temperature value
    WriteControlBuffer[3] := $00;  // set +40C high temperature value

    ftStatus := FT2232CI2C.Write(fthandle, @WriteControlBuffer, 4, True, 20, False,
                                 NO_WRITE_TYPE, @WriteDataBuffer, 0,
                                 True, 20, @PageWriteData);
  end;

  if (ftStatus = FTC_SUCCESS) then
  begin
    WriteControlBuffer[1] := $A2;  // Set min low temperature value command
    WriteControlBuffer[2] := $C9;  // set -55C high temperature value
    WriteControlBuffer[3] := $00;  // set -55C high temperature value

    ftStatus := FT2232CI2C.Write(fthandle, @WriteControlBuffer, 4, True, 20, False,
                                 NO_WRITE_TYPE, @WriteDataBuffer, 0,
                                 True, 20, @PageWriteData);
  end;
                                 
  if (ftStatus = FTC_SUCCESS) then
  begin
    WriteControlBuffer[1] := $EE;  // Start Temperature conversion

    ftStatus := FT2232CI2C.Write(fthandle, @WriteControlBuffer, 2, True, 20, True,
                                 NO_WRITE_TYPE, @WriteDataBuffer, 0,
                                 True, 20, @PageWriteData);

    if (ftStatus = FTC_SUCCESS) then
    begin
      DisplayLines.Add('Set max temperature = +40C, min temperature = -55C');
      DisplayLines.Add('Set DS1621 to read temperature continuously');
    end
    else
      DisplayFT2232CI2CDllError('Program_DS1621_Device', 'Write', ftStatus);
  end
  else
    DisplayFT2232CI2CDllError('Program_DS1621_Device', 'Write', ftStatus);

  Result := ftStatus;
end;

function  Program_M41T81_Device: FTC_STATUS;
var
  ftStatus: FTC_STATUS;
  ControlDevAddress: Byte;
  WriteControlBuffer: WriteControlByteBuffer;
  WriteDataBuffer: WriteDataByteBuffer;
  WriteDataBufferIndex: Dword;
  PageWriteData: FtcPageWriteData;
  TimeSeconds, TimeMinutes, TimeHours, DayOfWeek, DayOfMonth, MonthOfYear, YearOfCentury: Byte;
begin
  if DateTimeDialog.ShowModal = mrOK then
  begin
    ControlDevAddress := (M41T81DevAddress and $FE);

    WriteControlBuffer[0] := ControlDevAddress;

    WriteControlBuffer[1] := $00;  // Tenths/Hundredths of a Second Register Address

    //for WriteDataBufferIndex := 0 to 7 do
    //  WriteDataBuffer[WriteDataBufferIndex] := $00;  // set D7 of the Seconds Register(01h) to start the clock

    WriteDataBuffer[0] := $00; // Tenths/Hundredths of a Second Register
    TimeSeconds := DateTimeDialog.GetTimeSeconds;
    TimeSeconds := ((TimeSeconds div 10) SHL 4);
    TimeSeconds := TimeSeconds + (DateTimeDialog.GetTimeSeconds mod 10);
    WriteDataBuffer[1] := TimeSeconds;
    TimeMinutes := DateTimeDialog.GetTimeMinutes;
    TimeMinutes := ((TimeMinutes div 10) SHL 4);
    TimeMinutes := TimeMinutes + (DateTimeDialog.GetTimeMinutes mod 10);
    WriteDataBuffer[2] := TimeMinutes;
    TimeHours := DateTimeDialog.GetTimeHours;
    TimeHours := ((TimeHours div 10) SHL 4);
    TimeHours := TimeHours + (DateTimeDialog.GetTimeHours mod 10);
    WriteDataBuffer[3] := TimeHours;
    DayOfWeek := DateTimeDialog.GetDayOfWeek;
    WriteDataBuffer[4] := DayOfWeek;
    DayOfMonth := DateTimeDialog.GetDayOfMonth;
    DayOfMonth := ((DayOfMonth div 10) SHL 4);
    DayOfMonth := DayOfMonth + (DateTimeDialog.GetDayOfMonth mod 10);
    WriteDataBuffer[5] := DayOfMonth;
    MonthOfYear := DateTimeDialog.GetMonthOfYear;
    MonthOfYear := ((MonthOfYear div 10) SHL 4);
    MonthOfYear := MonthOfYear + (DateTimeDialog.GetMonthOfYear mod 10);
    WriteDataBuffer[6] := MonthOfYear;
    YearOfCentury := DateTimeDialog.GetYearOfCentury;
    WriteDataBuffer[7] := YearOfCentury;

    PageWriteData.dwNumPages := 1;
    PageWriteData.dwNumBytesPerPage := 8;

    ftStatus := FT2232CI2C.Write(fthandle, @WriteControlBuffer, 2, True, 20, True,
                                 PAGE_WRITE_TYPE, @WriteDataBuffer, 8,
                                 True, 20, @PageWriteData);

    if (ftStatus = FTC_SUCCESS) then
    begin
      WriteControlBuffer[1] := $0C;

      WriteDataBuffer[0] := $3F;

      ftStatus := FT2232CI2C.Write(fthandle, @WriteControlBuffer, 2, True, 20, True,
                                   BYTE_WRITE_TYPE, @WriteDataBuffer, 1,
                                   True, 20, @PageWriteData);
    end;

    if (ftStatus <> FTC_SUCCESS) then
      DisplayFT2232CI2CDllError('Program_M41T81_Device', 'Write', ftStatus);
  end;
  
  Result := ftStatus;
end;

function  Program_External_Device(DeviceName: String; DeviceIndex, ExternalDeviceIndex: Integer; ClockDivisor: Dword;
                                  DevAddress: Integer; bErase, bPageWrite: Boolean; ByteValue: String; var DisplayLines: TStringList): Boolean;
var
ftStatus: FTC_STATUS;
LocationID: Dword;
begin
  if (FT2232CI2CDll = True) then
  begin
    LocationID := LocationIDs[DeviceIndex];
    ftStatus := FT2232CI2C.OpenEx(DeviceName, LocationID, @ftHandle);

    if (ftStatus = FTC_SUCCESS) then
    begin
      ftStatus := FT2232CI2C.InitDevice(ftHandle, ClockDivisor);

      ftStatus := FT2232CI2C.SetMode(ftHandle, CommsMode);

      if (ftStatus = FTC_SUCCESS) then
      begin
        if (ftStatus = FTC_SUCCESS) then
        begin
          case ExternalDeviceIndex of
            I2C24C02ChipIndex: ftStatus := Program_24C02_Device(DevAddress, bErase, bPageWrite, ByteValue, DisplayLines);
            I2CM24C64ChipIndex: ftStatus := Program_M24C64_Device(DevAddress, bErase, bPageWrite, ByteValue, DisplayLines);
            I2CDS1621ChipIndex: ftStatus := Program_DS1621_Device(DevAddress, DisplayLines);
            I2CM41T81ChipIndex: ftStatus := Program_M41T81_Device;
          end;
        end
        else
          DisplayFT2232CI2CDllError('Program_Device', 'InitDevice', ftStatus);
      end
      else
        DisplayFT2232CI2CDllError('FastStandardModeCheckBoxClick', 'SetMode', ftStatus);

      if (ftHandle <> 0) then
      begin
        FT2232CI2C.Close(ftHandle);

        ftHandle := 0;
      end;
    end
    else
      DisplayFT2232CI2CDllError('Program_Device', 'OpenEx', ftStatus);
  end;

  if (ftStatus = FTC_SUCCESS) then
    Result := True
  else
    Result := False;
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -