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.

PortRead_MCP23S17.cpp 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #include "PortRead_MCP23S17.h"
  2. /* begin() is called from Scanner_IOE::begin().
  3. Configures port to to read (input with pullup enabled).
  4. */
  5. void PortRead_MCP23S17::begin(const uint8_t strobeOn)
  6. {
  7. if (strobeOn == LOW) //if active low
  8. {
  9. pullUp = readPins;
  10. }
  11. else
  12. {
  13. pullUp = 0;
  14. }
  15. digitalWrite(SS, LOW); //enable Slave Select
  16. SPI.transfer(port.DEVICE_ADDR << 1); //write command
  17. SPI.transfer(port.num); //configure IODIR
  18. SPI.transfer(readPins); //0=output (for LED), 1=input (for read)
  19. digitalWrite(SS, HIGH); //enable Slave Select
  20. digitalWrite(SS, LOW); //disable Slave Select
  21. SPI.transfer(port.DEVICE_ADDR << 1); //write command
  22. SPI.transfer(port.num + 0x0C); //configure GPPU
  23. SPI.transfer(pullUp); //0=pull-up disabled (for LED), 1=pull-up enabled (for read)
  24. digitalWrite(SS, HIGH); //disable Slave Select
  25. }
  26. /* read() returns portState.
  27. Only portState bits of readPins are valid.
  28. */
  29. uint8_t PortRead_MCP23S17::read()
  30. {
  31. uint8_t portState; //bit wise
  32. digitalWrite(SS, LOW); //enable Slave Select
  33. SPI.transfer( (port.DEVICE_ADDR << 1) | 1); //read command
  34. SPI.transfer(port.num + 0x12); //GPIO register address to read data from
  35. portState = SPI.transfer(0); //save the data (0 is dummy data to send)
  36. digitalWrite(SS, HIGH); //disable Slave Select
  37. return portState;
  38. }