keybrd library is an open source library for creating custom-keyboard firmware.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
此仓库已存档。您可以查看文件和克隆,但不能推送或创建工单/合并请求。

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. }