久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 3134|回復: 0
打印 上一主題 下一主題
收起左側

ADS1115的Arduino程序和pdf資料

[復制鏈接]
跳轉到指定樓層
樓主
有需要的請自取




單片機源程序如下:
  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)


評分

參與人數 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎勵!

查看全部評分

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏1 分享淘帖 頂 踩
回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規則

手機版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

Powered by 單片機教程網

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 交专区videossex农村 | 草久久 | 亚洲资源在线 | 人人精品| 欧美日韩一区二区视频在线观看 | 久久久激情视频 | 一区二区三区欧美在线 | 成人欧美一区二区三区黑人孕妇 | 精品久久久久久久人人人人传媒 | 成人h电影在线观看 | 欧美久久久久久久久中文字幕 | 精品中文字幕一区二区 | 国产激情视频网 | 99精品欧美一区二区三区综合在线 | 免费在线观看一级毛片 | 国产综合精品一区二区三区 | 欧美 日韩 视频 | 国内精品久久久久 | 欧美成人a∨高清免费观看 老司机午夜性大片 | 欧美国产一区二区 | 日韩黄色小视频 | 美女久久久久 | 人人艹人人 | 国产精品成人一区 | 欧美日韩精品一区二区三区四区 | 国产日韩精品在线 | 欧美日韩不卡合集视频 | 色婷婷久久久久swag精品 | 日韩一区在线播放 | 亚洲午夜精品在线观看 | 欧美一区二区三区视频 | 午夜小电影| 欧美一级在线 | 91精品久久久久久久久 | 久久久精品一区 | 国产一区2区 | 亚洲美乳中文字幕 | 亚洲高清免费观看 | 大陆一级毛片免费视频观看 | 午夜小视频在线播放 | 97精品超碰一区二区三区 |