0

I'm working to develop a simple code for MCP23017 that I want to use to run LCD128x64.

I was succeeded to set IODIRB as output and blink LED as a test.

Now I want to read the pin state, but I couldn't. Here's my code. I'm using the nice twi library that comes with Wire library.

uint8_t mcp23017_read(uint8_t add_reg){
    uint8_t data;
    twi_writeTo(MCP23017_ADD,add_reg,2,1,0);    
    twi_readFrom(MCP23017_ADD,data,1,1);

    return data;
}

I've tried this version too:

uint8_t *mcp23017_read(uint8_t add_reg){
    uint8_t data[1];
    twi_writeTo(MCP23017_ADD,add_reg,2,1,0);    
    twi_readFrom(MCP23017_ADD,data,1,1);

    return *data;
}

==================================================

Here's Arduino call function:

void loop() {
  // put your main code here, to run repeatedly:
  data = mcp23017_read(GPIOB);
  Serial.println(data);
  delay(100);
}
5
  • how do you know that you cannot read the pin state?
    – jsotola
    Nov 10, 2019 at 19:10
  • I'm connecting the pin with a resistor to 5V or GND.
    – R1S8K
    Nov 10, 2019 at 19:17
  • 1
    here is an example about reading inputs tronixstuff.com/2011/08/26/arduino-mcp23017-tutorial
    – jsotola
    Nov 10, 2019 at 19:31
  • 1
    Is there a reason why you use TWI but not Wire?
    – chrisl
    Nov 10, 2019 at 20:40
  • Yes, I can same like 300 bytes of the flash memory. And I want to learn how to study other people's code. The twi is a very good example. I learned how ISR work and how functions should handle buffered TX/Rx data.
    – R1S8K
    Nov 10, 2019 at 21:04

2 Answers 2

2

I don't know how this lib works. But I'm pretty sure that you have to pass a pointer onto the data variable into the twi_readFrom method.

uint8_t mcp23017_read( uint8_t add_reg )
{
    uint8_t data;
    twi_writeTo( MCP23017_ADD, add_reg, 2, 1, 0 );    
    // I inserted an & below
    twi_readFrom( MCP23017_ADD, &data, 1, 1 );

    return data;
}

Or in case of the second function:

// I removed the star operator
uint8_t mcp23017_read(uint8_t add_reg)
{
    uint8_t data[1];

    twi_writeTo( MCP23017_ADD, add_reg, 2, 1, 0 );    
    // you pass the pointer to the array element 0
    twi_readFrom( MCP23017_ADD, data, 1, 1 );

    // I returned the element 0
    return data[0];
}

Update:

Just for completeness: The OP found also a failure on the write function. He had posted a separate answer, I voted up to mark it. Because this is the accepted answer, I also copied his post to here for completeness and the readers convenience.

Original post by R1S8K:

OK, I found a the answer and it was a good lesson in dealing with functions that receives pointers.

I forgot that the writeTo function also receives a pointer and I have to pass an array for both read/write functions.

void mcp23017_read(uint8_t *add_reg, uint8_t *buffer){
    twi_writeTo(MCP23017_ADD, add_reg, 1, 1, 1);    
    twi_readFrom(MCP23017_ADD, buffer, 1, 1);
}
2
  • It isn't working ! I don't know what I'm doing wrong. Should I launch a repstart or not ... etc.
    – R1S8K
    Nov 10, 2019 at 22:49
  • Yep, that's very nice :) Thanks for improving my thread.
    – R1S8K
    Nov 11, 2019 at 14:56
1

OK, I found one little mistake in my coding.

I forgot that the writeTo function also receives a pointer and I have to pass an array for both read/write functions.

void mcp23017_read(uint8_t *add_reg, uint8_t *buffer){
    twi_writeTo(MCP23017_ADD, add_reg, 1, 1, 1);    
    twi_readFrom(MCP23017_ADD, buffer, 1, 1);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.