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.

PortWrite_MCP23S17.cpp 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #include "PortWrite_MCP23S17.h"
  2. /* writePort() sets registerAddr to data.
  3. */
  4. void PortWrite_MCP23S17::writePort(const uint8_t registerAddr, const uint8_t data)
  5. {
  6. digitalWrite(SS, LOW); //enable Slave Select
  7. SPI.transfer(port.ADDR << 1); //write command
  8. SPI.transfer(registerAddr); //register address to write data to
  9. SPI.transfer(data); //data
  10. digitalWrite(SS, HIGH); //disable Slave Select
  11. }
  12. /* begin() should be called once from sketch in setup().
  13. PortRead_MCP23S17 and PortWrite_MCP23S17 should be on seperate ports on the same MCP23S17.
  14. Output pins can be used for strobe pins and LEDs.
  15. */
  16. void PortWrite_MCP23S17::begin()
  17. {
  18. pinMode(SS, OUTPUT); //configure controller's Slave Select pin to output
  19. digitalWrite(SS, HIGH); //disable Slave Select
  20. SPI.begin();
  21. SPI.beginTransaction(SPISettings (SPI_CLOCK_DIV8, MSBFIRST, SPI_MODE0)); //control SPI bus todo is slow clock needed?
  22. writePort(port.num, 0); //configure port direction (port.num) to output (0)
  23. //SPI.endTransaction() is not called to release the SPI bus
  24. // because keyboard only has one SPI device.
  25. }
  26. /*
  27. strobePin is bitwise, where pin being strobed is 1.
  28. pinLogicLevel is HIGH or LOW.
  29. port.outputVal can be shared by LEDs.
  30. The functions does not reset the other pins so that they can be used for LEDs.
  31. */
  32. void PortWrite_MCP23S17::write(const uint8_t strobePin, const uint8_t pinLogicLevel)
  33. {
  34. if (pinLogicLevel == LOW)
  35. {
  36. port.outputVal &= ~strobePin; //set strobePin output to low
  37. }
  38. else
  39. {
  40. port.outputVal |= strobePin; //set strobePin output to high
  41. }
  42. writePort(port.num + 0x12, port.outputVal); //set GPIO port pins for stobe and LEDs
  43. }