Simple MCP23017 I2C 16bit IO expander IC
Embed:
(wiki syntax)
Show/hide line numbers
MCP23017.cpp
00001 /* MCP23017 Library for mbed 00002 * Copyright (c) 2014, Takuya Urakawa 00003 * 00004 * This library is released under the MIT License 00005 * See http://opensource.org/licenses/mit-license.php 00006 */ 00007 00008 #include "MCP23017.h" 00009 00010 namespace MCP23017 { 00011 00012 MCP23017::MCP23017(I2C &_i2c, char _addr): 00013 mI2c(_i2c) 00014 { 00015 mWriteAddr = _addr & 0xFE; 00016 mReadAddr = _addr | 0x01; 00017 } 00018 00019 void MCP23017::init(void){ 00020 00021 // init config 00022 i2cSend(REG_IOCON, 0); 00023 00024 // port 0 00025 i2cSend(REG_GPIO, 0, 0); 00026 00027 // port dirrection all input 00028 i2cSend(REG_IODIR, 0, 0); 00029 00030 // interupt off 00031 i2cSend(REG_GPINTEN, 0, 0); 00032 00033 // clear interrupt 00034 read(MCP23017_PORTA); 00035 read(MCP23017_PORTB); 00036 } 00037 00038 void MCP23017::setConfig(char _value){ 00039 00040 i2cSend(REG_IOCON, _value); 00041 } 00042 00043 void MCP23017::setDirrection(char _port, char _value){ 00044 00045 i2cSend(REG_IODIR + _port, _value); 00046 } 00047 00048 void MCP23017::setPullUp(char _port, char _value){ 00049 00050 i2cSend(REG_GPPU + _port, _value); 00051 } 00052 00053 void MCP23017::setInputPolarity(char _port, char _value){ 00054 00055 i2cSend(REG_IPOL + _port, _value); 00056 } 00057 00058 00059 void MCP23017::write(char _port, char _value){ 00060 00061 i2cSend(REG_GPIO + _port, _value); 00062 } 00063 00064 char MCP23017::read(char _port){ 00065 00066 return i2cRead(REG_GPIO + _port); 00067 } 00068 00069 //void MCP23017::setInterruptConfig(char _port, char _intcon, char _defval){ 00070 // 00071 // i2cSend(REG_INTCON + _port, _intcon); 00072 // i2cSend(REG_DEFVAL + _port, _defval); 00073 00074 //} 00075 00076 //void MCP23017::interrupt(char _port, char _value){ 00077 00078 // i2cSend(REG_GPINTEN + _port, _value); 00079 // //Read from GPIO to clear interrupt flag 00080 // read(_port); 00081 //} 00082 00083 //char MCP23017::readIntcap(char _port){ 00084 00085 // return i2cRead(REG_INTCAP + _port); 00086 //} 00087 00088 } // end of namespace 00089
Generated on Wed Jul 13 2022 12:03:45 by 1.7.2