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