mcp23008.lua 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. ---
  2. -- @description Expands the ESP8266's GPIO to 8 more I/O pins via I2C with the MCP23008 I/O Expander
  3. -- MCP23008 Datasheet: http://ww1.microchip.com/downloads/en/DeviceDoc/21919e.pdf
  4. -- Tested on NodeMCU 0.9.5 build 20150213.
  5. -- @date March 02, 2015
  6. -- @author Miguel
  7. -- GitHub: https://github.com/AllAboutEE
  8. -- YouTube: https://www.youtube.com/user/AllAboutEE
  9. -- Website: http://AllAboutEE.com
  10. --------------------------------------------------------------------------------
  11. local moduleName = ...
  12. local M = {}
  13. _G[moduleName] = M
  14. -- Constant device address.
  15. local MCP23008_ADDRESS = 0x20
  16. -- Registers' address as defined in the MCP23008's datashseet
  17. -- luacheck: push no unused
  18. local MCP23008_IODIR = 0x00
  19. local MCP23008_IPOL = 0x01
  20. local MCP23008_GPINTEN = 0x02
  21. local MCP23008_DEFVAL = 0x03
  22. local MCP23008_INTCON = 0x04
  23. local MCP23008_IOCON = 0x05
  24. local MCP23008_GPPU = 0x06
  25. local MCP23008_INTF = 0x07
  26. local MCP23008_INTCAP = 0x08
  27. local MCP23008_GPIO = 0x09
  28. local MCP23008_OLAT = 0x0A
  29. -- luacheck: pop
  30. -- Default value for i2c communication
  31. local id = 0
  32. -- pin modes for I/O direction
  33. M.INPUT = 1
  34. M.OUTPUT = 0
  35. -- pin states for I/O i.e. on/off
  36. M.HIGH = 1
  37. M.LOW = 0
  38. -- Weak pull-up resistor state
  39. M.ENABLE = 1
  40. M.DISABLE = 0
  41. ---
  42. -- @name write
  43. -- @description Writes one byte to a register
  44. -- @param registerAddress The register where we'll write data
  45. -- @param data The data to write to the register
  46. -- @return void
  47. ----------------------------------------------------------
  48. local function write(registerAddress, data)
  49. i2c.start(id)
  50. i2c.address(id,MCP23008_ADDRESS,i2c.TRANSMITTER) -- send MCP's address and write bit
  51. i2c.write(id,registerAddress)
  52. i2c.write(id,data)
  53. i2c.stop(id)
  54. end
  55. ---
  56. -- @name read
  57. -- @description Reads the value of a regsiter
  58. -- @param registerAddress The reigster address from which to read
  59. -- @return The byte stored in the register
  60. ----------------------------------------------------------
  61. local function read(registerAddress)
  62. -- Tell the MCP which register you want to read from
  63. i2c.start(id)
  64. i2c.address(id,MCP23008_ADDRESS,i2c.TRANSMITTER) -- send MCP's address and write bit
  65. i2c.write(id,registerAddress)
  66. i2c.stop(id)
  67. i2c.start(id)
  68. -- Read the data form the register
  69. i2c.address(id,MCP23008_ADDRESS,i2c.RECEIVER) -- send the MCP's address and read bit
  70. local data = i2c.read(id,1) -- we expect only one byte of data
  71. i2c.stop(id)
  72. return string.byte(data) -- i2c.read returns a string so we convert to it's int value
  73. end
  74. ---
  75. --! @name begin
  76. --! @description Sets the MCP23008 device address's last three bits.
  77. -- Note: The address is defined as binary 0100[A2][A1][A0] where
  78. -- A2, A1, and A0 are defined by the connection of the pins,
  79. -- e.g. if the pins are connected all to GND then the paramter address
  80. -- will need to be 0x0.
  81. -- @param address The 3 least significant bits (LSB) of the address
  82. -- @param pinSDA The pin to use for SDA
  83. -- @param pinSCL The pin to use for SCL
  84. -- @param speed The speed of the I2C signal
  85. -- @return void
  86. ---------------------------------------------------------------------------
  87. function M.begin(address,pinSDA,pinSCL,speed)
  88. MCP23008_ADDRESS = bit.bor(MCP23008_ADDRESS,address)
  89. i2c.setup(id,pinSDA,pinSCL,speed)
  90. end
  91. ---
  92. -- @name writeGPIO
  93. -- @description Writes a byte of data to the GPIO register
  94. -- @param dataByte The byte of data to write
  95. -- @return void
  96. ----------------------------------------------------------
  97. function M.writeGPIO(dataByte)
  98. write(MCP23008_GPIO,dataByte)
  99. end
  100. ---
  101. -- @name readGPIO
  102. -- @description reads a byte of data from the GPIO register
  103. -- @return One byte of data
  104. ----------------------------------------------------------
  105. function M.readGPIO()
  106. return read(MCP23008_GPIO)
  107. end
  108. ---
  109. -- @name writeIODIR
  110. -- @description Writes one byte of data to the IODIR register
  111. -- @param dataByte The byte of data to write
  112. -- @return void
  113. ----------------------------------------------------------
  114. function M.writeIODIR(dataByte)
  115. write(MCP23008_IODIR,dataByte)
  116. end
  117. ---
  118. -- @name readIODIR
  119. -- @description Reads a byte from the IODIR register
  120. -- @return The byte of data in IODIR
  121. ----------------------------------------------------------
  122. function M.readIODIR()
  123. return read(MCP23008_IODIR)
  124. end
  125. ---
  126. -- @name writeGPPU The byte to write to the GPPU
  127. -- @description Writes a byte of data to the
  128. -- PULL-UP RESISTOR CONFIGURATION (GPPU)REGISTER
  129. -- @param databyte the value to write to the GPPU register.
  130. -- each bit in this byte is assigned to an individual GPIO pin
  131. -- @return void
  132. ----------------------------------------------------------------
  133. function M.writeGPPU(dataByte)
  134. write(MCP23008_GPPU,dataByte)
  135. end
  136. ---
  137. -- @name readGPPU
  138. -- @description Reads the GPPU (Pull-UP resistors register) byte
  139. -- @return The GPPU byte i.e. state of all internal pull-up resistors
  140. -------------------------------------------------------------------
  141. function M.readGPPU()
  142. return read(MCP23008_GPPU)
  143. end
  144. return M