【中科蓝讯AB32VG1 RISC-V板“碰上”RTT测评】ADC
[复制链接]
本帖最后由 xiyue521 于 2021-5-16 23:33 编辑
首先使能adc后寻找设备挂载: (rt_adc_device_t)rt_device_find(ADC_DEV_NAME)
然后使能adc:rt_adc_enable(adc_dev, 0);
获取adc的值:value = rt_adc_read(adc_dev, 0)
因为setting那没有讲adc0在哪个引脚,所以就去手册查找了,是PA5:还纠结了好久最终还是找到了。
代码如下,创建一个线程,然后初始化adc,使能adc,读取adc值:
#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); // 初始化线程
旋转电位器:
还有就是kprintf不支持浮点数的打印,所以要处理一下,把整数和小数分开打印就行了
|