You are reading a single comment by @allObjects and its replies. Click here to read the full conversation.
  • *** DRAFT ...in progress ***

    Configuring the MCP23017 I2C Expander

    The nice part about the Expander is that it initializes as all inputs. Therefore, reading the buttons should work right of the bat.

    Connect with Espruino standard as follows (disconnect Espruino first from power/USB, and reconnect after making the connections):

    | Espruino --------- 'shield'
    |
    | GND ------------ GND1
    | VBat ------------ +5V
    | B6 (I2C1 SCL) ----> SCL
    | B7 (I2C1 SDA) <---> SDA

    Copy this code into the edit pane of Espruino' Web IDE and upload it.

    I2C1.setup(I2C1.setup({scl:B6, sda:B7,bitrate:400000}); // 400kHz to start with
    var buttonsBefore = new UInt8Arr(1); buttonsBefore.fill(0x00), buttonsNow;
    setInterval(function(){
      I2C1.write(0,0x13); // command to read GPA0..7
      buttonsNow = I2C1.readFrom(0,1);
      buttonsNow[0] = (buttonsNow[0] | 0xE0) ^ 0xFF; // mask and invert
      if (buttonsNow[0] !== buttonsBefore[0]) {
        console.log(buttonsNow[0]); // shows state of buttons 0..4 binary coded
        buttonsBefore[0] = buttonsNow[0];
      }
    },500); 
    

    The expander has a nice feature: the input can be reversed so only masking is required. Activating this feature is part of the initialization. At the same time we will configure some of the pins as output in order to drive first the back light LED and later the LCD module.

About

Avatar for allObjects @allObjects started