mcp9808.py 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. # Copyright (c) 2014 Adafruit Industries
  2. # Author: Tony DiCola
  3. #
  4. # Permission is hereby granted, free of charge, to any person obtaining a copy
  5. # of this software and associated documentation files (the "Software"), to deal
  6. # in the Software without restriction, including without limitation the rights
  7. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. # copies of the Software, and to permit persons to whom the Software is
  9. # furnished to do so, subject to the following conditions:
  10. #
  11. # The above copyright notice and this permission notice shall be included in
  12. # all copies or substantial portions of the Software.
  13. #
  14. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. # THE SOFTWARE.
  21. # Ported from Adafruit code for Raspberry Pi
  22. # https://github.com/adafruit/Adafruit_Python_MCP9808/
  23. # Default I2C address for device.
  24. MCP9808_I2CADDR_DEFAULT = 0x18
  25. # Register addresses.
  26. MCP9808_REG_CONFIG = 0x01
  27. MCP9808_REG_UPPER_TEMP = 0x02
  28. MCP9808_REG_LOWER_TEMP = 0x03
  29. MCP9808_REG_CRIT_TEMP = 0x04
  30. MCP9808_REG_AMBIENT_TEMP = 0x05
  31. MCP9808_REG_MANUF_ID = 0x06
  32. MCP9808_REG_DEVICE_ID = 0x07
  33. # Configuration register values.
  34. MCP9808_REG_CONFIG_SHUTDOWN = 0x0100
  35. MCP9808_REG_CONFIG_CRITLOCKED = 0x0080
  36. MCP9808_REG_CONFIG_WINLOCKED = 0x0040
  37. MCP9808_REG_CONFIG_INTCLR = 0x0020
  38. MCP9808_REG_CONFIG_ALERTSTAT = 0x0010
  39. MCP9808_REG_CONFIG_ALERTCTRL = 0x0008
  40. MCP9808_REG_CONFIG_ALERTSEL = 0x0002
  41. MCP9808_REG_CONFIG_ALERTPOL = 0x0002
  42. MCP9808_REG_CONFIG_ALERTMODE = 0x0001
  43. from machine import I2C, Pin
  44. def _bytes_to_int_be(data):
  45. return data[1] + (data[0]<<8)
  46. class Mcp9808(object):
  47. """Class to represent an Adafruit MCP9808 precision temperature measurement
  48. board.
  49. """
  50. def __init__(self, sensor_id='mcp9808',
  51. address=MCP9808_I2CADDR_DEFAULT, scl_pinno=5, sda_pinno=4,
  52. i2c=None):
  53. """Initialize MCP9808 device on the specified I2C address and bus number.
  54. Address defaults to 0x18 and bus number defaults to the appropriate bus
  55. for the hardware.
  56. """
  57. self.sensor_id = sensor_id
  58. if i2c is None:
  59. self.i2c = I2C(scl=Pin(scl_pinno, Pin.IN),sda=Pin(sda_pinno, Pin.IN))
  60. else:
  61. self.i2c = i2c
  62. self.address=address
  63. assert self.begin(), "Invalid values read from I2C bus for MCP9808"
  64. def _read_uint16(self, register):
  65. return _bytes_to_int_be(self.i2c.readfrom_mem(self.address,register,
  66. 2))
  67. def begin(self):
  68. """Start taking temperature measurements. Returns True if the device is
  69. intialized, False otherwise.
  70. """
  71. # Check manufacturer and device ID match expected values.
  72. mid = self._read_uint16(MCP9808_REG_MANUF_ID)
  73. did = self._read_uint16(MCP9808_REG_DEVICE_ID)
  74. print('Read manufacturer ID: {0:04X}'.format(mid))
  75. print('Read device ID: {0:04X}'.format(did))
  76. return mid == 0x0054 and did == 0x0400
  77. def sample(self):
  78. """Read sensor and return its value in degrees celsius."""
  79. # Read temperature register value.
  80. t = self._read_uint16(MCP9808_REG_AMBIENT_TEMP)
  81. # Scale and convert to signed value.
  82. temp = (t & 0x0FFF) / 16.0
  83. if t & 0x1000:
  84. temp -= 256.0
  85. return temp