Skip to content

Commit 1267ecb

Browse files
committed
Add M5_ADS1100
0 parents  commit 1267ecb

File tree

7 files changed

+445
-0
lines changed

7 files changed

+445
-0
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2021 M5Stack
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# M5_ADS1100
2+
3+
## Overview
4+
5+
M5Stack-**UNIT ADC**
6+
7+
## Related Link
8+
9+
[Docment & Datasheet](https://docs.m5stack.com/en/unit/adc)
10+
11+
## License
12+
13+
[UNIT_ENV - MIT](LICENSE)
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
*******************************************************************************
3+
* Copyright (c) 2021 by M5Stack
4+
*
5+
* Visit the website for more information:https://docs.m5stack.com/en/core/gray
6+
* 获取更多资料请访问:https://docs.m5stack.com/zh_CN
7+
*
8+
* describe: ADC. A/D转换器
9+
* date:2021/8/18
10+
*******************************************************************************
11+
Please connect to Port A,Use ADC Unit to convert 0 ~ 12V analog voltage into 16-bit data and display it on the screen.
12+
请连接端口A,利用ADC单元将0 ~ 12V模拟电压转换成16位数据显示在屏幕上。
13+
*/
14+
15+
#include <M5Stack.h>
16+
#include "M5_ADS1100.h"
17+
18+
ADS1100 ads;
19+
20+
void setup(void)
21+
{
22+
M5.begin(); //Init M5Stack. 初始化M5Stack
23+
M5.Power.begin(); //Init power 初始化电源模块
24+
M5.lcd.setTextSize(2); //Set the text size to 2. 设置文字大小为2
25+
26+
// The address can be changed making the option of connecting multiple devices
27+
// 地址可以改变,以连接多个设备
28+
ads.getAddr_ADS1100(ADS1100_DEFAULT_ADDRESS); // 0x48, 1001 000 (ADDR = GND)
29+
30+
//The ADC gain (PGA). ADC增益(PGA)
31+
ads.setGain(GAIN_ONE); // 1x gain(default)
32+
// ads.setGain(GAIN_TWO); // 2x gain
33+
// ads.setGain(GAIN_FOUR); // 4x gain
34+
// ads.setGain(GAIN_EIGHT); // 8x gain
35+
36+
//Device operating mode. 设备工作模式
37+
ads.setMode(MODE_CONTIN); // Continuous conversion mode (default)
38+
// ads.setMode(MODE_SINGLE); // Single-conversion mode
39+
40+
//Data rate. 数据速率
41+
ads.setRate(RATE_8); // 8SPS (default)
42+
// ads.setRate(RATE_16); // 16SPS
43+
// ads.setRate(RATE_32); // 32SPS
44+
// ads.setRate(RATE_128); // 128SPS
45+
46+
ads.setOSMode(OSMODE_SINGLE); // Set to start a single-conversion. 设置开始一次转换
47+
48+
ads.begin(); //Sets up the Hardware. 设置硬件
49+
}
50+
51+
void loop(void)
52+
{
53+
byte error;
54+
int8_t address;
55+
56+
address = ads.ads_i2cAddress;
57+
Wire.beginTransmission(address);
58+
error = Wire.endTransmission();
59+
if (error == 0) //If the device is connected. 如果连接上设备
60+
{
61+
int16_t result;
62+
result = ads.Measure_Differential();
63+
M5.Lcd.fillScreen(BLACK);
64+
char data[20] = { 0 };
65+
sprintf(data, "%d", result);
66+
M5.Lcd.drawCentreString(data, 160, 100, 4);
67+
}
68+
else
69+
{
70+
M5.Lcd.drawString("No Found ADC sensor.",20, 100, 2);
71+
}
72+
delay(1000);
73+
}

library.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "M5_ADS1100",
3+
"description": "Library for M5_ADS1100",
4+
"keywords": "ADS1100",
5+
"authors": {
6+
"name": "M5Stack",
7+
"url": "http://www.m5stack.com"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "https://github.com/m5stack/M5_ADS1100.git"
12+
},
13+
"version": "0.0.1",
14+
"framework": "arduino",
15+
"platforms": "espressif32"
16+
}

library.properties

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
name=M5_ADS1100
2+
version=0.0.1
3+
author=M5Stack
4+
maintainer=M5Stack,Sean
5+
sentence=Library for M5_ADS1100
6+
paragraph=See more on http://M5Stack.com
7+
category=Device Control
8+
url=https://github.com/m5stack/M5_ADS1100
9+
architectures=esp32
10+
includes=ADS1100.h

