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.
MCP23008.h
00001 /* MCP23008 library for Arduino 00002 Copyright (C) 2013 Stefan Jaensch, based on MCP23017 from 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_MCP23008_H 00020 #define MBED_MCP23008_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 0x01 00030 #define GPINTEN 0x02 00031 #define DEFVAL 0x03 00032 #define INTCON 0x04 00033 #define IOCON 0x05 00034 #define GPPU 0x06 00035 #define INTF 0x07 00036 #define INTCAP 0x08 00037 #define GPIO 0x09 00038 #define OLAT 0x0A 00039 00040 #define I2C_BASE_ADDRESS 0x40 00041 00042 #define DIR_OUTPUT 0 00043 #define DIR_INPUT 1 00044 00045 00046 #define MCP23008_GP0 ( 0x01 ) 00047 #define MCP23008_GP1 ( 0x02 ) 00048 #define MCP23008_GP2 ( 0x04 ) 00049 #define MCP23008_GP3 ( 0x08 ) 00050 #define MCP23008_GP4 ( 0x10 ) 00051 #define MCP23008_GP5 ( 0x20 ) 00052 #define MCP23008_GP6 ( 0x40 ) 00053 #define MCP23008_GP7 ( 0x80 ) 00054 00055 00056 /** MCP23008 class 00057 * 00058 * Allow access to an I2C connected MCP23008 8-bit I/O extender chip 00059 * Example: 00060 * @code 00061 * MCP23008 *par_port; 00062 ... 00063 par_port = new MCP23008( p9, p10, I2C_ADD); 00064 par_port->config(0xF0,0,0); 00065 * @endcode 00066 * 00067 */ 00068 class MCP23008 { 00069 public: 00070 /** Constructor for the MCP23008 connected to specified I2C pins at a specific address 00071 * 00072 * 8-bit I/O expander with I2C interface 00073 * 00074 * @param sda I2C data pin 00075 * @param scl I2C clock pin 00076 * @param i2cAddress I2C address 00077 */ 00078 MCP23008(PinName sda, PinName scl, int i2cAddress); 00079 00080 /** Reset MCP23008 device to its power-on state 00081 */ 00082 void reset(void); 00083 00084 /** Configure an MCP23008 device 00085 * 00086 * @param dir_config data direction value (1 = input, 0 = output) 00087 * @param pullup_config 100k pullup value (1 = enabled, 0 = disabled) 00088 * @param polarity_config polarity value (1 = flip, 0 = normal) 00089 */ 00090 void config(unsigned char dir_config, unsigned char pullup_config, unsigned char polarity_config); 00091 00092 /** Write a masked 16-bit value to the device 00093 * 00094 * @param data 8-bit data value 00095 * @param mask 8-bit mask value 00096 */ 00097 void write_mask(unsigned char data, unsigned char mask); 00098 00099 /** Read a 8-bit value from the device and apply mask 00100 * 00101 * @param mask 8-bit mask value 00102 * @return 8-bit data with mask applied 00103 */ 00104 int read_mask(unsigned char mask); 00105 00106 /** Write a 0/1 value to an output bit 00107 * 00108 * @param value 0 or 1 00109 * @param bit_number bit number range 0 --> 7 00110 */ 00111 void write_bit(int value, int bit_number); 00112 00113 /** Read a 0/1 value from an input bit 00114 * 00115 * @param bit_number bit number range 0 --> 7 00116 * @return 0/1 value read 00117 */ 00118 int read_bit(int bit_number); 00119 00120 00121 00122 00123 protected: 00124 void writeRegister(int regAddress, unsigned char data); 00125 unsigned char readRegister(int regAddress); 00126 00127 00128 I2C _i2c; 00129 int MCP23008_i2cAddress; // physical I2C address 00130 unsigned char shadow_GPIO, shadow_IODIR, shadow_GPPU, shadow_IPOL; // Cached copies of the register values 00131 }; 00132 00133 #endif
Generated on Sat Jul 16 2022 03:23:51 by
