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

12345678910111213141516171819202122232425262728293031323334353637
  1. #include "PortWrite_MCP23S17.h"
  2. /* begin() is called from Scanner_IOE::begin().
  3. Initiates SPI bus and configures port pins to output.
  4. MCP23S17 SPI interface is 10 MHz max.
  5. The electrical limitation to bus speed is bus capacitance and the length of the wires involved.
  6. Longer wires require lower clock speeds.
  7. */
  8. void PortWrite_MCP23S17::begin()
  9. {
  10. pinMode(SS, OUTPUT); //configure controller's Slave Select pin to output
  11. digitalWrite(SS, HIGH); //disable Slave Select
  12. SPI.begin();
  13. SPI.beginTransaction(SPISettings (5000000, MSBFIRST, SPI_MODE0)); //control SPI bus, 5 MHz
  14. //SPI.endTransaction() not called to release SPI bus because keyboard only has one SPI device.
  15. transfer(port.DEVICE_ADDR << 1, port.num, 0); //configure port direction (port.num) to output (0)
  16. }
  17. /* write() sets pin output to logicLevel.
  18. pin is bit pattern, where pin being set is 1.
  19. logicLevel is HIGH or LOW.
  20. write() does not overwrite the other pins.
  21. */
  22. void PortWrite_MCP23S17::write(const uint8_t pin, const bool logicLevel)
  23. {
  24. if (logicLevel == LOW)
  25. {
  26. port.outputVal &= ~pin; //set pin output to low
  27. }
  28. else
  29. {
  30. port.outputVal |= pin; //set pin output to high
  31. }
  32. transfer(port.DEVICE_ADDR << 1, port.num + 0x12, port.outputVal); //set GPIO port to outputVal
  33. }