单片机教程网

电脑版
提示:原网页已由神马搜索转码, 内容由www.51hei.com提供.
查看:2896|回复:0
打印上一主题下一主题

ADS1115的Arduino程序和pdf资料

[复制链接]
跳转到指定楼层
楼主
ID:597850发表于 2019-8-8 16:43|只看该作者|只看大图回帖奖励
有需要的请自取




单片机源程序如下:
  1. #if ARDUINO >= 100
  2. #include "Arduino.h"
  3. #else
  4. #include "WProgram.h"
  5. #endif

  6. #include< Wire.h>

  7. #include "Adafruit_ADS1015.h"

  8. /**************************************************************************/
  9. /*!
  10.    @brief  Abstract away platform differences in Arduino wire library
  11. */
  12. /**************************************************************************/
  13. static uint8_t i2cread(void) {
  14.   #if ARDUINO >= 100
  15.   return Wire.read();
  16.   #else
  17.   return Wire.receive();
  18.   #endif
  19. }

  20. /**************************************************************************/
  21. /*!
  22.    @brief  Abstract away platform differences in Arduino wire library
  23. */
  24. /**************************************************************************/
  25. static void i2cwrite(uint8_t x) {
  26.   #if ARDUINO >= 100
  27.   Wire.write((uint8_t)x);
  28.   #else
  29.   Wire.send(x);
  30.   #endif
  31. }

  32. /**************************************************************************/
  33. /*!
  34.    @brief  Writes 16-bits to the specified destination register
  35. */
  36. /**************************************************************************/
  37. static void writeRegister(uint8_t i2cAddress, uint8_t reg, uint16_t value) {
  38.   Wire.beginTransmission(i2cAddress);
  39.   i2cwrite((uint8_t)reg);
  40.   i2cwrite((uint8_t)(value>>8));
  41.   i2cwrite((uint8_t)(value& 0xFF));
  42.   Wire.endTransmission();
  43. }

  44. /**************************************************************************/
  45. /*!
  46.    @brief  Writes 16-bits to the specified destination register
  47. */
  48. /**************************************************************************/
  49. static uint16_t readRegister(uint8_t i2cAddress, uint8_t reg) {
  50.   Wire.beginTransmission(i2cAddress);
  51.   i2cwrite(ADS1015_REG_POINTER_CONVERT);
  52.   Wire.endTransmission();
  53.   Wire.requestFrom(i2cAddress, (uint8_t)2);
  54.   return ((i2cread()<< 8) | i2cread());  
  55. }

  56. /**************************************************************************/
  57. /*!
  58.    @brief  Instantiates a new ADS1015 class w/appropriate properties
  59. */
  60. /**************************************************************************/
  61. Adafruit_ADS1015::Adafruit_ADS1015(uint8_t i2cAddress)
  62. {
  63.    m_i2cAddress = i2cAddress;
  64.    m_conversionDelay = ADS1015_CONVERSIONDELAY;
  65.    m_bitShift = 4;
  66.    m_gain = GAIN_TWOTHIRDS; /* +/- 6.144V range (limited to VDD +0.3V max!) */
  67. }

  68. /**************************************************************************/
  69. /*!
  70.    @brief  Instantiates a new ADS1115 class w/appropriate properties
  71. */
  72. /**************************************************************************/
  73. Adafruit_ADS1115::Adafruit_ADS1115(uint8_t i2cAddress)
  74. {
  75.    m_i2cAddress = i2cAddress;
  76.    m_conversionDelay = ADS1115_CONVERSIONDELAY;
  77.    m_bitShift = 0;
  78.    m_gain = GAIN_TWOTHIRDS; /* +/- 6.144V range (limited to VDD +0.3V max!) */
  79. }

  80. /**************************************************************************/
  81. /*!
  82.    @brief  Sets up the HW (reads coefficients values, etc.)
  83. */
  84. /**************************************************************************/
  85. void Adafruit_ADS1015::begin() {
  86.   Wire.begin();
  87. }

  88. /**************************************************************************/
  89. /*!
  90.    @brief  Sets the gain and input voltage range
  91. */
  92. /**************************************************************************/
  93. void Adafruit_ADS1015::setGain(adsGain_t gain)
  94. {
  95.   m_gain = gain;
  96. }

  97. /**************************************************************************/
  98. /*!
  99.    @brief  Gets a gain and input voltage range
  100. */
  101. /**************************************************************************/
  102. adsGain_t Adafruit_ADS1015::getGain()
  103. {
  104.   return m_gain;
  105. }

  106. /**************************************************************************/
  107. /*!
  108.    @brief  Gets a single-ended ADC reading from the specified channel
  109. */
  110. /**************************************************************************/
  111. uint16_t Adafruit_ADS1015::readADC_SingleEnded(uint8_t channel) {
  112.   if (channel > 3)
  113.   {
  114.    return 0;
  115.   }
  116.   
  117.   // Start with default values
  118.   uint16_t config = ADS1015_REG_CONFIG_CQUE_NONE   | // Disable the comparator (default val)
  119.                ADS1015_REG_CONFIG_CLAT_NONLAT  | // Non-latching (default val)
  120.                ADS1015_REG_CONFIG_CPOL_ACTVLOW | // Alert/Rdy active low   (default val)
  121.                ADS1015_REG_CONFIG_CMODE_TRAD   | // Traditional comparator (default val)
  122.                ADS1015_REG_CONFIG_DR_1600SPS   | // 1600 samples per second (default)
  123.                ADS1015_REG_CONFIG_MODE_SINGLE;   // Single-shot mode (default)

  124.   // Set PGA/voltage range
  125.   config |= m_gain;

  126.   // Set single-ended input channel
  127.   switch (channel)
  128.   {
  129.    case (0):
  130.      config |= ADS1015_REG_CONFIG_MUX_SINGLE_0;
  131.      break;
  132.    case (1):
  133.      config |= ADS1015_REG_CONFIG_MUX_SINGLE_1;
  134.      break;
  135.    case (2):
  136.      config |= ADS1015_REG_CONFIG_MUX_SINGLE_2;
  137.      break;
  138.    case (3):
  139.      config |= ADS1015_REG_CONFIG_MUX_SINGLE_3;
  140.      break;
  141.   }

  142.   // Set 'start single-conversion' bit
  143.   config |= ADS1015_REG_CONFIG_OS_SINGLE;

  144.   // Write config register to the ADC
  145.   writeRegister(m_i2cAddress, ADS1015_REG_POINTER_CONFIG, config);

  146.   // Wait for the conversion to complete
  147.   delay(m_conversionDelay);

  148.   // Read the conversion results
  149.   // Shift 12-bit results right 4 bits for the ADS1015
  150.   return readRegister(m_i2cAddress, ADS1015_REG_POINTER_CONVERT) >> m_bitShift;  
  151. }

  152. /**************************************************************************/
  153. /*!
  154.    @brief  Reads the conversion results, measuring the voltage
  155.          difference between the P (AIN0) and N (AIN1) input.  Generates
  156.          a signed value since the difference can be either
  157.          positive or negative.
  158. */
  159. /**************************************************************************/
  160. int16_t Adafruit_ADS1015::readADC_Differential_0_1() {
  161.   // Start with default values
  162.   uint16_t config = ADS1015_REG_CONFIG_CQUE_NONE   | // Disable the comparator (default val)
  163.                ADS1015_REG_CONFIG_CLAT_NONLAT  | // Non-latching (default val)
  164.                ADS1015_REG_CONFIG_CPOL_ACTVLOW | // Alert/Rdy active low   (default val)
  165.                ADS1015_REG_CONFIG_CMODE_TRAD   | // Traditional comparator (default val)
  166.                ADS1015_REG_CONFIG_DR_1600SPS   | // 1600 samples per second (default)
  167.                ADS1015_REG_CONFIG_MODE_SINGLE;   // Single-shot mode (default)

  168.   // Set PGA/voltage range
  169.   config |= m_gain;
  170.               
  171.   // Set channels
  172.   config |= ADS1015_REG_CONFIG_MUX_DIFF_0_1;       // AIN0 = P, AIN1 = N

  173.   // Set 'start single-conversion' bit
  174.   config |= ADS1015_REG_CONFIG_OS_SINGLE;

  175.   // Write config register to the ADC
  176.   writeRegister(m_i2cAddress, ADS1015_REG_POINTER_CONFIG, config);

  177.   // Wait for the conversion to complete
  178.   delay(m_conversionDelay);

  179.   // Read the conversion results
  180.   uint16_t res = readRegister(m_i2cAddress, ADS1015_REG_POINTER_CONVERT) >> m_bitShift;
  181.   if (m_bitShift == 0)
  182.   {
  183.    return (int16_t)res;
  184.   }
  185.   else
  186.   {
  187.    // Shift 12-bit results right 4 bits for the ADS1015,
  188.    // making sure we keep the sign bit intact
  189.    if (res > 0x07FF)
  190.    {
  191.      // negative number - extend the sign to 16th bit
  192.      res |= 0xF000;
  193.    }
  194.    return (int16_t)res;
  195.   }
  196. }

  197. /**************************************************************************/
  198. /*!
  199.    @brief  Reads the conversion results, measuring the voltage
  200.          difference between the P (AIN2) and N (AIN3) input.  Generates
  201.          a signed value since the difference can be either
  202.          positive or negative.
  203. */
  204. /**************************************************************************/
  205. int16_t Adafruit_ADS1015::readADC_Differential_2_3() {
  206.   // Start with default values
  207.   uint16_t config = ADS1015_REG_CONFIG_CQUE_NONE   | // Disable the comparator (default val)
  208.                ADS1015_REG_CONFIG_CLAT_NONLAT  | // Non-latching (default val)
  209.                ADS1015_REG_CONFIG_CPOL_ACTVLOW | // Alert/Rdy active low   (default val)
  210.                ADS1015_REG_CONFIG_CMODE_TRAD   | // Traditional comparator (default val)
  211.                ADS1015_REG_CONFIG_DR_1600SPS   | // 1600 samples per second (default)
  212.                ADS1015_REG_CONFIG_MODE_SINGLE;   // Single-shot mode (default)

  213.   // Set PGA/voltage range
  214.   config |= m_gain;

  215.   // Set channels
  216.   config |= ADS1015_REG_CONFIG_MUX_DIFF_2_3;       // AIN2 = P, AIN3 = N

  217.   // Set 'start single-conversion' bit
  218.   config |= ADS1015_REG_CONFIG_OS_SINGLE;

  219.   // Write config register to the ADC
  220.   writeRegister(m_i2cAddress, ADS1015_REG_POINTER_CONFIG, config);

  221.   // Wait for the conversion to complete
  222.   delay(m_conversionDelay);

  223.   // Read the conversion results
  224.   uint16_t res = readRegister(m_i2cAddress, ADS1015_REG_POINTER_CONVERT) >> m_bitShift;
  225.   if (m_bitShift == 0)
  226.   {
  227.    return (int16_t)res;
  228.   }
  229.   else
  230.   {
  231.    // Shift 12-bit results right 4 bits for the ADS1015,
  232.    // making sure we keep the sign bit intact
  233.    if (res > 0x07FF)
  234.    {
  235.      // negative number - extend the sign to 16th bit
  236.      res |= 0xF000;
  237.    }
  238.    return (int16_t)res;
  239.   }
  240. }

  241. /**************************************************************************/
  242. /*!
  243.    @brief  Sets up the comparator to operate in basic mode, causing the
  244.          ALERT/RDY pin to assert (go from high to low) when the ADC
  245.          value exceeds the specified threshold.

  246.          This will also set the ADC in continuous conversion mode.
  247. */
  248. /**************************************************************************/
  249. void Adafruit_ADS1015::startComparator_SingleEnded(uint8_t channel, int16_t threshold)
  250. {
  251.   // Start with default values
  252.   uint16_t config = ADS1015_REG_CONFIG_CQUE_1CONV   | // Comparator enabled and asserts on 1 match
  253.                ADS1015_REG_CONFIG_CLAT_LATCH   | // Latching mode
  254.                ADS1015_REG_CONFIG_CPOL_ACTVLOW | // Alert/Rdy active low   (default val)
  255.                ADS1015_REG_CONFIG_CMODE_TRAD   | // Traditional comparator (default val)
  256.                ADS1015_REG_CONFIG_DR_1600SPS   | // 1600 samples per second (default)
  257.                ADS1015_REG_CONFIG_MODE_CONTIN  | // Continuous conversion mode
  258.                ADS1015_REG_CONFIG_MODE_CONTIN;   // Continuous conversion mode

  259.   // Set PGA/voltage range
  260.   config |= m_gain;
  261.               
  262.   // Set single-ended input channel
  263.   switch (channel)
  264.   {
  265.    case (0):
  266.      config |= ADS1015_REG_CONFIG_MUX_SINGLE_0;
  267.      break;
  268.    case (1):
  269.      config |= ADS1015_REG_CONFIG_MUX_SINGLE_1;
  270.      break;
  271.    case (2):
  272.      config |= ADS1015_REG_CONFIG_MUX_SINGLE_2;
  273.      break;
  274.    case (3):
  275.      config |= ADS1015_REG_CONFIG_MUX_SINGLE_3;
  276.      break;
  277.   }

  278.   // Set the high threshold register
  279.   // Shift 12-bit results left 4 bits for the ADS1015
  280.   writeRegister(m_i2cAddress, ADS1015_REG_POINTER_HITHRESH, threshold<< m_bitShift);

  281.   // Write config register to the ADC
  282.   writeRegister(m_i2cAddress, ADS1015_REG_POINTER_CONFIG, config);
  283. }

  284. /**************************************************************************/
  285. /*!
  286.    @brief  In order to clear the comparator, we need to read the
  287.          conversion results.  This function reads the last conversion
  288.          results without changing the config value.
  289. */
  290. /**************************************************************************/
  291. int16_t Adafruit_ADS1015::getLastConversionResults()
  292. {
  293.   // Wait for the conversion to complete
  294.   delay(m_conversionDelay);

  295.   // Read the conversion results
  296.   uint16_t res = readRegister(m_i2cAddress, ADS1015_REG_POINTER_CONVERT) >> m_bitShift;
  297.   if (m_bitShift == 0)
  298.   {
  299.    return (int16_t)res;
  300.   }
  301.   else
  302.   {
  303.    // Shift 12-bit results right 4 bits for the ADS1015,
  304.    // making sure we keep the sign bit intact
  305.    if (res > 0x07FF)
  306.    {
  307.      // negative number - extend the sign to 16th bit
  308.      res |= 0xF000;
  309.    }
  310.    return (int16_t)res;
  311.   }
  312. }


复制代码

所有资料51hei提供下载:
4通道 ADS1115 小型 16位 高精密 模数转换器 通道 开发板模块.rar(1.38 MB, 下载次数: 23)


评分

黑币 +50
收起理由
+ 50
共享资料的黑币奖励!

查看全部评分

手机版|小黑屋|51黑电子论坛|51黑电子论坛6群QQ管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网