A library for the Microchip MCP23008 I2C 8-bit I/O expander. Uses an external I2C object in case you are sharing the bus with other devices.

Fork of MCP23008 by Daniel Wyatt

Committer:
antonio_actus
Date:
Wed Aug 24 18:09:39 2016 +0000
Revision:
2:d9beb7a97ff3
Parent:
1:3bda347154e6
Renamed params info

Who changed what in which revision?

UserRevisionLine numberNew contents of line
antonio_actus 1:3bda347154e6 1 #include "mbed.h"
antonio_actus 1:3bda347154e6 2
antonio_actus 1:3bda347154e6 3 /** MCP23008 class
antonio_actus 1:3bda347154e6 4 *
antonio_actus 1:3bda347154e6 5 * Allow access to an I2C connected MCP23008 8-bit I/O extender chip
antonio_actus 1:3bda347154e6 6 *
antonio_actus 1:3bda347154e6 7 */
antonio_actus 1:3bda347154e6 8 class MCP23008_I2C {
antonio_actus 1:3bda347154e6 9 public:
antonio_actus 1:3bda347154e6 10 enum Frequency {
antonio_actus 1:3bda347154e6 11 Frequency_100KHz = 100000,
antonio_actus 1:3bda347154e6 12 Frequency_400KHz = 400000,
antonio_actus 1:3bda347154e6 13 /* Note: 1.7MHz probably won't work for mbed */
antonio_actus 1:3bda347154e6 14 Frequency_1700KHz = 1700000
antonio_actus 1:3bda347154e6 15 };
antonio_actus 1:3bda347154e6 16 enum Pin {
antonio_actus 1:3bda347154e6 17 Pin_GP0 = 0x01,
antonio_actus 1:3bda347154e6 18 Pin_GP1 = 0x02,
antonio_actus 1:3bda347154e6 19 Pin_GP2 = 0x04,
antonio_actus 1:3bda347154e6 20 Pin_GP3 = 0x08,
antonio_actus 1:3bda347154e6 21 Pin_GP4 = 0x10,
antonio_actus 1:3bda347154e6 22 Pin_GP5 = 0x20,
antonio_actus 1:3bda347154e6 23 Pin_GP6 = 0x40,
antonio_actus 1:3bda347154e6 24 Pin_GP7 = 0x80,
antonio_actus 1:3bda347154e6 25 Pin_All = 0xFF
antonio_actus 1:3bda347154e6 26 };
antonio_actus 1:3bda347154e6 27 /** Constructor
antonio_actus 1:3bda347154e6 28 *
antonio_actus 2:d9beb7a97ff3 29 * @param i2c I2C Bus
antonio_actus 2:d9beb7a97ff3 30 * @param deviceAddress I2C slave address (PCF8574 (or PCF8574A) or MCP23008 portexpander,
antonio_actus 1:3bda347154e6 31 */
antonio_actus 1:3bda347154e6 32 MCP23008_I2C ( I2C *i2c, char deviceAddress);
antonio_actus 1:3bda347154e6 33
antonio_actus 1:3bda347154e6 34 /** Set pins to input mode
antonio_actus 1:3bda347154e6 35 *
antonio_actus 1:3bda347154e6 36 * This function is used to set which pins are inputs (if any). Example:
antonio_actus 1:3bda347154e6 37 * set_inputs ( Pin_GP0 | Pin_GP1 | Pin_GP2 );
antonio_actus 1:3bda347154e6 38 * Note that these are set to input in addition to the previously set.
antonio_actus 1:3bda347154e6 39 * In other words, the following:
antonio_actus 1:3bda347154e6 40 * set_inputs ( Pin_GP1 );
antonio_actus 1:3bda347154e6 41 * set_inputs ( Pin_GP2 );
antonio_actus 1:3bda347154e6 42 * Results in at least two pins set to input.
antonio_actus 1:3bda347154e6 43 *
antonio_actus 1:3bda347154e6 44 * @param pins A bitmask of pins to set to input mode.
antonio_actus 1:3bda347154e6 45 */
antonio_actus 1:3bda347154e6 46 void set_input_pins ( uint8_t pins );
antonio_actus 1:3bda347154e6 47 /** Set pins to output mode
antonio_actus 1:3bda347154e6 48 *
antonio_actus 1:3bda347154e6 49 * This function is used to set which pins are outputs (if any). Example:
antonio_actus 1:3bda347154e6 50 * set_outputs ( Pin_GP0 | Pin_GP1 | Pin_GP2 );
antonio_actus 1:3bda347154e6 51 * Note that these are set to output in addition to the previously set.
antonio_actus 1:3bda347154e6 52 * In other words, the following:
antonio_actus 1:3bda347154e6 53 * set_outputs ( Pin_GP1 );
antonio_actus 1:3bda347154e6 54 * set_outputs ( Pin_GP2 );
antonio_actus 1:3bda347154e6 55 * Results in at least two pins set to output.
antonio_actus 1:3bda347154e6 56 *
antonio_actus 1:3bda347154e6 57 * @param pins A bitmask of pins to set to output mode.
antonio_actus 1:3bda347154e6 58 */
antonio_actus 1:3bda347154e6 59 void set_output_pins ( uint8_t pins );
antonio_actus 1:3bda347154e6 60
antonio_actus 1:3bda347154e6 61 /** Write to the output pins.
antonio_actus 1:3bda347154e6 62 *
antonio_actus 1:3bda347154e6 63 * This function is used to set output pins on or off.
antonio_actus 1:3bda347154e6 64 *
antonio_actus 1:3bda347154e6 65 * @param values A bitmask indicating whether a pin should be on or off.
antonio_actus 1:3bda347154e6 66 */
antonio_actus 1:3bda347154e6 67 void write_outputs ( uint8_t values );
antonio_actus 1:3bda347154e6 68 /** Read back the outputs.
antonio_actus 1:3bda347154e6 69 *
antonio_actus 1:3bda347154e6 70 * This function is used to read the last values written to the output pins.
antonio_actus 1:3bda347154e6 71 *
antonio_actus 1:3bda347154e6 72 * @returns The value from the OLAT register.
antonio_actus 1:3bda347154e6 73 */
antonio_actus 1:3bda347154e6 74 uint8_t read_outputs ();
antonio_actus 1:3bda347154e6 75
antonio_actus 1:3bda347154e6 76 /** Read from the input pins.
antonio_actus 1:3bda347154e6 77 *
antonio_actus 1:3bda347154e6 78 * This function is used to read the values from the input pins.
antonio_actus 1:3bda347154e6 79 *
antonio_actus 1:3bda347154e6 80 * @returns A bitmask of the current state of the input pins.
antonio_actus 1:3bda347154e6 81 */
antonio_actus 1:3bda347154e6 82 uint8_t read_inputs ();
antonio_actus 1:3bda347154e6 83
antonio_actus 1:3bda347154e6 84 /** Set the input pin polarity.
antonio_actus 1:3bda347154e6 85 *
antonio_actus 1:3bda347154e6 86 * This function sets the polarity of the input pins.
antonio_actus 1:3bda347154e6 87 * A 1 bit is inverted polarity, a 0 is normal.
antonio_actus 1:3bda347154e6 88 *
antonio_actus 1:3bda347154e6 89 * @param values A bitmask of the input polarity.
antonio_actus 1:3bda347154e6 90 */
antonio_actus 1:3bda347154e6 91 void set_input_polarity ( uint8_t values );
antonio_actus 1:3bda347154e6 92 /** Read back the current input pin polarity.
antonio_actus 1:3bda347154e6 93 *
antonio_actus 1:3bda347154e6 94 * This function reads the current state of the input pin polarity.
antonio_actus 1:3bda347154e6 95 *
antonio_actus 1:3bda347154e6 96 * @returns The value from the IPOL register.
antonio_actus 1:3bda347154e6 97 */
antonio_actus 1:3bda347154e6 98 uint8_t get_input_polarity ();
antonio_actus 1:3bda347154e6 99
antonio_actus 1:3bda347154e6 100 /** Enable and disable the internal pull-up resistors for input pins.
antonio_actus 1:3bda347154e6 101 *
antonio_actus 1:3bda347154e6 102 * This function enables the internal 100 kΩ pull-up resistors.
antonio_actus 1:3bda347154e6 103 * A 1 bit enables the pull-up resistor for the corresponding input pin.
antonio_actus 1:3bda347154e6 104 *
antonio_actus 1:3bda347154e6 105 * @param values A bitmask indicating which pull-up resistors should be enabled/disabled.
antonio_actus 1:3bda347154e6 106 */
antonio_actus 1:3bda347154e6 107 void set_pullups ( uint8_t values );
antonio_actus 1:3bda347154e6 108 /** Get the current state of the internal pull-up resistors.
antonio_actus 1:3bda347154e6 109 *
antonio_actus 1:3bda347154e6 110 * @returns The current state of the pull-up resistors.
antonio_actus 1:3bda347154e6 111 */
antonio_actus 1:3bda347154e6 112 uint8_t get_pullups ();
antonio_actus 1:3bda347154e6 113
antonio_actus 1:3bda347154e6 114 /** Generate an interrupt when a pin changes.
antonio_actus 1:3bda347154e6 115 *
antonio_actus 1:3bda347154e6 116 * This function enables interrupt generation for the specified pins.
antonio_actus 1:3bda347154e6 117 * The interrupt is active-low by default.
antonio_actus 1:3bda347154e6 118 * The function acknowledge_interrupt must be called before another
antonio_actus 1:3bda347154e6 119 * interrupt will be generated.
antonio_actus 1:3bda347154e6 120 * Example:
antonio_actus 1:3bda347154e6 121 * @code
antonio_actus 1:3bda347154e6 122 * InterruptIn in ( p16 );
antonio_actus 1:3bda347154e6 123 * MCP23008 mcp ( p9, p10, 0 );
antonio_actus 1:3bda347154e6 124 * in.fall ( &interrupt );
antonio_actus 1:3bda347154e6 125 * mcp.interrupt_on_changes ( MCP23008::Pin_GP0 );
antonio_actus 1:3bda347154e6 126 * while ( 1 ) {
antonio_actus 1:3bda347154e6 127 * wait ( 1 );
antonio_actus 1:3bda347154e6 128 * }
antonio_actus 1:3bda347154e6 129 * @endcode
antonio_actus 1:3bda347154e6 130 *
antonio_actus 1:3bda347154e6 131 * @param pins A bitmask of the pins that may generate an interrupt.
antonio_actus 1:3bda347154e6 132 */
antonio_actus 1:3bda347154e6 133 void interrupt_on_changes ( uint8_t pins );
antonio_actus 1:3bda347154e6 134 /** Disables interrupts for the specified pins.
antonio_actus 1:3bda347154e6 135 *
antonio_actus 1:3bda347154e6 136 * @param values A bitmask indicating which interrupts should be disabled.
antonio_actus 1:3bda347154e6 137 */
antonio_actus 1:3bda347154e6 138 void disable_interrupts ( uint8_t pins );
antonio_actus 1:3bda347154e6 139
antonio_actus 1:3bda347154e6 140 /** Acknowledge a generated interrupt.
antonio_actus 1:3bda347154e6 141 *
antonio_actus 1:3bda347154e6 142 * This function must be called when an interrupt is generated to discover
antonio_actus 1:3bda347154e6 143 * which pin caused the interrupt and to enable future interrupts.
antonio_actus 1:3bda347154e6 144 *
antonio_actus 1:3bda347154e6 145 * @param pin An output paramter that specifies which pin generated the interrupt.
antonio_actus 1:3bda347154e6 146 * @param values The current state of the input pins.
antonio_actus 1:3bda347154e6 147 */
antonio_actus 1:3bda347154e6 148 void acknowledge_interrupt ( uint8_t &pin, uint8_t &values );
antonio_actus 1:3bda347154e6 149
antonio_actus 1:3bda347154e6 150 private:
antonio_actus 1:3bda347154e6 151 uint8_t read_register ( uint8_t reg );
antonio_actus 1:3bda347154e6 152 void write_register ( int reg, int value );
antonio_actus 1:3bda347154e6 153 void write_mask ( uint8_t reg, uint8_t mask, bool value );
antonio_actus 1:3bda347154e6 154 //I2C bus
antonio_actus 1:3bda347154e6 155 I2C *_i2c;
antonio_actus 1:3bda347154e6 156 char _slaveAddress;
antonio_actus 1:3bda347154e6 157 void reset ();
antonio_actus 1:3bda347154e6 158
antonio_actus 1:3bda347154e6 159
antonio_actus 1:3bda347154e6 160 };