xiyue521 发表于 2021-5-16 02:51

【中科蓝讯AB32VG1 RISC-V板“碰上”RTT测评】ADC

本帖最后由 xiyue521 于 2021-5-16 23:33 编辑

<p class="imagemiddle" style="text-align: center;">&nbsp;</p>

<p><span style="font-size:20px;">首先使能adc后寻找设备挂载:&nbsp;(rt_adc_device_t)rt_device_find(ADC_DEV_NAME)<br />
<br />
然后使能adc:rt_adc_enable(adc_dev, 0);</span></p>

<p><span style="font-size:20px;">获取adc的值:value = rt_adc_read(adc_dev, 0)</span></p>

<p><span style="font-size:20px;">因为setting那没有讲adc0在哪个引脚,所以就去手册查找了,是PA5:还纠结了好久最终还是找到了。</span><br />
&nbsp;</p>

<p class="imagemiddle" style="text-align: center;">&nbsp;</p>

<p class="imagemiddle" style="text-align: center;">&nbsp;</p>

<p class="imagemiddle" style="text-align: center;">&nbsp;</p>

<p class="imagemiddle" style="text-align: center;"><span style="font-size:20px;"></span></p>

<p class="imagemiddle" style="text-align: center;"><span style="font-size:20px;"></span></p>

<p class="imagemiddle" style="text-align: center;">&nbsp;</p>

<p class="imagemiddle" style="text-align: center;"><span style="font-size:20px;"></span></p>

<p class="imagemiddle" style="text-align: center;"><span style="font-size:20px;"></span></p>

<p class="imagemiddle"><span style="font-size:20px;">代码如下,创建一个线程,然后初始化adc,使能adc,读取adc值:</span></p>

<pre>
<code>
#define ADC_DEV_NAME      "adc0"


static void adc0(void* pdata)
{
    rt_err_t ret = RT_EOK;
    rt_uint32_t value,vol;
    rt_adc_device_t adc_dev;

    adc_dev = (rt_adc_device_t)rt_device_find(ADC_DEV_NAME);
    if (adc_dev == RT_NULL)
    {
      rt_kprintf("can't find %s device!\n", ADC_DEV_NAME);
    }

    ret = rt_adc_enable(adc_dev, 0);

    while(1)
    {
      value = rt_adc_read(adc_dev, 0);
      rt_kprintf("\nthe value is   :%d \n", value);
      vol = value * 3300 / 1024;
      rt_kprintf("the voltage is :%d.%03d \n", vol / 1000, vol % 1000);

      rt_thread_mdelay(100);   
   
    }
}


static int adc_Thread_Init(void)
{
    rt_thread_t thread = RT_NULL;
    thread = rt_thread_create("adc_thread", adc0, RT_NULL, 512, 10, 10);
    if(thread == RT_NULL)
    {
      rt_kprintf("Thread_ADC Init ERROR");
      return RT_ERROR;
    }
    rt_thread_startup(thread);


}
INIT_APP_EXPORT(adc_Thread_Init);// 初始化线程</code></pre>

<p class="imagemiddle"><span style="font-size:20px;">旋转电位器:</span></p>

<p class="imagemiddle"><span style="font-size:20px;">还有就是kprintf不支持浮点数的打印,所以要处理一下,把整数和小数分开打印就行了</span></p>

<p class="imagemiddle" style="text-align: center;"><span style="font-size:20px;"></span></p>

<p class="imagemiddle" style="text-align: center;">&nbsp;</p>

<p class="imagemiddle" style="text-align: center;">&nbsp;</p>

<p class="imagemiddle" style="text-align: center;">&nbsp;</p>

<p>&nbsp;</p>

soso 发表于 2021-5-17 10:10

<p>加油,期待楼主后续。</p>
页: [1]
查看完整版本: 【中科蓝讯AB32VG1 RISC-V板“碰上”RTT测评】ADC