文档章节

使用Arduino驱动 ADS1115 ADC采样芯片

o
 osc_g8254g7s
发布于 2019/08/19 20:54
字数 740
阅读 10
收藏 0

钉钉、微博极速扩容黑科技,点击观看阿里云弹性计算年度发布会!>>>

使用Arduino驱动 ADS1115 ADC采样芯片

如图,这是这颗ADC采样芯片的内部结构图,可以看到, 从芯片引脚到芯片内部的ADC模块,还有几个部分: MUX,PGA, 这两个分别叫做多路复用器、和增益放大器, MUX用来切换采样引脚,而PGA则用来切换测量量程。

我参照芯片官方的数据手册写出了这个简易的测试程序,只用到了最基本的IIC的通讯, 使用功能也比较有限,高级一些的功能都需要使用基于IIC通讯的SMBUS协议, 这个就没有继续深究了。

#include <Wire.h>

const int8_t ADS1115_address = 0x48;



enum ADS1115Conf{
  AIN0_2AIN1  =  0b000, // << 12,
  AIN0_2AIN3  =  0b001, // << 12,
  AIN1_2AIN3  =  0b010, // << 12,
  AIN2_2AIN3  =  0b011, // << 12,
  AIN0_2GND   =  0b100, // << 12,
  AIN1_2GND   =  0b101, // << 12,
  AIN2_2GND   =  0b110, // << 12,
  AIN3_2GND   =  0b111, // << 12,

  FSR_6_144V  =  0b000, // << 9,
  FSR_4_096V  =  0b001, // << 9,
  FSR_2_048V  =  0b010, // << 9,
  FSR_1_024V  =  0b011, // << 9,
  FSR_0_512V  =  0b100, // << 9,
  FSR_0_256V  =  0b101, // << 9,

  CONTINU_CONV = 0, //<<8
  SINGLE_SHOT =  1,

  SPS_8       =  0b000, // << 5,
  SPS_16      =  0b001, // << 5,
  SPS_32      =  0b010, // << 5,
  SPS_64      =  0b011, // << 5,
  SPS_128     =  0b100, // << 5,
  SPS_250     =  0b101, // << 5,
  SPS_475     =  0b110, // << 5,
  SPS_860     =  0b111, // << 5,

}typedef ADS1115Conf;

/**
 * [ADS1115Configure description]
 * @Author     叶鹏程
 * @DateTime   2019-08-19T20:44:30+0800
 * @discrption : 芯片初始化配置
 *  
 * @param      mux                      [输入通道配置]
 * @param      pga                      [量程设置]
 * @param      mode                     [单次或持续转换模式]
 * @param      dr                       [采样速率设置]
 */
void ADS1115Configure(ADS1115Conf mux, ADS1115Conf pga, ADS1115Conf mode, ADS1115Conf dr){

  uint16_t conf;

  Wire.beginTransmission(ADS1115_address);
  Wire.write(0x01);  //set Address Pointer Register as conf Register
  Wire.endTransmission();

  /*read chip's conf register data */
  Wire.requestFrom(ADS1115_address, 2);
  conf = 0;
  conf = Wire.read();
  conf = conf<<8;
  conf |= Wire.read();

  /*change it*/
  conf = 0x8000;
  conf &= (~(0x0007<< 12)); conf |= mux   << 12;
  conf &= (~(0x0007<< 9 )); conf |= pga   << 9;
  conf &= (~(0x0007<< 8 )); conf |= mode  << 8;
  conf &= (~(0x0007<< 5 )); conf |= dr    << 5;

  // conf = 0xf483;
  /* trans back*/
   Wire.beginTransmission(ADS1115_address);
   Wire.write(0x01);  //set Address Pointer Register as conf Register
   Wire.write(uint8_t(conf>>8));  //conf MSB
   Wire.write(uint8_t(conf));  //conf LSB
   Wire.endTransmission();

    /**/
  Wire.beginTransmission(ADS1115_address);
  Wire.write(0x00);  //set Address Pointer Register as conf Register
  Wire.endTransmission();
}

/**
 * [ADS1115SetChannel description]
 * @Author     叶鹏程
 * @DateTime   2019-08-19T20:43:57+0800
 * @discrption :芯片输入通道设置
 *  
 * @param      mux                      [description]
 */
