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
Diff: MCP23S17.cpp
- Revision:
- 2:6144709f1700
- Child:
- 3:089a2a754567
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/MCP23S17.cpp Sat Aug 21 14:46:59 2010 +0000
@@ -0,0 +1,61 @@
+/* MCP23S17 - drive the Microchip MCP23S17 16-bit Port Extender using SPI
+* Copyright (c) 2010 Romilly Cocking
+* Released under the MIT License: http://mbed.org/license/mit
+*
+* version 0.1
+*/
+
+#include "mbed.h"
+#include "MCP23S17.h"
+
+MCP23S17::MCP23S17(SPI& spi, PinName ncs, char writeOpcode) : _spi(spi), _ncs(ncs) {
+ _writeOpcode = writeOpcode;
+ _readOpcode = _writeOpcode | 1; // low order bit = 1 for read
+ _init();
+}
+
+char MCP23S17::_read(char address) {
+ _ncs = 0;
+ _spi.write(_readOpcode);
+ _spi.write(address);
+ char result = _spi.write(0);
+ _ncs = 1;
+ return result;
+}
+
+void MCP23S17::_write(char address, char data) {
+ _ncs = 0;
+ _spi.write(_writeOpcode);
+ _spi.write(address);
+ _spi.write(data);
+ _ncs = 1;
+}
+
+void MCP23S17::_init() {
+ _write(IOCON, (IOCON_BYTE_MODE | IOCON_HAEN )); // Hardware addressing on, operations toggle between A and B registers
+}
+
+void MCP23S17::directionA(char direction) {
+ _write(IODIRA, direction);
+}
+
+void MCP23S17::directionB(char direction) {
+ _write(IODIRB, direction);
+}
+
+void MCP23S17::outputA(char byte) {
+ _write(OLATA, byte);
+}
+
+void MCP23S17::outputB(char byte) {
+ _write(OLATB, byte);
+}
+
+char MCP23S17::inputA() {
+ return _read(GPIOA);
+}
+
+char MCP23S17::inputB() {
+ return _read(GPIOB);
+}
+
