Jurica Resetar / MCP23017

Dependents:   acd52832_beep_buzzer_ints

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 /** MCP23017 class
00046  *
00047  * Allow access to an I2C connected MCP23017 16-bit I/O extender chip
00048  * Example:
00049  * @code
00050  *      MCP23017     *par_port; 
00051  * @endcode
00052  *
00053  */
00054  
00055      /*
00056     *   Interrupt setters and getters
00057     */
00058     void setInt();
00059     void clearInt();
00060     bool getInt();
00061     
00062     
00063 class MCP23017 {
00064 public:
00065     /** Constructor for the MCP23017 connected to specified I2C pins at a specific address
00066      *
00067      * 16-bit I/O expander with I2C interface
00068      *
00069      * @param   sda         I2C data pin
00070      * @param   scl         I2C clock pin
00071      * @param   i2cAddress  I2C address
00072      */
00073     MCP23017(PinName sda, PinName scl, int i2cAddress);
00074 
00075     /** Reset MCP23017 device to its power-on state
00076      */    
00077     void reset(void);
00078 
00079     /** Write a 0/1 value to an output bit
00080      *
00081      * @param   value         0 or 1
00082      * @param   bit_number    bit number range 0 --> 15
00083      */   
00084     void write_bit(int value, int bit_number);
00085       
00086     /** Write a masked 16-bit value to the device
00087      *
00088      * @param   data    16-bit data value
00089      * @param   mask    16-bit mask value
00090      */       
00091     void write_mask(unsigned short data, unsigned short mask);
00092 
00093     /** Read a 0/1 value from an input bit
00094      *
00095      * @param   bit_number    bit number range 0 --> 15
00096      * @return                0/1 value read
00097      */       
00098     int  read_bit(int bit_number);
00099     
00100     /** Read a 16-bit value from the device and apply mask
00101      *
00102      * @param   mask    16-bit mask value
00103      * @return          16-bit data with mask applied
00104      */     
00105     int  read_mask(unsigned short mask);
00106 
00107     /** Configure an MCP23017 device
00108      *
00109      * @param   dir_config         data direction value (1 = input, 0 = output)
00110      * @param   pullup_config      100k pullup value (1 = enabled, 0 = disabled)
00111      * @param   polarity_config    polarity value (1 = flip, 0 = normal)
00112      */           
00113     void config(unsigned short dir_config, unsigned short pullup_config, unsigned short polarity_config);
00114 
00115     void writeRegister(int regAddress, unsigned char  val);
00116     void writeRegister(int regAddress, unsigned short val);
00117     int  readRegister(int regAddress);
00118     
00119 
00120 
00121 /*----------------------------------------------------------------------------- 
00122  * pinmode
00123  * Set units to sequential, bank0 mode
00124  */  
00125     void pinMode(int pin, int mode); 
00126     void digitalWrite(int pin, int val);
00127     int  digitalRead(int pin);
00128 
00129 // These provide a more advanced mapping of the chip functionality
00130 // See the data sheet for more information on what they do
00131 
00132 //Returns a word with the current pin states (ie contents of the GPIO register)
00133     unsigned short digitalWordRead();
00134 // Allows you to write a word to the GPIO register
00135     void digitalWordWrite(unsigned short w);
00136 // Sets up the polarity mask that the MCP23017 supports
00137 // if set to 1, it will flip the actual pin value.
00138     void inputPolarityMask(unsigned short mask);
00139 //Sets which pins are inputs or outputs (1 = input, 0 = output) NB Opposite to arduino's
00140 //definition for these
00141     void inputOutputMask(unsigned short mask);
00142 // Allows enabling of the internal 100k pullup resisters (1 = enabled, 0 = disabled)
00143     void internalPullupMask(unsigned short mask);
00144     int read(void);
00145     void write(int data);
00146 
00147 protected:
00148     I2C     _i2c;
00149     int     MCP23017_i2cAddress;                        // physical I2C address
00150     unsigned short   shadow_GPIO, shadow_IODIR, shadow_GPPU, shadow_IPOL;     // Cached copies of the register values
00151     
00152 };
00153 
00154 #endif