当前位置:   article > 正文

MCP3008 模数转换芯片驱动_mcp3008树莓派

mcp3008树莓派

        几天前用树莓派采集模拟信号时用到了MCP3008 AD转换器,被驱动问题困扰好几天。后来听说可以通过pip3 install mcp3008安装驱动库,但发现下载的库竟然没有关键的代码文件。于是又去GitHub上搜索,发现了库作者的代码,在此存一下。

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. '''
  4. RPi_mcp3008 is a library to listen to the MCP3008 A/D converter chip,
  5. as described in the datasheet.
  6. https://www.adafruit.com/datasheets/MCP3008.pdf
  7. Copyright (C) 2021 Luiz Eduardo Amaral <luizamaral306@gmail.com>
  8. This program is free software: you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation, either version 3 of the License, or
  11. (at your option) any later version.
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. '''
  19. import spidev
  20. # Modes Single
  21. CH0 = 8 # single-ended CH0
  22. CH1 = 9 # single-ended CH1
  23. CH2 = 10 # single-ended CH2
  24. CH3 = 11 # single-ended CH3
  25. CH4 = 12 # single-ended CH4
  26. CH5 = 13 # single-ended CH5
  27. CH6 = 14 # single-ended CH6
  28. CH7 = 15 # single-ended CH7
  29. # Modes Diff
  30. DF0 = 0 # differential CH0 = IN+ CH1 = IN-
  31. DF1 = 1 # differential CH0 = IN- CH1 = IN+
  32. DF2 = 2 # differential CH2 = IN+ CH3 = IN-
  33. DF3 = 3 # differential CH2 = IN- CH3 = IN+
  34. DF4 = 4 # differential CH4 = IN+ CH5 = IN-
  35. DF5 = 5 # differential CH4 = IN- CH5 = IN+
  36. DF6 = 6 # differential CH6 = IN+ CH7 = IN-
  37. DF7 = 7 # differential CH6 = IN- CH7 = IN+
  38. RESOLUTION = 1 << 10 # 10 bits resolution
  39. class MCP3008(spidev.SpiDev):
  40. '''
  41. Object that listens the MCP3008 in the SPI port of the RPi.
  42. Connects the object to the specified SPI device.
  43. The initialization arguments are MCP3008(bus=0, device=0) where:
  44. MCP3008(X, Y) will open /dev/spidev-X.Y, same as spidev.SpiDev.open(X, Y).
  45. '''
  46. def __init__(self, bus=0, device=0, max_speed_hz=976000):
  47. self.bus = bus
  48. self.device = device
  49. self.open(self.bus, self.device)
  50. self.modes = False
  51. self.max_speed_hz = max_speed_hz
  52. def __del__(self):
  53. self.close()
  54. def __enter__(self):
  55. return self
  56. def __exit__(self, type, value, tb):
  57. self.close()
  58. def __repr__(self):
  59. return 'MCP3008 object at bus {0}, device {1}'.format(self.bus, self.device)
  60. def __call__(self, norm=False):
  61. return self.read(self.modes, norm)
  62. @classmethod
  63. def fixed(cls, modes, bus=0, device=0):
  64. '''
  65. Initializes the class with fixed modes, which turns the instance callable.
  66. The modes argument is a list with the modes of operation to be read (e.g.
  67. [mcp3008.CH0,mcp3008.Df0]).
  68. When calling the instance the object will execute a reading of and return the
  69. values (e.g. print instance()).
  70. When calling the instance, you can pass the optional argument norm to
  71. normalize
  72. the data (e.g. print instance(5.2)).
  73. '''
  74. instance = cls(bus, device)
  75. instance.modes = modes
  76. return instance
  77. def _read_single(self, mode):
  78. '''
  79. Returns the value of a single mode reading
  80. '''
  81. if not 0 <= mode <= 15:
  82. raise IndexError('Outside the channels scope, please use: 0, 1 ..., 7')
  83. request = [0x1, mode << 4, 0x0] # [start bit, configuration, listen space]
  84. _, byte1, byte2 = self.xfer2(request)
  85. value = (byte1%4 << 8) + byte2
  86. return value
  87. def read(self, modes, norm=False):
  88. '''
  89. Returns the raw value (0 ... 1024) of the reading.
  90. The modes argument is a list with the modes of operation to be read (e.g.
  91. [mcp3008.CH0,mcp3008.Df0]).
  92. norm is a normalization factor, usually Vref.
  93. '''
  94. reading = []
  95. for mode in modes:
  96. reading.append(self._read_single(mode))
  97. if norm:
  98. return [float(norm)*value/RESOLUTION for value in reading]
  99. else:
  100. return reading
  101. def read_all(self, norm=False):
  102. '''
  103. Returns a list with the readings of all the modes
  104. Data Order:
  105. [DF0, DF1, DF2, DF3, DF4, DF5, DF6, DF7,
  106. CH0, CH1, CH2, CH3, CH4, CH5, CH6, CH7]
  107. norm is a normalization factor, usually Vref.
  108. '''
  109. return self.read(range(16), norm)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/230616
推荐阅读
相关标签
  

闽ICP备14008679号