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.
MCP23S17.cpp
00001 /* MCP23S17 - drive the Microchip MCP23S17 16-bit Port Extender using SPI 00002 * Copyright (c) 2010 Romilly Cocking 00003 * Released under the MIT License: http://mbed.org/license/mit 00004 * 00005 * version 0.4 00006 */ 00007 00008 #include "mbed.h" 00009 #include "MCP23S17.h" 00010 00011 MCP23S17::MCP23S17(SPI& spi, PinName ncs, char writeOpcode) : _spi(spi), _ncs(ncs) { 00012 _writeOpcode = writeOpcode; 00013 _readOpcode = _writeOpcode | 1; // low order bit = 1 for read 00014 _init(); 00015 } 00016 00017 char MCP23S17::_read(char address) { 00018 _ncs = 0; 00019 _spi.write(_readOpcode); 00020 _spi.write(address); 00021 char result = _spi.write(0); 00022 _ncs = 1; 00023 return result; 00024 } 00025 00026 char MCP23S17::_read(Port port, char address) { 00027 return _read(address + (char) port); 00028 } 00029 00030 void MCP23S17::_write(char address, char data) { 00031 _ncs = 0; 00032 _spi.write(_writeOpcode); 00033 _spi.write(address); 00034 _spi.write(data); 00035 _ncs = 1; 00036 } 00037 00038 void MCP23S17::_write(Port port, char address, char data) { 00039 _write(address + (char) port, data); 00040 } 00041 00042 void MCP23S17::_init() { 00043 _write(IOCON, (IOCON_BYTE_MODE | IOCON_HAEN )); // Hardware addressing on, operations toggle between A and B registers 00044 } 00045 00046 void MCP23S17::direction(Port port, char direction) { 00047 _write(port, IODIRA, direction); 00048 } 00049 00050 void MCP23S17::configurePullUps(Port port, char offOrOn) { 00051 _write(port, GPPUA, offOrOn); 00052 } 00053 00054 void MCP23S17::interruptEnable(Port port, char interruptsEnabledMask) { 00055 _write(port, GPINTENA, interruptsEnabledMask); 00056 } 00057 00058 void MCP23S17::mirrorInterrupts(bool mirror) { 00059 char iocon = _read(IOCON); 00060 if (mirror) { 00061 iocon = iocon | INTERRUPT_MIRROR_BIT; 00062 } else { 00063 iocon = iocon & ~INTERRUPT_MIRROR_BIT; 00064 } 00065 _write(IOCON, iocon); 00066 00067 } 00068 00069 void MCP23S17::interruptPolarity(Polarity polarity) { 00070 char iocon = _read(IOCON); 00071 if (polarity == ACTIVE_LOW) { 00072 iocon = iocon & ~INTERRUPT_POLARITY_BIT; 00073 } else { 00074 iocon = iocon | INTERRUPT_POLARITY_BIT; 00075 } 00076 _write(IOCON, iocon); 00077 } 00078 00079 void MCP23S17::defaultValue(Port port, char valuesToCompare) { 00080 _write(port, DEFVALA, valuesToCompare); 00081 } 00082 00083 void MCP23S17::interruptControl(Port port, char interruptContolBits) { 00084 _write(port, INTCONA, interruptContolBits); 00085 } 00086 00087 void MCP23S17::write(Port port, char byte) { 00088 _write(port, OLATA, byte); 00089 } 00090 00091 char MCP23S17::read(Port port) { 00092 return _read(port, GPIOAA); 00093 } 00094
Generated on Sun Jul 24 2022 13:03:31 by
1.7.2