void ADS1115SetChannel(ADS1115Conf mux){

  uint16_t conf;

  Wire.beginTransmission(ADS1115_address);
  Wire.write(0x01);  //set Address Pointer Register as conf Register
  Wire.endTransmission();

  /*read chip's conf register data */
  Wire.requestFrom(ADS1115_address, 2);
  conf = 0;
  conf = Wire.read();
  conf = conf<<8;
  conf |= Wire.read();

  /*change it*/
  conf = conf & (~(0x0007<< 12)); conf |= mux << 12;
  

  /* trans back*/
  Wire.beginTransmission(ADS1115_address);
  Wire.write(0x01);  //set Address Pointer Register as conf Register
  Wire.write(uint8_t(conf>>8));  //conf MSB
  Wire.write(uint8_t(conf));  //conf LSB
  Wire.endTransmission();

   /**/
  Wire.beginTransmission(ADS1115_address);
  Wire.write(0x00);  //set Address Pointer Register as conf Register
  Wire.endTransmission();
}

int16_t ADS1115ReadResult(void){
  int16_t ret;

  Wire.requestFrom(ADS1115_address, 2);
  ret = 0;
  ret = Wire.read();
  ret = ret<<8;
  ret |= Wire.read();

  return ret;
}

void setup() {
  // put your setup code here, to run once:
  Wire.begin();
 
  Serial.begin(115200);
  while (!Serial);             // Leonardo: wait for serial monitor
  Serial.println("Arduino Running!");
  Serial.println("configing ADS1115.");
  delay(200);
  ADS1115Configure(AIN3_2GND, FSR_6_144V, CONTINU_CONV, SPS_250);
  Serial.println("configed."); 
  
  Wire.beginTransmission(ADS1115_address);
   Wire.write(0x00);  //set Address Pointer Register as conv Register
   Wire.write(0x00);  
   Wire.write(0x00);
   Wire.endTransmission();
}

void loop() {
  int16_t adc_result;
  double v;
  Serial.print("adc conversion result: ");
  
  ADS1115SetChannel(AIN0_2GND);
  delay(10);
  adc_result = ADS1115ReadResult(); v = adc_result * 6.144 / 0x7fff;
  Serial.print("\tA0: "); Serial.print(v); Serial.print('V');
  
  ADS1115SetChannel(AIN1_2GND);
  delay(10);
  adc_result = ADS1115ReadResult(); v = adc_result * 6.144 / 0x7fff;
  Serial.print("\tA1: "); Serial.print(v); Serial.print('V'); 

  ADS1115SetChannel(AIN2_2GND);
  delay(10);
  adc_result = ADS1115ReadResult(); v = adc_result * 6.144 / 0x7fff;
  Serial.print("\tA2: "); Serial.print(v); Serial.print('V'); 
  
  ADS1115SetChannel(AIN3_2GND);
  delay(10);
  adc_result = ADS1115ReadResult(); v = adc_result * 6.144 / 0x7fff;
  Serial.print("\tA3: "); Serial.print(v); Serial.print('V'); 

  Serial.println();
  delay(100);
  // put your main code here, to run repeatedly:

}
o
粉丝 0
博文 500
码字总数 0
作品 0
私信 提问
加载中
请先登录后再评论。

暂无文章

263. Ugly Number

题目: 263. Ugly Number 题目地址:https://leetcode.com/problems/ugly-number/ Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers......

JiaMing
33分钟前
46
0
HCIA_ARP01

ARP(地址解析协议) eNSP 常用路由器:AR2220 常用交换机:S5700、S3700 常用终端:PC、MCS(主播服务器) 设备连线:Copper(以太网用到的双绞线)、Serial(串口线,2SA接口)、Auto(自动连...

创业789
36分钟前
13
0
如何在Rails 4中使用问题 - How to use concerns in Rails 4

问题: The default Rails 4 project generator now creates the directory "concerns" under controllers and models. 默认的Rails 4项目生成器现在在控制器和模型下创建目录“Concer”。 ......

fyin1314
41分钟前
19
0
【LeetCode】 57 括号生成

题目: 解题思路: https://leetcode-cn.com/problems/generate-parentheses/solution/hui-su-suan-fa-by-liweiwei1419/ 代码: import java.util.ArrayList;import java.util.List;publ......

JaneRoad
昨天
8
0
度小满上线“推有钱”贷款推广平台,合伙人直推佣金为贷款金额的2.5%

来源 | 镭射财经 作者 | 黄老邪 2020年,蓬勃发展的消金行业受到新冠肺炎疫情的冲击,用户增长放缓、逾期率上升成为共性问题。但疫情或许只是导火索,对于消金行业来说,用户增长红利的消退,...

镭射财经
昨天
13
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部