Denver trai project

Dependencies:   mbed TextLCD

Committer:
mglmx
Date:
Wed Jun 13 12:02:48 2018 +0000
Revision:
33:24ce12dec157
New interrupts programmed

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mglmx 33:24ce12dec157 1 /* MCP23017 - drive the Microchip MCP23017 16-bit Port Extender using I2C
mglmx 33:24ce12dec157 2 * Copyright (c) 2010 Wim Huiskamp, Romilly Cocking (original version for SPI)
mglmx 33:24ce12dec157 3 *
mglmx 33:24ce12dec157 4 * Released under the MIT License: http://mbed.org/license/mit
mglmx 33:24ce12dec157 5 *
mglmx 33:24ce12dec157 6 * version 0.2 Initial Release
mglmx 33:24ce12dec157 7 * version 0.3 Cleaned up
mglmx 33:24ce12dec157 8 * version 0.4 Fixed problem with _read method
mglmx 33:24ce12dec157 9 * version 0.5 Added support for 'Banked' access to registers
mglmx 33:24ce12dec157 10 */
mglmx 33:24ce12dec157 11
mglmx 33:24ce12dec157 12 #include "mbed.h"
mglmx 33:24ce12dec157 13 #include "MCP23017.h"
mglmx 33:24ce12dec157 14
mglmx 33:24ce12dec157 15 /** Create an MCP23017 object connected to the specified I2C object and using the specified deviceAddress
mglmx 33:24ce12dec157 16 *
mglmx 33:24ce12dec157 17 * @param I2C &i2c the I2C port to connect to
mglmx 33:24ce12dec157 18 * @param char deviceAddress the address of the MCP23017
mglmx 33:24ce12dec157 19 */
mglmx 33:24ce12dec157 20 MCP23017::MCP23017(I2C &i2c, char deviceAddress) : _i2c(i2c) {
mglmx 33:24ce12dec157 21 _writeOpcode = deviceAddress & 0xFE; // low order bit = 0 for write
mglmx 33:24ce12dec157 22 _readOpcode = deviceAddress | 0x01; // low order bit = 1 for read
mglmx 33:24ce12dec157 23 _init();
mglmx 33:24ce12dec157 24 }
mglmx 33:24ce12dec157 25
mglmx 33:24ce12dec157 26 /** Read from specified MCP23017 register
mglmx 33:24ce12dec157 27 *
mglmx 33:24ce12dec157 28 * @param char address the internal registeraddress of the MCP23017
mglmx 33:24ce12dec157 29 * @returns data from register
mglmx 33:24ce12dec157 30 */
mglmx 33:24ce12dec157 31 char MCP23017::_read(char address) {
mglmx 33:24ce12dec157 32 char data[2];
mglmx 33:24ce12dec157 33
mglmx 33:24ce12dec157 34 data[0] = address;
mglmx 33:24ce12dec157 35 _i2c.write(_writeOpcode, data, 1); // Select Register for reading
mglmx 33:24ce12dec157 36 _i2c.read(_readOpcode, data, 1); // Read from selected Register
mglmx 33:24ce12dec157 37
mglmx 33:24ce12dec157 38 return data[0];
mglmx 33:24ce12dec157 39 }
mglmx 33:24ce12dec157 40
mglmx 33:24ce12dec157 41
mglmx 33:24ce12dec157 42 /** Write to specified MCP23017 register
mglmx 33:24ce12dec157 43 *
mglmx 33:24ce12dec157 44 * @param char address the internal registeraddress of the MCP23017
mglmx 33:24ce12dec157 45 */
mglmx 33:24ce12dec157 46 void MCP23017::_write(char address, char byte) {
mglmx 33:24ce12dec157 47 char data[2];
mglmx 33:24ce12dec157 48
mglmx 33:24ce12dec157 49 data[0] = address;
mglmx 33:24ce12dec157 50 data[1] = byte;
mglmx 33:24ce12dec157 51 _i2c.write(_writeOpcode, data, 2); // Write data to selected Register
mglmx 33:24ce12dec157 52 }
mglmx 33:24ce12dec157 53
mglmx 33:24ce12dec157 54
mglmx 33:24ce12dec157 55 /** Init MCP23017
mglmx 33:24ce12dec157 56 *
mglmx 33:24ce12dec157 57 * @param
mglmx 33:24ce12dec157 58 * @returns
mglmx 33:24ce12dec157 59 */
mglmx 33:24ce12dec157 60 void MCP23017::_init() {
mglmx 33:24ce12dec157 61
mglmx 33:24ce12dec157 62 _bankMode = NOT_BNK; // This may not be true after software reset without hardware reset !!!
mglmx 33:24ce12dec157 63
mglmx 33:24ce12dec157 64 _write(IOCON_AB[_bankMode][PORT_A], (IOCON_BYTE_MODE | IOCON_HAEN )); // Hardware addressing on, no-autoincrement, 16 bit mode (operations toggle between A and B registers)
mglmx 33:24ce12dec157 65
mglmx 33:24ce12dec157 66 }
mglmx 33:24ce12dec157 67
mglmx 33:24ce12dec157 68 /** Set I/O direction of specified MCP23017 Port
mglmx 33:24ce12dec157 69 *
mglmx 33:24ce12dec157 70 * @param Port Port address (Port_A or Port_B)
mglmx 33:24ce12dec157 71 * @param char direction pin direction (0 = output, 1 = input)
mglmx 33:24ce12dec157 72 */
mglmx 33:24ce12dec157 73 void MCP23017::direction(Port port, char direction) {
mglmx 33:24ce12dec157 74 _write(IODIR_AB[_bankMode][port], direction);
mglmx 33:24ce12dec157 75 }
mglmx 33:24ce12dec157 76
mglmx 33:24ce12dec157 77 /** Set Pull-Up Resistors on specified MCP23017 Port
mglmx 33:24ce12dec157 78 *
mglmx 33:24ce12dec157 79 * @param Port Port address (Port_A or Port_B)
mglmx 33:24ce12dec157 80 * @param char offOrOn per pin (0 = off, 1 = on)
mglmx 33:24ce12dec157 81 */
mglmx 33:24ce12dec157 82 void MCP23017::configurePullUps(Port port, char offOrOn) {
mglmx 33:24ce12dec157 83
mglmx 33:24ce12dec157 84 _write(GPPU_AB[_bankMode][port], offOrOn);
mglmx 33:24ce12dec157 85 }
mglmx 33:24ce12dec157 86
mglmx 33:24ce12dec157 87 /** Configere the Banked or Non-Banked mode
mglmx 33:24ce12dec157 88 *
mglmx 33:24ce12dec157 89 * @param Bank bankMode
mglmx 33:24ce12dec157 90 * @param char offOrOn per pin (0 = off, 1 = on)
mglmx 33:24ce12dec157 91 */
mglmx 33:24ce12dec157 92 void MCP23017::configureBanked(Bank bankMode) {
mglmx 33:24ce12dec157 93
mglmx 33:24ce12dec157 94 if (bankMode == NOT_BNK) {
mglmx 33:24ce12dec157 95 // Non-Banked sequential registers (default POR)
mglmx 33:24ce12dec157 96 // Hardware addressing on, , no-autoincrement, 16 bit mode (operations do toggle between A and B registers)
mglmx 33:24ce12dec157 97 _write(IOCON_AB[_bankMode][PORT_A], (IOCON_BYTE_MODE | IOCON_HAEN ));
mglmx 33:24ce12dec157 98 _bankMode = NOT_BNK;
mglmx 33:24ce12dec157 99 }
mglmx 33:24ce12dec157 100 else {
mglmx 33:24ce12dec157 101 // Banked registers
mglmx 33:24ce12dec157 102 // Hardware addressing on, no-autoincrement, 8 bit mode
mglmx 33:24ce12dec157 103 _write(IOCON_AB[_bankMode][PORT_A], (IOCON_BANK | IOCON_BYTE_MODE | IOCON_HAEN ));
mglmx 33:24ce12dec157 104 _bankMode = BNK;
mglmx 33:24ce12dec157 105 }
mglmx 33:24ce12dec157 106 }
mglmx 33:24ce12dec157 107
mglmx 33:24ce12dec157 108
mglmx 33:24ce12dec157 109 void MCP23017::interruptEnable(Port port, char interruptsEnabledMask) {
mglmx 33:24ce12dec157 110
mglmx 33:24ce12dec157 111 _write(GPINTEN_AB[_bankMode][port], interruptsEnabledMask);
mglmx 33:24ce12dec157 112
mglmx 33:24ce12dec157 113 }
mglmx 33:24ce12dec157 114
mglmx 33:24ce12dec157 115 void MCP23017::mirrorInterrupts(bool mirror) {
mglmx 33:24ce12dec157 116 char iocon = _read(IOCON_AB[_bankMode][PORT_A]);
mglmx 33:24ce12dec157 117
mglmx 33:24ce12dec157 118 if (mirror) {
mglmx 33:24ce12dec157 119 iocon = iocon | INTERRUPT_MIRROR_BIT;
mglmx 33:24ce12dec157 120 }
mglmx 33:24ce12dec157 121 else {
mglmx 33:24ce12dec157 122 iocon = iocon & ~INTERRUPT_MIRROR_BIT;
mglmx 33:24ce12dec157 123 }
mglmx 33:24ce12dec157 124
mglmx 33:24ce12dec157 125 _write(IOCON_AB[_bankMode][PORT_A], iocon);
mglmx 33:24ce12dec157 126
mglmx 33:24ce12dec157 127 }
mglmx 33:24ce12dec157 128
mglmx 33:24ce12dec157 129 void MCP23017::interruptPolarity(Polarity polarity) {
mglmx 33:24ce12dec157 130 char iocon = _read(IOCON_AB[_bankMode][PORT_A]);
mglmx 33:24ce12dec157 131
mglmx 33:24ce12dec157 132 if (polarity == ACTIVE_LOW) {
mglmx 33:24ce12dec157 133 iocon = iocon & ~INTERRUPT_POLARITY_BIT;
mglmx 33:24ce12dec157 134 } else {
mglmx 33:24ce12dec157 135 iocon = iocon | INTERRUPT_POLARITY_BIT;
mglmx 33:24ce12dec157 136 }
mglmx 33:24ce12dec157 137 _write(IOCON_AB[_bankMode][PORT_A], iocon);
mglmx 33:24ce12dec157 138 }
mglmx 33:24ce12dec157 139
mglmx 33:24ce12dec157 140 void MCP23017::defaultValue(Port port, char valuesToCompare) {
mglmx 33:24ce12dec157 141
mglmx 33:24ce12dec157 142 _write(DEFVAL_AB[_bankMode][port], valuesToCompare);
mglmx 33:24ce12dec157 143
mglmx 33:24ce12dec157 144 }
mglmx 33:24ce12dec157 145
mglmx 33:24ce12dec157 146 void MCP23017::interruptControl(Port port, char interruptControlBits) {
mglmx 33:24ce12dec157 147
mglmx 33:24ce12dec157 148 _write(INTCON_AB[_bankMode][port], interruptControlBits);
mglmx 33:24ce12dec157 149
mglmx 33:24ce12dec157 150 }
mglmx 33:24ce12dec157 151
mglmx 33:24ce12dec157 152 /** Write to specified MCP23017 Port
mglmx 33:24ce12dec157 153 *
mglmx 33:24ce12dec157 154 * @param Port Port address (Port_A or Port_B)
mglmx 33:24ce12dec157 155 * @param char byte data to write
mglmx 33:24ce12dec157 156 */
mglmx 33:24ce12dec157 157 void MCP23017::write(Port port, char byte) {
mglmx 33:24ce12dec157 158 _write(OLAT_AB[_bankMode][port], byte);
mglmx 33:24ce12dec157 159 }
mglmx 33:24ce12dec157 160
mglmx 33:24ce12dec157 161 /** Read from specified MCP23017 Port
mglmx 33:24ce12dec157 162 *
mglmx 33:24ce12dec157 163 * @param Port Port address (Port_A or Port_B)
mglmx 33:24ce12dec157 164 * @returns data from Port
mglmx 33:24ce12dec157 165 */
mglmx 33:24ce12dec157 166 char MCP23017::read(Port port) {
mglmx 33:24ce12dec157 167 return _read(GPIO_AB[_bankMode][port]);
mglmx 33:24ce12dec157 168 }
mglmx 33:24ce12dec157 169
mglmx 33:24ce12dec157 170