keybrd library is an open source library for creating custom-keyboard firmware.
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
Dieses Repo ist archiviert. Du kannst Dateien sehen und es klonen, kannst aber nicht pushen oder Issues/Pull-Requests öffnen.

PortMCP23S17.cpp 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #include "PortMCP23S17.h"
  2. /* transfer() writes data to registerAddr, reads portSate from registerAddr, and returns portState.
  3. MCP23S17 SPI interface is 10 MHz max.
  4. The electrical limitation to bus speed is bus capacitance and the length of the wires involved.
  5. Longer wires require lower clock speeds.
  6. */
  7. uint8_t PortMCP23S17::transfer(const uint8_t command, const uint8_t registerAddr,
  8. const uint8_t data)
  9. {
  10. uint8_t portState; //bit pattern
  11. SPI.beginTransaction( SPISettings(5000000, MSBFIRST, SPI_MODE0) ); //control SPI bus, 5 MHz
  12. digitalWrite(SS, LOW); //enable Slave Select
  13. SPI.transfer(command); //write or read command
  14. SPI.transfer(registerAddr); //register address to write data to
  15. portState = SPI.transfer(data); //write data, read portState
  16. digitalWrite(SS, HIGH); //disable Slave Select
  17. SPI.endTransaction();
  18. return portState;
  19. }
  20. /* begin() is called from Scanner_IOE::begin(). Initiates SPI bus.
  21. */
  22. void PortMCP23S17::beginProtocol()
  23. {
  24. pinMode(SS, OUTPUT); //configure controller's Slave Select pin to output
  25. digitalWrite(SS, HIGH); //disable Slave Select
  26. SPI.begin();
  27. }
  28. /* begin() is called from Scanner_IOE::begin().
  29. strobeOn is logic level of strobe on, HIGH or LOW
  30. configure IODIR and GPPU.
  31. */
  32. void PortMCP23S17::begin(const uint8_t strobeOn)
  33. {
  34. uint8_t pullUp; //bits, GPPU 0=pull-up disabled, 1=pull-up enabled
  35. if (strobeOn == LOW) //if active low, use internal pull-up resistors
  36. {
  37. pullUp = readPins;
  38. }
  39. else //active high requires external pull-down resistors
  40. {
  41. pullUp = 0;
  42. }
  43. transfer(deviceAddr << 1, portNum, readPins); //configure IODIR
  44. transfer(deviceAddr << 1, portNum + 0x0C, pullUp); //configure GPPU
  45. }
  46. /* write() sets pin output to logicLevel (useful for strobePin, one LED pin, or multiple pins).
  47. pin is bit pattern, where pin being set is 1.
  48. logicLevel is HIGH or LOW.
  49. write() does not overwrite the other pins.
  50. */
  51. void PortMCP23S17::write(const uint8_t pin, const bool logicLevel)
  52. {
  53. if (logicLevel == LOW)
  54. {
  55. outputVal &= ~pin; //set pin output to low
  56. }
  57. else
  58. {
  59. outputVal |= pin; //set pin output to high
  60. }
  61. transfer(deviceAddr << 1, portNum + 0x12, outputVal); //set GPIO port to outputVal
  62. }
  63. /* read() returns portState. Only portState pins with pull resistors are valid.
  64. */
  65. uint8_t PortMCP23S17::read()
  66. {
  67. return transfer( (deviceAddr << 1) | 1, portNum + 0x12, 0); //read from GPIO
  68. }