Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: Component_Test_Interface FalconWing MX_Spoile_Test Simple_Power_Distribution ... more
MCP23017.h
00001 /* MCP23017 - drive the Microchip MCP23017 16-bit Port Extender using I2C 00002 * Copyright (c) 2010 Wim Huiskamp, Romilly Cocking (original version for SPI) 00003 * 00004 * Released under the MIT License: http://mbed.org/license/mit 00005 * 00006 * version 0.2 Initial Release 00007 * version 0.3 Cleaned up 00008 * version 0.4 Fixed problem with _read method 00009 * version 0.5 Added support for 'Banked' access to registers 00010 */ 00011 #include "mbed.h" 00012 00013 #ifndef MCP23017_H 00014 #define MCP23017_H 00015 00016 // All register addresses assume IOCON.BANK = 0 (POR default) 00017 #define IODIRA 0x00 00018 #define IODIRB 0x01 00019 #define IPOLA 0x02 00020 #define IPOLB 0x03 00021 #define GPINTENA 0x04 00022 #define GPINTENB 0x05 00023 #define DEFVALA 0x06 00024 #define DEFVALB 0x07 00025 #define INTCONA 0x08 00026 #define INTCONB 0x09 00027 #define IOCONA 0x0A 00028 #define IOCONB 0x0B 00029 #define GPPUA 0x0C 00030 #define GPPUB 0x0D 00031 #define INTFA 0x0E 00032 #define INTFB 0x0F 00033 #define INTCAPA 0x10 00034 #define INTCAPB 0x11 00035 #define GPIOA 0x12 00036 #define GPIOB 0x13 00037 #define OLATA 0x14 00038 #define OLATB 0x15 00039 00040 // The following register addresses assume IOCON.BANK = 1 00041 #define IODIRA_BNK 0x00 00042 #define IPOLA_BNK 0x01 00043 #define GPINTENA_BNK 0x02 00044 #define DEFVALA_BNK 0x03 00045 #define INTCONA_BNK 0x04 00046 #define IOCONA_BNK 0x05 00047 #define GPPUA_BNK 0x06 00048 #define INTFA_BNK 0x07 00049 #define INTCAPA_BNK 0x08 00050 #define GPIOA_BNK 0x09 00051 #define OLATA_BNK 0x0A 00052 00053 #define IODIRB_BNK 0x10 00054 #define IPOLB_BNK 0x11 00055 #define GPINTENB_BNK 0x12 00056 #define DEFVALB_BNK 0x13 00057 #define INTCONB_BNK 0x14 00058 #define IOCONB_BNK 0x15 00059 #define GPPUB_BNK 0x16 00060 #define INTFB_BNK 0x17 00061 #define INTCAPB_BNK 0x18 00062 #define GPIOB_BNK 0x19 00063 #define OLATB_BNK 0x1A 00064 00065 // This array allows structured access to Port_A and Port_B registers for both bankModes 00066 const int IODIR_AB[2][2] = {{IODIRA, IODIRB}, {IODIRA_BNK, IODIRB_BNK}}; 00067 const int IPOL_AB[2][2] = {{IPOLA, IPOLB}, {IPOLA_BNK, IPOLB_BNK}}; 00068 const int GPINTEN_AB[2][2] = {{GPINTENA, GPINTENB}, {GPINTENA_BNK, GPINTENB_BNK}}; 00069 const int DEFVAL_AB[2][2] = {{DEFVALA, DEFVALB}, {DEFVALA_BNK, DEFVALB_BNK}}; 00070 const int INTCON_AB[2][2] = {{INTCONA, INTCONB}, {INTCONA_BNK, INTCONB_BNK}}; 00071 const int IOCON_AB[2][2] = {{IOCONA, IOCONB}, {IOCONA_BNK, IOCONB_BNK}}; 00072 const int GPPU_AB[2][2] = {{GPPUA, GPPUB}, {GPPUA_BNK, GPPUB_BNK}}; 00073 const int INTF_AB[2][2] = {{INTFA, INTFB}, {INTFA_BNK, INTFB_BNK}}; 00074 const int INTCAP_AB[2][2] = {{INTCAPA, INTCAPB}, {INTCAPA_BNK, INTCAPB_BNK}}; 00075 const int GPIO_AB[2][2] = {{GPIOA, GPIOB}, {GPIOA_BNK, GPIOB_BNK}}; 00076 const int OLAT_AB[2][2] = {{OLATA, OLATB}, {OLATA_BNK, OLATB_BNK}}; 00077 00078 00079 // Control settings 00080 #define IOCON_BANK 0x80 // Banked registers for Port A and B 00081 #define IOCON_BYTE_MODE 0x20 // Disables sequential operation, Address Ptr does not increment 00082 // If Disabled and Bank = 0, operations toggle between Port A and B registers 00083 // If Disabled and Bank = 1, operations do not increment registeraddress 00084 #define IOCON_HAEN 0x08 // Hardware address enable 00085 00086 #define INTERRUPT_POLARITY_BIT 0x02 00087 #define INTERRUPT_MIRROR_BIT 0x40 00088 00089 #define PORT_DIR_OUT 0x00 00090 #define PORT_DIR_IN 0xFF 00091 00092 enum Polarity { ACTIVE_LOW , ACTIVE_HIGH }; 00093 enum Port { PORT_A=0, PORT_B=1 }; 00094 enum Bank { NOT_BNK=0, BNK=1 }; 00095 00096 class MCP23017 { 00097 public: 00098 /** Create an MCP23017 object connected to the specified I2C object and using the specified deviceAddress 00099 * 00100 * @param I2C &i2c the I2C port to connect to 00101 * @param char deviceAddress the address of the MCP23017 00102 */ 00103 MCP23017(I2C &i2c, char deviceAddress); 00104 00105 /** Set I/O direction of specified MCP23017 Port 00106 * 00107 * @param Port Port address (Port_A or Port_B) 00108 * @param char direction pin direction (0 = output, 1 = input) 00109 */ 00110 void direction(Port port, char direction); 00111 00112 /** Set Pull-Up Resistors on specified MCP23017 Port 00113 * 00114 * @param Port Port address (Port_A or Port_B) 00115 * @param char offOrOn per pin (0 = off, 1 = on) 00116 */ 00117 void configurePullUps(Port port, char offOrOn); 00118 00119 void configureBanked(Bank bankmode); 00120 void interruptEnable(Port port, char interruptsEnabledMask); 00121 void interruptPolarity(Polarity polarity); 00122 void mirrorInterrupts(bool mirror); 00123 void defaultValue(Port port, char valuesToCompare); 00124 void interruptControl(Port port, char interruptControlBits); 00125 00126 /** Read from specified MCP23017 Port 00127 * 00128 * @param Port Port address (Port_A or Port_B) 00129 * @returns data from Port 00130 */ 00131 char read(Port port); 00132 00133 /** Write to specified MCP23017 Port 00134 * 00135 * @param Port Port address (Port_A or Port_B) 00136 * @param char byte data to write 00137 */ 00138 void write(Port port, char byte); 00139 00140 protected: 00141 I2C &_i2c; 00142 char _readOpcode; 00143 char _writeOpcode; 00144 Bank _bankMode; 00145 00146 /** Init MCP23017 00147 * 00148 * @param 00149 * @returns 00150 */ 00151 void _init(); 00152 00153 /** Write to specified MCP23017 register 00154 * 00155 * @param char address the internal registeraddress of the MCP23017 00156 */ 00157 void _write(char address, char byte); 00158 00159 /** Read from specified MCP23017 register 00160 * 00161 * @param char address the internal registeraddress of the MCP23017 00162 * @returns data from register 00163 */ 00164 char _read(char address); 00165 }; 00166 00167 #endif
Generated on Wed Jul 13 2022 15:03:48 by
1.7.2
MCP2317 I2C 16 bit I/O expander