// I2C_EepromAccess.cpp : Defines the entry point for the console application. // #include #include #include #include "stdafx.h" // I2CC - Include the SPIIC DLL extern "C" { #include "I2CC.h" } int _tmain(int argc, _TCHAR* argv[]) { int ch; // // Go to the work directory // printf("\nGoing to the work directory...\n"); #ifdef _DEBUG _chdir("..\\debug"); #else _chdir("..\\release"); #endif; // // Initialise the I2C library // printf("\nInitialising the I2C library... "); i2c_I2CC(); printf("done\n"); // // Initialise the I2C library // printf("\nAllocate and clear receive buffer... "); unsigned char pDataWr[2]; unsigned char pDataRd[2]; // // Set I2C speed to 100kHz // printf("\nSet I2C speed to 100kHz...\n"); i2c_SetSpeed(100); // // Executing I2C write transfer // pDataWr[0] = 0x12; pDataWr[1] = 0x34; printf("\nWriting 0x%02X to address 0x%02x...\n", pDataWr[1], pDataWr[0]); i2c_CmdBufAppend(2, 0x50, 1, 2, (char *)pDataWr, true, true); // Cmd = 2 : I2C write // Address = 0x50 // AddrType = 1 : 7-bit address // Length = 2 : 2 byte payload // pDataWr : buffer with 2 bytes payload (0x1234) // STA = true : generate start // STO = true : generate stop i2c_RunMaster(); i2c_CmdBufDeleteAll(); pDataWr[0] = 0x13; pDataWr[1] = 0x56; printf("Writing 0x%02X to address 0x%02x...\n", pDataWr[1], pDataWr[0]); i2c_CmdBufAppend(2, 0x50, 1, 2, (char *)pDataWr, true, true); // Cmd = 2 : I2C write // Address = 0x50 // AddrType = 1 : 7-bit address // Length = 2 : 2 byte payload // pDataWr : buffer with 2 bytes payload (0x1356) // STA = true : generate start // STO = true : generate stop i2c_RunMaster(); i2c_CmdBufDeleteAll(); // // Executing dummy write to set EEPROM address // printf("\nExecuting dummy write to set EEPROM address...\n"); pDataWr[0] = 0x12; i2c_CmdBufAppend(2, 0x50, 1, 1, (char *)pDataWr, true, true); // Cmd = 2 : I2C write // Address = 0x50 // AddrType = 1 : 7-bit address // Length = 1 : 1 byte payload // pDataWr : buffer with 1 byte payload (0x12) // STA = true : generate start // STO = true : generate stop i2c_RunMaster(); i2c_CmdBufDeleteAll(); // // Executing I2C read transfer // printf("\nReading EEPROM...\n"); pDataRd[0] = 0; pDataRd[1] = 0; i2c_CmdBufAppend(1, 0x50, 1, 1, (char *)&pDataRd[0], true, true); // Cmd = 1 : I2C read // Address = 0x50 // AddrType = 1 : 7-bit address // Length = 1 : 1 byte payload // pDataRd : buffer receiving 1 byte // STA = true : generate start // STO = true : generate stop i2c_CmdBufAppend(1, 0x50, 1, 1, (char *)&pDataRd[1], true, true); // Cmd = 1 : I2C read // Address = 0x50 // AddrType = 1 : 7-bit address // Length = 1 : 1 byte payload // pDataRd : buffer receiving 1 byte // STA = true : generate start // STO = true : generate stop i2c_RunMaster(); i2c_CmdBufDeleteAll(); printf("\tpDataRd[0] : 0x%02X\n", pDataRd[0]); printf("\tpDataRd[1] : 0x%02X\n", pDataRd[1]); // // Perform library cleanup // printf("\nUnitialising the I2C library... "); i2c_Terminate(); printf("done\n\n"); // // Wait for user input to close the window // printf("Press any key...", 1); ch = _getch(); return 0; }