Added support for banked registers

Dependents:   Component_Test_Interface FalconWing MX_Spoile_Test Simple_Power_Distribution ... more

Committer:
wim
Date:
Mon Dec 20 16:04:04 2010 +0000
Revision:
2:2d4ee919e8a7
Parent:
1:e2edbd61f4d0
Child:
3:72da9cd002bd

        

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