Added support for banked registers

Dependents:   Component_Test_Interface FalconWing MX_Spoile_Test Simple_Power_Distribution ... more

Committer:
wim
Date:
Mon Dec 20 15:53:38 2010 +0000
Revision:
1:e2edbd61f4d0
Parent:
0:1a9288cc0630
Child:
3:72da9cd002bd
Cleaned up and added documentation

Who changed what in which revision?

UserRevisionLine numberNew 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 0:1a9288cc0630 8 */
wim 0:1a9288cc0630 9
wim 0:1a9288cc0630 10 #include "mbed.h"
wim 0:1a9288cc0630 11 #include "MCP23017.h"
wim 0:1a9288cc0630 12
wim 1:e2edbd61f4d0 13 /** Create an MCP23017 object connected to the specified I2C object and using the specified deviceAddress
wim 1:e2edbd61f4d0 14 *
wim 1:e2edbd61f4d0 15 * @param I2C &i2c the I2C port to connect to
wim 1:e2edbd61f4d0 16 * @param char deviceAddress the address of the MSC23017
wim 1:e2edbd61f4d0 17 */
wim 0:1a9288cc0630 18 MCP23017::MCP23017(I2C &i2c, char deviceAddress) : _i2c(i2c) {
wim 0:1a9288cc0630 19 _writeOpcode = deviceAddress & 0xFE; // low order bit = 0 for write
wim 0:1a9288cc0630 20 _readOpcode = deviceAddress | 0x01; // low order bit = 1 for read
wim 0:1a9288cc0630 21 _init();
wim 0:1a9288cc0630 22 }
wim 0:1a9288cc0630 23
wim 1:e2edbd61f4d0 24 /** Read from specified MCP23017 register
wim 1:e2edbd61f4d0 25 *
wim 1:e2edbd61f4d0 26 * @param char address the internal registeraddress of the MSC23017
wim 1:e2edbd61f4d0 27 * @returns data from register
wim 1:e2edbd61f4d0 28 */
wim 0:1a9288cc0630 29 char MCP23017::_read(char address) {
wim 0:1a9288cc0630 30 char data[2];
wim 0:1a9288cc0630 31 char result;
wim 0:1a9288cc0630 32
wim 0:1a9288cc0630 33 data[0] = address;
wim 0:1a9288cc0630 34 _i2c.write(_writeOpcode, data, 1); // Select Register for reading
wim 0:1a9288cc0630 35 result = _i2c.read(_readOpcode); // Read from selected Register
wim 0:1a9288cc0630 36
wim 0:1a9288cc0630 37 return result;
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 1:e2edbd61f4d0 43 * @param char address the internal registeraddress of the MSC23017
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