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@9:068b1e8909bb, 2010-08-28 (annotated)
- Committer:
- romilly
- Date:
- Sat Aug 28 09:48:38 2010 +0000
- Revision:
- 9:068b1e8909bb
- Parent:
- 8:841b19734955
- Child:
- 10:d00c555d15b2
Added pull=up configuration
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| romilly | 7:53498e24592c | 1 | /* MCP23S17 - drive the Microchip MCP23S17 16-bit Port Extender using SPI |
| romilly | 7:53498e24592c | 2 | * Copyright (c) 2010 Romilly Cocking |
| romilly | 7:53498e24592c | 3 | * Released under the MIT License: http://mbed.org/license/mit |
| romilly | 7:53498e24592c | 4 | * |
| romilly | 9:068b1e8909bb | 5 | * version 0.4 |
| romilly | 7:53498e24592c | 6 | */ |
| romilly | 7:53498e24592c | 7 | |
| romilly | 7:53498e24592c | 8 | #include "mbed.h" |
| romilly | 7:53498e24592c | 9 | #include "MCP23S17.h" |
| romilly | 7:53498e24592c | 10 | |
| romilly | 7:53498e24592c | 11 | MCP23S17::MCP23S17(SPI& spi, PinName ncs, char writeOpcode) : _spi(spi), _ncs(ncs) { |
| romilly | 7:53498e24592c | 12 | _writeOpcode = writeOpcode; |
| romilly | 7:53498e24592c | 13 | _readOpcode = _writeOpcode | 1; // low order bit = 1 for read |
| romilly | 7:53498e24592c | 14 | _init(); |
| romilly | 7:53498e24592c | 15 | } |
| romilly | 7:53498e24592c | 16 | |
| romilly | 7:53498e24592c | 17 | char MCP23S17::_read(char address) { |
| romilly | 7:53498e24592c | 18 | _ncs = 0; |
| romilly | 7:53498e24592c | 19 | _spi.write(_readOpcode); |
| romilly | 7:53498e24592c | 20 | _spi.write(address); |
| romilly | 7:53498e24592c | 21 | char result = _spi.write(0); |
| romilly | 7:53498e24592c | 22 | _ncs = 1; |
| romilly | 7:53498e24592c | 23 | return result; |
| romilly | 7:53498e24592c | 24 | } |
| romilly | 7:53498e24592c | 25 | |
| romilly | 8:841b19734955 | 26 | char MCP23S17::_read(Port port, char address) { |
| romilly | 8:841b19734955 | 27 | return _read(address + (char) port); |
| romilly | 8:841b19734955 | 28 | } |
| romilly | 8:841b19734955 | 29 | |
| romilly | 7:53498e24592c | 30 | void MCP23S17::_write(char address, char data) { |
| romilly | 7:53498e24592c | 31 | _ncs = 0; |
| romilly | 7:53498e24592c | 32 | _spi.write(_writeOpcode); |
| romilly | 7:53498e24592c | 33 | _spi.write(address); |
| romilly | 7:53498e24592c | 34 | _spi.write(data); |
| romilly | 7:53498e24592c | 35 | _ncs = 1; |
| romilly | 7:53498e24592c | 36 | } |
| romilly | 7:53498e24592c | 37 | |
| romilly | 8:841b19734955 | 38 | void MCP23S17::_write(Port port, char address, char data) { |
| romilly | 8:841b19734955 | 39 | _write(address + (char) port, data); |
| romilly | 8:841b19734955 | 40 | } |
| romilly | 8:841b19734955 | 41 | |
| romilly | 7:53498e24592c | 42 | void MCP23S17::_init() { |
| romilly | 7:53498e24592c | 43 | _write(IOCON, (IOCON_BYTE_MODE | IOCON_HAEN )); // Hardware addressing on, operations toggle between A and B registers |
| romilly | 7:53498e24592c | 44 | } |
| romilly | 7:53498e24592c | 45 | |
| romilly | 8:841b19734955 | 46 | void MCP23S17::direction(Port port, char direction) { |
| romilly | 8:841b19734955 | 47 | _write(port, IODIRA, direction); |
| romilly | 7:53498e24592c | 48 | } |
| romilly | 7:53498e24592c | 49 | |
| romilly | 9:068b1e8909bb | 50 | void MCP23S17::configurePullUps(Port port, char offOrOn) { |
| romilly | 9:068b1e8909bb | 51 | _write(port, GPPUA, offOrOn); |
| romilly | 9:068b1e8909bb | 52 | } |
| romilly | 7:53498e24592c | 53 | |
| romilly | 8:841b19734955 | 54 | void MCP23S17::interruptEnable(Port port, char interruptsEnabledMask) { |
| romilly | 8:841b19734955 | 55 | _write(port, GPINTENA, interruptsEnabledMask); |
| romilly | 7:53498e24592c | 56 | } |
| romilly | 7:53498e24592c | 57 | |
| romilly | 7:53498e24592c | 58 | void MCP23S17::mirrorInterrupts(bool mirror) { |
| romilly | 7:53498e24592c | 59 | char iocon = _read(IOCON); |
| romilly | 7:53498e24592c | 60 | if (mirror) { |
| romilly | 7:53498e24592c | 61 | iocon = iocon | INTERRUPT_MIRROR_BIT; |
| romilly | 7:53498e24592c | 62 | } else { |
| romilly | 7:53498e24592c | 63 | iocon = iocon & ~INTERRUPT_MIRROR_BIT; |
| romilly | 7:53498e24592c | 64 | } |
| romilly | 7:53498e24592c | 65 | _write(IOCON, iocon); |
| romilly | 7:53498e24592c | 66 | |
| romilly | 7:53498e24592c | 67 | } |
| romilly | 7:53498e24592c | 68 | |
| romilly | 7:53498e24592c | 69 | void MCP23S17::interruptPolarity(Polarity polarity) { |
| romilly | 7:53498e24592c | 70 | char iocon = _read(IOCON); |
| romilly | 7:53498e24592c | 71 | if (polarity == ACTIVE_LOW) { |
| romilly | 7:53498e24592c | 72 | iocon = iocon & ~INTERRUPT_POLARITY_BIT; |
| romilly | 7:53498e24592c | 73 | } else { |
| romilly | 7:53498e24592c | 74 | iocon = iocon | INTERRUPT_POLARITY_BIT; |
| romilly | 7:53498e24592c | 75 | } |
| romilly | 7:53498e24592c | 76 | _write(IOCON, iocon); |
| romilly | 7:53498e24592c | 77 | } |
| romilly | 7:53498e24592c | 78 | |
| romilly | 8:841b19734955 | 79 | void MCP23S17::defaultValue(Port port, char valuesToCompare) { |
| romilly | 8:841b19734955 | 80 | _write(port, DEFVALA, valuesToCompare); |
| romilly | 7:53498e24592c | 81 | } |
| romilly | 7:53498e24592c | 82 | |
| romilly | 8:841b19734955 | 83 | void MCP23S17::interruptControl(Port port, char interruptContolBits) { |
| romilly | 8:841b19734955 | 84 | _write(port, INTCONA, interruptContolBits); |
| romilly | 7:53498e24592c | 85 | } |
| romilly | 7:53498e24592c | 86 | |
| romilly | 8:841b19734955 | 87 | void MCP23S17::write(Port port, char byte) { |
| romilly | 8:841b19734955 | 88 | _write(port, OLATA, byte); |
| romilly | 7:53498e24592c | 89 | } |
| romilly | 7:53498e24592c | 90 | |
| romilly | 8:841b19734955 | 91 | char MCP23S17::read(Port port) { |
| romilly | 8:841b19734955 | 92 | return _read(port, GPIOA); |
| romilly | 7:53498e24592c | 93 | } |
| romilly | 7:53498e24592c | 94 |
