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@0:930da696072e, 2010-08-18 (annotated)
- Committer:
- romilly
- Date:
- Wed Aug 18 12:30:39 2010 +0000
- Revision:
- 0:930da696072e
- Child:
- 1:5abd129839e7
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| romilly | 0:930da696072e | 1 | #include "mbed.h" |
| romilly | 0:930da696072e | 2 | |
| romilly | 0:930da696072e | 3 | /* first attempt at driving an MCP23S17 |
| romilly | 0:930da696072e | 4 | * |
| romilly | 0:930da696072e | 5 | * Turns alternate bits of B register on and off |
| romilly | 0:930da696072e | 6 | * |
| romilly | 0:930da696072e | 7 | * I have not added many comments yet. |
| romilly | 0:930da696072e | 8 | * This is a proof of concept, |
| romilly | 0:930da696072e | 9 | * not a finished example. |
| romilly | 0:930da696072e | 10 | */ |
| romilly | 0:930da696072e | 11 | |
| romilly | 0:930da696072e | 12 | DigitalOut myled(LED1); |
| romilly | 0:930da696072e | 13 | |
| romilly | 0:930da696072e | 14 | SPI spi(p5, p6, p7); |
| romilly | 0:930da696072e | 15 | DigitalOut ncs(p20); |
| romilly | 0:930da696072e | 16 | |
| romilly | 0:930da696072e | 17 | void write(char command, char address, char data) { |
| romilly | 0:930da696072e | 18 | ncs = 0; |
| romilly | 0:930da696072e | 19 | spi.write(command); |
| romilly | 0:930da696072e | 20 | spi.write(address); |
| romilly | 0:930da696072e | 21 | spi.write(data); |
| romilly | 0:930da696072e | 22 | ncs = 1; |
| romilly | 0:930da696072e | 23 | } |
| romilly | 0:930da696072e | 24 | |
| romilly | 0:930da696072e | 25 | void init() { |
| romilly | 0:930da696072e | 26 | write(0x40, 0x0A, 0xA0); |
| romilly | 0:930da696072e | 27 | write(0x40, 0x10, 0x00); |
| romilly | 0:930da696072e | 28 | } |
| romilly | 0:930da696072e | 29 | |
| romilly | 0:930da696072e | 30 | void output(char byte) { |
| romilly | 0:930da696072e | 31 | ncs = 0; |
| romilly | 0:930da696072e | 32 | write(0x40,0x1A, byte); // configures for multi-write - could send a series of bytes for immediate output |
| romilly | 0:930da696072e | 33 | ncs = 1; |
| romilly | 0:930da696072e | 34 | } |
| romilly | 0:930da696072e | 35 | |
| romilly | 0:930da696072e | 36 | int main() { |
| romilly | 0:930da696072e | 37 | init(); |
| romilly | 0:930da696072e | 38 | while(1) { |
| romilly | 0:930da696072e | 39 | myled = 1; |
| romilly | 0:930da696072e | 40 | wait(0.2); |
| romilly | 0:930da696072e | 41 | output(0xAA); |
| romilly | 0:930da696072e | 42 | myled = 0; |
| romilly | 0:930da696072e | 43 | wait(0.2); |
| romilly | 0:930da696072e | 44 | output(0x55); |
| romilly | 0:930da696072e | 45 | } |
| romilly | 0:930da696072e | 46 | } |
MCP23S17 I/O Expander