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.
Fork of MCP23S17 by
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) : _ncs(ncs) { 00012 _spi = spi; 00013 _writeOpcode = writeOpcode; 00014 _readOpcode = _writeOpcode | 1; // low order bit = 1 for read 00015 _init(); 00016 } 00017 00018 char MCP23S17::_read(char address) { 00019 _ncs = 0; 00020 _spi->write(_readOpcode); 00021 _spi->write(address); 00022 char result = _spi->write(0); 00023 _ncs = 1; 00024 return result; 00025 } 00026 00027 char MCP23S17::_read(Port port, char address) { 00028 return _read(address + (char) port); 00029 } 00030 00031 void MCP23S17::_write(char address, char data) { 00032 _ncs = 0; 00033 _spi->write(_writeOpcode); 00034 _spi->write(address); 00035 _spi->write(data); 00036 _ncs = 1; 00037 } 00038 00039 void MCP23S17::_write(Port port, char address, char data) { 00040 _write(address + (char) port, data); 00041 } 00042 00043 void MCP23S17::_init() { 00044 _write(IOCON, (IOCON_BYTE_MODE | IOCON_HAEN )); // Hardware addressing on, operations toggle between A and B registers 00045 } 00046 00047 void MCP23S17::direction(Port port, char direction) { 00048 _write(port, IODIRA, direction); 00049 } 00050 00051 void MCP23S17::configurePullUps(Port port, char offOrOn) { 00052 _write(port, GPPUA, offOrOn); 00053 } 00054 00055 void MCP23S17::interruptEnable(Port port, char interruptsEnabledMask) { 00056 _write(port, GPINTENA, interruptsEnabledMask); 00057 } 00058 00059 void MCP23S17::mirrorInterrupts(bool mirror) { 00060 char iocon = _read(IOCON); 00061 if (mirror) { 00062 iocon = iocon | INTERRUPT_MIRROR_BIT; 00063 } else { 00064 iocon = iocon & ~INTERRUPT_MIRROR_BIT; 00065 } 00066 _write(IOCON, iocon); 00067 00068 } 00069 00070 void MCP23S17::interruptPolarity(Polarity polarity) { 00071 char iocon = _read(IOCON); 00072 if (polarity == ACTIVE_LOW) { 00073 iocon = iocon & ~INTERRUPT_POLARITY_BIT; 00074 } else { 00075 iocon = iocon | INTERRUPT_POLARITY_BIT; 00076 } 00077 _write(IOCON, iocon); 00078 } 00079 00080 void MCP23S17::defaultValue(Port port, char valuesToCompare) { 00081 _write(port, DEFVALA, valuesToCompare); 00082 } 00083 00084 void MCP23S17::interruptControl(Port port, char interruptContolBits) { 00085 _write(port, INTCONA, interruptContolBits); 00086 } 00087 00088 void MCP23S17::write(Port port, char byte) { 00089 _write(port, OLATA, byte); 00090 } 00091 00092 char MCP23S17::read(Port port) { 00093 return _read(port, GPIOA); 00094 } 00095
Generated on Sat Jul 16 2022 02:01:28 by
1.7.2
