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.

Port_MCP23017.cpp 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #include "Port_MCP23017.h"
  2. /* beginProtocol() is called from Scanner_IOE::begin(). Initiates I2C bus.
  3. MCP23017 supports I2C SCL Clock Frequencies: 100 kHz, 400 kHz, 1000 kHz (Datasheet page 1 & 6)
  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. http://playground.arduino.cc/Main/WireLibraryDetailedReference > Wire.setclock()
  7. */
  8. void Port_MCP23017::beginProtocol()
  9. {
  10. Wire.begin(); //initiate I2C bus to 100 kHz
  11. }
  12. /* begin() is called from Scanner_IOE::begin().
  13. Configures port's IODIR and GPPU.
  14. */
  15. void Port_MCP23017::begin(const uint8_t activeState)
  16. {
  17. uint8_t pullUp; //bits, GPPU 0=pull-up disabled, 1=pull-up enabled
  18. if (activeState == LOW) //if active low
  19. {
  20. pullUp = readPins; //0=pull-up disabled (for LED), 1=pull-up enabled (for read)
  21. }
  22. else //if active high
  23. {
  24. pullUp = 0; //0=pull-up disabled (external pull-down resistors)
  25. }
  26. Wire.beginTransmission(deviceAddr);
  27. Wire.write(portNum); //IODIR
  28. Wire.write(readPins); //0=output (for strobe and LED), 1=input (for read)
  29. Wire.endTransmission();
  30. Wire.beginTransmission(deviceAddr);
  31. Wire.write(portNum + 0x0C); //GPPU
  32. Wire.write(pullUp);
  33. Wire.endTransmission();
  34. }
  35. /* writeLow() sets pin output LOW.
  36. pin is bit pattern, where pin being set is 1.
  37. */
  38. void Port_MCP23017::writeLow(const uint8_t pin)
  39. {
  40. outputVal &= ~pin; //set pin output to low
  41. Wire.beginTransmission(deviceAddr);
  42. Wire.write(portNum + 0x12); //GPIO
  43. Wire.write(outputVal);
  44. Wire.endTransmission();
  45. }
  46. /* writeHigh() sets pin output HIGH.
  47. pin is bit pattern, where pin being set is 1.
  48. */
  49. void Port_MCP23017::writeHigh(const uint8_t pin)
  50. {
  51. outputVal |= pin; //set pin output to high
  52. Wire.beginTransmission(deviceAddr);
  53. Wire.write(portNum + 0x12); //GPIO
  54. Wire.write(outputVal);
  55. Wire.endTransmission();
  56. }
  57. /* read() returns portState.
  58. Only portState bits of readPins are valid.
  59. */
  60. uint8_t Port_MCP23017::read()
  61. {
  62. Wire.beginTransmission(deviceAddr);
  63. Wire.write(portNum + 0x12); //GPIO
  64. Wire.endTransmission(false); //MCP23017 needs false to send a restart
  65. Wire.requestFrom(deviceAddr, 1u); //request one byte from input port
  66. return Wire.read();
  67. }