keybrd library is an open source library for creating custom-keyboard firmware.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
Ce dépôt est archivé. Vous pouvez voir les fichiers et le cloner, mais vous ne pouvez pas pousser ni ouvrir de ticket/demande d'ajout.

Port_MCP23018.cpp 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #include "Port_MCP23018.h"
  2. /* beginProtocol() is called from Scanner_IOE::begin(). Initiates I2C bus.
  3. MCP23018 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_MCP23018::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_MCP23018::begin(const uint8_t strobeOn)
  16. {
  17. uint8_t pullUp; //bits, GPPU 0=pull-up disabled, 1=pull-up enabled
  18. if (strobeOn == 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 (for 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. /* write() sets pin output to logicLevel.
  36. pin is bit pattern, where pin being strobed is 1.
  37. logicLevel is HIGH or LOW.
  38. write() does not overwrite the other pins.
  39. */
  40. void Port_MCP23018::write(const uint8_t pin, const bool logicLevel)
  41. {
  42. if (logicLevel == LOW)
  43. {
  44. outputVal &= ~pin; //set pin output to low
  45. }
  46. else
  47. {
  48. outputVal |= pin; //set pin output to high
  49. }
  50. Wire.beginTransmission(deviceAddr);
  51. Wire.write(portNum + 0x12); //GPIO
  52. Wire.write(outputVal);
  53. Wire.endTransmission();
  54. }
  55. /* read() returns portState.
  56. Only portState bits of readPins are valid.
  57. */
  58. uint8_t Port_MCP23018::read()
  59. {
  60. Wire.beginTransmission(deviceAddr);
  61. Wire.write(portNum + 0x12); //GPIO
  62. Wire.endTransmission(false); //MCP23018 needs false to send a restart ??really?
  63. Wire.requestFrom(deviceAddr, 1u); //request one byte from input port
  64. return Wire.read();
  65. }