cycyy

Dependencies:   MCP23017 WattBob_TextLCD

Committer:
mihaidd
Date:
Wed May 08 03:58:47 2019 +0000
Revision:
0:a9b4ee4ed395
ccc

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mihaidd 0:a9b4ee4ed395 1 /* MCP23017 library for Arduino
mihaidd 0:a9b4ee4ed395 2 Copyright (C) 2009 David Pye <davidmpye@gmail.com
mihaidd 0:a9b4ee4ed395 3 Modified for use on the MBED ARM platform
mihaidd 0:a9b4ee4ed395 4
mihaidd 0:a9b4ee4ed395 5 This program is free software: you can redistribute it and/or modify
mihaidd 0:a9b4ee4ed395 6 it under the terms of the GNU General Public License as published by
mihaidd 0:a9b4ee4ed395 7 the Free Software Foundation, either version 3 of the License, or
mihaidd 0:a9b4ee4ed395 8 (at your option) any later version.
mihaidd 0:a9b4ee4ed395 9
mihaidd 0:a9b4ee4ed395 10 This program is distributed in the hope that it will be useful,
mihaidd 0:a9b4ee4ed395 11 but WITHOUT ANY WARRANTY; without even the implied warranty of
mihaidd 0:a9b4ee4ed395 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
mihaidd 0:a9b4ee4ed395 13 GNU General Public License for more details.
mihaidd 0:a9b4ee4ed395 14
mihaidd 0:a9b4ee4ed395 15 You should have received a copy of the GNU General Public License
mihaidd 0:a9b4ee4ed395 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
mihaidd 0:a9b4ee4ed395 17 */
mihaidd 0:a9b4ee4ed395 18
mihaidd 0:a9b4ee4ed395 19 #ifndef MBED_MCP23017_H
mihaidd 0:a9b4ee4ed395 20 #define MBED_MCP23017_H
mihaidd 0:a9b4ee4ed395 21
mihaidd 0:a9b4ee4ed395 22 #include "mbed.h"
mihaidd 0:a9b4ee4ed395 23
mihaidd 0:a9b4ee4ed395 24 //
mihaidd 0:a9b4ee4ed395 25 // Register defines from data sheet - we set IOCON.BANK to 0
mihaidd 0:a9b4ee4ed395 26 // as it is easier to manage the registers sequentially.
mihaidd 0:a9b4ee4ed395 27 //
mihaidd 0:a9b4ee4ed395 28 #define IODIR 0x00
mihaidd 0:a9b4ee4ed395 29 #define IPOL 0x02
mihaidd 0:a9b4ee4ed395 30 #define GPINTEN 0x04
mihaidd 0:a9b4ee4ed395 31 #define DEFVAL 0x06
mihaidd 0:a9b4ee4ed395 32 #define INTCON 0x08
mihaidd 0:a9b4ee4ed395 33 #define IOCON 0x0A
mihaidd 0:a9b4ee4ed395 34 #define GPPU 0x0C
mihaidd 0:a9b4ee4ed395 35 #define INTF 0x0E
mihaidd 0:a9b4ee4ed395 36 #define INTCAP 0x10
mihaidd 0:a9b4ee4ed395 37 #define GPIO 0x12
mihaidd 0:a9b4ee4ed395 38 #define OLAT 0x14
mihaidd 0:a9b4ee4ed395 39
mihaidd 0:a9b4ee4ed395 40 #define I2C_BASE_ADDRESS 0x40
mihaidd 0:a9b4ee4ed395 41
mihaidd 0:a9b4ee4ed395 42 #define DIR_OUTPUT 0
mihaidd 0:a9b4ee4ed395 43 #define DIR_INPUT 1
mihaidd 0:a9b4ee4ed395 44
mihaidd 0:a9b4ee4ed395 45 /** MCP23017 class
mihaidd 0:a9b4ee4ed395 46 *
mihaidd 0:a9b4ee4ed395 47 * Allow access to an I2C connected MCP23017 16-bit I/O extender chip
mihaidd 0:a9b4ee4ed395 48 * Example:
mihaidd 0:a9b4ee4ed395 49 * @code
mihaidd 0:a9b4ee4ed395 50 * MCP23017 *par_port;
mihaidd 0:a9b4ee4ed395 51 * @endcode
mihaidd 0:a9b4ee4ed395 52 *
mihaidd 0:a9b4ee4ed395 53 */
mihaidd 0:a9b4ee4ed395 54 class MCP23017 {
mihaidd 0:a9b4ee4ed395 55 public:
mihaidd 0:a9b4ee4ed395 56 /** Constructor for the MCP23017 connected to specified I2C pins at a specific address
mihaidd 0:a9b4ee4ed395 57 *
mihaidd 0:a9b4ee4ed395 58 * 16-bit I/O expander with I2C interface
mihaidd 0:a9b4ee4ed395 59 *
mihaidd 0:a9b4ee4ed395 60 * @param sda I2C data pin
mihaidd 0:a9b4ee4ed395 61 * @param scl I2C clock pin
mihaidd 0:a9b4ee4ed395 62 * @param i2cAddress I2C address
mihaidd 0:a9b4ee4ed395 63 */
mihaidd 0:a9b4ee4ed395 64 MCP23017(PinName sda, PinName scl, int i2cAddress);
mihaidd 0:a9b4ee4ed395 65
mihaidd 0:a9b4ee4ed395 66 /** Reset MCP23017 device to its power-on state
mihaidd 0:a9b4ee4ed395 67 */
mihaidd 0:a9b4ee4ed395 68 void reset(void);
mihaidd 0:a9b4ee4ed395 69
mihaidd 0:a9b4ee4ed395 70 /** Write a 0/1 value to an output bit
mihaidd 0:a9b4ee4ed395 71 *
mihaidd 0:a9b4ee4ed395 72 * @param value 0 or 1
mihaidd 0:a9b4ee4ed395 73 * @param bit_number bit number range 0 --> 15
mihaidd 0:a9b4ee4ed395 74 */
mihaidd 0:a9b4ee4ed395 75 void write_bit(int value, int bit_number);
mihaidd 0:a9b4ee4ed395 76
mihaidd 0:a9b4ee4ed395 77 /** Write a masked 16-bit value to the device
mihaidd 0:a9b4ee4ed395 78 *
mihaidd 0:a9b4ee4ed395 79 * @param data 16-bit data value
mihaidd 0:a9b4ee4ed395 80 * @param mask 16-bit mask value
mihaidd 0:a9b4ee4ed395 81 */
mihaidd 0:a9b4ee4ed395 82 void write_mask(unsigned short data, unsigned short mask);
mihaidd 0:a9b4ee4ed395 83
mihaidd 0:a9b4ee4ed395 84 /** Read a 0/1 value from an input bit
mihaidd 0:a9b4ee4ed395 85 *
mihaidd 0:a9b4ee4ed395 86 * @param bit_number bit number range 0 --> 15
mihaidd 0:a9b4ee4ed395 87 * @return 0/1 value read
mihaidd 0:a9b4ee4ed395 88 */
mihaidd 0:a9b4ee4ed395 89 int read_bit(int bit_number);
mihaidd 0:a9b4ee4ed395 90
mihaidd 0:a9b4ee4ed395 91 /** Read a 16-bit value from the device and apply mask
mihaidd 0:a9b4ee4ed395 92 *
mihaidd 0:a9b4ee4ed395 93 * @param mask 16-bit mask value
mihaidd 0:a9b4ee4ed395 94 * @return 16-bit data with mask applied
mihaidd 0:a9b4ee4ed395 95 */
mihaidd 0:a9b4ee4ed395 96 int read_mask(unsigned short mask);
mihaidd 0:a9b4ee4ed395 97
mihaidd 0:a9b4ee4ed395 98 /** Configure an MCP23017 device
mihaidd 0:a9b4ee4ed395 99 *
mihaidd 0:a9b4ee4ed395 100 * @param dir_config data direction value (1 = input, 0 = output)
mihaidd 0:a9b4ee4ed395 101 * @param pullup_config 100k pullup value (1 = enabled, 0 = disabled)
mihaidd 0:a9b4ee4ed395 102 * @param polarity_config polarity value (1 = flip, 0 = normal)
mihaidd 0:a9b4ee4ed395 103 */
mihaidd 0:a9b4ee4ed395 104 void config(unsigned short dir_config, unsigned short pullup_config, unsigned short polarity_config);
mihaidd 0:a9b4ee4ed395 105
mihaidd 0:a9b4ee4ed395 106 void writeRegister(int regAddress, unsigned char val);
mihaidd 0:a9b4ee4ed395 107 void writeRegister(int regAddress, unsigned short val);
mihaidd 0:a9b4ee4ed395 108 int readRegister(int regAddress);
mihaidd 0:a9b4ee4ed395 109
mihaidd 0:a9b4ee4ed395 110 /*-----------------------------------------------------------------------------
mihaidd 0:a9b4ee4ed395 111 * pinmode
mihaidd 0:a9b4ee4ed395 112 * Set units to sequential, bank0 mode
mihaidd 0:a9b4ee4ed395 113 */
mihaidd 0:a9b4ee4ed395 114 void pinMode(int pin, int mode);
mihaidd 0:a9b4ee4ed395 115 void digitalWrite(int pin, int val);
mihaidd 0:a9b4ee4ed395 116 int digitalRead(int pin);
mihaidd 0:a9b4ee4ed395 117
mihaidd 0:a9b4ee4ed395 118 // These provide a more advanced mapping of the chip functionality
mihaidd 0:a9b4ee4ed395 119 // See the data sheet for more information on what they do
mihaidd 0:a9b4ee4ed395 120
mihaidd 0:a9b4ee4ed395 121 //Returns a word with the current pin states (ie contents of the GPIO register)
mihaidd 0:a9b4ee4ed395 122 unsigned short digitalWordRead();
mihaidd 0:a9b4ee4ed395 123 // Allows you to write a word to the GPIO register
mihaidd 0:a9b4ee4ed395 124 void digitalWordWrite(unsigned short w);
mihaidd 0:a9b4ee4ed395 125 // Sets up the polarity mask that the MCP23017 supports
mihaidd 0:a9b4ee4ed395 126 // if set to 1, it will flip the actual pin value.
mihaidd 0:a9b4ee4ed395 127 void inputPolarityMask(unsigned short mask);
mihaidd 0:a9b4ee4ed395 128 //Sets which pins are inputs or outputs (1 = input, 0 = output) NB Opposite to arduino's
mihaidd 0:a9b4ee4ed395 129 //definition for these
mihaidd 0:a9b4ee4ed395 130 void inputOutputMask(unsigned short mask);
mihaidd 0:a9b4ee4ed395 131 // Allows enabling of the internal 100k pullup resisters (1 = enabled, 0 = disabled)
mihaidd 0:a9b4ee4ed395 132 void internalPullupMask(unsigned short mask);
mihaidd 0:a9b4ee4ed395 133 int read(void);
mihaidd 0:a9b4ee4ed395 134 void write(int data);
mihaidd 0:a9b4ee4ed395 135
mihaidd 0:a9b4ee4ed395 136 protected:
mihaidd 0:a9b4ee4ed395 137 I2C _i2c;
mihaidd 0:a9b4ee4ed395 138 int MCP23017_i2cAddress; // physical I2C address
mihaidd 0:a9b4ee4ed395 139 unsigned short shadow_GPIO, shadow_IODIR, shadow_GPPU, shadow_IPOL; // Cached copies of the register values
mihaidd 0:a9b4ee4ed395 140
mihaidd 0:a9b4ee4ed395 141 };
mihaidd 0:a9b4ee4ed395 142
mihaidd 0:a9b4ee4ed395 143 #endif