src/M5_ADS1100.cpp

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
#include "M5_ADS1100.h"
2+
3+
4+
#if ARDUINO >= 100
5+
#include "Arduino.h"
6+
#else
7+
#include "WProgram.h"
8+
#endif
9+
10+
#include <Wire.h>
11+
12+
/**************************************************************************/
13+
/*
14+
Abstract away platform differences in Arduino wire library
15+
*/
16+
/**************************************************************************/
17+
static uint8_t i2cread(void)
18+
{
19+
#if ARDUINO >= 100
20+
return Wire.read();
21+
#else
22+
return Wire.receive();
23+
#endif
24+
}
25+
26+
/**************************************************************************/
27+
/*
28+
Abstract away platform differences in Arduino wire library
29+
*/
30+
/**************************************************************************/
31+
static void i2cwrite(uint8_t x)
32+
{
33+
#if ARDUINO >= 100
34+
Wire.write((uint8_t)x);
35+
#else
36+
Wire.send(x);
37+
#endif
38+
}
39+
40+
/**************************************************************************/
41+
/*
42+
Writes 8-bits to the destination register
43+
*/
44+
/**************************************************************************/
45+
static void writeRegister(uint8_t i2cAddress, uint8_t value)
46+
{
47+
Wire.beginTransmission(i2cAddress);
48+
i2cwrite((uint8_t)value);
49+
Wire.endTransmission();
50+
}
51+
52+
/**************************************************************************/
53+
/*
54+
Reads 16-bits from the destination register
55+
*/
56+
/**************************************************************************/
57+
static uint16_t readRegister(uint8_t i2cAddress)
58+
{
59+
Wire.beginTransmission(i2cAddress);
60+
Wire.endTransmission();
61+
Wire.requestFrom(i2cAddress, (uint8_t)2);
62+
return (int16_t)((i2cread() << 8) | i2cread());
63+
}
64+
65+
/**************************************************************************/
66+
/*
67+
Instantiates a new ADS1100 class with appropriate properties
68+
*/
69+
/**************************************************************************/
70+
void ADS1100::getAddr_ADS1100(uint8_t i2cAddress)
71+
{
72+
ads_i2cAddress = i2cAddress;
73+
ads_conversionDelay = ADS1100_CONVERSIONDELAY;
74+
}
75+
76+
/**************************************************************************/
77+
/*
78+
Sets up the Hardware
79+
*/
80+
/**************************************************************************/
81+
void ADS1100::begin()
82+
{
83+
Wire.begin();
84+
}
85+
86+
/**************************************************************************/
87+
/*
88+
Sets the Operational status/single-shot conversion start
89+
This determines the operational status of the device
90+
*/
91+
/**************************************************************************/
92+
void ADS1100::setOSMode(adsOSMode_t osmode)
93+
{
94+
ads_osmode = osmode;
95+
}
96+
97+
/**************************************************************************/
98+
/*
99+
Gets the Operational status/single-shot conversion start
100+
*/
101+
/**************************************************************************/
102+
adsOSMode_t ADS1100::getOSMode()
103+
{
104+
return ads_osmode;
105+
}
106+
107+
/**************************************************************************/
108+
/*
109+
Sets the Device operating mode
110+
This controls the current operational mode of the ADS1100
111+
*/
112+
/**************************************************************************/
113+
void ADS1100::setMode(adsMode_t mode)
114+
{
115+
ads_mode = mode;
116+
}
117+
118+
/**************************************************************************/
119+
/*
120+
Gets the Device operating mode
121+
*/
122+
/**************************************************************************/
123+
adsMode_t ADS1100::getMode()
124+
{
125+
return ads_mode;
126+
}
127+
128+
/**************************************************************************/
129+
/*
130+
Sets the Date Rate
131+
This controls the data rate setting
132+
*/
133+
/**************************************************************************/
134+
void ADS1100::setRate(adsRate_t rate)
135+
{
136+
ads_rate = rate;
137+
}
138+
139+
/**************************************************************************/
140+
/*
141+
Gets the Date Rate
142+
*/
143+
/**************************************************************************/
144+
adsRate_t ADS1100::getRate()
145+
{
146+
return ads_rate;
147+
}
148+
149+
/**************************************************************************/
150+
/*
151+
Sets the gain and input voltage range
152+
This configures the programmable gain amplifier
153+
*/
154+
/**************************************************************************/
155+
void ADS1100::setGain(adsGain_t gain)
156+
{
157+
ads_gain = gain;
158+
}
159+
160+
/**************************************************************************/
161+
/*
162+
Gets a gain and input voltage range
163+
*/
164+
/**************************************************************************/
165+
adsGain_t ADS1100::getGain()
166+
{
167+
return ads_gain;
168+
}
169+
170+
/**************************************************************************/
171+
/*
172+
Reads the conversion results, measuring the voltage
173+
difference between the P and N input
174+
Generates a signed value since the difference can be either
175+
positive or negative
176+
*/
177+
/**************************************************************************/
178+
int16_t ADS1100::Measure_Differential()
179+
{
180+
// Start with default values
181+
uint16_t config;
182+
183+
// Set Operational status/single-shot conversion start
184+
config |= ads_osmode;
185+
186+
// Set Device operating mode
187+
config |= ads_mode;
188+
189+
// Set Data rate
190+
config |= ads_rate;
191+
192+
// Set PGA/voltage range
193+
config |= ads_gain;
194+
195+
// Write config register to the ADC
196+
writeRegister(ads_i2cAddress, config);
197+
198+
// Wait for the conversion to complete
199+
delay(ads_conversionDelay);
200+
201+
// Read the conversion results
202+
uint16_t raw_adc = readRegister(ads_i2cAddress);
203+
return (int16_t)raw_adc;
204+
}

0 commit comments

Comments
 (0)