MCP23S17 Test

Dependencies:   MCP23S17 mbed mbed-rtos

Committer:
ryood
Date:
Fri Oct 28 05:41:48 2016 +0000
Revision:
0:5610193796b3
Child:
1:5321e8f5b339
first commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ryood 0:5610193796b3 1 /*
ryood 0:5610193796b3 2 * MCP23S17 Test
ryood 0:5610193796b3 3 *
ryood 0:5610193796b3 4 * https://developer.mbed.org/users/romilly/code/MCP23S17/
ryood 0:5610193796b3 5 *
ryood 0:5610193796b3 6 * Created: 2016.10.28
ryood 0:5610193796b3 7 *
ryood 0:5610193796b3 8 */
ryood 0:5610193796b3 9
ryood 0:5610193796b3 10 #include "mbed.h"
ryood 0:5610193796b3 11 #include "MCP23S17.h"
ryood 0:5610193796b3 12
ryood 0:5610193796b3 13 #define OPCODE (0x40)
ryood 0:5610193796b3 14
ryood 0:5610193796b3 15 SPI Spi(PA_7, PA_6, PA_5); // mosi, miso, sclk
ryood 0:5610193796b3 16
ryood 0:5610193796b3 17 // MCP23S17(SPI& spi, PinName ncs, char writeOpcode);
ryood 0:5610193796b3 18 MCP23S17 Mcp23s17(Spi, PB_6, OPCODE);
ryood 0:5610193796b3 19
ryood 0:5610193796b3 20 int main()
ryood 0:5610193796b3 21 {
ryood 0:5610193796b3 22 printf("\r\n\n*** MCP23S17 Test ***\r\n");
ryood 0:5610193796b3 23
ryood 0:5610193796b3 24 // PORTA output
ryood 0:5610193796b3 25 Mcp23s17.direction(PORT_A, 0x00);
ryood 0:5610193796b3 26
ryood 0:5610193796b3 27 // PORTB input
ryood 0:5610193796b3 28 Mcp23s17.direction(PORT_B, 0xFF);
ryood 0:5610193796b3 29 // PORTB pull-up
ryood 0:5610193796b3 30 Mcp23s17.configurePullUps(PORT_B, 0xFF);
ryood 0:5610193796b3 31
ryood 0:5610193796b3 32 // LED Check
ryood 0:5610193796b3 33 for (int i = 0; i < 8; i++) {
ryood 0:5610193796b3 34 Mcp23s17.write(PORT_A, (1 << i));
ryood 0:5610193796b3 35 wait(0.2);
ryood 0:5610193796b3 36 }
ryood 0:5610193796b3 37 Mcp23s17.write(PORT_A, 0x00);
ryood 0:5610193796b3 38
ryood 0:5610193796b3 39 while (true) {
ryood 0:5610193796b3 40 char data = ~Mcp23s17.read(PORT_B);
ryood 0:5610193796b3 41 Mcp23s17.write(PORT_A, data);
ryood 0:5610193796b3 42
ryood 0:5610193796b3 43 printf("%02x\r\n", data);
ryood 0:5610193796b3 44 }
ryood 0:5610193796b3 45 }
ryood 0:5610193796b3 46