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

bus2 addressing with C code

1290 Views - Created 07/03/2017

07/03/2017

Posted by:
rs232

Last edited: 07/03/2017

Users Avatar

Hello,
I'm trying to programming the IO PI PLUS with C language.
My hello world program is a blinking led! I have jumpered correctly the IO PI PLUS board:

0x20 bus1
0x21 bus2

If I try to use 0x20 address all works correctly, but when I change it to 0x21 I have no the same result! :(

This is my C code:


#include <ABE_IoPi.h>

int main(int argc, char **argv){
    IOPi_init(0x21); // initialise one of the io pi buses on i2c address 0x21
    set_port_direction(0x21,0, 0x00); // set the direction for bank 0 to be outputs
    set_port_direction(0x21,1, 0x00); // set the direction for bank 1 to be outputs
        
    write_port(0x21,1, 0xFF);
        
    while (1){
        write_pin(0x21,0x1, 1); // turn pin 1 on and off at 1 second intervals
        usleep(1000000);
        write_pin(0x21,0x1, 0);
        usleep(1000000);
    }

    return (0);
}

I tried to use a python equivalent code and with this one, the 0x21 bus address works correctly.
My led is blinking!

#!/usr/bin/python
from ABE_helpers import ABEHelpers
from ABE_IoPi import IoPi
import time

i2c_helper = ABEHelpers()
i2c_bus = i2c_helper.get_smbus()

bus = IoPi(i2c_bus, 0x21)

bus.set_port_direction(0, 0x00)
bus.write_port(0, 0x00)

while True:
    bus.write_pin(1, 1)
    time.sleep(1)
    bus.write_pin(1, 0)
    time.sleep(1)


Could you please let me know what's is wrong in my C code?

thanks
rs232.


 

 

07/03/2017

Posted by:
andrew

Users Avatar

Location:
United Kingdom

Users Twitter  Users Website  

Hello

I just tried your code on an IO Pi Plus and it appears to be working correctly.  Pin 1 on bus 2 was switching on and off.

Can you please check that you are using the most recent version of the IO Pi C library from our github repository.

AB Electronics UK Tech Support

07/03/2017

Posted by:
rs232

Users Avatar

Hello Andrew,
thanks for your reply.

I think to have cloned last github version. But I'll check.
Do you know some difference?

rs232

07/03/2017

Posted by:
andrew

Users Avatar

Location:
United Kingdom

Users Twitter  Users Website  

There was a bug with an earlier version of the library where it would not select the correct chip when writing to the IO Pi.

AB Electronics UK Tech Support

09/03/2017

Posted by:
rs232

Users Avatar

Hello Andrew,
thanks for your reply.

You were right, with the last github version it works!

Many thanks

rs232

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.