【ESP32-C3-DevKitM-1】ESP32-C3的ADC
<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri"><b><span style="font-size:14.0000pt"><span style="font-family:黑体"><span style="font-weight:bold"><span style="font-style:normal">ADC的评测</span></span></span></span></b></span></span></p><p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri"><span style="font-size:10.5000pt"><span style="font-family:宋体">ESP-C3<font face="宋体">的</font><font face="Calibri">ADC</font><font face="宋体">有两个</font><font face="Calibri">,5</font><font face="宋体">通道的已校准的</font><font face="Calibri">ADC1</font><font face="宋体">和</font><font face="Calibri">1</font><font face="宋体">通道的未校准的</font><font face="Calibri">ADC2</font><font face="宋体">,采样速率为</font><font face="Calibri">200Ksps</font><font face="宋体">,</font><font face="Calibri">DHL</font><font face="宋体">和</font><font face="Calibri">INL</font><font face="宋体">分别为</font></span></span><span style="font-size:10.5000pt"><span style="font-family:宋体">±</span></span><span style="font-size:10.5000pt"><span style="font-family:宋体">7LSB和</span></span><span style="font-size:10.5000pt"><span style="font-family:宋体">±12LSB,只能说达到了够用的级别。</span></span></span></span></p>
<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri"><span style="font-size:10.5000pt"><span style="font-family:宋体"><font face="宋体">按照之前的方法,新建一个工程,添加模板,如下图,然后编译运行</font></span></span></span></span></p>
<p style="text-align:justify"></p>
<p style="text-align:justify"><span style="color:#e74c3c;"><span style="font-size:10.5pt"><span style="font-family:Calibri"><span style="font-size:10.5000pt"><span style="font-family:宋体"><font face="宋体">这里要注意一点,选择模板的时候要选择</font>esp32-c3<font face="宋体">中的</font><font face="Calibri">adc</font><font face="宋体">例程,而不是</font><font face="Calibri">single-read</font><font face="宋体">中的</font><font face="Calibri">adc</font><font face="宋体">例程。</font></span></span></span></span></span></p>
<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri"><span style="font-size:10.5000pt"><span style="font-family:宋体"><font face="宋体">例程中包含用</font>DMA<font face="宋体">传输数据和普通的,我接下来是测试普通的</font><font face="Calibri">ADC</font><font face="宋体">采样。</font></span></span></span></span></p>
<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri"><span style="font-size:10.5000pt"><span style="font-family:宋体"><font face="宋体">我稍微改动了一下</font>ESP-IDF<font face="宋体">提供的例程,这样更好讲,如下图</font></span></span></span></span></p>
<p style="text-align:justify"></p>
<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri"><span style="font-size:10.5000pt"><span style="font-family:宋体"><font face="宋体">我讲一下基本流程。</font></span></span></span></span></p>
<ol>
<li style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri"><span style="font-size:10.0000pt"><span style="background:#e8f2fe"><span style="font-family:Consolas"><span style="color:#000000">adc1_config_width(</span></span></span></span><i><span style="font-size:10.0000pt"><span style="background:#e8f2fe"><span style="font-family:Consolas"><span style="color:#0000c0"><span style="font-style:italic">ADC_WIDTH_BIT_12</span></span></span></span></span></i><span style="font-size:10.0000pt"><span style="background:#e8f2fe"><span style="font-family:Consolas"><span style="color:#000000">);</span></span></span></span> <span style="font-size:10.5000pt"><span style="font-family:宋体"><font face="宋体">这句为配置</font>ADC1<font face="宋体">的位数,可为</font><font face="Calibri">9-12</font><font face="宋体">位</font></span></span></span></span></li>
<li style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri"><span style="font-size:10.0000pt"><span style="background:#e8f2fe"><span style="font-family:Consolas"><span style="color:#000000">adc1_config_channel_atten(</span></span></span></span><i><span style="font-size:10.0000pt"><span style="background:#e8f2fe"><span style="font-family:Consolas"><span style="color:#0000c0"><span style="font-style:italic">ADC1_CHANNEL_4</span></span></span></span></span></i><span style="font-size:10.0000pt"><span style="background:#e8f2fe"><span style="font-family:Consolas"><span style="color:#000000">, </span></span></span></span><i><span style="font-size:10.0000pt"><span style="background:#e8f2fe"><span style="font-family:Consolas"><span style="color:#0000c0"><span style="font-style:italic">ADC_ATTEN_DB_11</span></span></span></span></span></i><span style="font-size:10.0000pt"><span style="background:#e8f2fe"><span style="font-family:Consolas"><span style="color:#000000">);</span></span></span></span><span style="font-size:10.5000pt"><span style="font-family:宋体"><font face="宋体">这句是配置通道和衰减数,通道</font>4<font face="宋体">的话对应的就是</font><font face="Calibri">GPIO4</font><font face="宋体">,</font><font face="Calibri">adc_atten_t</font><font face="宋体">衰减系数有</font><font face="Calibri">4</font><font face="宋体">种,相对应有</font><font face="Calibri">4</font><font face="宋体">种不同的电压测量范围,如下图,所以要选择合适的系数</font></span></span></span></span></li>
</ol>
<p style="text-align:justify"></p>
<ol start="3">
<li style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri"><span style="font-size:10.5000pt"><span style="font-family:宋体"><font face="宋体">要进行电压校准</font></span></span></span></span></li>
</ol>
<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri"><span style="font-size:10.0000pt"><span style="background:#e8f2fe"><span style="font-family:Consolas"><span style="color:#000000">adc_chars = calloc(1, sizeof(esp_adc_cal_characteristics_t));</span></span></span></span></span></span></p>
<p style="text-align:left"><span style="font-size:10.5pt"><span style="font-family:Calibri"><span style="font-size:10.0000pt"><span style="background:#e8f2fe"><span style="font-family:Consolas"><span style="color:#000000">esp_adc_cal_characterize(ADC_UNIT_1,ADC_ATTEN_DB_11, ADC_WIDTH_BIT_12, 1100, adc_chars);</span></span></span></span></span></span></p>
<p style="text-align:left"><span style="font-size:10.5pt"><span style="font-family:Calibri"><span style="font-size:10.5000pt"><span style="font-family:宋体"><font face="宋体">首先先用</font></span></span><span style="font-size:10.5000pt"><span style="font-family:Calibri">calloc</span></span><span style="font-size:10.5000pt"><span style="font-family:宋体"><font face="宋体">申请一段内存,然后调用</font></span></span><span style="font-size:10.0000pt"><span style="background:#e8f2fe"><span style="font-family:Consolas"><span style="color:#000000">esp_adc_cal_characterize</span></span></span></span><span style="font-size:10.0000pt"><span style="background:#e8f2fe"><span style="font-family:Consolas"><span style="color:#000000"> API</span></span></span></span><span style="font-size:10.5000pt"><span style="font-family:宋体"><font face="宋体">,具体的输入参数可以看</font>API<font face="宋体">介绍,其中第四个参数是默认的参考电压,</font><font face="Calibri">ESP32-C3</font><font face="宋体">的参考电压为</font><font face="Calibri">1100mv</font><font face="宋体">,但是我发现一个问题,第四个参数其实填什么数字都可以,比如</font><font face="Calibri">0mv</font><font face="宋体">或者</font><font face="Calibri">3300mv</font><font face="宋体">,并不会影响其结果。</font></span></span></span></span></p>
<ol start="4">
<li style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri"><span style="font-size:10.5000pt"><span style="font-family:宋体"><font face="宋体">进行读取</font>ADC<font face="宋体">原始数值</font></span></span></span></span></li>
</ol>
<p style="text-indent:10.0000pt; text-align:left"><span style="font-size:10.5pt"><span style="font-family:Calibri"><span style="font-size:10.0000pt"><span style="background:#e8f2fe"><span style="font-family:Consolas"><span style="color:#000000">adc_reading= adc1_get_raw(</span></span></span></span><i><span style="font-size:10.0000pt"><span style="background:#d4d4d4"><span style="font-family:Consolas"><span style="color:#0000c0"><span style="font-style:italic">ADC1_CHANNEL_4</span></span></span></span></span></i><span style="font-size:10.0000pt"><span style="background:#e8f2fe"><span style="font-family:Consolas"><span style="color:#000000">);</span></span></span></span></span></span></p>
<ol start="5">
<li style="text-align:left"><span style="font-size:10.5pt"><span style="font-family:Calibri"><span style="font-size:10.0000pt"><span style="background:#e8f2fe"><span style="font-family:宋体"><span style="color:#000000"><font face="宋体">进行数据转换</font></span></span></span></span></span></span></li>
</ol>
<p style="text-align:left"><span style="font-size:10.5pt"><span style="font-family:Calibri"><span style="font-size:10.5000pt"><span style="font-family:宋体"><font face="宋体">这里本来我是想用以前的</font>ADC<font face="宋体">数值转换的思想,就是原始数据直接乘上一个系数,后来发现数据相差较大,经过询问官方技术后,得知要用</font></span></span><span style="font-size:10.0000pt"><span style="background:#e8f2fe"><span style="font-family:Consolas"><span style="color:#000000">esp_adc_cal_raw_to_voltage(adc_reading, adc_chars);</span></span></span></span><span style="font-size:10.5000pt"><span style="font-family:宋体"><font face="宋体">转换为真实数据,因为还需要引入存有校准数据的</font>adc_chars<font face="宋体">,最后打印出来</font></span></span></span></span></p>
<p style="text-align:left"> </p>
<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri"><b><span style="font-size:12.0000pt"><span style="font-family:黑体"><span style="font-weight:bold"><span style="font-style:normal"><font face="黑体">实际测试</font></span></span></span></span></b></span></span></p>
<p style="text-align:justify"></p>
<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri"><span style="font-size:10.5000pt"><span style="font-family:宋体"><font face="宋体">精度还是能接受的,可能输入</font>DC-DC<font face="宋体">电源也有一些影响。</font></span></span></span></span></p>
<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri"><span style="font-size:10.5000pt"><span style="font-family:宋体"><span style="color:#ff0000"><font face="宋体">还有一点,最好不要超推荐量程工作,比如在衰减</font>11db<font face="宋体">时,</font><font face="Calibri">ADC</font><font face="宋体">最高能测</font><font face="Calibri">3000mv</font><font face="宋体">左右,推荐为</font><font face="Calibri">2600mv</font><font face="宋体">,当越到后面差的越大,可能有</font><font face="Calibri">50-100mv</font><font face="宋体">的误差。</font></span></span></span></span></span></p>
<p>芯片虽然便宜 但是该有的功能还是都有的<br/>ADC测量2600mV以上误差较大问题数据手册里说了吗?</p> <p>第四个参数其实填什么数字都可以,</p>
<p>因为它是默认的电压</p>
<p>有DMA的ADC吗?没找到唉。在S系列里面DMA都是给IIS的,要想用ADC的DMA就必须走IIS曲线实现。但IIS工程好像并不支持C3系列</p>
tinnu 发表于 2021-7-1 22:25
有DMA的ADC吗?没找到唉。在S系列里面DMA都是给IIS的,要想用ADC的DMA就必须走IIS曲线实现。但IIS工程好像 ...
<p>这里面的continuous_read就是DMA的ADC例程</p>
littleshrimp 发表于 2021-7-1 22:03
芯片虽然便宜 但是该有的功能还是都有的ADC测量2600mV以上误差较大问题数据手册里说了吗?
<p>确实 该有的都有的,性价比蛮高的</p>
<p>在在线编程手册有提及</p>
<p></p>
页:
[1]