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.2KB

1234567891011121314151617181920212223242526272829303132333435
  1. #include "PortWrite_MCP23S17.h"
  2. /* begin() is called from Scanner_IOE::begin().
  3. Initiates SPI bus and configures write pins to output.
  4. MCP23S17 SPI interface is 10 MHz max.
  5. */
  6. void PortWrite_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 (5000000, MSBFIRST, SPI_MODE0)); //control SPI bus, 5 MHz
  12. //SPI.endTransaction() not called to release SPI bus because keyboard only has one SPI device.
  13. push(port.DEVICE_ADDR << 1, port.num, 0); //configure port direction (port.num) to output (0)
  14. }
  15. /* write() sets pin output to logicLevel.
  16. pin is bitwise, where pin being set is 1.
  17. logicLevel is HIGH or LOW.
  18. write() does not overwrite the other pins.
  19. */
  20. void PortWrite_MCP23S17::write(const uint8_t pin, const bool logicLevel)
  21. {
  22. if (logicLevel == LOW)
  23. {
  24. port.outputVal &= ~pin; //set pin output to low
  25. }
  26. else
  27. {
  28. port.outputVal |= pin; //set pin output to high
  29. }
  30. push(port.DEVICE_ADDR << 1, port.num + 0x12, port.outputVal); //set GPIO port pin to outputVal
  31. }