Need help with MCP23017 over I2C / STM32F407VG

General discussion on mikroC PRO for ARM.
Post Reply
Author
Message
p77
Posts: 10
Joined: 03 Nov 2016 16:33

Need help with MCP23017 over I2C / STM32F407VG

#1 Post by p77 » 18 Apr 2023 18:22

Hello guys,

I have been working a couple of days on this one and right now I feel really stuck.
I have 5 MCP23017s on my PCB where 2 of them are strictly used as outputs and 3 strictly as inputs.
I have to be honest and say that in no possible way am I going to use the port expander click library since there is no data, information, or help with the functions and couldn't even compile the example.

I am doing this the old-fashioned way, datasheet on the right, PCB and debugger on the left, and PC in the middle.
As for the "OUTPUT" ICs the debugging took just over a couple of hours and they are working like a charm.

The initialization here:

Code: Select all

    buffer[0] = MCP23017_IODIRA; // MCP23017 IODIRA register address
    buffer[1] = 0x00; // Set all pins of port A as outputs
    I2C2_Start(); // Start I2C communication
    I2C2_Write(address, buffer, 2, END_MODE_STOP); // Write to MCP23017 IODIRA and IODIRB registers
    buffer[0] = MCP23017_IODIRB; // MCP23017 IODIRB register address
    buffer[1] = 0x00; // Set all pins of port B as outputs
    I2C2_Start(); // Start I2C communication
    I2C2_Write(address, buffer, 2, END_MODE_STOP); // Write to MCP23017 IODIRA and IODIRB registers
And the SetOutputs here:

Code: Select all

    buffer[0] = port;
    buffer[1] = datax;
    I2C2_Start();
    I2C2_Write(address, buffer, 2, END_MODE_STOP);
Now for the Reading of INPUT pins, I am completely lost. From what I can see in the datasheet of the MCP23017 and "I2C" help section, looks like there is no possibility to make this work or I am really tired of debugging so my question is:

Does anyone have a sample code for initializing and reading port states of MCP23017 over I2C in MikroC pro for ARM?

The I2C library is not even close to what it was before on older architectures.
I would love to have a simple library with elementary functions such as "S" "SR" "W" "R" etc.

here is just one not-working version of read function for example

Code: Select all

 I2C2_Start(); // Start I2C communication
 I2C2_Write(0x20, 0x12, 1, END_MODE_RESTART);
 I2C2_Read(0x20, dataci, 2, END_MODE_STOP);
Where the register 0x12 has been tested through a range of 0x00 up to 0x20 returning nothing but zeros
Where 0x20 has been replaced with 0x41 to resemble the R/W bit being 1 for reading etc.

Thank you for your help. Best regards,

Nermin

UPDATE:

My Current read function is as follows:

Code: Select all

     I2C2_Start(); // Start I2C communication
     I2C2_Write(address, 0x12, 1, END_MODE_RESTART);
     I2C2_Read(address, bufferc, 2, END_MODE_STOP);
And what is actually being returned is simply the IODIRA/B register state. Basically, if I write 0xFF to IODIRA the read function will return 0xFF, If I write 0xCC to IODIRA the function will return 0xCC.
However, if I read from IODRA/B registers eg:

Code: Select all

     I2C2_Start(); // Start I2C communication
     I2C2_Write(address, 0x00, 1, END_MODE_RESTART);
     I2C2_Read(address, bufferc, 2, END_MODE_STOP);
The function always returns 0x00 disregarding what was written in registers.

Thanks.

Post Reply

Return to “mikroC PRO for ARM General”