Friday, October 4, 2013

connecting CC2540 (BLE112) to MCP23009 using I2C

CC2540 does not have hardware i2c interface. Implementing i2c protocol in software is quite easy task. A lot of samples could be found around the net.

Ive used i2c implementation found on: https://github.com/RedBearLab/Biscuit

Remember to change #defines to reflect your wiring. Also check .c file for hardcoded ports. You need to change this before running code.

My wiring of MCP23009 to BLE112:


wiring MCP23009 i2c extender to CC2540 (BLE112)
Next use the code below to turn on LED diode.

First you need to establish connection to MCP23009 (i2c address: 0x40) and set few registers:

    i2c_init();
    i2c_start(0x40);
      
    // io mode
    i2c_write(0x00);  // IODIR
    i2c_write(0x00);  // 00 - all pins as output
    i2c_stop();
    // polarity
    i2c_restart(0x40);
    i2c_write(0x01);
    i2c_write(0x00);
    i2c_stop();    
    // config
    i2c_restart(0x40);    
    i2c_write(0x05);
    i2c_write(0x00);
    i2c_stop();    
    // pullup
    i2c_restart(0x40);    
    i2c_write(0x06);
    i2c_write(0x00); // 00 - disable pullups
    i2c_stop();    
 
Registers can be found in datasheet, quick look to understand what registers was set in code:



Then you can turn on LED:

    i2c_restart(0x40);
    i2c_write(0x09);
    i2c_write(0xFE);
    i2c_stop();    

Useful links:

MCP23009 datasheet

No comments: