Basic library of routines to interface to a Microchip MCP23017 16-bit I/O expander using an I2C interface.
Dependents: Assignment_2_herpe Final_V1 ass2 ass2 ... more
MCP23017.h@12:6d9d2b277f26, 2010-11-29 (annotated)
- Committer:
- jimherd
- Date:
- Mon Nov 29 12:41:09 2010 +0000
- Revision:
- 12:6d9d2b277f26
- Parent:
- 11:8726c7be6d72
- Child:
- 13:d57de266cf19
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jimherd | 0:a6a5d942b58f | 1 | /* MCP23017 library for Arduino |
jimherd | 0:a6a5d942b58f | 2 | Copyright (C) 2009 David Pye <davidmpye@gmail.com |
jimherd | 12:6d9d2b277f26 | 3 | Modified for use on the MBED ARM platform |
jimherd | 0:a6a5d942b58f | 4 | |
jimherd | 0:a6a5d942b58f | 5 | This program is free software: you can redistribute it and/or modify |
jimherd | 0:a6a5d942b58f | 6 | it under the terms of the GNU General Public License as published by |
jimherd | 0:a6a5d942b58f | 7 | the Free Software Foundation, either version 3 of the License, or |
jimherd | 0:a6a5d942b58f | 8 | (at your option) any later version. |
jimherd | 0:a6a5d942b58f | 9 | |
jimherd | 0:a6a5d942b58f | 10 | This program is distributed in the hope that it will be useful, |
jimherd | 0:a6a5d942b58f | 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
jimherd | 0:a6a5d942b58f | 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
jimherd | 0:a6a5d942b58f | 13 | GNU General Public License for more details. |
jimherd | 0:a6a5d942b58f | 14 | |
jimherd | 0:a6a5d942b58f | 15 | You should have received a copy of the GNU General Public License |
jimherd | 0:a6a5d942b58f | 16 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
jimherd | 0:a6a5d942b58f | 17 | */ |
jimherd | 0:a6a5d942b58f | 18 | |
jimherd | 0:a6a5d942b58f | 19 | #ifndef MBED_MCP23017_H |
jimherd | 0:a6a5d942b58f | 20 | #define MBED_MCP23017_H |
jimherd | 0:a6a5d942b58f | 21 | |
jimherd | 1:d54d7002bae2 | 22 | #include "mbed.h" |
jimherd | 1:d54d7002bae2 | 23 | |
jimherd | 0:a6a5d942b58f | 24 | // |
jimherd | 0:a6a5d942b58f | 25 | // Register defines from data sheet - we set IOCON.BANK to 0 |
jimherd | 0:a6a5d942b58f | 26 | // as it is easier to manage the registers sequentially. |
jimherd | 0:a6a5d942b58f | 27 | // |
jimherd | 0:a6a5d942b58f | 28 | #define IODIR 0x00 |
jimherd | 0:a6a5d942b58f | 29 | #define IPOL 0x02 |
jimherd | 0:a6a5d942b58f | 30 | #define GPINTEN 0x04 |
jimherd | 0:a6a5d942b58f | 31 | #define DEFVAL 0x06 |
jimherd | 0:a6a5d942b58f | 32 | #define INTCON 0x08 |
jimherd | 0:a6a5d942b58f | 33 | #define IOCON 0x0A |
jimherd | 0:a6a5d942b58f | 34 | #define GPPU 0x0C |
jimherd | 0:a6a5d942b58f | 35 | #define INTF 0x0E |
jimherd | 0:a6a5d942b58f | 36 | #define INTCAP 0x10 |
jimherd | 0:a6a5d942b58f | 37 | #define GPIO 0x12 |
jimherd | 0:a6a5d942b58f | 38 | #define OLAT 0x14 |
jimherd | 0:a6a5d942b58f | 39 | |
jimherd | 0:a6a5d942b58f | 40 | #define I2C_BASE_ADDRESS 0x40 |
jimherd | 0:a6a5d942b58f | 41 | |
jimherd | 0:a6a5d942b58f | 42 | #define DIR_OUTPUT 0 |
jimherd | 0:a6a5d942b58f | 43 | #define DIR_INPUT 1 |
jimherd | 0:a6a5d942b58f | 44 | |
jimherd | 11:8726c7be6d72 | 45 | /** MCP23017 class |
jimherd | 11:8726c7be6d72 | 46 | * |
jimherd | 11:8726c7be6d72 | 47 | */ |
jimherd | 0:a6a5d942b58f | 48 | class MCP23017 { |
jimherd | 0:a6a5d942b58f | 49 | public: |
jimherd | 0:a6a5d942b58f | 50 | /** Constructor for the MCP23017 connected to specified I2C pins at a specific address |
jimherd | 0:a6a5d942b58f | 51 | * |
jimherd | 0:a6a5d942b58f | 52 | * 16-bit I/O expander with I2C interface |
jimherd | 0:a6a5d942b58f | 53 | * |
jimherd | 0:a6a5d942b58f | 54 | * @param sda I2C data pin |
jimherd | 0:a6a5d942b58f | 55 | * @param scl I2C clock pin |
jimherd | 0:a6a5d942b58f | 56 | * @param i2cAddress I2C address |
jimherd | 0:a6a5d942b58f | 57 | */ |
jimherd | 0:a6a5d942b58f | 58 | MCP23017(PinName sda, PinName scl, int i2cAddress); |
jimherd | 0:a6a5d942b58f | 59 | |
jimherd | 0:a6a5d942b58f | 60 | /** Reset MCP23017 device to its power-on state |
jimherd | 0:a6a5d942b58f | 61 | */ |
jimherd | 0:a6a5d942b58f | 62 | void reset(void); |
jimherd | 0:a6a5d942b58f | 63 | |
jimherd | 0:a6a5d942b58f | 64 | /** Write a 0/1 value to an output bit |
jimherd | 0:a6a5d942b58f | 65 | * |
jimherd | 0:a6a5d942b58f | 66 | * @param value 0 or 1 |
jimherd | 0:a6a5d942b58f | 67 | * @param bit_number bit number range 0 --> 15 |
jimherd | 0:a6a5d942b58f | 68 | */ |
jimherd | 0:a6a5d942b58f | 69 | void write_bit(int value, int bit_number); |
jimherd | 0:a6a5d942b58f | 70 | |
jimherd | 10:a3b9eabb0e50 | 71 | /** Write a masked 16-bit value to the device |
jimherd | 0:a6a5d942b58f | 72 | * |
jimherd | 0:a6a5d942b58f | 73 | * @param data 16-bit data value |
jimherd | 0:a6a5d942b58f | 74 | * @param mask 16-bit mask value |
jimherd | 0:a6a5d942b58f | 75 | */ |
jimherd | 0:a6a5d942b58f | 76 | void write_mask(unsigned short data, unsigned short mask); |
jimherd | 0:a6a5d942b58f | 77 | |
jimherd | 0:a6a5d942b58f | 78 | /** Read a 0/1 value from an input bit |
jimherd | 0:a6a5d942b58f | 79 | * |
jimherd | 0:a6a5d942b58f | 80 | * @param bit_number bit number range 0 --> 15 |
jimherd | 0:a6a5d942b58f | 81 | * @return 0/1 value read |
jimherd | 0:a6a5d942b58f | 82 | */ |
jimherd | 0:a6a5d942b58f | 83 | int read_bit(int bit_number); |
jimherd | 0:a6a5d942b58f | 84 | |
jimherd | 10:a3b9eabb0e50 | 85 | /** Read a 16-bit value from the device and apply mask |
jimherd | 0:a6a5d942b58f | 86 | * |
jimherd | 0:a6a5d942b58f | 87 | * @param mask 16-bit mask value |
jimherd | 0:a6a5d942b58f | 88 | * @return 16-bit data with mask applied |
jimherd | 0:a6a5d942b58f | 89 | */ |
jimherd | 0:a6a5d942b58f | 90 | int read_mask(unsigned short mask); |
jimherd | 0:a6a5d942b58f | 91 | |
jimherd | 0:a6a5d942b58f | 92 | /** Configure an MCP23017 device |
jimherd | 0:a6a5d942b58f | 93 | * |
jimherd | 0:a6a5d942b58f | 94 | * @param dir_config data direction value (1 = input, 0 = output) |
jimherd | 0:a6a5d942b58f | 95 | * @param pullup_config 100k pullup value (1 = enabled, 0 = disabled) |
jimherd | 0:a6a5d942b58f | 96 | * @param polarity_config polarity value (1 = flip, 0 = normal) |
jimherd | 0:a6a5d942b58f | 97 | */ |
jimherd | 0:a6a5d942b58f | 98 | void config(unsigned short dir_config, unsigned short pullup_config, unsigned short polarity_config); |
jimherd | 0:a6a5d942b58f | 99 | |
jimherd | 12:6d9d2b277f26 | 100 | void writeRegister(int regAddress, unsigned char val); |
jimherd | 12:6d9d2b277f26 | 101 | void writeRegister(int regAddress, unsigned short val); |
jimherd | 12:6d9d2b277f26 | 102 | int readRegister(int regAddress); |
jimherd | 0:a6a5d942b58f | 103 | |
jimherd | 12:6d9d2b277f26 | 104 | /*----------------------------------------------------------------------------- |
jimherd | 0:a6a5d942b58f | 105 | * pinmode |
jimherd | 0:a6a5d942b58f | 106 | * Set units to sequential, bank0 mode |
jimherd | 0:a6a5d942b58f | 107 | */ |
jimherd | 7:c282ea90cdb5 | 108 | void pinMode(int pin, int mode); |
jimherd | 0:a6a5d942b58f | 109 | void digitalWrite(int pin, int val); |
jimherd | 0:a6a5d942b58f | 110 | int digitalRead(int pin); |
jimherd | 0:a6a5d942b58f | 111 | |
jimherd | 0:a6a5d942b58f | 112 | // These provide a more advanced mapping of the chip functionality |
jimherd | 0:a6a5d942b58f | 113 | // See the data sheet for more information on what they do |
jimherd | 0:a6a5d942b58f | 114 | |
jimherd | 0:a6a5d942b58f | 115 | //Returns a word with the current pin states (ie contents of the GPIO register) |
jimherd | 0:a6a5d942b58f | 116 | unsigned short digitalWordRead(); |
jimherd | 0:a6a5d942b58f | 117 | // Allows you to write a word to the GPIO register |
jimherd | 0:a6a5d942b58f | 118 | void digitalWordWrite(unsigned short w); |
jimherd | 0:a6a5d942b58f | 119 | // Sets up the polarity mask that the MCP23017 supports |
jimherd | 0:a6a5d942b58f | 120 | // if set to 1, it will flip the actual pin value. |
jimherd | 0:a6a5d942b58f | 121 | void inputPolarityMask(unsigned short mask); |
jimherd | 0:a6a5d942b58f | 122 | //Sets which pins are inputs or outputs (1 = input, 0 = output) NB Opposite to arduino's |
jimherd | 0:a6a5d942b58f | 123 | //definition for these |
jimherd | 0:a6a5d942b58f | 124 | void inputOutputMask(unsigned short mask); |
jimherd | 0:a6a5d942b58f | 125 | // Allows enabling of the internal 100k pullup resisters (1 = enabled, 0 = disabled) |
jimherd | 0:a6a5d942b58f | 126 | void internalPullupMask(unsigned short mask); |
jimherd | 0:a6a5d942b58f | 127 | int read(void); |
jimherd | 0:a6a5d942b58f | 128 | void write(int data); |
jimherd | 0:a6a5d942b58f | 129 | |
jimherd | 0:a6a5d942b58f | 130 | protected: |
jimherd | 0:a6a5d942b58f | 131 | I2C _i2c; |
jimherd | 0:a6a5d942b58f | 132 | int MCP23017_i2cAddress; // physical I2C address |
jimherd | 0:a6a5d942b58f | 133 | unsigned short shadow_GPIO, shadow_IODIR, shadow_GPPU, shadow_IPOL; // Cached copies of the register values |
jimherd | 0:a6a5d942b58f | 134 | |
jimherd | 0:a6a5d942b58f | 135 | }; |
jimherd | 0:a6a5d942b58f | 136 | |
jimherd | 0:a6a5d942b58f | 137 | #endif |