MCP23S17 Test
Dependencies: MCP23S17 mbed mbed-rtos
main.cpp@3:42a3ae578926, 2016-10-30 (annotated)
- Committer:
- ryood
- Date:
- Sun Oct 30 07:19:46 2016 +0000
- Revision:
- 3:42a3ae578926
- Parent:
- 2:c8c30a78bbb3
Change the MCP23S17 Library to https://developer.mbed.org/users/stjo2809/code/MCP23S17/; Impl. Interrupt;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ryood | 0:5610193796b3 | 1 | /* |
ryood | 0:5610193796b3 | 2 | * MCP23S17 Test |
ryood | 0:5610193796b3 | 3 | * |
ryood | 3:42a3ae578926 | 4 | * MCP23S17 Library: |
ryood | 3:42a3ae578926 | 5 | * https://developer.mbed.org/users/stjo2809/code/MCP23S17/ |
ryood | 0:5610193796b3 | 6 | * |
ryood | 2:c8c30a78bbb3 | 7 | * mbed: rev 121 |
ryood | 2:c8c30a78bbb3 | 8 | * mbed-rtos: rev 117 |
ryood | 2:c8c30a78bbb3 | 9 | * |
ryood | 0:5610193796b3 | 10 | * Created: 2016.10.28 |
ryood | 0:5610193796b3 | 11 | * |
ryood | 0:5610193796b3 | 12 | */ |
ryood | 0:5610193796b3 | 13 | |
ryood | 0:5610193796b3 | 14 | #include "mbed.h" |
ryood | 1:5321e8f5b339 | 15 | #include "rtos.h" |
ryood | 0:5610193796b3 | 16 | #include "MCP23S17.h" |
ryood | 0:5610193796b3 | 17 | |
ryood | 2:c8c30a78bbb3 | 18 | SPI Spi(PC_12, PC_11, PC_10); // SPI3: mosi, miso, sclk |
ryood | 2:c8c30a78bbb3 | 19 | //SPI Spi(PA_7, PA_6, PA_5); // SPI1: mosi, miso, sclk |
ryood | 0:5610193796b3 | 20 | |
ryood | 3:42a3ae578926 | 21 | InterruptIn Mcp23s17Int(PA_14); |
ryood | 3:42a3ae578926 | 22 | |
ryood | 3:42a3ae578926 | 23 | // MCP23S17(int hardwareaddress, SPI& spi, PinName nCs, PinName nReset); |
ryood | 3:42a3ae578926 | 24 | MCP23S17 Mcp23s17(0x00, Spi, PD_2, PA_13); |
ryood | 3:42a3ae578926 | 25 | |
ryood | 3:42a3ae578926 | 26 | void dataChanged() |
ryood | 3:42a3ae578926 | 27 | { |
ryood | 3:42a3ae578926 | 28 | char data = Mcp23s17.intcapb(); |
ryood | 3:42a3ae578926 | 29 | wait_us(1); |
ryood | 3:42a3ae578926 | 30 | Mcp23s17.gpioa(data); |
ryood | 3:42a3ae578926 | 31 | } |
ryood | 0:5610193796b3 | 32 | |
ryood | 0:5610193796b3 | 33 | int main() |
ryood | 0:5610193796b3 | 34 | { |
ryood | 0:5610193796b3 | 35 | printf("\r\n\n*** MCP23S17 Test ***\r\n"); |
ryood | 0:5610193796b3 | 36 | |
ryood | 3:42a3ae578926 | 37 | // Reset MCP23S17 |
ryood | 3:42a3ae578926 | 38 | Mcp23s17.reset(); |
ryood | 3:42a3ae578926 | 39 | |
ryood | 0:5610193796b3 | 40 | // PORTA output |
ryood | 3:42a3ae578926 | 41 | Mcp23s17.iodira(0x00); |
ryood | 0:5610193796b3 | 42 | |
ryood | 0:5610193796b3 | 43 | // PORTB input |
ryood | 3:42a3ae578926 | 44 | Mcp23s17.iodirb(0xFF); |
ryood | 0:5610193796b3 | 45 | // PORTB pull-up |
ryood | 3:42a3ae578926 | 46 | Mcp23s17.gppub(0xFF); |
ryood | 3:42a3ae578926 | 47 | // PORTB invert polarity |
ryood | 3:42a3ae578926 | 48 | Mcp23s17.ipolb(0xFF); |
ryood | 3:42a3ae578926 | 49 | // PORTB enable on change interrupt |
ryood | 3:42a3ae578926 | 50 | Mcp23s17.gpintenb(0xFF); |
ryood | 3:42a3ae578926 | 51 | |
ryood | 3:42a3ae578926 | 52 | // Attach the callback function |
ryood | 3:42a3ae578926 | 53 | Mcp23s17Int.rise(&dataChanged); |
ryood | 0:5610193796b3 | 54 | |
ryood | 0:5610193796b3 | 55 | // LED Check |
ryood | 0:5610193796b3 | 56 | for (int i = 0; i < 8; i++) { |
ryood | 3:42a3ae578926 | 57 | Mcp23s17.gpioa(1 << i); |
ryood | 3:42a3ae578926 | 58 | wait(0.1); |
ryood | 0:5610193796b3 | 59 | } |
ryood | 3:42a3ae578926 | 60 | Mcp23s17.gpioa(0x00); |
ryood | 2:c8c30a78bbb3 | 61 | |
ryood | 0:5610193796b3 | 62 | while (true) { |
ryood | 0:5610193796b3 | 63 | } |
ryood | 0:5610193796b3 | 64 | } |
ryood | 0:5610193796b3 | 65 |