I’m trying to write into a register of an I/O expander (MCP23016) but that doesn’t work. (nicely described here) With the old library and on an older WinCE it worked once.
Reading inputs works without problems (all GPIOs are inputs by default). So there is no hardware issue. But for reading I don’t need to set any regsiter address.
I can’t read what I wrote, but I don’t get any error message.
Attached the full VS 2013 project. Here the main part:
[OpenI2C();
tVersion ver;
I2c_GetVersion(&ver); //< Get I2C library vaersion
printf("I2CDemo (I2CLib Version %d.%d.%d)\n", ver.Major, ver.Minor, ver.Build);
uchar gpioDir[1] = { 0x00 };
//set 7bit address of MC23016
if (!I2c_SetConfigInt(i2c, L"SlaveAddr", 0x23, StoreVolatile))
printf("Error: I2c_SetConfigInt Slave address\n");
//set register address size to 8 bit
I2c_SetConfigInt(i2c, L"RegisterAddrSize", 8, StoreVolatile);
//set register address for IODIR0
I2c_SetConfigInt(i2c, L"RegisterAddr", 0x06, StoreVolatile);
//set all GPIOs as outputs
if (I2c_Write(i2c, gpioDir, 1) != 1)
printf("Error: write data\n");
//read IODIR0 back to check if correctly set
BYTE data[8] = { 0 };
DWORD returnValue = 0;
I2c_SetConfigInt(i2c, L"RegisterAddrSize", 8, StoreVolatile);
I2c_SetConfigInt(i2c, L"RegisterAddr", 0x06, StoreVolatile);
returnValue = I2c_Read(i2c, (DWORD*)data, 8);
printf("value: %#8x\n", returnValue);
//set register address for IODIR1
I2c_SetConfigInt(i2c, L"RegisterAddr", 0x07, StoreVolatile);
//set all GPIOs as outputs
if (I2c_Write(i2c, gpioDir, 1) != 1)
printf("Error: write data\n");
//set register address and size to 0
I2c_SetConfigInt(i2c, L"RegisterAddrSize", 0, StoreVolatile);
I2c_SetConfigInt(i2c, L"RegisterAddr", 0, StoreVolatile);
//set all outputs on
uchar i2cDataWrite[2] = { 0xFF, 0xFF };
if(I2c_Write(i2c, i2cDataWrite, 2) != 2)
printf("Error: write data\n");
I2c_Close(i2c);
I2c_Deinit(i2c);
i2c = NULL;][2]
.