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.
Dependents: MCP23S17Test MCP23S17_Basic_IO_Demo HelloWorld Lab3-SnakeGame ... more
main.cpp
- Committer:
- romilly
- Date:
- 2010-08-21
- Revision:
- 1:5abd129839e7
- Parent:
- 0:930da696072e
File content as of revision 1:5abd129839e7:
#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());
}
}
MCP23S17 I/O Expander