Advertisement
Guest User

Untitled

a guest
Jan 27th, 2020
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 KB | None | 0 0
  1. // A simple IO demo using the MCP23S17 library
  2. //
  3. // MCP23S17 Library Copyright (c) 2010 Romilly Cocking
  4. // Released under the MIT License: http://mbed.org/license/mit
  5. //
  6. // See http://mbed.org/users/romilly/notebook/mcp23s17-addressable-16-bit-io-expander-with-spi/
  7. //
  8. //
  9. // MCP23S17 datasheet http://ww1.microchip.com/downloads/en/DeviceDoc/21952b.pdf
  10. // uses MCP23S17 library version 0.4
  11. #include "mbed.h"
  12. #include "MCP23S17.h"
  13. // Create SPI bus
  14. SPI spi(p5, p6, p7);
  15. //
  16. // Wiring Connections:
  17. // mbed p5,p6,p7 are tied to MCP23S17 SI, SO, SCK pins
  18. // mbed p20 to MCP23S17 CS
  19. // MCP23S17 reset pin pulled high
  20. // MCP23S17 GPA0 connected to GPB0 for loopback test
  21. // A0, A1, A2 of the MCP23S17 are tied to ground on the breadboard, so the 8-bit address for writes is 0x40
  22. // This is referred to as the opcode in the device datasheet
  23. char Opcode = 0x40;
  24. // Next create a MCP23S17
  25. // mbed p20 is connected to ~chipSelect on the MCP23S17
  26. MCP23S17 chip = MCP23S17(spi, p20, Opcode);
  27. // Optional software reset - mbed p14 to MCP23S17 reset pin
  28. // DigitalOut reset(p14);
  29. DigitalOut led1(LED1); // mbed LED1 is used for test status display
  30. int main() {
  31. // The MCP23S17 reset pin can just be pulled high, since it has a power on reset circuit.
  32. // The reset pin can be used for a software forced reset by pulling it low with an mbed GPIO pin.
  33. // But just leave it pulled high for this simple demo code.
  34. // After a power on reset, both IO ports default to input mode
  35. //
  36. // Here is the optional code for a software reset
  37. // reset = 0;
  38. // wait_us(1);
  39. // reset = 1;
  40. //
  41. // Set all 8 Port A bits to output direction
  42. chip.direction(PORT_A, 0x00);
  43. // Set all 8 Port B bits to input direction
  44. chip.direction(PORT_B, 0xFF);
  45. led1=0;
  46. // Start Loopback test sending out and reading back values
  47. // loopback test uses A0 and B0 pins - so use a wire to jumper those two pins on MCP23S17 together
  48. while (1) {
  49. led1 = chip.read(PORT_B) & 0x01;
  50. wait(0.1);
  51. if (led1) chip.write(PORT_A, 0x55);
  52. else chip.write(PORT_A, 0x00);
  53. }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement