简介
RT-Thread DAC->ADC 使用, 就是DAC1输出模拟量, ADC1 读取模拟量转化成电压值, 基于开发板 : Bearpi Std(小熊派标准板)主芯片: STM32L431RCT6串口: Usart1DAC1: PA5, OUT2ADC1: PC2, IN3
将板子上的E53 接口, 5 和 6用排线相连, 即实现内部DAC1->ADC1
步骤
创建项目
参考 [BearPi RT-Thread项目创建](https://blog.csdn.net/halo_hsuh/article/details/134931437?spm=1001.2014.3001.5502)
RT-Thread开启ADC&DAC支持
rtconfig.h 中加入
#define RT_USING_ADC
#define RT_USING_DAC
将DAC&ADC文件库加入Keil项目中
env 环境加指令 scons --target=mdk5 -s
CubeMX配置DAC1
双击打开 board\CubeMX_Config\CubeMX_Config.ioc,
开启ADC1 通道3输入
开启DAC1 通道2输出
注册设备
board.h # 开启加入ADC1 & DAC1
#define BSP_USING_ADC
#define BSP_USING_ADC1
#define BSP_USING_DAC
#define BSP_USING_DAC1
这里编译的时候会有报错, 解决是在drv_adc.c & drv_dac.c加入头文件#include "drv_common.h" // 这里面包含了adc.h
编译烧录
串口使用list device时候可以发现adc1 & dac1设备了
用户代码使用dac 输出 adc输入
#include <rtthread.h>
#include <rtdevice.h>
#include <stdlib.h>#define ADC_DEV_NAME "adc1" /* ADC 设备名称 */
#define ADC_DEV_CHANNEL 3 /* ADC 通道 */
#define REFER_VOLTAGE 330 /* 参考电压 3.3V,数据精度乘以100保留2位小数*/
#define CONVERT_BITS (1 << 12) /* 转换位数为12位 */#define DAC_DEV_NAME "dac1" /* DAC 设备名称 */
#define DAC_DEV_CHANNEL 2 /* DAC 通道 */
#define REFER_VOLTAGE 330 /* 参考电压 3.3V,数据精度乘以100保留2位小数*/
#define CONVERT_BITS (1 << 12) /* 转换位数为12位 */static rt_adc_device_t adc_dev = RT_NULL;
static rt_dac_device_t dac_dev = RT_NULL;static rt_err_t adc_dac_init()
{ /* ADC Init */if (RT_NULL == adc_dev){adc_dev = (rt_adc_device_t)rt_device_find(ADC_DEV_NAME);if (adc_dev == RT_NULL){rt_kprintf("[adc] adc sample run failed! can't find %s device!\n", ADC_DEV_NAME);return RT_ERROR;}}/* DAC Init */if (RT_NULL == dac_dev){dac_dev = (rt_dac_device_t)rt_device_find(DAC_DEV_NAME);if (dac_dev == RT_NULL){rt_kprintf("dac sample run failed! can't find %s device!\n", DAC_DEV_NAME);return RT_ERROR;}}return RT_EOK;
}static rt_err_t adc_dac_set_enabled(rt_bool_t enabled)
{if (enabled){if (RT_EOK != rt_adc_enable(adc_dev, ADC_DEV_CHANNEL))return -RT_ERROR;if (RT_EOK != rt_dac_enable(dac_dev, DAC_DEV_CHANNEL))return -RT_ERROR;}else{if (RT_EOK != rt_adc_disable(adc_dev, ADC_DEV_CHANNEL))return -RT_ERROR;if (RT_EOK != rt_dac_disable(dac_dev, DAC_DEV_CHANNEL))return -RT_ERROR;}return RT_EOK;
}static int adc_dac_sample(int argc, char *argv[])
{rt_uint32_t dac_value, adc_value, dac_vol, adc_vol;adc_dac_init();adc_dac_set_enabled(RT_TRUE);/* 设置输出值 */dac_value = atoi(argv[1]);rt_dac_write(dac_dev, DAC_DEV_CHANNEL, dac_value);rt_kprintf("[dac] the value is :%d \n", dac_value);/* 转换为对应电压值 */dac_vol = dac_value * REFER_VOLTAGE / CONVERT_BITS;rt_kprintf("the voltage is :%d.%02d \n", dac_vol / 100, dac_vol % 100);/* 读取采样值 */adc_value = rt_adc_read(adc_dev, ADC_DEV_CHANNEL);rt_kprintf("[adc] the value is :%d \n", adc_value);/* 转换为对应电压值 */adc_vol = adc_value * REFER_VOLTAGE / CONVERT_BITS;rt_kprintf("[adc] the voltage is :%d.%02d \n", adc_vol / 100, adc_vol % 100);adc_dac_set_enabled(RT_FALSE);
}/* 导出到 msh 命令列表中 */
MSH_CMD_EXPORT(adc_dac_sample, adc&dac voltage convert sample);
编译烧录
使用串口软件输入指令 adc_dac_sample 100 这个100是转化值, dac将100值转化成模拟量,再由adc读取, 转化成数值,再转化成 电压, 如图
可以看到 dac输出100, adc接收到97dac输出200, adc接收到196
代码
完整项目代码
参考
BearPi Std 板从入门到放弃 - 引气入体篇(7)(DAC)
BearPi Std 板从入门到放弃 - 引气入体篇(8)(ADC)
BearPi Std 板从入门到放弃 - 引气入体篇(9)(DAC->ADC)
RT-Thread 设备和驱动 - DAC 设备
RT-Thread 设备和驱动 - ADC 设备