灵儿巴斯

 找回密码
 立即注册
查看: 1087|回复: 1

adc1115 读取模拟量转数字信号

[复制链接]

110

主题

29

回帖

658

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
658
发表于 2025-4-26 01:28:36 | 显示全部楼层 |阅读模式
  1. # The MIT License (MIT)
  2. #
  3. # Copyright (c) 2016 Radomir Dopieralski (@deshipu),
  4. #               2017 Robert Hammelrath (@robert-hh)
  5. #
  6. # Permission is hereby granted, free of charge, to any person obtaining a copy
  7. # of this software and associated documentation files (the "Software"), to deal
  8. # in the Software without restriction, including without limitation the rights
  9. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. # copies of the Software, and to permit persons to whom the Software is
  11. # furnished to do so, subject to the following conditions:
  12. #
  13. # The above copyright notice and this permission notice shall be included in
  14. # all copies or substantial portions of the Software.
  15. #
  16. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. # THE SOFTWARE.
  23. #
  24. import utime as time

  25. _REGISTER_MASK = const(0x03)
  26. _REGISTER_CONVERT = const(0x00)
  27. _REGISTER_CONFIG = const(0x01)
  28. _REGISTER_LOWTHRESH = const(0x02)
  29. _REGISTER_HITHRESH = const(0x03)

  30. _OS_MASK = const(0x8000)
  31. _OS_SINGLE = const(0x8000)  # Write: Set to start a single-conversion
  32. _OS_BUSY = const(0x0000)  # Read: Bit=0 when conversion is in progress
  33. _OS_NOTBUSY = const(0x8000)  # Read: Bit=1 when no conversion is in progress

  34. _MUX_MASK = const(0x7000)
  35. _MUX_DIFF_0_1 = const(0x0000)  # Differential P  =  AIN0, N  =  AIN1 (default)
  36. _MUX_DIFF_0_3 = const(0x1000)  # Differential P  =  AIN0, N  =  AIN3
  37. _MUX_DIFF_1_3 = const(0x2000)  # Differential P  =  AIN1, N  =  AIN3
  38. _MUX_DIFF_2_3 = const(0x3000)  # Differential P  =  AIN2, N  =  AIN3
  39. _MUX_SINGLE_0 = const(0x4000)  # Single-ended AIN0
  40. _MUX_SINGLE_1 = const(0x5000)  # Single-ended AIN1
  41. _MUX_SINGLE_2 = const(0x6000)  # Single-ended AIN2
  42. _MUX_SINGLE_3 = const(0x7000)  # Single-ended AIN3

  43. _PGA_MASK = const(0x0E00)
  44. _PGA_6_144V = const(0x0000)  # +/-6.144V range  =  Gain 2/3
  45. _PGA_4_096V = const(0x0200)  # +/-4.096V range  =  Gain 1
  46. _PGA_2_048V = const(0x0400)  # +/-2.048V range  =  Gain 2 (default)
  47. _PGA_1_024V = const(0x0600)  # +/-1.024V range  =  Gain 4
  48. _PGA_0_512V = const(0x0800)  # +/-0.512V range  =  Gain 8
  49. _PGA_0_256V = const(0x0A00)  # +/-0.256V range  =  Gain 16

  50. _MODE_MASK = const(0x0100)
  51. _MODE_CONTIN = const(0x0000)  # Continuous conversion mode
  52. _MODE_SINGLE = const(0x0100)  # Power-down single-shot mode (default)

  53. _DR_MASK = const(0x00E0)     # Values ADS1015/ADS1115
  54. _DR_128SPS = const(0x0000)   # 128 /8 samples per second
  55. _DR_250SPS = const(0x0020)   # 250 /16 samples per second
  56. _DR_490SPS = const(0x0040)   # 490 /32 samples per second
  57. _DR_920SPS = const(0x0060)   # 920 /64 samples per second
  58. _DR_1600SPS = const(0x0080)  # 1600/128 samples per second (default)
  59. _DR_2400SPS = const(0x00A0)  # 2400/250 samples per second
  60. _DR_3300SPS = const(0x00C0)  # 3300/475 samples per second
  61. _DR_860SPS = const(0x00E0)  # -   /860 samples per Second

  62. _CMODE_MASK = const(0x0010)
  63. _CMODE_TRAD = const(0x0000)  # Traditional comparator with hysteresis (default)
  64. _CMODE_WINDOW = const(0x0010)  # Window comparator

  65. _CPOL_MASK = const(0x0008)
  66. _CPOL_ACTVLOW = const(0x0000)  # ALERT/RDY pin is low when active (default)
  67. _CPOL_ACTVHI = const(0x0008)  # ALERT/RDY pin is high when active

  68. _CLAT_MASK = const(0x0004)  # Determines if ALERT/RDY pin latches once asserted
  69. _CLAT_NONLAT = const(0x0000)  # Non-latching comparator (default)
  70. _CLAT_LATCH = const(0x0004)  # Latching comparator

  71. _CQUE_MASK = const(0x0003)
  72. _CQUE_1CONV = const(0x0000)  # Assert ALERT/RDY after one conversions
  73. _CQUE_2CONV = const(0x0001)  # Assert ALERT/RDY after two conversions
  74. _CQUE_4CONV = const(0x0002)  # Assert ALERT/RDY after four conversions
  75. # Disable the comparator and put ALERT/RDY in high state (default)
  76. _CQUE_NONE = const(0x0003)

  77. _GAINS = (
  78.     _PGA_6_144V,  # 2/3x
  79.     _PGA_4_096V,  # 1x
  80.     _PGA_2_048V,  # 2x
  81.     _PGA_1_024V,  # 4x
  82.     _PGA_0_512V,  # 8x
  83.     _PGA_0_256V   # 16x
  84. )

  85. _GAINS_V = (
  86.     6.144,  # 2/3x
  87.     4.096,  # 1x
  88.     2.048,  # 2x
  89.     1.024,  # 4x
  90.     0.512,  # 8x
  91.     0.256  # 16x
  92. )

  93. _CHANNELS = {
  94.     (0, None): _MUX_SINGLE_0,
  95.     (1, None): _MUX_SINGLE_1,
  96.     (2, None): _MUX_SINGLE_2,
  97.     (3, None): _MUX_SINGLE_3,
  98.     (0, 1): _MUX_DIFF_0_1,
  99.     (0, 3): _MUX_DIFF_0_3,
  100.     (1, 3): _MUX_DIFF_1_3,
  101.     (2, 3): _MUX_DIFF_2_3,
  102. }

  103. _RATES = (
  104.     _DR_128SPS,   # 128/8 samples per second
  105.     _DR_250SPS,   # 250/16 samples per second
  106.     _DR_490SPS,   # 490/32 samples per second
  107.     _DR_920SPS,   # 920/64 samples per second
  108.     _DR_1600SPS,  # 1600/128 samples per second (default)
  109.     _DR_2400SPS,  # 2400/250 samples per second
  110.     _DR_3300SPS,  # 3300/475 samples per second
  111.     _DR_860SPS    # - /860 samples per Second
  112. )


  113. class ADS1115:
  114.     def __init__(self, i2c, address, gain):
  115.         self.i2c = i2c
  116.         self.address = address
  117.         self.gain = gain
  118.         self.temp2 = bytearray(2)

  119.     def _write_register(self, register, value):
  120.         self.temp2[0] = value >> 8
  121.         self.temp2[1] = value & 0xff
  122.         self.i2c.writeto_mem(self.address, register, self.temp2)

  123.     def _read_register(self, register):
  124.         self.i2c.readfrom_mem_into(self.address, register, self.temp2)
  125.         return (self.temp2[0] << 8) | self.temp2[1]

  126.     def raw_to_v(self, raw):
  127.         v_p_b = _GAINS_V[self.gain] / 32768
  128.         return raw * v_p_b

  129.     def set_conv(self, rate=4, channel1=0, channel2=None):
  130.         """Set mode for read_rev"""
  131.         self.mode = (_CQUE_NONE | _CLAT_NONLAT |
  132.                      _CPOL_ACTVLOW | _CMODE_TRAD | _RATES[rate] |
  133.                      _MODE_SINGLE | _OS_SINGLE | _GAINS[self.gain] |
  134.                      _CHANNELS[(channel1, channel2)])

  135.     def read(self, rate=4, channel1=0, channel2=None):
  136.         """Read voltage between a channel and GND.
  137.            Time depends on conversion rate."""
  138.         self._write_register(_REGISTER_CONFIG, (_CQUE_NONE | _CLAT_NONLAT |
  139.                              _CPOL_ACTVLOW | _CMODE_TRAD | _RATES[rate] |
  140.                              _MODE_SINGLE | _OS_SINGLE | _GAINS[self.gain] |
  141.                              _CHANNELS[(channel1, channel2)]))
  142.         while not self._read_register(_REGISTER_CONFIG) & _OS_NOTBUSY:
  143.             time.sleep_ms(1)
  144.         res = self._read_register(_REGISTER_CONVERT)
  145.         return res if res < 32768 else res - 65536

  146.     def read_rev(self):
  147.         """Read voltage between a channel and GND. and then start
  148.            the next conversion."""
  149.         res = self._read_register(_REGISTER_CONVERT)
  150.         self._write_register(_REGISTER_CONFIG, self.mode)
  151.         return res if res < 32768 else res - 65536

  152.     def alert_start(self, rate=4, channel1=0, channel2=None,
  153.                     threshold_high=0x4000, threshold_low=0, latched=False) :
  154.         """Start continuous measurement, set ALERT pin on threshold."""
  155.         self._write_register(_REGISTER_LOWTHRESH, threshold_low)
  156.         self._write_register(_REGISTER_HITHRESH, threshold_high)
  157.         self._write_register(_REGISTER_CONFIG, _CQUE_1CONV |
  158.                              _CLAT_LATCH if latched else _CLAT_NONLAT |
  159.                              _CPOL_ACTVLOW | _CMODE_TRAD | _RATES[rate] |
  160.                              _MODE_CONTIN | _GAINS[self.gain] |
  161.                              _CHANNELS[(channel1, channel2)])

  162.     def conversion_start(self, rate=4, channel1=0, channel2=None):
  163.         """Start continuous measurement, trigger on ALERT/RDY pin."""
  164.         self._write_register(_REGISTER_LOWTHRESH, 0)
  165.         self._write_register(_REGISTER_HITHRESH, 0x8000)
  166.         self._write_register(_REGISTER_CONFIG, _CQUE_1CONV | _CLAT_NONLAT |
  167.                              _CPOL_ACTVLOW | _CMODE_TRAD | _RATES[rate] |
  168.                              _MODE_CONTIN | _GAINS[self.gain] |
  169.                              _CHANNELS[(channel1, channel2)])

  170.     def alert_read(self):
  171.         """Get the last reading from the continuous measurement."""
  172.         res = self._read_register(_REGISTER_CONVERT)
  173.         return res if res < 32768 else res - 65536


  174. class ADS1113(ADS1115):
  175.     def __init__(self, i2c, address=0x48):
  176.         super().__init__(i2c, address, 1)

  177.     def raw_to_v(self, raw):
  178.         return super().raw_to_v(raw)

  179.     def read(self, rate=4):
  180.         return super().read(rate, 0, 1)

  181.     def alert_start(self, rate=4, threshold_high=0x4000, threshold_low=0, latched=False):
  182.         return super().alert_start(rate, 0, 1, threshold_high, threshold_low, latched)

  183.     def alert_read(self):
  184.         return super().alert_read()


  185. class ADS1114(ADS1115):
  186.     def __init__(self, i2c, address=0x48, gain=1):
  187.         super().__init__(i2c, address, gain)

  188.     def raw_to_v(self, raw):
  189.         return super().raw_to_v(raw)

  190.     def read(self, rate=4):
  191.         return super().read(rate, 0, 1)

  192.     def alert_start(self, rate=4, threshold_high=0x4000, threshold_low=0, latched=False):
  193.         return super().alert_start(rate, 0, 1, threshold_high,
  194.             threshold_low, latched)

  195.     def alert_read(self):
  196.         return super().alert_read()


  197. class ADS1015(ADS1115):
  198.     def __init__(self, i2c, address=0x48, gain=1):
  199.         super().__init__(i2c, address, gain)

  200.     def raw_to_v(self, raw):
  201.         return super().raw_to_v(raw << 4)

  202.     def read(self, rate=4, channel1=0, channel2=None):
  203.         return super().read(rate, channel1, channel2) >> 4

  204.     def alert_start(self, rate=4, channel1=0, channel2=None, threshold_high=0x400,
  205.         threshold_low=0, latched=False):
  206.         return super().alert_start(rate, channel1, channel2, threshold_high << 4,
  207.             threshold_low << 4, latched)

  208.     def alert_read(self):
  209.         return super().alert_read() >> 4

复制代码
上面是裤,下面是main


  1. <blockquote>from machine import SoftI2C, Pin
复制代码


回复

使用道具 举报

110

主题

29

回帖

658

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
658
 楼主| 发表于 2025-4-26 01:46:53 | 显示全部楼层
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

粤ICP备2021173439号-2

GMT+8, 2026-4-14 23:33 , Processed in 0.062676 second(s), 18 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表