关于DAC的模块分析在《STM32-一文搞懂DAC》已经记过了,这里只记快速使用。
目录
1 概述
STM32的DAC模块只存在于大容量产品中,是12位数字输入,电压输出的数字/模拟转换器,2个输出通道,每个通道都有单独的转换器,支持双DAC模式。DAC可以通过引脚输入参考电压VREF+以获得更精确的转换结果。DAC模块在APB1时钟下!
2 配置步骤
- (1)DAC模块时钟使能(在APB1下)、输出IO端口时钟使能(APB2下)。
- void RCC_APB1PeriphClockCmd(uint32_t RCC_APB1Periph, FunctionalState NewState);
- void RCC_APB2PeriphClockCmd(uint32_t RCC_APB2Periph, FunctionalState NewState)
- (2)输出端口配置
- void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct)
- (3)DAC模块配置
- void DAC_Init(uint32_t DAC_Channel, DAC_InitTypeDef* DAC_InitStruct);
主要配置前文《stm32-一文搞懂DAC》中提到的控制寄存器DAC_CR。
typedef struct { uint32_t DAC_Trigger; /*!< Specifies the external trigger for the selected DAC channel. This parameter can be a value of @ref DAC_trigger_selection */ uint32_t DAC_WaveGeneration; /*!< Specifies whether DAC channel noise waves or triangle waves are generated, or whether no wave is generated. This parameter can be a value of @ref DAC_wave_generation */ uint32_t DAC_LFSRUnmask_TriangleAmplitude; /*!< Specifies the LFSR mask for noise wave generation or the maximum amplitude triangle generation for the DAC channel. This parameter can be a value of @ref DAC_lfsrunmask_triangleamplitude */ uint32_t DAC_OutputBuffer; /*!< Specifies whether the DAC channel output buffer is enabled or disabled. This parameter can be a value of @ref DAC_output_buffer */ }DAC_InitTypeDef;
DAC_Trigger 通道触发源选择,可选通道如下
#define DAC_Trigger_None ((uint32_t)0x00000000) /*!< Conversion is automatic once the DAC1_DHRxxxx register