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