RT-Thread Settings配置,将DAC驱动打开(具体打开步骤如下图所示)。
1)打开DAC
2)勾选输出通道
3)检查是否配置好IO口
4)生成MDK代码
(1)在/drivers/include/config文件夹下新建dac_config.h
#ifndef __DAC_CONFIG_H__
#define __DAC_CONFIG_H__
#include <rtthread.h>
#ifdef __cplusplus
extern "C" {
#endif
#ifdef BSP_USING_DAC1
#ifndef DAC1_CONFIG
#define DAC1_CONFIG \
{ \
.Instance = DAC1, \
}
#endif /* DAC1_CONFIG */
#endif /* BSP_USING_DAC1 */
#ifdef BSP_USING_DAC2
#ifndef DAC2_CONFIG
#define DAC2_CONFIG \
{ \
.Instance = DAC2, \
}
#endif /* DAC2_CONFIG */
#endif /* BSP_USING_DAC2 */
#ifdef __cplusplus
}
#endif
#endif /* __DAC_CONFIG_H__ */
(2)修改/drivers/include/drv_config.h
#if defined(SOC_SERIES_STM32F0)
#include "config/dma_config.h"
#include "config/uart_config.h"
#include "config/spi_config.h"
#include "config/tim_config.h"
#include "config/pwm_config.h"
#include "config/adc_config.h"
#elif defined(SOC_SERIES_STM32F1)
#include "config/dac_config.h" //新增
#include "config/dma_config.h"
#include "config/uart_config.h"
#include "config/spi_config.h"
#include "config/adc_config.h"
#include "config/tim_config.h"
#include "config/sdio_config.h"
#include "config/pwm_config.h"
#include "config/usbd_config.h"
#include "config/pulse_encoder_config.h"
(3)在drivers文件夹下新建drv_dac.c
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2020-06-18 thread-liu the first version
* 2020-10-09 thread-liu Porting for stm32h7xx
*/
#include <board.h>
#include<rtthread.h>
#include<rtdevice.h>
#if defined(BSP_USING_DAC1) || defined(BSP_USING_DAC2)
#include "drv_config.h"
//#define DRV_DEBUG
#define LOG_TAG "drv.dac"
#include <drv_log.h>
static DAC_HandleTypeDef dac_config[] =
{
#ifdef BSP_USING_DAC1
DAC1_CONFIG,
#endif
#ifdef BSP_USING_DAC2
DAC2_CONFIG,
#endif
};
struct stm32_dac
{
DAC_HandleTypeDef DAC_Handler;
struct rt_dac_device stm32_dac_device;
};
static struct stm32_dac stm32_dac_obj[sizeof(dac_config) / sizeof(dac_config[0])];
static rt_uint32_t stm32_dac_get_channel(rt_uint32_t channel)
{
rt_uint32_t stm32_channel = 0;
switch (channel)
{
case 1:
stm32_channel = DAC_CHANNEL_1;
break;
case 2:
stm32_channel = DAC_CHANNEL_2;
break;
default:
RT_ASSERT(0);
break;
}
return stm32_channel;
}
static rt_err_t stm32_dac_enabled(struct rt_dac_device *device, rt_uint32_t channel)
{
uint32_t dac_channel;
DAC_HandleTypeDef *stm32_dac_handler;
RT_ASSERT(device != RT_NULL);
stm32_dac_handler = device->parent.user_data;
#if 1
if ((channel <= 2) && (channel > 0))
{
/* set stm32 dac channel */
dac_channel = stm32_dac_get_channel(channel);
}
else
{
LOG_E("dac channel must be 1 or 2.");
return -RT_ERROR;
}
HAL_DAC_Start(stm32_dac_handler, dac_channel);
#endif
return RT_EOK;
}
static rt_err_t stm32_dac_disabled(struct rt_dac_device *device, rt_uint32_t channel)
{
uint32_t dac_channel;
DAC_HandleTypeDef *stm32_dac_handler;
RT_ASSERT(device != RT_NULL);
stm32_dac_handler = device->parent.user_data;
#if 1
if ((channel <= 2) && (channel > 0))
{
/* set stm32 dac channel */
dac_channel = stm32_dac_get_channel(channel);
}
else
{
LOG_E("dac channel must be 1 or 2.");
return -RT_ERROR;
}
HAL_DAC_Stop(stm32_dac_handler, dac_channel);
#endif
return RT_EOK;
}
static rt_err_t stm32_set_dac_value(struct rt_dac_device *device, rt_uint32_t channel, rt_uint32_t *value)
{
uint32_t dac_channel;
DAC_ChannelConfTypeDef DAC_ChanConf;
DAC_HandleTypeDef *stm32_dac_handler;
RT_ASSERT(device != RT_NULL);
RT_ASSERT(value != RT_NULL);
stm32_dac_handler = device->parent.user_data;
rt_memset(&DAC_ChanConf, 0, sizeof(DAC_ChanConf));
#if 1
if ((channel <= 2) && (channel > 0))
{
/* set stm32 dac channel */
dac_channel = stm32_dac_get_channel(channel);
}
else
{
LOG_E("dac channel must be 1 or 2.");
return -RT_ERROR;
}
#endif
#if 1
DAC_ChanConf.DAC_Trigger = DAC_TRIGGER_NONE;
DAC_ChanConf.DAC_OutputBuffer = DAC_OUTPUTBUFFER_DISABLE;
#endif
/* config dac out channel*/
if (HAL_DAC_ConfigChannel(stm32_dac_handler, &DAC_ChanConf, dac_channel) != HAL_OK)
{
LOG_D("Config dac out channel Error!\n");
return -RT_ERROR;
}
/* set dac channel out value*/
if (HAL_DAC_SetValue(stm32_dac_handler, dac_channel, DAC_ALIGN_12B_R, *value) != HAL_OK)
{
LOG_D("Setting dac channel out value Error!\n");
return -RT_ERROR;
}
/* start dac */
if (HAL_DAC_Start(stm32_dac_handler, dac_channel) != HAL_OK)
{
LOG_D("Start dac Error!\n");
return -RT_ERROR;
}
return RT_EOK;
}
static const struct rt_dac_ops stm_dac_ops =
{
.disabled = stm32_dac_disabled,
.enabled = stm32_dac_enabled,
.convert = stm32_set_dac_value,
};
static int stm32_dac_init(void)
{
int result = RT_EOK;
/* save dac name */
char name_buf[5] = {'d', 'a', 'c', '0', 0};
int i = 0;
for (i = 0; i < sizeof(dac_config) / sizeof(dac_config[0]); i++)
{
/* dac init */
name_buf[3] = '0';
stm32_dac_obj[i].DAC_Handler = dac_config[i];
#if defined(DAC1)
if (stm32_dac_obj[i].DAC_Handler.Instance == DAC1)
{
name_buf[3] = '1';
}
#endif
#if defined(DAC2)
if (stm32_dac_obj[i].dac_Handler.Instance == DAC2)
{
name_buf[3] = '2';
}
#endif
if (HAL_DAC_Init(&stm32_dac_obj[i].DAC_Handler) != HAL_OK)
{
LOG_E("%s init failed", name_buf);
result = -RT_ERROR;
}
else
{
/* register dac device */
if (rt_hw_dac_register(&stm32_dac_obj[i].stm32_dac_device, name_buf, &stm_dac_ops, &stm32_dac_obj[i].DAC_Handler) == RT_EOK)
{
LOG_D("%s init success", name_buf);
}
else
{
LOG_E("%s register failed", name_buf);
result = -RT_ERROR;
}
}
}
return result;
}
INIT_DEVICE_EXPORT(stm32_dac_init);
#endif /* BSP_USING_DAC */
1)在bord.h里面添加#define BSP_USING_DAC1。(仿照ADC的步骤来写)
2)在stm32f1xx_hal_conf_bak.h里面添加#define HAL_DAC_MODULE_ENABLED
我是新建了一个user_dac.c文件,将用户代码写在该文件里面。通过DAC 设备将电压输出。
根据官方提供的用户代码修改后:
/*
* 程序清单: DAC 设备使用例程
* 例程导出了 dac_sample 命令到控制终端
* 命令调用格式:dac_sample
* 程序功能:通过 DAC 设备将数字值转换为模拟量,并输出电压值。
* 示例代码参考电压为3.3V,转换位数为12位。
*/
#include <rtthread.h>
#include <rtdevice.h>
#include <stdlib.h>
#define DAC_DEV_NAME "dac1" /* DAC 设备名称 */
#define DAC_DEV_CHANNEL 1 /* DAC 通道 */
#define DAC_DEV_CHANNEL2 2 /* DAC 通道 */
#define REFER_VOLTAGE 330 /* 参考电压 3.3V,数据精度乘以100保留2位小数*/
#define CONVERT_BITS (1 << 12) /* 转换位数为12位 */
rt_uint32_t dac_date=3000;
rt_uint32_t dac_date2=2000;
static int dac1_vol_sample();
static int dac2_vol_sample();
static void dac1_write_distance_entry(void *parameter)
{
while(1)
{
dac1_vol_sample();
rt_thread_mdelay(1000);
dac2_vol_sample();
rt_thread_mdelay(1000);
}
}
static int dac1_vol_sample()
{
rt_dac_device_t dac_dev;
rt_uint32_t value, vol;
rt_err_t ret = RT_EOK;
/* 查找设备 */
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;
}
/* 打开通道 */
ret = rt_dac_enable(dac_dev, DAC_DEV_CHANNEL);
/* 设置输出值 */
value=dac_date;
rt_dac_write(dac_dev, DAC_DEV_CHANNEL, value);
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);
/* 延时查看效果,关闭通道后无输出 */
// rt_thread_mdelay(500);
/* 关闭通道 */
// ret = rt_dac_disable(dac_dev, DAC_DEV_CHANNEL);
return ret;
}
static int dac2_vol_sample()
{
rt_dac_device_t dac_dev;
rt_uint32_t value, vol;
rt_err_t ret = RT_EOK;
/* 查找设备 */
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;
}
/* 打开通道 */
ret = rt_dac_enable(dac_dev, DAC_DEV_CHANNEL2);
/* 设置输出值 */
value=dac_date2;
rt_dac_write(dac_dev, DAC_DEV_CHANNEL2, value);
rt_kprintf("the value2 is :%d \n", value);
/* 转换为对应电压值 */
vol = value * REFER_VOLTAGE / CONVERT_BITS;
rt_kprintf("the voltage2 is :%d.%02d \n", vol / 100, vol % 100);
/* 延时查看效果,关闭通道后无输出 */
// rt_thread_mdelay(500);
/* 关闭通道 */
// ret = rt_dac_disable(dac_dev, DAC_DEV_CHANNEL2);
return ret;
}
int dac1_write_distance_sample(void)
{
rt_thread_t dac1_thread;
dac1_thread = rt_thread_create("dac1",
dac1_write_distance_entry,
"pr_dac1",
1024,
RT_THREAD_PRIORITY_MAX / 2,
20);
if (dac1_thread != RT_NULL) {
rt_thread_startup(dac1_thread);
}
return RT_EOK;
}
INIT_APP_EXPORT(dac1_write_distance_sample);
打开dac输出线程,打印dac输出数据。INIT_APP_EXPORT(dac1_write_distance_sample);
1)如果CubeMX配置后出现串口错误,需打开CubeMX,将串口进行初始化配置。
2)不仅要看输出的串口数据,更加需要通过万用表去测量输出IO口的具体电压值,双向验证代码。
3)STM32F103ZET6的两路DAC,并非有两个DAC,而是有两通道的DAC,不然使用的时候会报错,并且得不到想要的结果。