keybrd library is an open source library for creating custom-keyboard firmware.
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
Tento repozitář je archivovaný. Můžete prohlížet soubory, klonovat, ale nemůžete nahrávat a vytvářet nové úkoly a požadavky na natažení.

PortWrite_MCP23S17.cpp 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #include "PortWrite_MCP23S17.h"
  2. void PortWrite_MCP23S17::writePort(const uint8_t registerAddr, const uint8_t data)
  3. {
  4. //slower clock
  5. //SPI.beginTransaction(SPISettings (SPI_CLOCK_DIV8, MSBFIRST, SPI_MODE0)); //control SPI bus todo move to begin()
  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. //SPI.endTransaction(); //release the SPI bus
  12. }
  13. /*
  14. If PortRead_MCP23S17 is instantiated on the same port, do NOT use PortWrite_MCP23S17::begin().
  15. Otherwise readPins could be overwritten.
  16. Output pins can be used for strobe pins and LEDs.
  17. SPI.endTransaction() is not called because keyboard only has one SPI device, so no need to release the SPI bus
  18. */
  19. void PortWrite_MCP23S17::begin()
  20. {
  21. pinMode(SS, OUTPUT); //configure controller's Slave Select pin to output
  22. digitalWrite(SS, HIGH); //disable Slave Select
  23. SPI.begin();
  24. SPI.beginTransaction(SPISettings (SPI_CLOCK_DIV8, MSBFIRST, SPI_MODE0)); //control SPI bus
  25. writePort(port.num, 0); //configure port direction (port.num) to output (0)
  26. }
  27. /*
  28. pin is bitwise, where pin being strobed is 1.
  29. strobe is HIGH or LOW (for active high or active low).
  30. port.outputVal can be shared by LEDs.
  31. The functions does not reset the other pins so that they can be used for LEDs.
  32. */
  33. void PortWrite_MCP23S17::write(const uint8_t pin, const bool strobe)
  34. {
  35. if (strobe == LOW) //if active low
  36. {
  37. port.outputVal &= ~pin; //set pin output to low
  38. }
  39. else //if active high
  40. {
  41. port.outputVal |= pin; //set pin output to high
  42. }
  43. writePort(port.num + 0x12, port.outputVal); //set GPIO port pins for stobe and LEDs
  44. }