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
Revision 2:6144709f1700, committed 2010-08-21
- Comitter:
- romilly
- Date:
- Sat Aug 21 14:46:59 2010 +0000
- Parent:
- 1:5abd129839e7
- Child:
- 3:089a2a754567
- Commit message:
Changed in this revision
--- /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);
+}
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/MCP23S17.h Sat Aug 21 14:46:59 2010 +0000
@@ -0,0 +1,47 @@
+/* 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"
+
+#ifndef SER23K256_H
+#define SER23K256_H
+
+// all register addresses assume IOVCON.BANK = 0 (POR default)
+
+#define IODIRA 0x00
+#define IODIRB 0x01
+#define IOCON 0x0A
+#define GPIOA 0x12
+#define GPIOB 0x13
+#define OLATA 0x14
+#define OLATB 0x15
+
+// Control settings
+
+#define IOCON_BANK 0x80 // Banked registers
+#define IOCON_BYTE_MODE 0x20 // Disables sequential operation. If bank = 0, operations toggle between A and B registers
+#define IOCON_HAEN 0x08 // Hardware address enable
+
+class MCP23S17 {
+public:
+ MCP23S17(SPI& spi, PinName ncs, char writeOpcode);
+ void directionA(char direction);
+ void directionB(char direction);
+ char inputA();
+ char inputB();
+ void outputA(char byte);
+ void outputB(char byte);
+protected:
+ SPI& _spi;
+ DigitalOut _ncs;
+ void _init();
+ void _write(char address, char data);
+ char _read(char address);
+ char _readOpcode;
+ char _writeOpcode;
+};
+
+#endif
\ No newline at end of file
--- a/main.cpp Sat Aug 21 07:05:21 2010 +0000
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,88 +0,0 @@
-#include "mbed.h"
-
-/*
-* next baby step towards towards a library for MCP23S17
-* I'm proposing to allow 8-bit and 16-bit conditioning, reads, and writes
-* and interrupt conditioning
-*
-* Unless someone asks for them, I'm not going to implement
-* multi-reads or writes where a sequence of many bytes are read from or written
-* to the same register
-*
-* This is currently experimental code. I'm still checking that I've understood the chip API.
-*/
-
-
-// all register addresses assume IOVCON.BANK = 0 (POR default)
-
-#define IODIRA 0x00
-#define IODIRB 0x01
-#define IOCON 0x0A
-#define GPIOA 0x12
-#define GPIOB 0x13
-#define OLATA 0x14
-#define OLATB 0x15
-
-// Control settings
-
-#define IOCON_BANK 0x80 // Banked registers
-#define IOCON_BYTE_MODE 0x20 // Disables sequential operation. If bank = 0, operations toggle between A and B registers
-#define IOCON_HAEN 0x08 // Hardware address enable
-
-SPI spi(p5, p6, p7);
-DigitalOut ncs(p20); // not chip select; bring this low to enable the chip
-char writeOpcode = 0x40; // A0, A1, A2 are tied to ground on the breadboard.
-char readOpcode = writeOpcode | 1; // low order bit = 1 for read
-
-void _write(char address, char data) {
- ncs = 0;
- spi.write(writeOpcode);
- spi.write(address);
- spi.write(data);
- ncs = 1;
-}
-
-char _read(char address) {
- ncs = 0;
- spi.write(readOpcode);
- spi.write(address);
- char result = spi.write(0);
- ncs = 1;
- return result;
-}
-
-void init() {
- _write(IOCON, (IOCON_BYTE_MODE | IOCON_HAEN )); // Hardware addressing on, operations toggle between A and B registers
-}
-
-void directionA(char direction) {
- _write(IODIRA, direction);
-}
-
-void directionB(char direction) {
- _write(IODIRB, direction);
-}
-
-void outputA(char byte) {
- _write(OLATA, byte);
-}
-
-void outputB(char byte) {
- _write(OLATB, byte);
-}
-
-char inputA() {
- return _read(GPIOA);
-}
-
-int main() {
- init();
- directionA(0xFF); // all 8 bits set to input
- directionB(0x00); // all 8 bits set to ouptut
- while(1) {
- wait(0.2);
- // copy inputs from A to outputs on B
- outputB(inputA());
-
- }
-}
