aconno dev team / MCP23017

Dependents:   tof100 gaasgjdvas LED_GAME11 tof100

Fork of MCP23017 by jim herd

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MCP23017.h Source File

MCP23017.h

00001 /*  MCP23017 library for Arduino
00002     Copyright (C) 2009 David Pye    <davidmpye@gmail.com
00003     Modified for use on the MBED ARM platform
00004 
00005     This program is free software: you can redistribute it and/or modify
00006     it under the terms of the GNU General Public License as published by
00007     the Free Software Foundation, either version 3 of the License, or
00008     (at your option) any later version.
00009 
00010     This program is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013     GNU General Public License for more details.
00014 
00015     You should have received a copy of the GNU General Public License
00016     along with this program.  If not, see <http://www.gnu.org/licenses/>.
00017 */
00018 
00019 #ifndef     MBED_MCP23017_H
00020 #define     MBED_MCP23017_H
00021 
00022 #include    "mbed.h"
00023 
00024 //
00025 // Register defines from data sheet - we set IOCON.BANK to 0
00026 // as it is easier to manage the registers sequentially.
00027 //
00028 #define     IODIR       0x00
00029 #define     IPOL        0x02
00030 #define     GPINTEN     0x04
00031 #define     DEFVAL      0x06
00032 #define     INTCON      0x08
00033 #define     IOCON       0x0A
00034 #define     GPPU        0x0C
00035 #define     INTF        0x0E
00036 #define     INTCAP      0x10
00037 #define     GPIO        0x12
00038 #define     OLAT        0x14
00039 
00040 #define     I2C_BASE_ADDRESS    0x40
00041 
00042 #define     DIR_OUTPUT      0
00043 #define     DIR_INPUT       1
00044 
00045 /*
00046  *   Interrupt setters and getters
00047  */
00048 void setInt();
00049 void clearInt();
00050 bool getInt();
00051     
00052 /** MCP23017 class
00053  *
00054  * Allow access to an I2C connected MCP23017 16-bit I/O extender chip
00055  * Example:
00056  * @code
00057  *      MCP23017     *par_port; 
00058  * @endcode
00059  *
00060  */    
00061 class MCP23017 {
00062 public:
00063     /** Constructor for the MCP23017 connected to specified I2C pins at a specific address
00064      *
00065      * 16-bit I/O expander with I2C interface
00066      *
00067      * @param   sda         I2C data pin
00068      * @param   scl         I2C clock pin
00069      * @param   i2cAddress  I2C address
00070      */
00071     MCP23017(PinName sda, PinName scl, int i2cAddress);
00072 
00073     /** Reset MCP23017 device to its power-on state
00074      */    
00075     void reset(void);
00076 
00077     /** Write a 0/1 value to an output bit
00078      *
00079      * @param   value         0 or 1
00080      * @param   bit_number    bit number range 0 --> 15
00081      */   
00082     void write_bit(int value, int bit_number);
00083       
00084     /** Write a masked 16-bit value to the device
00085      *
00086      * @param   data    16-bit data value
00087      * @param   mask    16-bit mask value
00088      */       
00089     void write_mask(unsigned short data, unsigned short mask);
00090 
00091     /** Read a 0/1 value from an input bit
00092      *
00093      * @param   bit_number    bit number range 0 --> 15
00094      * @return                0/1 value read
00095      */       
00096     int  read_bit(int bit_number);
00097     
00098     /** Read a 16-bit value from the device and apply mask
00099      *
00100      * @param   mask    16-bit mask value
00101      * @return          16-bit data with mask applied
00102      */     
00103     int  read_mask(unsigned short mask);
00104 
00105     /** Configure an MCP23017 device
00106      *
00107      * @param   dir_config         data direction value (1 = input, 0 = output)
00108      * @param   pullup_config      100k pullup value (1 = enabled, 0 = disabled)
00109      * @param   polarity_config    polarity value (1 = flip, 0 = normal)
00110      */           
00111     void config(unsigned short dir_config, unsigned short pullup_config, unsigned short polarity_config);
00112 
00113     void writeRegister(int regAddress, unsigned char  val);
00114     void writeRegister(int regAddress, unsigned short val);
00115     int  readRegister(int regAddress);
00116 
00117 /*----------------------------------------------------------------------------- 
00118  * pinmode
00119  * Set units to sequential, bank0 mode
00120  */  
00121     void pinMode(int pin, int mode); 
00122     void digitalWrite(int pin, int val);
00123     int  digitalRead(int pin);
00124 
00125 // These provide a more advanced mapping of the chip functionality
00126 // See the data sheet for more information on what they do
00127 
00128 //Returns a word with the current pin states (ie contents of the GPIO register)
00129     unsigned short digitalWordRead();
00130 // Allows you to write a word to the GPIO register
00131     void digitalWordWrite(unsigned short w);
00132 // Sets up the polarity mask that the MCP23017 supports
00133 // if set to 1, it will flip the actual pin value.
00134     void inputPolarityMask(unsigned short mask);
00135 //Sets which pins are inputs or outputs (1 = input, 0 = output) NB Opposite to arduino's
00136 //definition for these
00137     void inputOutputMask(unsigned short mask);
00138 // Allows enabling of the internal 100k pullup resisters (1 = enabled, 0 = disabled)
00139     void internalPullupMask(unsigned short mask);
00140     int read(void);
00141     void write(int data);
00142 
00143 protected:
00144     I2C     _i2c;
00145     int     MCP23017_i2cAddress;                        // physical I2C address
00146     unsigned short   shadow_GPIO, shadow_IODIR, shadow_GPPU, shadow_IPOL;     // Cached copies of the register values
00147     
00148 };
00149 
00150 #endif