首页 > 技术知识 > 正文

AD采样是应用非常广泛的一种模式,其中包括DMA方式和非DMA方式。

rt-thread操作系统下ADC采样-adc数据采集系统rt-thread操作系统下ADC采样-adc数据采集系统1

部分代码如下:

复制#include #include #define ADC_DEV_NAME “adc0” /* ADC 设备名称 */ #define ADC_DEV_CHANNEL 7 /* ADC 通道 */ #define REFER_VOLTAGE 330 /* 参考电压 3.3V,数据精度乘以 100 保留 2 位小数*/ #define CONVERT_BITS (1 << 10) /* 转换位数为 10 位 */ static int adc_vol_sample(int argc, char *argv[]) { rt_adc_device_t adc_dev; rt_uint32_t value, vol; rt_err_t ret = RT_EOK; /* 查找设备 */ adc_dev = (rt_adc_device_t)rt_device_find(ADC_DEV_NAME); if (adc_dev == RT_NULL) { rt_kprintf(“adc sample run failed! cant find %s device!\n”, ADC_DEV_NAME); return RT_ERROR; } /* 使能设备 */ ret = rt_adc_enable(adc_dev, ADC_DEV_CHANNEL); /* 读取采样值 */ value = rt_adc_read(adc_dev, ADC_DEV_CHANNEL); rt_kprintf(“the value is :%d \n”, value); /* 转换为对应电压值 */ vol = value * REFER_VOLTAGE / CONVERT_BITS; rt_kprintf(“the voltage is :%d.%02d \n”, vol / 100, vol % 100); /* 关闭通道 */ ret = rt_adc_disable(adc_dev, ADC_DEV_CHANNEL); return ret; } /* 导出到 msh 命令列表中 */ MSH_CMD_EXPORT(adc_vol_sample, adc voltage convert sample);

最后做一个总结,首先新建一个 rt-thread studio 的工程,接着配置 rt-thread setting,使能 sdk 的 adc,配置完后 crtl+s 保存,接着在 application 文件夹下新建测试源文件,在源文件中添加官方的 adc 设备测试代码,后面编译好后下载到开发板就可以开始测量电压了。 审核编辑:汤梓红

猜你喜欢