About Us Knowledge Base Community Forum Electronics Toolbox FAQ Shipping & Delivery

Community Forum

Share your projects and post your questions

Search
Register or Sign In
Forum

Exception when trying to initialize bus2 in iopiread.cpp demo code

251 Views - Created 14/04/2021

14/04/2021

Posted by:
eirik

Users Avatar

Hi,

demo app throws an exception at this line: IoPi bus2(0x21); Failed to write to i2c device for write.

Initializing bus1, works fine, and I can read/write pins.

Tried the command: sudo i2cdetect -y 1

Got this:

     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
20: 20 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- 68 -- -- -- -- -- -- -- 
70: -- -- -- -- -- -- -- --                         

Seems like address x21 is not found.

I use the Pi4 with two monitors, one being a touch panel using the built in flat connector. Not sure if the touch monitor occupies resources on the i2c bus (i am very much a newbie here).

No other devices are connected.

I have pulled the latest releases using: sudo apt-get install libi2c-dev

 

Thank you in advance for the support.

&eirik

 

14/04/2021

Posted by:
andrew

Users Avatar

Location:
United Kingdom

Users Twitter  Users Website  

Hi eirik

Are you using the Expander Pi C++ library or the IO Pi library?  The Expander Pi only has an IO chip on address 0x20 so the IO Pi library demos will not work as they are looking for a second chip on address 0x21.

You can download the Expander Pi library from https://github.com/abelectronicsuk/ABElectronics_CPP_Libraries/tree/master/ExpanderPi

AB Electronics UK Tech Support

14/04/2021

Posted by:
eirik

Users Avatar

Hi,

thank you for super quick response.

Had just realized that I used the wrong demo code example, and was about to close the case.

Everything works fine now.

Thank you again.

&eirik

14/04/2021

Posted by:
eirik

Users Avatar

Hi Again,

forgot to ask how to combine digital inputs and output?

There seems to be a mapping tied to "banks":

From iowrite. cpp

        expi.io_set_port_direction(0, 0x00); // set the direction for bank 0 to be outputs
        expi.io_set_port_direction(1, 0x00); // set the direction for bank 1 to be outputs
        expi.io_write_port(1, 0xFF);

From ioread.cpp

        expi.io_set_port_direction(0, 0xFF); // set bank 0 to be inputs
        expi.io_set_port_direction(1, 0xFF); // set bank 1 to be inputs
        expi.io_set_port_pullups(0, 0xFF);   // enable internal pullups for bank 0
        expi.io_invert_port(0, 0xFF);          // invert output so bank will read as 0       
        expi.io_set_port_pullups(1, 0xFF);   // enable internal pullups for bank 1
        expi.io_invert_port(1, 0xFF);          // invert output so bank will read as 1
 

How do I configure, for example, 4 outputs and 4 inputs?

 

&eirik

15/04/2021

Posted by:
andrew

Users Avatar

Location:
United Kingdom

Users Twitter  Users Website  

There are two functions for setting the direction of pins.

io_set_pin_direction can be used to set the direction of an individual pin.

io_set_pin_direction(unsigned char pin, unsigned char direction) takes two parameters.

pin - 1 to 16
direction - 1 = input, 0 = output

The IO bus is split into two ports, port 0 controls pins 1 to 8 and port 1 controls pins 9 to 16.  You can set the direction of a port using the io_set_port_direction function.

io_set_port_direction(unsigned char port, unsigned char direction) takes two parameters.

port - 0 = pins 1 to 8, port 1 = pins 9 to 16
direction - an 8 bit value of 0 to 255, or 0x00 to 0xFF in hex.  Each of the 8 bits represents a pin on the port with pin 1 being the lowest or right most bit.  So to set pins 1 to 4 as inputs and 5 to 8 as outputs you would use a value of 0b00001111 or 15 in decimal, 0x0F in hex.

AB Electronics UK Tech Support

23/04/2021

Posted by:
eirik

Users Avatar

Hi Andrew,

got it, Thank you again.

 

/eirik

Note: documents in Portable Document Format (PDF) require Adobe Acrobat Reader 5.0 or higher to view, download Adobe Acrobat Reader or other PDF reading software for your computer or mobile device.