Knud Dalgaard / 310-TMC3-TestHW

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MCP23008.cpp Source File

MCP23008.cpp

00001 /*  MCP23017 library for Arduino
00002     Copyright (C) 2009 David Pye    <davidmpye@gmail.com
00003     Modified for use on the MBED ARM platform
00004 
00005     This program is free software: you can redistribute it and/or modify
00006     it under the terms of the GNU General Public License as published by
00007     the Free Software Foundation, either version 3 of the License, or
00008     (at your option) any later version.
00009 
00010     This program is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013     GNU General Public License for more details.
00014 
00015     You should have received a copy of the GNU General Public License
00016     along with this program.  If not, see <http://www.gnu.org/licenses/>.
00017 */
00018 
00019 #include "MCP23008.h"
00020 #include "mbed.h"
00021 
00022 //===============================================================================================
00023 // Init and setup functions
00024 //===============================================================================================
00025 
00026 /*-----------------------------------------------------------------------------
00027  *
00028  */
00029 MCP23008::MCP23008(PinName sda, PinName scl, int i2cAddress)  : _i2c(sda, scl)
00030 {
00031     MCP23008_i2cAddress = i2cAddress;
00032     reset();                                  // initialise chip to power-on condition
00033 }
00034 
00035 /*-----------------------------------------------------------------------------
00036  * reset
00037  * Set configuration (IOCON) and direction(IODIR) registers to initial state
00038  */
00039 void MCP23008::reset()
00040 {
00041     //
00042     // set direction registers to inputs
00043     //
00044     writeRegister(IODIR, (unsigned char)0xFF);
00045     //
00046     // set all other registers to zero (last of 10 registers is OLAT)
00047     //
00048     for (int reg_addr = IPOL ; reg_addr <= OLAT ; reg_addr++) {
00049         writeRegister(reg_addr, (unsigned char)0x00);
00050     }
00051     
00052     //
00053     // Set the shadow registers to power-on state
00054     //
00055     shadow_IODIR = 0xFF;
00056     shadow_GPIO  = 0;
00057     shadow_GPPU  = 0;
00058     shadow_IPOL  = 0;
00059 }
00060 
00061 
00062 
00063 /*-----------------------------------------------------------------------------
00064  * Config
00065  * set direction and pull-up registers
00066  */
00067 void MCP23008::config(unsigned char dir_config, unsigned char pullup_config,  unsigned char polarity_config)
00068 {
00069     shadow_IODIR = dir_config;
00070     writeRegister(IODIR, (unsigned char)shadow_IODIR);
00071     shadow_GPPU = pullup_config;
00072     writeRegister(GPPU, (unsigned char)shadow_GPPU);
00073     shadow_IPOL = polarity_config;
00074     writeRegister(IPOL, (unsigned char)shadow_IPOL);
00075 }
00076 
00077 
00078 
00079 /*-----------------------------------------------------------------------------
00080  * writeRegister
00081  * write a byte
00082  */
00083 void MCP23008::writeRegister(int regAddress, unsigned char data)
00084 {
00085     char  buffer[2];
00086 
00087     buffer[0] = regAddress;
00088     buffer[1] = data;
00089     _i2c.write(MCP23008_i2cAddress, buffer, 2);
00090 }
00091 
00092 
00093 
00094 /*-----------------------------------------------------------------------------
00095  * readRegister
00096  */
00097 unsigned char MCP23008::readRegister(int regAddress)
00098 {
00099     char buffer[1];
00100 
00101     buffer[0] = regAddress;
00102     _i2c.write(MCP23008_i2cAddress, buffer, 1);
00103     _i2c.read(MCP23008_i2cAddress, buffer, 1);
00104 
00105     return ( (unsigned char)buffer[0] );
00106 }
00107 
00108 
00109 //===============================================================================================
00110 // User functions
00111 //===============================================================================================
00112 
00113 /*-----------------------------------------------------------------------------
00114  * Write a combination of bits to the 8-bit port
00115  */
00116 void MCP23008::write_mask(unsigned char data, unsigned char mask)
00117 {
00118     shadow_GPIO = (shadow_GPIO & ~mask) | data;
00119     writeRegister(GPIO, (unsigned char)shadow_GPIO);
00120 }
00121 
00122 /*-----------------------------------------------------------------------------
00123  * read_mask
00124  */
00125 int  MCP23008::read_mask(unsigned char mask)
00126 {
00127     shadow_GPIO = readRegister(GPIO);
00128     return (shadow_GPIO & mask);
00129 }
00130 
00131 
00132 /*-----------------------------------------------------------------------------
00133  * write_bit
00134  * Write a 1/0 to a single bit of the 16-bit port
00135  */
00136 void MCP23008::write_bit(int value, int bit_number) {
00137     if (value == 0) {
00138         shadow_GPIO &= ~(1 << bit_number);
00139     } else {
00140         shadow_GPIO |= 1 << bit_number;
00141     }
00142     writeRegister(GPIO, (unsigned char)shadow_GPIO);
00143 }
00144 
00145 
00146 /*-----------------------------------------------------------------------------
00147  * read_bit
00148  * Read a single bit from the 16-bit port
00149  */
00150 int  MCP23008::read_bit(int bit_number) {
00151     shadow_GPIO = readRegister(GPIO);
00152     return  ((shadow_GPIO >> bit_number) & 0x01);
00153 }
00154