Simple MCP23017 I2C 16bit IO expander IC

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MCP23017.h Source File

MCP23017.h

00001 /* MCP23017 Library for mbed
00002  * Copyright (c) 2014, Takuya Urakawa
00003  * 
00004  * This library is released under the MIT License
00005  * See http://opensource.org/licenses/mit-license.php
00006  */
00007 
00008 #ifndef _MCP23017_H_
00009 #define _MCP23017_H_
00010 
00011 #include "mbed.h"
00012 
00013 /** \def usefull defines */
00014 #define MCP23017_DEFAULT_ADDR 0x40
00015 #define MCP23017_PORTA 0x00
00016 #define MCP23017_PORTB 0x01
00017 
00018 namespace MCP23017 {    
00019     
00020 /** MCP23017 class
00021  *
00022  *  Allow access MCP23017 I2C 16bit io expander IC
00023  *  But this library can not controll interrupt functions
00024  *
00025  *  Example:
00026  *  @code
00027  *  #include "mbed.h"
00028  *  #include "MCP23017.h"
00029  *
00030  *  I2C i2c(dp5, dp27); // LPC1114
00031  *  MCP23017::MCP23017 io(&i2c,MCP23017_DEFAULT_ADDR);
00032  *
00033  *  int main(void)
00034  *  {   
00035  *    i2c.frequency(400000);
00036  *  
00037  *    // software reset
00038  *    io.init();
00039  *  
00040  *    io.setDirrection(MCP23017_PORTA, 0xFF);    // set all of the PORTA pins to input
00041  *    io.setDirrection(MCP23017_PORTB, 0x00);    // set all of the PORTB pins to output
00042  *    io.setPullUp(MCP23017_PORTA, 0xFF);        // activate all of the PORTA pin pull-ups
00043  *    io.setInputPolarity(MCP23017_PORTA, 0xFF); // invert all of the PORTA pins input polarity
00044  *
00045  *    while(true){
00046  *      // write PORTA to PORTB
00047  *      io.write(1,io.read(0));
00048  *    }
00049  *
00050  *    return 0;
00051  *  }
00052  *  @endcode
00053  */
00054 class MCP23017{
00055 public:
00056     
00057     /** Constructor
00058      *
00059      *  @param _i2c   pointer to I2C class
00060      *  @param _addr  i2c device address
00061      */
00062     MCP23017(I2C &_i2c, char _addr);
00063 
00064     /** Software Reset
00065      */
00066     void init(void);
00067 
00068     /** Configure MCP23017 
00069      *  Write IOCON register
00070      *  See MCP23017 datasheet p18
00071      *  http://ww1.microchip.com/downloads/jp/DeviceDoc/21952b.pdf
00072      *  
00073      *  @attention BANK bit must set 0
00074      *  
00075      *  @param _value  write byte  
00076      */
00077     void setConfig(char _value);
00078 
00079     /** Set I/O dirrection
00080      *  
00081      *  @param _port   port(MCP23017_PORTA or MCP23017_PORTB)
00082      *  @param _value  write byte(0=OUTPUT, 1=INPUT)
00083      */
00084     void setDirrection(char _port, char _value);
00085     
00086     /** Set pullup
00087      *  
00088      *  @param _port   port (MCP23017_PORTA or MCP23017_PORTB)
00089      *  @param _value  write byte (0=Disable, 1=Enable)
00090      */
00091     void setPullUp(char _port, char _value);
00092     
00093     /** Set Input Polarity
00094      *  
00095      *  @param _port   port(MCP23017_PORTA or MCP23017_PORTB)
00096      *  @param _value  write byte (0=normal, 1=inverted)
00097      */
00098     void setInputPolarity(char _port, char _value);
00099     
00100     /** Write GPIO
00101      *
00102      *  @param _port   port(MCP23017_PORTA or MCP23017_PORTB)
00103      *  @param _value  write byte 
00104      */
00105     void write(char _port, char _value);
00106     
00107     /** Read GPIO
00108      *
00109      *  @param _port   port(MCP23017_PORTA or MCP23017_PORTB)
00110      *  @return        byte
00111      */
00112     char read(char _port);
00113     
00114     
00115     // void setInterruptConfig(char _port, char _intcon, char _defval);
00116     // void interrupt(char _port, char _value);
00117     // char readIntcap(char _port);
00118 
00119 
00120 private:
00121     I2C  &mI2c;
00122     char mWriteAddr;
00123     char mReadAddr;
00124 
00125 
00126     inline void i2cSend(char _command, char _data1, char _data2){
00127         char commands[3];
00128         commands[0] = _command;
00129         commands[1] = _data1;
00130         commands[2] = _data2;
00131 
00132         mI2c.write(mWriteAddr, commands, 3);
00133     }
00134     
00135     inline void i2cSend(char _command, char _data1){
00136         char commands[2];
00137         commands[0] = _command;
00138         commands[1] = _data1;
00139         
00140         mI2c.write(mWriteAddr, commands, 2);
00141     }
00142     
00143     inline char i2cRead(char _command){
00144         char commands[2];
00145         commands[0] = _command;
00146         mI2c.write(mWriteAddr, commands, 1);
00147         //return (char)mI2c->read(0);
00148         mI2c.read(mReadAddr,commands,1);
00149         return commands[0];
00150     }
00151     
00152     
00153     // registors
00154     enum MCP23017_REG {
00155         // IO Dirrection
00156         REG_IODIR = 0x00,
00157 
00158         // Input polarity
00159         REG_IPOL = 0x02,
00160 
00161         // Interrupt-On-Change enable
00162         REG_GPINTEN = 0x04,
00163 
00164         // Default intterupt comparison value
00165         // Refer to INTCON
00166         REG_DEFVAL = 0x06,
00167 
00168         // Intterupt compare value
00169         // 1 = compare against DEFVAL value
00170         // 0 = compare against the previous value
00171         REG_INTCON = 0x08,
00172 
00173         // IC setting
00174         // bank/mirror/seqop/disslw/haen/odr/intpol/notimp
00175         // must be set bank = 0 for using this library
00176         // See MCP23017 datasheet
00177         REG_IOCON = 0x0A,
00178 
00179         // Pullup
00180         REG_GPPU = 0x0C,
00181 
00182         // Interrupt Flag
00183         // 1 = pin caused interrupt
00184         // 0 = no interrupt 
00185         // Read-Only
00186         REG_INTF = 0x0E,
00187 
00188         // Interrupt capture
00189         // value of GPIO at time of last interrupt
00190         // Read-Only
00191         REG_INTCAP = 0x10,
00192 
00193         // GPIO
00194         REG_GPIO = 0x12,
00195 
00196         // Output Latch
00197         REG_OLAT = 0x14
00198     };
00199     
00200 }; // end of class
00201 }; // end of namespace
00202 
00203 #endif
00204