Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: relaekort_til_motorstyring Nucleo_MCP23S17_Test BSM02 POT_V_1_1
MCP23S17.cpp
00001 /* mbed MCP23S17 Library, for driving the MCP23S17 16-Bit I/O Expander with Serial Interface (SPI) 00002 * Copyright (c) 2015, Created by Steen Joergensen (stjo2809) inspired by Romilly Cocking MCP23S17 library 00003 * 00004 * Permission is hereby granted, free of charge, to any person obtaining a copy 00005 * of this software and associated documentation files (the "Software"), to deal 00006 * in the Software without restriction, including without limitation the rights 00007 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00008 * copies of the Software, and to permit persons to whom the Software is 00009 * furnished to do so, subject to the following conditions: 00010 * 00011 * The above copyright notice and this permission notice shall be included in 00012 * all copies or substantial portions of the Software. 00013 * 00014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00015 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00016 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00017 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00018 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00019 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 00020 * THE SOFTWARE. 00021 */ 00022 00023 #include "mbed.h" 00024 #include "MCP23S17.h" 00025 00026 //============================================================================= 00027 // Public functions 00028 //============================================================================= 00029 00030 MCP23S17::MCP23S17(int hardwareaddress, SPI& spi, PinName nCs, PinName nReset) : _hardwareaddress(hardwareaddress), _spi(spi), _nCs(nCs), _nReset(nReset) 00031 { 00032 _make_opcode(_hardwareaddress); 00033 _initialization(); 00034 } 00035 00036 MCP23S17::MCP23S17(int hardwareaddress, SPI& spi, PinName nCs) : _hardwareaddress(hardwareaddress), _spi(spi), _nCs(nCs), _nReset(NC) // _nReset(NC) added for the compiler 00037 { 00038 _make_opcode(_hardwareaddress); 00039 _initialization(); 00040 } 00041 00042 char MCP23S17::read(char reg_address) 00043 { 00044 return _read(reg_address); 00045 } 00046 00047 void MCP23S17::write(char reg_address, char data) 00048 { 00049 _write(reg_address, data); 00050 } 00051 00052 void MCP23S17::bit(char reg_address, int bitnumber, bool high_low) 00053 { 00054 char i; 00055 00056 if(bitnumber >= 1 || bitnumber <= 8) 00057 { 00058 if(high_low == 1) 00059 { 00060 i = _read(reg_address); 00061 i = i | (0x01 << (bitnumber-1)); 00062 _write(reg_address, i); 00063 } 00064 if(high_low == 0) 00065 { 00066 i = _read(reg_address); 00067 i = i & ~(0x01 << (bitnumber-1)); 00068 _write(reg_address, i); 00069 } 00070 } 00071 } 00072 00073 void MCP23S17::reset() 00074 { 00075 _nReset = 0; 00076 wait_us(5); 00077 _nReset = 1; 00078 _initialization(); 00079 } 00080 00081 char MCP23S17::iodira() 00082 { 00083 return _read(IODIRA_ADDR); 00084 } 00085 00086 void MCP23S17::iodira(char data) 00087 { 00088 _write(IODIRA_ADDR, data); 00089 } 00090 00091 char MCP23S17::iodirb() 00092 { 00093 return _read(IODIRB_ADDR); 00094 } 00095 00096 void MCP23S17::iodirb(char data) 00097 { 00098 _write(IODIRB_ADDR, data); 00099 } 00100 00101 char MCP23S17::ipola() 00102 { 00103 return _read(IPOLA_ADDR); 00104 } 00105 00106 void MCP23S17::ipola(char data) 00107 { 00108 _write(IPOLA_ADDR, data); 00109 } 00110 00111 char MCP23S17::ipolb() 00112 { 00113 return _read(IPOLB_ADDR); 00114 } 00115 00116 void MCP23S17::ipolb(char data) 00117 { 00118 _write(IPOLB_ADDR, data); 00119 } 00120 00121 char MCP23S17::gpintena() 00122 { 00123 return _read(GPINTENA_ADDR); 00124 } 00125 00126 void MCP23S17::gpintena(char data) 00127 { 00128 _write(GPINTENA_ADDR, data); 00129 } 00130 00131 char MCP23S17::gpintenb() 00132 { 00133 return _read(GPINTENB_ADDR); 00134 } 00135 00136 void MCP23S17::gpintenb(char data) 00137 { 00138 _write(GPINTENB_ADDR, data); 00139 } 00140 00141 char MCP23S17::defvala() 00142 { 00143 return _read(DEFVALA_ADDR); 00144 } 00145 00146 void MCP23S17::defvala(char data) 00147 { 00148 _write(DEFVALA_ADDR, data); 00149 } 00150 00151 char MCP23S17::defvalb() 00152 { 00153 return _read(DEFVALB_ADDR); 00154 } 00155 00156 void MCP23S17::defvalb(char data) 00157 { 00158 _write(DEFVALB_ADDR, data); 00159 } 00160 00161 char MCP23S17::intcona() 00162 { 00163 return _read(INTCONA_ADDR); 00164 } 00165 00166 void MCP23S17::intcona(char data) 00167 { 00168 _write(INTCONA_ADDR, data); 00169 } 00170 00171 char MCP23S17::intconb() 00172 { 00173 return _read(INTCONB_ADDR); 00174 } 00175 00176 void MCP23S17::intconb(char data) 00177 { 00178 _write(INTCONB_ADDR, data); 00179 } 00180 00181 char MCP23S17::iocon() 00182 { 00183 return _read(IOCON_ADDR); 00184 } 00185 00186 void MCP23S17::iocon(char data) 00187 { 00188 _write(IOCON_ADDR, data); 00189 } 00190 00191 char MCP23S17::gppua() 00192 { 00193 return _read(GPPUA_ADDR); 00194 } 00195 00196 void MCP23S17::gppua(char data) 00197 { 00198 _write(GPPUA_ADDR, data); 00199 } 00200 00201 char MCP23S17::gppub() 00202 { 00203 return _read(GPPUB_ADDR); 00204 } 00205 00206 void MCP23S17::gppub(char data) 00207 { 00208 _write(GPPUB_ADDR, data); 00209 } 00210 00211 char MCP23S17::intfa() 00212 { 00213 return _read(INTFA_ADDR); 00214 } 00215 00216 char MCP23S17::intfb() 00217 { 00218 return _read(INTFB_ADDR); 00219 } 00220 00221 char MCP23S17::intcapa() 00222 { 00223 return _read(INTCAPA_ADDR); 00224 } 00225 00226 char MCP23S17::intcapb() 00227 { 00228 return _read(INTCAPB_ADDR); 00229 } 00230 00231 char MCP23S17::gpioa() 00232 { 00233 return _read(GPIOA_ADDR); 00234 } 00235 00236 void MCP23S17::gpioa(char data) 00237 { 00238 _write(GPIOA_ADDR, data); 00239 } 00240 00241 char MCP23S17::gpiob() 00242 { 00243 return _read(GPIOB_ADDR); 00244 } 00245 00246 void MCP23S17::gpiob(char data) 00247 { 00248 _write(GPIOB_ADDR, data); 00249 } 00250 00251 char MCP23S17::olata() 00252 { 00253 return _read(OLATA_ADDR); 00254 } 00255 00256 void MCP23S17::olata(char data) 00257 { 00258 _write(OLATA_ADDR, data); 00259 } 00260 00261 char MCP23S17::olatb() 00262 { 00263 return _read(OLATB_ADDR); 00264 } 00265 00266 void MCP23S17::olatb(char data) 00267 { 00268 _write(OLATB_ADDR, data); 00269 } 00270 00271 void MCP23S17::intmirror(bool mirror) 00272 { 00273 char kopi_iocon = _read(IOCON_ADDR); 00274 if (mirror) 00275 { 00276 kopi_iocon = kopi_iocon | INTERRUPT_MIRROR_BIT; 00277 } 00278 else 00279 { 00280 kopi_iocon = kopi_iocon & ~INTERRUPT_MIRROR_BIT; 00281 } 00282 _write(IOCON_ADDR, kopi_iocon); 00283 } 00284 00285 void MCP23S17::intpol(bool polarity) 00286 { 00287 char kopi_iocon = _read(IOCON_ADDR); 00288 if (polarity == false) 00289 { 00290 kopi_iocon = kopi_iocon | INTERRUPT_POLARITY_BIT; 00291 } 00292 else 00293 { 00294 kopi_iocon = kopi_iocon & ~INTERRUPT_POLARITY_BIT; 00295 } 00296 _write(IOCON_ADDR, kopi_iocon); 00297 } 00298 00299 //============================================================================= 00300 // Private functions 00301 //============================================================================= 00302 00303 void MCP23S17::_initialization() 00304 { 00305 _write(IOCON_ADDR, 0x2A); // setup af control register (BANK = 0, MIRROR = 0, SEQOP = 1, DISSLW = 0, HAEN = 1, ODR = 0, INTPOL = 1, NC = 0) 00306 _nCs = 1; 00307 } 00308 00309 void MCP23S17::_make_opcode(int _hardwareaddress) 00310 { 00311 switch(_hardwareaddress) 00312 { 00313 case 0: 00314 _writeopcode = 0x40; 00315 _readopcode = 0x41; 00316 break; 00317 00318 case 1: 00319 _writeopcode = 0x42; 00320 _readopcode = 0x43; 00321 break; 00322 00323 case 2: 00324 _writeopcode = 0x44; 00325 _readopcode = 0x45; 00326 break; 00327 00328 case 3: 00329 _writeopcode = 0x46; 00330 _readopcode = 0x47; 00331 break; 00332 00333 case 4: 00334 _writeopcode = 0x48; 00335 _readopcode = 0x49; 00336 break; 00337 00338 case 5: 00339 _writeopcode = 0x4A; 00340 _readopcode = 0x4B; 00341 break; 00342 00343 case 6: 00344 _writeopcode = 0x4C; 00345 _readopcode = 0x4D; 00346 break; 00347 00348 case 7: 00349 _writeopcode = 0x4E; 00350 _readopcode = 0x4F; 00351 break; 00352 } 00353 } 00354 00355 char MCP23S17::_read(char address) 00356 { 00357 _nCs = 0; 00358 _spi.write(_readopcode); 00359 _spi.write(address); 00360 char response = _spi.write(0xFF); // 0xFF data to get response 00361 _nCs = 1; 00362 return response; 00363 } 00364 00365 void MCP23S17::_write(char address, char data) 00366 { 00367 _nCs = 0; 00368 _spi.write(_writeopcode); 00369 _spi.write(address); 00370 _spi.write(data); 00371 _nCs = 1; 00372 } 00373
Generated on Tue Jul 12 2022 21:13:04 by
