Added support for banked registers
Dependents: Component_Test_Interface FalconWing MX_Spoile_Test Simple_Power_Distribution ... more
MCP23017.cpp@3:72da9cd002bd, 2011-08-21 (annotated)
- Committer:
- wim
- Date:
- Sun Aug 21 13:59:48 2011 +0000
- Revision:
- 3:72da9cd002bd
- Parent:
- 1:e2edbd61f4d0
- Child:
- 4:868db61f5f4e
Fixed problem in _read() method
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wim | 0:1a9288cc0630 | 1 | /* MCP23017 - drive the Microchip MCP23017 16-bit Port Extender using I2C |
wim | 0:1a9288cc0630 | 2 | * Copyright (c) 2010 Wim Huiskamp, Romilly Cocking (original version for SPI) |
wim | 0:1a9288cc0630 | 3 | * |
wim | 0:1a9288cc0630 | 4 | * Released under the MIT License: http://mbed.org/license/mit |
wim | 0:1a9288cc0630 | 5 | * |
wim | 1:e2edbd61f4d0 | 6 | * version 0.2 Initial Release |
wim | 1:e2edbd61f4d0 | 7 | * version 0.3 Cleaned up |
wim | 3:72da9cd002bd | 8 | * version 0.4 Fixed problem with _read method |
wim | 0:1a9288cc0630 | 9 | */ |
wim | 0:1a9288cc0630 | 10 | |
wim | 0:1a9288cc0630 | 11 | #include "mbed.h" |
wim | 0:1a9288cc0630 | 12 | #include "MCP23017.h" |
wim | 0:1a9288cc0630 | 13 | |
wim | 1:e2edbd61f4d0 | 14 | /** Create an MCP23017 object connected to the specified I2C object and using the specified deviceAddress |
wim | 1:e2edbd61f4d0 | 15 | * |
wim | 1:e2edbd61f4d0 | 16 | * @param I2C &i2c the I2C port to connect to |
wim | 3:72da9cd002bd | 17 | * @param char deviceAddress the address of the MCP23017 |
wim | 1:e2edbd61f4d0 | 18 | */ |
wim | 0:1a9288cc0630 | 19 | MCP23017::MCP23017(I2C &i2c, char deviceAddress) : _i2c(i2c) { |
wim | 0:1a9288cc0630 | 20 | _writeOpcode = deviceAddress & 0xFE; // low order bit = 0 for write |
wim | 0:1a9288cc0630 | 21 | _readOpcode = deviceAddress | 0x01; // low order bit = 1 for read |
wim | 0:1a9288cc0630 | 22 | _init(); |
wim | 0:1a9288cc0630 | 23 | } |
wim | 0:1a9288cc0630 | 24 | |
wim | 1:e2edbd61f4d0 | 25 | /** Read from specified MCP23017 register |
wim | 1:e2edbd61f4d0 | 26 | * |
wim | 3:72da9cd002bd | 27 | * @param char address the internal registeraddress of the MCP23017 |
wim | 1:e2edbd61f4d0 | 28 | * @returns data from register |
wim | 1:e2edbd61f4d0 | 29 | */ |
wim | 0:1a9288cc0630 | 30 | char MCP23017::_read(char address) { |
wim | 0:1a9288cc0630 | 31 | char data[2]; |
wim | 0:1a9288cc0630 | 32 | |
wim | 0:1a9288cc0630 | 33 | data[0] = address; |
wim | 3:72da9cd002bd | 34 | _i2c.write(_writeOpcode, data, 1); // Select Register for reading |
wim | 3:72da9cd002bd | 35 | _i2c.read(_readOpcode, data, 1); // Read from selected Register |
wim | 0:1a9288cc0630 | 36 | |
wim | 3:72da9cd002bd | 37 | return data[0]; |
wim | 0:1a9288cc0630 | 38 | } |
wim | 0:1a9288cc0630 | 39 | |
wim | 0:1a9288cc0630 | 40 | |
wim | 1:e2edbd61f4d0 | 41 | /** Write to specified MCP23017 register |
wim | 1:e2edbd61f4d0 | 42 | * |
wim | 3:72da9cd002bd | 43 | * @param char address the internal registeraddress of the MCP23017 |
wim | 1:e2edbd61f4d0 | 44 | */ |
wim | 0:1a9288cc0630 | 45 | void MCP23017::_write(char address, char byte) { |
wim | 0:1a9288cc0630 | 46 | char data[2]; |
wim | 0:1a9288cc0630 | 47 | |
wim | 0:1a9288cc0630 | 48 | data[0] = address; |
wim | 0:1a9288cc0630 | 49 | data[1] = byte; |
wim | 0:1a9288cc0630 | 50 | _i2c.write(_writeOpcode, data, 2); // Write data to selected Register |
wim | 0:1a9288cc0630 | 51 | } |
wim | 0:1a9288cc0630 | 52 | |
wim | 0:1a9288cc0630 | 53 | |
wim | 1:e2edbd61f4d0 | 54 | /** Init MCP23017 |
wim | 1:e2edbd61f4d0 | 55 | * |
wim | 1:e2edbd61f4d0 | 56 | * @param |
wim | 1:e2edbd61f4d0 | 57 | * @returns |
wim | 1:e2edbd61f4d0 | 58 | */ |
wim | 0:1a9288cc0630 | 59 | void MCP23017::_init() { |
wim | 0:1a9288cc0630 | 60 | _write(IOCON, (IOCON_BYTE_MODE | IOCON_HAEN )); // Hardware addressing on, operations toggle between A and B registers |
wim | 0:1a9288cc0630 | 61 | |
wim | 0:1a9288cc0630 | 62 | } |
wim | 0:1a9288cc0630 | 63 | |
wim | 1:e2edbd61f4d0 | 64 | /** Set I/O direction of specified MCP23017 Port |
wim | 1:e2edbd61f4d0 | 65 | * |
wim | 1:e2edbd61f4d0 | 66 | * @param Port Port address (Port_A or Port_B) |
wim | 1:e2edbd61f4d0 | 67 | * @param char direction pin direction (0 = output, 1 = input) |
wim | 1:e2edbd61f4d0 | 68 | */ |
wim | 0:1a9288cc0630 | 69 | void MCP23017::direction(Port port, char direction) { |
wim | 0:1a9288cc0630 | 70 | _write(port + IODIRA, direction); |
wim | 0:1a9288cc0630 | 71 | } |
wim | 0:1a9288cc0630 | 72 | |
wim | 1:e2edbd61f4d0 | 73 | /** Set Pull-Up Resistors on specified MCP23017 Port |
wim | 1:e2edbd61f4d0 | 74 | * |
wim | 1:e2edbd61f4d0 | 75 | * @param Port Port address (Port_A or Port_B) |
wim | 1:e2edbd61f4d0 | 76 | * @param char offOrOn per pin (0 = off, 1 = on) |
wim | 1:e2edbd61f4d0 | 77 | */ |
wim | 0:1a9288cc0630 | 78 | void MCP23017::configurePullUps(Port port, char offOrOn) { |
wim | 0:1a9288cc0630 | 79 | _write(port + GPPUA, offOrOn); |
wim | 0:1a9288cc0630 | 80 | } |
wim | 0:1a9288cc0630 | 81 | |
wim | 0:1a9288cc0630 | 82 | void MCP23017::interruptEnable(Port port, char interruptsEnabledMask) { |
wim | 0:1a9288cc0630 | 83 | _write(port + GPINTENA, interruptsEnabledMask); |
wim | 0:1a9288cc0630 | 84 | } |
wim | 0:1a9288cc0630 | 85 | |
wim | 0:1a9288cc0630 | 86 | void MCP23017::mirrorInterrupts(bool mirror) { |
wim | 0:1a9288cc0630 | 87 | char iocon = _read(IOCON); |
wim | 0:1a9288cc0630 | 88 | if (mirror) { |
wim | 0:1a9288cc0630 | 89 | iocon = iocon | INTERRUPT_MIRROR_BIT; |
wim | 0:1a9288cc0630 | 90 | } else { |
wim | 0:1a9288cc0630 | 91 | iocon = iocon & ~INTERRUPT_MIRROR_BIT; |
wim | 0:1a9288cc0630 | 92 | } |
wim | 0:1a9288cc0630 | 93 | _write(IOCON, iocon); |
wim | 0:1a9288cc0630 | 94 | |
wim | 0:1a9288cc0630 | 95 | } |
wim | 0:1a9288cc0630 | 96 | |
wim | 0:1a9288cc0630 | 97 | void MCP23017::interruptPolarity(Polarity polarity) { |
wim | 0:1a9288cc0630 | 98 | char iocon = _read(IOCON); |
wim | 0:1a9288cc0630 | 99 | if (polarity == ACTIVE_LOW) { |
wim | 0:1a9288cc0630 | 100 | iocon = iocon & ~INTERRUPT_POLARITY_BIT; |
wim | 0:1a9288cc0630 | 101 | } else { |
wim | 0:1a9288cc0630 | 102 | iocon = iocon | INTERRUPT_POLARITY_BIT; |
wim | 0:1a9288cc0630 | 103 | } |
wim | 0:1a9288cc0630 | 104 | _write(IOCON, iocon); |
wim | 0:1a9288cc0630 | 105 | } |
wim | 0:1a9288cc0630 | 106 | |
wim | 0:1a9288cc0630 | 107 | void MCP23017::defaultValue(Port port, char valuesToCompare) { |
wim | 0:1a9288cc0630 | 108 | _write(port + DEFVALA, valuesToCompare); |
wim | 0:1a9288cc0630 | 109 | } |
wim | 0:1a9288cc0630 | 110 | |
wim | 0:1a9288cc0630 | 111 | void MCP23017::interruptControl(Port port, char interruptControlBits) { |
wim | 0:1a9288cc0630 | 112 | _write(port + INTCONA, interruptControlBits); |
wim | 0:1a9288cc0630 | 113 | } |
wim | 0:1a9288cc0630 | 114 | |
wim | 1:e2edbd61f4d0 | 115 | /** Write to specified MCP23017 Port |
wim | 1:e2edbd61f4d0 | 116 | * |
wim | 1:e2edbd61f4d0 | 117 | * @param Port Port address (Port_A or Port_B) |
wim | 1:e2edbd61f4d0 | 118 | * @param char byte data to write |
wim | 1:e2edbd61f4d0 | 119 | */ |
wim | 0:1a9288cc0630 | 120 | void MCP23017::write(Port port, char byte) { |
wim | 0:1a9288cc0630 | 121 | _write(port + OLATA, byte); |
wim | 0:1a9288cc0630 | 122 | } |
wim | 0:1a9288cc0630 | 123 | |
wim | 1:e2edbd61f4d0 | 124 | /** Read from specified MCP23017 Port |
wim | 1:e2edbd61f4d0 | 125 | * |
wim | 1:e2edbd61f4d0 | 126 | * @param Port Port address (Port_A or Port_B) |
wim | 1:e2edbd61f4d0 | 127 | * @returns data from Port |
wim | 1:e2edbd61f4d0 | 128 | */ |
wim | 0:1a9288cc0630 | 129 | char MCP23017::read(Port port) { |
wim | 0:1a9288cc0630 | 130 | return _read(port + GPIOA); |
wim | 0:1a9288cc0630 | 131 | } |
wim | 0:1a9288cc0630 | 132 |