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