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.
Dependencies: mbed
Revision 0:574e155464d5, committed 2015-01-28
- Comitter:
- sheldonfernandes2404
- Date:
- Wed Jan 28 04:08:50 2015 +0000
- Commit message:
- 1
Changed in this revision
diff -r 000000000000 -r 574e155464d5 MCP23S17.cpp
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/MCP23S17.cpp Wed Jan 28 04:08:50 2015 +0000
@@ -0,0 +1,94 @@
+/* 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.4
+*/
+
+#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;
+}
+
+char MCP23S17::_read(Port port, char address) {
+ return _read(address + (char) port);
+}
+
+void MCP23S17::_write(char address, char data) {
+ _ncs = 0;
+ _spi.write(_writeOpcode);
+ _spi.write(address);
+ _spi.write(data);
+ _ncs = 1;
+}
+
+void MCP23S17::_write(Port port, char address, char data) {
+ _write(address + (char) port, data);
+}
+
+void MCP23S17::_init() {
+ _write(IOCON, (IOCON_BYTE_MODE | IOCON_HAEN )); // Hardware addressing on, operations toggle between A and B registers
+}
+
+void MCP23S17::direction(Port port, char direction) {
+ _write(port, IODIRA, direction);
+}
+
+void MCP23S17::configurePullUps(Port port, char offOrOn) {
+ _write(port, GPPUA, offOrOn);
+}
+
+void MCP23S17::interruptEnable(Port port, char interruptsEnabledMask) {
+ _write(port, GPINTENA, interruptsEnabledMask);
+}
+
+void MCP23S17::mirrorInterrupts(bool mirror) {
+ char iocon = _read(IOCON);
+ if (mirror) {
+ iocon = iocon | INTERRUPT_MIRROR_BIT;
+ } else {
+ iocon = iocon & ~INTERRUPT_MIRROR_BIT;
+ }
+ _write(IOCON, iocon);
+
+}
+
+void MCP23S17::interruptPolarity(Polarity polarity) {
+ char iocon = _read(IOCON);
+ if (polarity == ACTIVE_LOW) {
+ iocon = iocon & ~INTERRUPT_POLARITY_BIT;
+ } else {
+ iocon = iocon | INTERRUPT_POLARITY_BIT;
+ }
+ _write(IOCON, iocon);
+}
+
+void MCP23S17::defaultValue(Port port, char valuesToCompare) {
+ _write(port, DEFVALA, valuesToCompare);
+}
+
+void MCP23S17::interruptControl(Port port, char interruptContolBits) {
+ _write(port, INTCONA, interruptContolBits);
+}
+
+void MCP23S17::write(Port port, char byte) {
+ _write(port, OLATA, byte);
+}
+
+char MCP23S17::read(Port port) {
+ return _read(port, GPIOA);
+}
+
diff -r 000000000000 -r 574e155464d5 MCP23S17.h
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/MCP23S17.h Wed Jan 28 04:08:50 2015 +0000
@@ -0,0 +1,59 @@
+/* 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.4
+*/
+#include "mbed.h"
+
+#ifndef MCP23S17_H
+#define MCP23S17_H
+
+#define INTERRUPT_POLARITY_BIT 0x02
+#define INTERRUPT_MIRROR_BIT 0x40
+
+// all register addresses assume IOCON.BANK = 0 (POR default)
+
+#define IODIRA 0x00
+#define GPINTENA 0x04
+#define DEFVALA 0x06
+#define INTCONA 0x08
+#define IOCON 0x0A
+#define GPPUA 0x0C
+#define GPIOA 0x12
+#define OLATA 0x14
+
+// 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
+
+enum Polarity { ACTIVE_LOW , ACTIVE_HIGH };
+enum Port { PORT_A, PORT_B };
+
+class MCP23S17 {
+public:
+ MCP23S17(SPI& spi, PinName ncs, char writeOpcode);
+ void direction(Port port, char direction);
+ void configurePullUps(Port port, char offOrOn);
+ void interruptEnable(Port port, char interruptsEnabledMask);
+ void interruptPolarity(Polarity polarity);
+ void mirrorInterrupts(bool mirror);
+ void defaultValue(Port port, char valuesToCompare);
+ void interruptControl(Port port, char interruptContolBits);
+ char read(Port port);
+ void write(Port port, char byte);
+protected:
+ SPI& _spi;
+ DigitalOut _ncs;
+ void _init();
+ void _write(Port port, char address, char data);
+ void _write(char address, char data);
+ char _read(Port port, char address);
+ char _read(char address);
+ char _readOpcode;
+ char _writeOpcode;
+};
+
+#endif
\ No newline at end of file
diff -r 000000000000 -r 574e155464d5 main.cpp
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp Wed Jan 28 04:08:50 2015 +0000
@@ -0,0 +1,29 @@
+#include "mbed.h"
+#include "MCP23S17.h"
+
+//------------------------------//
+SPI spi(p5, p6, p7);
+char Opcode1 = 0x40;
+char Opcode2 = 0x42;
+MCP23S17 chip1 = MCP23S17(spi, p20, Opcode1);
+MCP23S17 chip2 = MCP23S17(spi, p20, Opcode2);
+//-------------------------------//
+
+int main()
+{
+
+ chip1.direction(PORT_A, 0x00); //output
+ chip2.direction(PORT_B, 0xFF); //input
+
+ while (1) {
+
+ if(chip2.read(PORT_B)) {
+ chip1.write(PORT_A, 0x00);
+ } else {
+ chip1.write(PORT_A, 0xAA);
+ }
+
+
+ }
+
+}
\ No newline at end of file
diff -r 000000000000 -r 574e155464d5 mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Wed Jan 28 04:08:50 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/4fc01daae5a5 \ No newline at end of file