// Usare il PIC16 come periferica I2C - Versione 0.1 - Gennaio 2010 // Copyright © 2009, VincenzoV.net (https://www.vincenzov.net) // Creative Commons | Attribuzione-Condividi allo stesso modo 3.0 Unported. // Creative Commons | Attribution-Share Alike 3.0 Unported // https://www.vincenzov.net/tutorial/elettronica-di-base/PIC16F690 // This is the PC code for FT2232h: the master device (VisualCpp) #include "stdafx.h" #include #include "..\FTCI2C.h" #define CLOCK_DIVISOR 79 // Set I2C bus clock frequency. Equivalent to 375KHz (if divisor set to 5) #define I2C_DATA 0x05 // Data to write to PIC (0-15, 0x0-0x0F) #define I2C_ADDR 0x51 // PIC addr (0-127, 0-0x7F) int _tmain(int argc, _TCHAR* argv[]) {DWORD DevicesFound; // Number of Ftxxx found FTC_STATUS ErrorCode; // Error code (FTC_SUCCESS mean no errors) char DeviceNameBuffer[100]; // Device name string from I2C_GetHiSpeedDeviceNameLocIDChannel() DWORD LocationID; // Where is located FTxxx (undused from user) char Channel[5]; // A or B (undused) DWORD HiSpeedDeviceType; // FT2232H or FT4232H(undused) FTC_HANDLE IicDeviceHandle; // Handle og opened device DWORD clock; // I2C bus clock (375 kHz, about 400 kHz max frequency) WriteControlByteBuffer WriteControlBuffer; // Command to send to I2C devices WriteDataByteBuffer WriteDataBuffer; // Data to send to I2C devices FTC_PAGE_WRITE_DATA PageWriteData; // Unused! ErrorCode = I2C_GetNumHiSpeedDevices(&DevicesFound); // Get the number of available FTx232H // NB: FT2232H appares as 2 devices... if (ErrorCode != FTC_SUCCESS) // Any error? {printf ("Error! %d", ErrorCode ); return -1;} else printf ("Found %d devices\n", DevicesFound ); // Get some identifiers of device 0. Change first parameter accordig your system ErrorCode = I2C_GetHiSpeedDeviceNameLocIDChannel(0, DeviceNameBuffer, 100, &LocationID, Channel, 5, &HiSpeedDeviceType); if (ErrorCode != FTC_SUCCESS) {printf ("Error! %d", ErrorCode ); return -2;} else printf ("Using device 0: %s\n", DeviceNameBuffer); // Open device and inizialize IicDeviceHandle as device handle ErrorCode = I2C_OpenHiSpeedDevice(DeviceNameBuffer, LocationID, Channel, &IicDeviceHandle); if (ErrorCode != FTC_SUCCESS) {printf ("Error! %d", ErrorCode ); return -3;} else printf ("Device opened\n"); // Set FT2232H software compatible with legacy FTxxx - Non required but change CLOCK_DIVISOR meaning I2C_TurnOnDivideByFiveClockingHiSpeedDevice(IicDeviceHandle); if (ErrorCode != FTC_SUCCESS) {printf ("Error! %d", ErrorCode ); return -8;} else printf ("Set clock as FT2232D...\n"); // Inizialize device as I2C master ErrorCode = I2C_InitDevice(IicDeviceHandle, CLOCK_DIVISOR); if (ErrorCode != FTC_SUCCESS) {printf ("Erroe! %d", ErrorCode ); return -5;} else {I2C_GetHiSpeedDeviceClock(CLOCK_DIVISOR, &clock); // Verify clock - Non required printf ("Device initialized; clock = %d Hz\n", clock);} // SetUp address // I2C_ADDR (7 bit address) 0 A6 A5 A4 A3 A2 A1 A0 // I2C_ADDR << 1 A6 A5 A4 A3 A2 A1 A0 0 // (I2C_ADDR << 1) & 0xFE A6 A5 A4 A3 A2 A1 A0 0 - Not required... WriteControlBuffer[0] = (I2C_ADDR << 1) & 0xFE; if (argc == 1) WriteDataBuffer[0] = 0xAA; // Default data to write to PIC (single byte) else WriteDataBuffer[0] = _ttoi(argv[1]); // Data to write to PIC (single byte) // Write DATA e COMMAND to I2C devices ErrorCode = I2C_Write(IicDeviceHandle, &WriteControlBuffer, 1, true, 20, true, BYTE_WRITE_TYPE, &WriteDataBuffer, 1, true, 20, &PageWriteData); if (ErrorCode != FTC_SUCCESS) {printf ("Erroe! %d", ErrorCode ); return -6;} else printf ("Write data 0x%x to PIC (address = 0x%x)\n",WriteDataBuffer[0], I2C_ADDR); // Read back DATA WriteControlBuffer[0] = (I2C_ADDR << 1) & 0xFE; ErrorCode = I2C_Read(IicDeviceHandle, &WriteControlBuffer, 1, true, 20, BLOCK_READ_TYPE, &WriteDataBuffer, 2); if (ErrorCode != FTC_SUCCESS) {printf ("Erroe! %d", ErrorCode ); return -6;} else printf ("Read back two byte from PIC: %d and %d \n", WriteDataBuffer[0], WriteDataBuffer[1]); // Close device if (ErrorCode != FTC_SUCCESS) {printf ("Error! %d", ErrorCode );return -4;} else printf ("Device closed\n"); return 0; }