【STM32】DAC相关函数和类型
00. 目錄
文章目錄
- 00. 目錄
- 01. DAC簡介
- 02. 相關類型
- 03. 相關函數
- 04. 結構體封裝
- 05. 預留
- 06. 附錄
- 07. 聲明
01. DAC簡介
STM32F4的DAC模塊(數字/模擬轉換模塊)是12位數字輸入,電壓輸出型的DAC。DAC可以配置為 8 位或 12 位模式,也可以與 DMA 控制器配合使用。DAC 工作在 12 位模式時,數據可以設置成左對齊或右對齊。DAC 模塊有 2 個輸出通道,每個通道都有單獨的轉換器。在雙 DAC 模式下,2 個通道可以獨立地進行轉換,也可以同時進行轉換并同步地更新 2 個通道的輸出。DAC 可以通過引腳輸入參考電壓 Vref+(通 ADC 共用)以獲得更精確的轉換結果。
STM32F4 的 DAC 模塊主要特點有:
① 2 個 DAC 轉換器:每個轉換器對應 1 個輸出通道
② 8 位或者 12 位單調輸出
③ 12 位模式下數據左對齊或者右對齊
④ 同步更新功能
⑤ 噪聲波形生成
⑥ 三角波形生成
⑦ 雙 DAC 通道同時或者分別轉換
⑧ 每個通道都有 DMA 功能
02. 相關類型
DAC Init structure
/** * @brief DAC Init structure definition*/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 wavesare 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 orthe 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_selection
/** @defgroup DAC_trigger_selection * @{*/#define DAC_Trigger_None ((uint32_t)0x00000000) /*!< Conversion is automatic once the DAC1_DHRxxxx register has been loaded, and not by external trigger */ #define DAC_Trigger_T2_TRGO ((uint32_t)0x00000024) /*!< TIM2 TRGO selected as external conversion trigger for DAC channel */ #define DAC_Trigger_T4_TRGO ((uint32_t)0x0000002C) /*!< TIM4 TRGO selected as external conversion trigger for DAC channel */ #define DAC_Trigger_T5_TRGO ((uint32_t)0x0000001C) /*!< TIM5 TRGO selected as external conversion trigger for DAC channel */ #define DAC_Trigger_T6_TRGO ((uint32_t)0x00000004) /*!< TIM6 TRGO selected as external conversion trigger for DAC channel */ #define DAC_Trigger_T7_TRGO ((uint32_t)0x00000014) /*!< TIM7 TRGO selected as external conversion trigger for DAC channel */ #define DAC_Trigger_T8_TRGO ((uint32_t)0x0000000C) /*!< TIM8 TRGO selected as external conversion trigger for DAC channel */ #define DAC_Trigger_Ext_IT9 ((uint32_t)0x00000034) /*!< EXTI Line9 event selected as external conversion trigger for DAC channel */ #define DAC_Trigger_Software ((uint32_t)0x0000003C) /*!< Conversion started by software trigger for DAC channel */#define IS_DAC_TRIGGER(TRIGGER) (((TRIGGER) == DAC_Trigger_None) || \((TRIGGER) == DAC_Trigger_T6_TRGO) || \((TRIGGER) == DAC_Trigger_T8_TRGO) || \((TRIGGER) == DAC_Trigger_T7_TRGO) || \((TRIGGER) == DAC_Trigger_T5_TRGO) || \((TRIGGER) == DAC_Trigger_T2_TRGO) || \((TRIGGER) == DAC_Trigger_T4_TRGO) || \((TRIGGER) == DAC_Trigger_Ext_IT9) || \((TRIGGER) == DAC_Trigger_Software))DAC_wave_generation
/** @defgroup DAC_wave_generation * @{*/#define DAC_WaveGeneration_None ((uint32_t)0x00000000) #define DAC_WaveGeneration_Noise ((uint32_t)0x00000040) #define DAC_WaveGeneration_Triangle ((uint32_t)0x00000080) #define IS_DAC_GENERATE_WAVE(WAVE) (((WAVE) == DAC_WaveGeneration_None) || \((WAVE) == DAC_WaveGeneration_Noise) || \((WAVE) == DAC_WaveGeneration_Triangle))DAC_lfsrunmask_triangleamplitude
/** @defgroup DAC_lfsrunmask_triangleamplitude* @{*/#define DAC_LFSRUnmask_Bit0 ((uint32_t)0x00000000) /*!< Unmask DAC channel LFSR bit0 for noise wave generation */ #define DAC_LFSRUnmask_Bits1_0 ((uint32_t)0x00000100) /*!< Unmask DAC channel LFSR bit[1:0] for noise wave generation */ #define DAC_LFSRUnmask_Bits2_0 ((uint32_t)0x00000200) /*!< Unmask DAC channel LFSR bit[2:0] for noise wave generation */ #define DAC_LFSRUnmask_Bits3_0 ((uint32_t)0x00000300) /*!< Unmask DAC channel LFSR bit[3:0] for noise wave generation */ #define DAC_LFSRUnmask_Bits4_0 ((uint32_t)0x00000400) /*!< Unmask DAC channel LFSR bit[4:0] for noise wave generation */ #define DAC_LFSRUnmask_Bits5_0 ((uint32_t)0x00000500) /*!< Unmask DAC channel LFSR bit[5:0] for noise wave generation */ #define DAC_LFSRUnmask_Bits6_0 ((uint32_t)0x00000600) /*!< Unmask DAC channel LFSR bit[6:0] for noise wave generation */ #define DAC_LFSRUnmask_Bits7_0 ((uint32_t)0x00000700) /*!< Unmask DAC channel LFSR bit[7:0] for noise wave generation */ #define DAC_LFSRUnmask_Bits8_0 ((uint32_t)0x00000800) /*!< Unmask DAC channel LFSR bit[8:0] for noise wave generation */ #define DAC_LFSRUnmask_Bits9_0 ((uint32_t)0x00000900) /*!< Unmask DAC channel LFSR bit[9:0] for noise wave generation */ #define DAC_LFSRUnmask_Bits10_0 ((uint32_t)0x00000A00) /*!< Unmask DAC channel LFSR bit[10:0] for noise wave generation */ #define DAC_LFSRUnmask_Bits11_0 ((uint32_t)0x00000B00) /*!< Unmask DAC channel LFSR bit[11:0] for noise wave generation */ #define DAC_TriangleAmplitude_1 ((uint32_t)0x00000000) /*!< Select max triangle amplitude of 1 */ #define DAC_TriangleAmplitude_3 ((uint32_t)0x00000100) /*!< Select max triangle amplitude of 3 */ #define DAC_TriangleAmplitude_7 ((uint32_t)0x00000200) /*!< Select max triangle amplitude of 7 */ #define DAC_TriangleAmplitude_15 ((uint32_t)0x00000300) /*!< Select max triangle amplitude of 15 */ #define DAC_TriangleAmplitude_31 ((uint32_t)0x00000400) /*!< Select max triangle amplitude of 31 */ #define DAC_TriangleAmplitude_63 ((uint32_t)0x00000500) /*!< Select max triangle amplitude of 63 */ #define DAC_TriangleAmplitude_127 ((uint32_t)0x00000600) /*!< Select max triangle amplitude of 127 */ #define DAC_TriangleAmplitude_255 ((uint32_t)0x00000700) /*!< Select max triangle amplitude of 255 */ #define DAC_TriangleAmplitude_511 ((uint32_t)0x00000800) /*!< Select max triangle amplitude of 511 */ #define DAC_TriangleAmplitude_1023 ((uint32_t)0x00000900) /*!< Select max triangle amplitude of 1023 */ #define DAC_TriangleAmplitude_2047 ((uint32_t)0x00000A00) /*!< Select max triangle amplitude of 2047 */ #define DAC_TriangleAmplitude_4095 ((uint32_t)0x00000B00) /*!< Select max triangle amplitude of 4095 */#define IS_DAC_LFSR_UNMASK_TRIANGLE_AMPLITUDE(VALUE) (((VALUE) == DAC_LFSRUnmask_Bit0) || \((VALUE) == DAC_LFSRUnmask_Bits1_0) || \((VALUE) == DAC_LFSRUnmask_Bits2_0) || \((VALUE) == DAC_LFSRUnmask_Bits3_0) || \((VALUE) == DAC_LFSRUnmask_Bits4_0) || \((VALUE) == DAC_LFSRUnmask_Bits5_0) || \((VALUE) == DAC_LFSRUnmask_Bits6_0) || \((VALUE) == DAC_LFSRUnmask_Bits7_0) || \((VALUE) == DAC_LFSRUnmask_Bits8_0) || \((VALUE) == DAC_LFSRUnmask_Bits9_0) || \((VALUE) == DAC_LFSRUnmask_Bits10_0) || \((VALUE) == DAC_LFSRUnmask_Bits11_0) || \((VALUE) == DAC_TriangleAmplitude_1) || \((VALUE) == DAC_TriangleAmplitude_3) || \((VALUE) == DAC_TriangleAmplitude_7) || \((VALUE) == DAC_TriangleAmplitude_15) || \((VALUE) == DAC_TriangleAmplitude_31) || \((VALUE) == DAC_TriangleAmplitude_63) || \((VALUE) == DAC_TriangleAmplitude_127) || \((VALUE) == DAC_TriangleAmplitude_255) || \((VALUE) == DAC_TriangleAmplitude_511) || \((VALUE) == DAC_TriangleAmplitude_1023) || \((VALUE) == DAC_TriangleAmplitude_2047) || \((VALUE) == DAC_TriangleAmplitude_4095))DAC_output_buffer
/** @defgroup DAC_output_buffer * @{*/#define DAC_OutputBuffer_Enable ((uint32_t)0x00000000) #define DAC_OutputBuffer_Disable ((uint32_t)0x00000002) #define IS_DAC_OUTPUT_BUFFER_STATE(STATE) (((STATE) == DAC_OutputBuffer_Enable) || \((STATE) == DAC_OutputBuffer_Disable))DAC_Channel_selection
/** @defgroup DAC_Channel_selection * @{*/#define DAC_Channel_1 ((uint32_t)0x00000000) #define DAC_Channel_2 ((uint32_t)0x00000010) #define IS_DAC_CHANNEL(CHANNEL) (((CHANNEL) == DAC_Channel_1) || \((CHANNEL) == DAC_Channel_2))DAC_data_alignement
/** @defgroup DAC_data_alignement * @{*/#define DAC_Align_12b_R ((uint32_t)0x00000000) #define DAC_Align_12b_L ((uint32_t)0x00000004) #define DAC_Align_8b_R ((uint32_t)0x00000008) #define IS_DAC_ALIGN(ALIGN) (((ALIGN) == DAC_Align_12b_R) || \((ALIGN) == DAC_Align_12b_L) || \((ALIGN) == DAC_Align_8b_R))DAC_wave_generation
/** @defgroup DAC_wave_generation * @{*/#define DAC_Wave_Noise ((uint32_t)0x00000040) #define DAC_Wave_Triangle ((uint32_t)0x00000080) #define IS_DAC_WAVE(WAVE) (((WAVE) == DAC_Wave_Noise) || \((WAVE) == DAC_Wave_Triangle))其它
/** @defgroup DAC_data * @{*/#define IS_DAC_DATA(DATA) ((DATA) <= 0xFFF0) /*** @}*//** @defgroup DAC_interrupts_definition * @{*/ #define DAC_IT_DMAUDR ((uint32_t)0x00002000) #define IS_DAC_IT(IT) (((IT) == DAC_IT_DMAUDR)) /*** @}*/ /** @defgroup DAC_flags_definition * @{*/ #define DAC_FLAG_DMAUDR ((uint32_t)0x00002000) #define IS_DAC_FLAG(FLAG) (((FLAG) == DAC_FLAG_DMAUDR))03. 相關函數
/* Function used to set the DAC configuration to the default reset state *****/ void DAC_DeInit(void);/* DAC channels configuration: trigger, output buffer, data format functions */ void DAC_Init(uint32_t DAC_Channel, DAC_InitTypeDef* DAC_InitStruct); void DAC_StructInit(DAC_InitTypeDef* DAC_InitStruct); void DAC_Cmd(uint32_t DAC_Channel, FunctionalState NewState); void DAC_SoftwareTriggerCmd(uint32_t DAC_Channel, FunctionalState NewState); void DAC_DualSoftwareTriggerCmd(FunctionalState NewState); void DAC_WaveGenerationCmd(uint32_t DAC_Channel, uint32_t DAC_Wave, FunctionalState NewState); void DAC_SetChannel1Data(uint32_t DAC_Align, uint16_t Data); void DAC_SetChannel2Data(uint32_t DAC_Align, uint16_t Data); void DAC_SetDualChannelData(uint32_t DAC_Align, uint16_t Data2, uint16_t Data1); uint16_t DAC_GetDataOutputValue(uint32_t DAC_Channel);/* DMA management functions ***************************************************/ void DAC_DMACmd(uint32_t DAC_Channel, FunctionalState NewState);/* Interrupts and flags management functions **********************************/ void DAC_ITConfig(uint32_t DAC_Channel, uint32_t DAC_IT, FunctionalState NewState); FlagStatus DAC_GetFlagStatus(uint32_t DAC_Channel, uint32_t DAC_FLAG); void DAC_ClearFlag(uint32_t DAC_Channel, uint32_t DAC_FLAG); ITStatus DAC_GetITStatus(uint32_t DAC_Channel, uint32_t DAC_IT); void DAC_ClearITPendingBit(uint32_t DAC_Channel, uint32_t DAC_IT);04. 結構體封裝
/** * @brief Digital to Analog Converter*/typedef struct {__IO uint32_t CR; /*!< DAC control register, Address offset: 0x00 */__IO uint32_t SWTRIGR; /*!< DAC software trigger register, Address offset: 0x04 */__IO uint32_t DHR12R1; /*!< DAC channel1 12-bit right-aligned data holding register, Address offset: 0x08 */__IO uint32_t DHR12L1; /*!< DAC channel1 12-bit left aligned data holding register, Address offset: 0x0C */__IO uint32_t DHR8R1; /*!< DAC channel1 8-bit right aligned data holding register, Address offset: 0x10 */__IO uint32_t DHR12R2; /*!< DAC channel2 12-bit right aligned data holding register, Address offset: 0x14 */__IO uint32_t DHR12L2; /*!< DAC channel2 12-bit left aligned data holding register, Address offset: 0x18 */__IO uint32_t DHR8R2; /*!< DAC channel2 8-bit right-aligned data holding register, Address offset: 0x1C */__IO uint32_t DHR12RD; /*!< Dual DAC 12-bit right-aligned data holding register, Address offset: 0x20 */__IO uint32_t DHR12LD; /*!< DUAL DAC 12-bit left aligned data holding register, Address offset: 0x24 */__IO uint32_t DHR8RD; /*!< DUAL DAC 8-bit right aligned data holding register, Address offset: 0x28 */__IO uint32_t DOR1; /*!< DAC channel1 data output register, Address offset: 0x2C */__IO uint32_t DOR2; /*!< DAC channel2 data output register, Address offset: 0x30 */__IO uint32_t SR; /*!< DAC status register, Address offset: 0x34 */ } DAC_TypeDef;05. 預留
06. 附錄
6.1 【STM32】STM32系列教程匯總
網址:【STM32】STM32系列教程匯總
07. 聲明
STM32F4xx英文參考手冊
總結
以上是生活随笔為你收集整理的【STM32】DAC相关函数和类型的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【STM32】光敏传感器示例
- 下一篇: 【STM32】DAC程序示例