Simple MCP23017 I2C 16bit IO expander IC

Files at this revision

API Documentation at this revision

Comitter:
hsgw
Date:
Mon Jun 02 05:38:36 2014 +0000
Commit message:
1st commit

Changed in this revision

MCP23017.cpp Show annotated file Show diff for this revision Revisions of this file
MCP23017.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MCP23017.cpp	Mon Jun 02 05:38:36 2014 +0000
@@ -0,0 +1,89 @@
+/* MCP23017 Library for mbed
+ * Copyright (c) 2014, Takuya Urakawa
+ * 
+ * This library is released under the MIT License
+ * See http://opensource.org/licenses/mit-license.php
+ */
+
+#include "MCP23017.h"
+
+namespace MCP23017 {
+
+MCP23017::MCP23017(I2C &_i2c, char _addr): 
+	mI2c(_i2c)
+{
+	mWriteAddr = _addr & 0xFE;
+	mReadAddr = _addr | 0x01;
+}
+
+void MCP23017::init(void){
+	
+	// init config
+	i2cSend(REG_IOCON, 0);
+
+	// port 0
+	i2cSend(REG_GPIO, 0, 0);
+
+	// port dirrection all input
+	i2cSend(REG_IODIR, 0, 0);
+
+	// interupt off
+	i2cSend(REG_GPINTEN, 0, 0);
+
+	// clear interrupt
+	read(MCP23017_PORTA);
+	read(MCP23017_PORTB);
+}
+
+void MCP23017::setConfig(char _value){
+	
+	i2cSend(REG_IOCON, _value);
+}
+
+void MCP23017::setDirrection(char _port, char _value){
+	
+	i2cSend(REG_IODIR + _port, _value);
+}
+
+void MCP23017::setPullUp(char _port, char _value){
+	
+	i2cSend(REG_GPPU + _port, _value);
+}
+
+void MCP23017::setInputPolarity(char _port, char _value){
+	
+	i2cSend(REG_IPOL + _port, _value);
+}
+
+
+void MCP23017::write(char _port, char _value){
+	
+	i2cSend(REG_GPIO + _port, _value);
+}
+
+char MCP23017::read(char _port){
+	
+	return i2cRead(REG_GPIO + _port);
+}
+
+//void MCP23017::setInterruptConfig(char _port, char _intcon, char _defval){
+//	
+//	i2cSend(REG_INTCON + _port, _intcon);
+//	i2cSend(REG_DEFVAL + _port, _defval);
+
+//}
+
+//void MCP23017::interrupt(char _port, char _value){
+
+//	i2cSend(REG_GPINTEN + _port, _value);
+//	//Read from GPIO to clear interrupt flag
+//	read(_port);
+//}
+
+//char MCP23017::readIntcap(char _port){
+
+//	return i2cRead(REG_INTCAP + _port);
+//}
+
+} // end of namespace
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MCP23017.h	Mon Jun 02 05:38:36 2014 +0000
@@ -0,0 +1,204 @@
+/* MCP23017 Library for mbed
+ * Copyright (c) 2014, Takuya Urakawa
+ * 
+ * This library is released under the MIT License
+ * See http://opensource.org/licenses/mit-license.php
+ */
+
+#ifndef _MCP23017_H_
+#define _MCP23017_H_
+
+#include "mbed.h"
+
+/** \def usefull defines */
+#define MCP23017_DEFAULT_ADDR 0x40
+#define MCP23017_PORTA 0x00
+#define MCP23017_PORTB 0x01
+
+namespace MCP23017 {	
+	
+/** MCP23017 class
+ *
+ *	Allow access MCP23017 I2C 16bit io expander IC
+ *  But this library can not controll interrupt functions
+ *
+ *  Example:
+ *  @code
+ *  #include "mbed.h"
+ *  #include "MCP23017.h"
+ *
+ *  I2C i2c(dp5, dp27); // LPC1114
+ *  MCP23017::MCP23017 io(&i2c,MCP23017_DEFAULT_ADDR);
+ *
+ *  int main(void)
+ *  {	
+ *	  i2c.frequency(400000);
+ *	
+ *	  // software reset
+ *	  io.init();
+ *	
+ *	  io.setDirrection(MCP23017_PORTA, 0xFF);    // set all of the PORTA pins to input
+ *	  io.setDirrection(MCP23017_PORTB, 0x00);    // set all of the PORTB pins to output
+ *	  io.setPullUp(MCP23017_PORTA, 0xFF);        // activate all of the PORTA pin pull-ups
+ *	  io.setInputPolarity(MCP23017_PORTA, 0xFF); // invert all of the PORTA pins input polarity
+ *
+ *    while(true){
+ *	  	// write PORTA to PORTB
+ *  	io.write(1,io.read(0));
+ *	  }
+ *
+ *	  return 0;
+ *  }
+ *  @endcode
+ */
+class MCP23017{
+public:
+	
+	/** Constructor
+	 *
+	 *  @param _i2c   pointer to I2C class
+	 *  @param _addr  i2c device address
+	 */
+    MCP23017(I2C &_i2c, char _addr);
+
+	/** Software Reset
+	 */
+	void init(void);
+
+	/** Configure MCP23017 
+	 *  Write IOCON register
+	 *  See MCP23017 datasheet p18
+	 *  http://ww1.microchip.com/downloads/jp/DeviceDoc/21952b.pdf
+	 *  
+	 *  @attention BANK bit must set 0
+	 *  
+	 *  @param _value  write byte  
+	 */
+	void setConfig(char _value);
+
+	/** Set I/O dirrection
+	 *  
+	 *  @param _port   port(MCP23017_PORTA or MCP23017_PORTB)
+	 *  @param _value  write byte(0=OUTPUT, 1=INPUT)
+	 */
+	void setDirrection(char _port, char _value);
+	
+	/** Set pullup
+	 *  
+	 *  @param _port   port (MCP23017_PORTA or MCP23017_PORTB)
+	 *  @param _value  write byte (0=Disable, 1=Enable)
+	 */
+	void setPullUp(char _port, char _value);
+	
+	/** Set Input Polarity
+	 *  
+	 *  @param _port   port(MCP23017_PORTA or MCP23017_PORTB)
+	 *  @param _value  write byte (0=normal, 1=inverted)
+	 */
+	void setInputPolarity(char _port, char _value);
+	
+	/** Write GPIO
+	 *
+	 *  @param _port   port(MCP23017_PORTA or MCP23017_PORTB)
+	 *  @param _value  write byte 
+	 */
+	void write(char _port, char _value);
+	
+	/** Read GPIO
+	 *
+	 *  @param _port   port(MCP23017_PORTA or MCP23017_PORTB)
+	 *  @return        byte
+	 */
+	char read(char _port);
+	
+	
+	// void setInterruptConfig(char _port, char _intcon, char _defval);
+	// void interrupt(char _port, char _value);
+	// char readIntcap(char _port);
+
+
+private:
+    I2C  &mI2c;
+	char mWriteAddr;
+	char mReadAddr;
+
+
+	inline void i2cSend(char _command, char _data1, char _data2){
+		char commands[3];
+		commands[0] = _command;
+		commands[1] = _data1;
+		commands[2] = _data2;
+
+		mI2c.write(mWriteAddr, commands, 3);
+	}
+	
+	inline void i2cSend(char _command, char _data1){
+		char commands[2];
+		commands[0] = _command;
+		commands[1] = _data1;
+		
+		mI2c.write(mWriteAddr, commands, 2);
+	}
+	
+	inline char i2cRead(char _command){
+		char commands[2];
+		commands[0] = _command;
+		mI2c.write(mWriteAddr, commands, 1);
+		//return (char)mI2c->read(0);
+		mI2c.read(mReadAddr,commands,1);
+		return commands[0];
+	}
+	
+	
+	// registors
+	enum MCP23017_REG {
+		// IO Dirrection
+		REG_IODIR = 0x00,
+
+		// Input polarity
+		REG_IPOL = 0x02,
+
+		// Interrupt-On-Change enable
+		REG_GPINTEN = 0x04,
+
+		// Default intterupt comparison value
+		// Refer to INTCON
+		REG_DEFVAL = 0x06,
+
+		// Intterupt compare value
+		// 1 = compare against DEFVAL value
+		// 0 = compare against the previous value
+		REG_INTCON = 0x08,
+
+		// IC setting
+		// bank/mirror/seqop/disslw/haen/odr/intpol/notimp
+		// must be set bank = 0 for using this library
+		// See MCP23017 datasheet
+		REG_IOCON = 0x0A,
+
+		// Pullup
+		REG_GPPU = 0x0C,
+
+		// Interrupt Flag
+		// 1 = pin caused interrupt
+		// 0 = no interrupt 
+		// Read-Only
+		REG_INTF = 0x0E,
+
+		// Interrupt capture
+		// value of GPIO at time of last interrupt
+		// Read-Only
+		REG_INTCAP = 0x10,
+
+		// GPIO
+		REG_GPIO = 0x12,
+
+		// Output Latch
+		REG_OLAT = 0x14
+	};
+	
+}; // end of class
+}; // end of namespace
+
+#endif
+