Added support for banked registers

Dependents:   Component_Test_Interface FalconWing MX_Spoile_Test Simple_Power_Distribution ... more

Committer:
wim
Date:
Sun Aug 21 13:59:48 2011 +0000
Revision:
3:72da9cd002bd
Parent:
2:2d4ee919e8a7
Child:
4:868db61f5f4e
Fixed problem in _read() method

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 3:72da9cd002bd 8 * version 0.4 Fixed problem with _read method
wim 0:1a9288cc0630 9 */
wim 0:1a9288cc0630 10 #include "mbed.h"
wim 0:1a9288cc0630 11
wim 0:1a9288cc0630 12 #ifndef MCP23017_H
wim 0:1a9288cc0630 13 #define MCP23017_H
wim 0:1a9288cc0630 14
wim 0:1a9288cc0630 15 // All register addresses assume IOCON.BANK = 0 (POR default)
wim 0:1a9288cc0630 16 #define IODIRA 0x00
wim 0:1a9288cc0630 17 #define IODIRB 0x01
wim 0:1a9288cc0630 18 #define GPINTENA 0x04
wim 0:1a9288cc0630 19 #define GPINTENB 0x05
wim 0:1a9288cc0630 20 #define DEFVALA 0x06
wim 0:1a9288cc0630 21 #define DEFVALB 0x07
wim 0:1a9288cc0630 22 #define INTCONA 0x08
wim 0:1a9288cc0630 23 #define INTCONB 0x09
wim 0:1a9288cc0630 24 #define IOCON 0x0A
wim 0:1a9288cc0630 25 //#define IOCON 0x0B
wim 0:1a9288cc0630 26 #define GPPUA 0x0C
wim 0:1a9288cc0630 27 #define GPPUB 0x0D
wim 0:1a9288cc0630 28 #define INTFA 0x0E
wim 0:1a9288cc0630 29 #define INTFB 0x0F
wim 0:1a9288cc0630 30 #define INTCAPA 0x10
wim 0:1a9288cc0630 31 #define INTCAPB 0x11
wim 0:1a9288cc0630 32 #define GPIOA 0x12
wim 0:1a9288cc0630 33 #define GPIOB 0x13
wim 0:1a9288cc0630 34 #define OLATA 0x14
wim 0:1a9288cc0630 35 #define OLATB 0x15
wim 0:1a9288cc0630 36
wim 0:1a9288cc0630 37 // Control settings
wim 0:1a9288cc0630 38 #define IOCON_BANK 0x80 // Banked registers for Port A and B
wim 0:1a9288cc0630 39 #define IOCON_BYTE_MODE 0x20 // Disables sequential operation, Address Ptr does not increment
wim 0:1a9288cc0630 40 // If Disabled and Bank = 0, operations toggle between Port A and B registers
wim 0:1a9288cc0630 41 #define IOCON_HAEN 0x08 // Hardware address enable
wim 0:1a9288cc0630 42
wim 0:1a9288cc0630 43 #define INTERRUPT_POLARITY_BIT 0x02
wim 0:1a9288cc0630 44 #define INTERRUPT_MIRROR_BIT 0x40
wim 0:1a9288cc0630 45
wim 0:1a9288cc0630 46 #define PORT_DIR_OUT 0x00
wim 0:1a9288cc0630 47 #define PORT_DIR_IN 0xFF
wim 0:1a9288cc0630 48
wim 0:1a9288cc0630 49 enum Polarity { ACTIVE_LOW , ACTIVE_HIGH };
wim 0:1a9288cc0630 50 enum Port { PORT_A, PORT_B };
wim 0:1a9288cc0630 51
wim 0:1a9288cc0630 52 class MCP23017 {
wim 0:1a9288cc0630 53 public:
wim 2:2d4ee919e8a7 54 /** Create an MCP23017 object connected to the specified I2C object and using the specified deviceAddress
wim 2:2d4ee919e8a7 55 *
wim 2:2d4ee919e8a7 56 * @param I2C &i2c the I2C port to connect to
wim 3:72da9cd002bd 57 * @param char deviceAddress the address of the MCP23017
wim 2:2d4ee919e8a7 58 */
wim 0:1a9288cc0630 59 MCP23017(I2C &i2c, char deviceAddress);
wim 2:2d4ee919e8a7 60
wim 2:2d4ee919e8a7 61 /** Set I/O direction of specified MCP23017 Port
wim 2:2d4ee919e8a7 62 *
wim 2:2d4ee919e8a7 63 * @param Port Port address (Port_A or Port_B)
wim 2:2d4ee919e8a7 64 * @param char direction pin direction (0 = output, 1 = input)
wim 2:2d4ee919e8a7 65 */
wim 0:1a9288cc0630 66 void direction(Port port, char direction);
wim 2:2d4ee919e8a7 67
wim 2:2d4ee919e8a7 68 /** Set Pull-Up Resistors on specified MCP23017 Port
wim 2:2d4ee919e8a7 69 *
wim 2:2d4ee919e8a7 70 * @param Port Port address (Port_A or Port_B)
wim 2:2d4ee919e8a7 71 * @param char offOrOn per pin (0 = off, 1 = on)
wim 2:2d4ee919e8a7 72 */
wim 0:1a9288cc0630 73 void configurePullUps(Port port, char offOrOn);
wim 2:2d4ee919e8a7 74
wim 0:1a9288cc0630 75 void interruptEnable(Port port, char interruptsEnabledMask);
wim 0:1a9288cc0630 76 void interruptPolarity(Polarity polarity);
wim 0:1a9288cc0630 77 void mirrorInterrupts(bool mirror);
wim 0:1a9288cc0630 78 void defaultValue(Port port, char valuesToCompare);
wim 0:1a9288cc0630 79 void interruptControl(Port port, char interruptControlBits);
wim 2:2d4ee919e8a7 80
wim 2:2d4ee919e8a7 81 /** Read from specified MCP23017 Port
wim 2:2d4ee919e8a7 82 *
wim 2:2d4ee919e8a7 83 * @param Port Port address (Port_A or Port_B)
wim 2:2d4ee919e8a7 84 * @returns data from Port
wim 2:2d4ee919e8a7 85 */
wim 0:1a9288cc0630 86 char read(Port port);
wim 2:2d4ee919e8a7 87
wim 2:2d4ee919e8a7 88 /** Write to specified MCP23017 Port
wim 2:2d4ee919e8a7 89 *
wim 2:2d4ee919e8a7 90 * @param Port Port address (Port_A or Port_B)
wim 2:2d4ee919e8a7 91 * @param char byte data to write
wim 2:2d4ee919e8a7 92 */
wim 0:1a9288cc0630 93 void write(Port port, char byte);
wim 1:e2edbd61f4d0 94
wim 0:1a9288cc0630 95 protected:
wim 0:1a9288cc0630 96 I2C &_i2c;
wim 0:1a9288cc0630 97 char _readOpcode;
wim 0:1a9288cc0630 98 char _writeOpcode;
wim 2:2d4ee919e8a7 99
wim 2:2d4ee919e8a7 100 /** Init MCP23017
wim 2:2d4ee919e8a7 101 *
wim 2:2d4ee919e8a7 102 * @param
wim 2:2d4ee919e8a7 103 * @returns
wim 2:2d4ee919e8a7 104 */
wim 2:2d4ee919e8a7 105 void _init();
wim 2:2d4ee919e8a7 106
wim 2:2d4ee919e8a7 107 /** Write to specified MCP23017 register
wim 2:2d4ee919e8a7 108 *
wim 3:72da9cd002bd 109 * @param char address the internal registeraddress of the MCP23017
wim 2:2d4ee919e8a7 110 */
wim 2:2d4ee919e8a7 111 void _write(char address, char byte);
wim 2:2d4ee919e8a7 112
wim 2:2d4ee919e8a7 113 /** Read from specified MCP23017 register
wim 2:2d4ee919e8a7 114 *
wim 3:72da9cd002bd 115 * @param char address the internal registeraddress of the MCP23017
wim 2:2d4ee919e8a7 116 * @returns data from register
wim 2:2d4ee919e8a7 117 */
wim 2:2d4ee919e8a7 118 char _read(char address);
wim 0:1a9288cc0630 119 };
wim 0:1a9288cc0630 120
wim 0:1a9288cc0630 121 #endif