jinglixixi 发表于 2020-8-12 10:39

【ESK32-360测评】+ A/D转换及应用

<p>1. A/D采集</p>

<p>在例程中提供A/D采集功能,它是通过采集外接的电位器来模拟电位变化,进而来获得数据的变化。此外,它通过串口通信来输出采集结果。</p>

<p></p>

<p>图1 电位器调节电位</p>

<p>&nbsp;</p>

<p>经编译下载,程序的运行效果如图2所示。</p>

<p></p>

<p>图2 调整前的数据显示</p>

<p>&nbsp;</p>

<p>为了改善观察效果,特添加了延时函数,修改后程序内容为:</p>

<div aria-label="代码段 小部件" contenteditable="false" role="region" tabindex="-1">
<pre data-widget="codesnippet">
<code class="hljs language-cpp"><span class="hljs-keyword">void</span> mdelay(<span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">int</span> z)
{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">int</span> x,y;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span class="hljs-keyword">for</span>(x=z;x&gt;<span class="hljs-number">0</span>;x--)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span class="hljs-keyword">for</span>(y=<span class="hljs-number">100</span>;y&gt;<span class="hljs-number">0</span>;y--);
}</code></pre>
<img src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==" /><span style="background: url(&quot;https://bbs.eeworld.com.cn/static/editor/plugins/widget/images/handle.png&quot;) rgba(220, 220, 220, 0.498039); top: -15px; left: 0px; display: block;"><img height="15" role="presentation" src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==" title="点击并拖拽以移动" width="15" /></span></div>

<pre>
<code class="language-cpp">int main(void)
{
  RETARGET_Configuration();
  ADC_Configuration();
  ADC_Cmd(HT_ADC0, ENABLE);
  ADC_SoftwareStartConvCmd(HT_ADC0, ENABLE);

  while (1)
  {
    if (gADC_SingleEndOfConversion)
    {
      printf("\rPotentiometer level is %04d \r\n", (int)gPotentiometerLevel);  //115200
    }
         mdelay(100);
  }
}</code></pre>

<p>修改后的运行效果如图3所示,这样数据的变化就很便于观察了。</p>

<p><v:rect id="_x0000_s1026" stroked="f"></v:rect><v:rect stroked="f"> </v:rect></p>

<p>图3 调整后的数据显示</p>

<p>&nbsp;</p>

<p>2.数据显示</p>

<p>前面为数据的显示,已经增添了数值显示函数,此时就可以排上用场了,实现图4显示效果的主程序如下:</p>

<pre>
<code class="language-cpp">int main(void)
{
  RETARGET_Configuration();
  ADC_Configuration();
  ADC_Cmd(HT_ADC0, ENABLE);
  ADC_SoftwareStartConvCmd(HT_ADC0, ENABLE);
 
  LCD_Init();
  LCD_Config();
  LCD_Clear(0x2c5c);
  LCD_BackColorSet(0x2c5c)
  LCD_TextColorSet(Yellow);
  LCD_StringLineDisplay(Line9, "     ADC=");
  while (1)
  {
    if (gADC_SingleEndOfConversion)
    {
       LCD_ShowNum(Line9,180,adc,4);
    }
         mdelay(100);
  }
}</code></pre>

<p></p>

<p>图4 采集数据显示</p>

<p>&nbsp;</p>

<p>3.波形绘制</p>

<p>为了能更直观地观察数据的变化趋势,绘制数据的波形曲线是一种有效的方法。</p>

<p>在利用前面完成的画线函数和区域填充函数的检测上,实现图5所示效果的主程序如下:</p>

<pre>
<code class="language-cpp">int main(void)
{
  int adc,p,q,t,l,k;
 
  RETARGET_Configuration();
  ADC_Configuration();
  ADC_Cmd(HT_ADC0, ENABLE);
  ADC_SoftwareStartConvCmd(HT_ADC0, ENABLE);
 
  LCD_Init();
  LCD_Config();
  LCD_Clear(0x2c5c);
  LCD_BackColorSet(0x2c5c);
  LCD_TextColorSet(Yellow);
  LCD_RectDraw(10, 10, 200, 300);
  LCD_DrawLine(105,12 , 105,308 ,Red);
  LCD_DrawLine(10,25 , 210,25 ,Red);
  LCD_StringLineDisplay(Line9, "  n=     adc=");
  mdelay(50000);
 
  for(t=0;t&lt;60;t++)
  {
         l=t*5+10;
         LCD_DrawLine(210,l,214,l,Yellow);
  }
        
  for(t=0;t&lt;10;t++)
  {
         l=t*21;    
         LCD_DrawLine(210-l,6,210-l,10,Yellow);
  }
  t=10;
  l=15;
  q=210;
  p=210;
  k=21;
  while (1)
  {
         mdelay(50000);
    if(gADC_SingleEndOfConversion)
    {
                   adc=(int)gPotentiometerLevel;
                   LCD_ShowNum(Line9,220,adc,4);
                   p=adc/21;
                   l=t+5;
                   LCD_ShowNum(Line9,60,t/5-1,3);
                   LCD_DrawLine(q,t,p,l,Yellow);
                   t=l;
                   q=p;
       
                   if(t&gt;=305)
                   {
                      LCD_fill(11,11,199,297,0x2c5c);
                      LCD_DrawLine(105,12 , 105,308 ,Red);
                LCD_DrawLine(10,25 , 210,25 ,Red);
                t=10;
                      l=15;
                q=210;
                   }
    }
  }
}</code></pre>

<p></p>

<p>图5 波形曲线绘制</p>

damiaa 发表于 2020-8-12 11:27

谢谢分享!

jinglixixi 发表于 2020-8-12 16:24

damiaa 发表于 2020-8-12 11:27
谢谢分享!

<p>感谢支持!!!</p>

okhxyyo 发表于 2020-8-12 16:34

<p>谢谢楼主分享!赞!!</p>

jinglixixi 发表于 2020-8-12 16:39

okhxyyo 发表于 2020-8-12 16:34
谢谢楼主分享!赞!!

<p>感谢鼓励!!!</p>

maoshen 发表于 2020-8-13 11:38

<p>谢谢楼主分享! 还可以用在机器人上把</p>

jinglixixi 发表于 2020-8-14 08:32

maoshen 发表于 2020-8-13 11:38
谢谢楼主分享! 还可以用在机器人上把

<p>看用途吧,应该可以。</p>
页: [1]
查看完整版本: 【ESK32-360测评】+ A/D转换及应用