【STM32 标准库】DAC

目录

1 DAC

 1.1 DAC简介

1.2 结构框图

2 软件配置

3 硬件设计

4 软件设计

4.1 功能描述

4.2 软件实现

4.2.1 DAC初始化

4.2.2 主程序


1 DAC

 1.1 DAC简介

        (1)数字模拟转换器(Digital to Analog Converter,DAC),可以将数字信号转换为模拟信号。

        (2)主要特性:

  • 2个DAC:每个DAC对应1个输出通道
  • 8位或12位单调输出
  • 12位模式下数据左对齐或右对齐(8位只有右对齐)
  • 同步更新、噪声波形生成、三角波形生成
  • 双DAC通道同时或者分别转换
  • 每个通道都有DMA功能
  • 外部触发转换(软件触发、硬件触发)
  • 输入参考电压VREF+.

1.2 结构框图

  • 通过DHRx寄存器间接向DORx寄存器中写入数据
  • 硬件触发(TENx=1,设置TSELx选择触发源)、软件触发(TENx=0)
  • 输出波形最大频率:250KHz

2 软件配置

DAC 相关库函数在 stm32f10x_dac.c 和 stm32f10x_dac.h 文件中。

(1)使能端口及 DAC 时钟,设置引脚为模拟输入

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);

RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;//模拟输入

(2)初始化 DAC,设置 DAC 工作模式

void DAC_Init(uint32_t DAC_Channel, DAC_InitTypeDef* DAC_InitStruct);

typedef struct

{

uint32_t DAC_Trigger; //DAC 触发选择

uint32_t DAC_WaveGeneration; //DAC 波形发生(噪声波、三角波)

uint32_t DAC_LFSRUnmask_TriangleAmplitude; //屏蔽/幅值选择器(只有使用波形发生器时才使用)

uint32_t DAC_OutputBuffer; //DAC 输出缓存——使用输出缓存可能导致输出无法到0,一般关闭

}DAC_InitTypeDef;

(3)使能 DAC 的输出通道

void DAC_Cmd(uint32_t DAC_Channel, FunctionalState NewState);

(4)设置 DAC 的输出值

DAC_SetChannel1Data(DAC_Align_12b_R, 0);

uint16_t DAC_GetDataOutputValue(uint32_t DAC_Channel);

3 硬件设计

4 软件设计

4.1 功能描述

        通过 KEY_UP 与 KEY1 按键控制 STM32 DAC1输出电压,通过串口将 DAC1 输出的电压值打印显示,DS0 指示灯闪烁提示系统运行。

4.2 软件实现

4.2.1 DAC初始化

4.2.2 主程序

### DAC Standard Library for Embedded Systems For embedded systems or microcontrollers, several libraries and functions support Digital-to-Analog Conversion (DAC). These tools facilitate interfacing with hardware DAC modules present on many microcontroller units (MCUs). #### Arduino Environment In the Arduino environment, developers can use simple commands to interact with built-in DACs found on certain boards like the Arduino Due or MKR series. The `analogWrite()` function allows writing analog values when used with pins that have DAC capabilities[^1]. ```cpp // Example of using DAC on an Arduino board supporting it void setup() { // Set pin as output which supports DAC pinMode(DAC0, OUTPUT); } void loop() { // Write a value between 0 and 255 to the DAC channel analogWrite(DAC0, 128); } ``` #### STM32 HAL Libraries STM32CubeMX generates initialization code based on selected peripherals including DAC channels. With this tool, one gets access to Hardware Abstraction Layer (HAL) APIs such as `HAL_DAC_Start()`, enabling control over DAC operations. ```c /* Initialize DAC peripheral */ static void MX_DAC_Init(void){ DAC_ChannelConfTypeDef sConfig = {0}; hdac.Instance = DAC; if (HAL_DAC_Init(&hdac) != HAL_OK){ Error_Handler(); } /* Configure DAC channel */ sConfig.DAC_Trigger = DAC_TRIGGER_NONE; sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE; if (HAL_DAC_ConfigChannel(&hdac, &sConfig, DAC_CHANNEL_1) != HAL_OK){ Error_Handler(); } } /* Start conversion on specified DAC channel */ if(HAL_DAC_Start(&hdac, DAC_CHANNEL_1)!= HAL_OK){ Error_Handler(); } ``` #### AVR Libc AVR libc provides macros and inline assembly routines for direct register manipulation suitable for low-level programming tasks involving ADC/DAC interfaces within Atmel’s AVR family processors. ```c #include <avr/io.h> int main(void){ DDRB |= _BV(PB0); // Set PB0 as output PORTB &= ~_BV(PB0); // Ensure PB0 is initially LOW // Enable DAC by setting appropriate bits in relevant registers according to datasheet specifications. while(1){ // Code here would set up data transfer from memory into the DAC converter. } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值