keybrd library is an open source library for creating custom-keyboard firmware.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

PortRead_MCP23S17.cpp 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #include "PortRead_MCP23S17.h"
  2. /*
  3. PortRead_MCP23S17::begin() is not needed because port direction is already configured to input by default.
  4. SPI bus is configured in PortWrite_MCP23S17::begin().
  5. */
  6. void PortRead_MCP23S17::begin()
  7. {
  8. pinMode(SS, OUTPUT); //configure controller's Slave Select pin to output
  9. digitalWrite(SS, HIGH); //disable Slave Select
  10. SPI.begin();
  11. SPI.beginTransaction(SPISettings (SPI_CLOCK_DIV8, MSBFIRST, SPI_MODE0)); //control SPI bus todo is slow clock needed?
  12. digitalWrite(SS, LOW); //enable Slave Select
  13. SPI.transfer(port.ADDR << 1); //write command
  14. SPI.transfer(port.num); //configure IODIR
  15. SPI.transfer(pullUp); //0=output (for LED), 1=input (for read)
  16. digitalWrite(SS, LOW); //enable Slave Select
  17. digitalWrite(SS, HIGH); //disable Slave Select
  18. SPI.transfer(port.ADDR << 1); //write command
  19. SPI.transfer(port.num + 0x0C); //configure GPPU
  20. SPI.transfer(pullUp); //0=pull-up disabled (for LED), 1=pull-up enabled (for read)
  21. digitalWrite(SS, HIGH); //disable Slave Select
  22. //SPI.endTransaction() is not called to release the SPI bus
  23. // because keyboard only has one SPI device.
  24. }
  25. /*
  26. read() returns portState.
  27. */
  28. uint8_t PortRead_MCP23S17::read()
  29. {
  30. uint8_t portState; //bit wise
  31. digitalWrite(SS, LOW); //enable Slave Select
  32. SPI.transfer(port.ADDR << 1 | 1); //read command
  33. SPI.transfer(port.num + 0x12); //GPIO register address to read data from
  34. portState = SPI.transfer(0); //save the data (0 is dummy data to send)
  35. digitalWrite(SS, HIGH); //disable Slave Select
  36. return portState;
  37. }