你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

【经验分享】STM32 DAC相关函数和类型

[复制链接]
STMCU小助手 发布时间:2022-6-16 17:00
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

  1. /**
  2.   * @brief  DAC Init structure definition
  3.   */

  4. typedef struct
  5. {
  6.   uint32_t DAC_Trigger;                      /*!< Specifies the external trigger for the selected DAC channel.
  7.                                                   This parameter can be a value of @ref DAC_trigger_selection */

  8.   uint32_t DAC_WaveGeneration;               /*!< Specifies whether DAC channel noise waves or triangle waves
  9.                                                   are generated, or whether no wave is generated.
  10.                                                   This parameter can be a value of @ref DAC_wave_generation */

  11.   uint32_t DAC_LFSRUnmask_TriangleAmplitude; /*!< Specifies the LFSR mask for noise wave generation or
  12.                                                   the maximum amplitude triangle generation for the DAC channel.
  13.                                                   This parameter can be a value of @ref DAC_lfsrunmask_triangleamplitude */

  14.   uint32_t DAC_OutputBuffer;                 /*!< Specifies whether the DAC channel output buffer is enabled or disabled.
  15.                                                   This parameter can be a value of @ref DAC_output_buffer */
  16. }DAC_InitTypeDef;
复制代码

DAC_trigger_selection

  1. /** @defgroup DAC_trigger_selection
  2.   * @{
  3.   */

  4. #define DAC_Trigger_None                   ((uint32_t)0x00000000) /*!< Conversion is automatic once the DAC1_DHRxxxx register
  5.                                                                        has been loaded, and not by external trigger */
  6. #define DAC_Trigger_T2_TRGO                ((uint32_t)0x00000024) /*!< TIM2 TRGO selected as external conversion trigger for DAC channel */
  7. #define DAC_Trigger_T4_TRGO                ((uint32_t)0x0000002C) /*!< TIM4 TRGO selected as external conversion trigger for DAC channel */
  8. #define DAC_Trigger_T5_TRGO                ((uint32_t)0x0000001C) /*!< TIM5 TRGO selected as external conversion trigger for DAC channel */
  9. #define DAC_Trigger_T6_TRGO                ((uint32_t)0x00000004) /*!< TIM6 TRGO selected as external conversion trigger for DAC channel */
  10. #define DAC_Trigger_T7_TRGO                ((uint32_t)0x00000014) /*!< TIM7 TRGO selected as external conversion trigger for DAC channel */
  11. #define DAC_Trigger_T8_TRGO                ((uint32_t)0x0000000C) /*!< TIM8 TRGO selected as external conversion trigger for DAC channel */                                                                       

  12. #define DAC_Trigger_Ext_IT9                ((uint32_t)0x00000034) /*!< EXTI Line9 event selected as external conversion trigger for DAC channel */
  13. #define DAC_Trigger_Software               ((uint32_t)0x0000003C) /*!< Conversion started by software trigger for DAC channel */

  14. #define IS_DAC_TRIGGER(TRIGGER) (((TRIGGER) == DAC_Trigger_None) || \
  15.                                  ((TRIGGER) == DAC_Trigger_T6_TRGO) || \
  16.                                  ((TRIGGER) == DAC_Trigger_T8_TRGO) || \
  17.                                  ((TRIGGER) == DAC_Trigger_T7_TRGO) || \
  18.                                  ((TRIGGER) == DAC_Trigger_T5_TRGO) || \
  19.                                  ((TRIGGER) == DAC_Trigger_T2_TRGO) || \
  20.                                  ((TRIGGER) == DAC_Trigger_T4_TRGO) || \
  21.                                  ((TRIGGER) == DAC_Trigger_Ext_IT9) || \
  22.                                  ((TRIGGER) == DAC_Trigger_Software))
复制代码

DAC_wave_generation

  1. /** @defgroup DAC_wave_generation
  2.   * @{
  3.   */

  4. #define DAC_WaveGeneration_None            ((uint32_t)0x00000000)
  5. #define DAC_WaveGeneration_Noise           ((uint32_t)0x00000040)
  6. #define DAC_WaveGeneration_Triangle        ((uint32_t)0x00000080)
  7. #define IS_DAC_GENERATE_WAVE(WAVE) (((WAVE) == DAC_WaveGeneration_None) || \
  8.                                     ((WAVE) == DAC_WaveGeneration_Noise) || \
  9.                                     ((WAVE) == DAC_WaveGeneration_Triangle))
复制代码

DAC_lfsrunmask_triangleamplitude


  1. /** @defgroup DAC_lfsrunmask_triangleamplitude
  2.   * @{
  3.   */

  4. #define DAC_LFSRUnmask_Bit0                ((uint32_t)0x00000000) /*!< Unmask DAC channel LFSR bit0 for noise wave generation */
  5. #define DAC_LFSRUnmask_Bits1_0             ((uint32_t)0x00000100) /*!< Unmask DAC channel LFSR bit[1:0] for noise wave generation */
  6. #define DAC_LFSRUnmask_Bits2_0             ((uint32_t)0x00000200) /*!< Unmask DAC channel LFSR bit[2:0] for noise wave generation */
  7. #define DAC_LFSRUnmask_Bits3_0             ((uint32_t)0x00000300) /*!< Unmask DAC channel LFSR bit[3:0] for noise wave generation */
  8. #define DAC_LFSRUnmask_Bits4_0             ((uint32_t)0x00000400) /*!< Unmask DAC channel LFSR bit[4:0] for noise wave generation */
  9. #define DAC_LFSRUnmask_Bits5_0             ((uint32_t)0x00000500) /*!< Unmask DAC channel LFSR bit[5:0] for noise wave generation */
  10. #define DAC_LFSRUnmask_Bits6_0             ((uint32_t)0x00000600) /*!< Unmask DAC channel LFSR bit[6:0] for noise wave generation */
  11. #define DAC_LFSRUnmask_Bits7_0             ((uint32_t)0x00000700) /*!< Unmask DAC channel LFSR bit[7:0] for noise wave generation */
  12. #define DAC_LFSRUnmask_Bits8_0             ((uint32_t)0x00000800) /*!< Unmask DAC channel LFSR bit[8:0] for noise wave generation */
  13. #define DAC_LFSRUnmask_Bits9_0             ((uint32_t)0x00000900) /*!< Unmask DAC channel LFSR bit[9:0] for noise wave generation */
  14. #define DAC_LFSRUnmask_Bits10_0            ((uint32_t)0x00000A00) /*!< Unmask DAC channel LFSR bit[10:0] for noise wave generation */
  15. #define DAC_LFSRUnmask_Bits11_0            ((uint32_t)0x00000B00) /*!< Unmask DAC channel LFSR bit[11:0] for noise wave generation */
  16. #define DAC_TriangleAmplitude_1            ((uint32_t)0x00000000) /*!< Select max triangle amplitude of 1 */
  17. #define DAC_TriangleAmplitude_3            ((uint32_t)0x00000100) /*!< Select max triangle amplitude of 3 */
  18. #define DAC_TriangleAmplitude_7            ((uint32_t)0x00000200) /*!< Select max triangle amplitude of 7 */
  19. #define DAC_TriangleAmplitude_15           ((uint32_t)0x00000300) /*!< Select max triangle amplitude of 15 */
  20. #define DAC_TriangleAmplitude_31           ((uint32_t)0x00000400) /*!< Select max triangle amplitude of 31 */
  21. #define DAC_TriangleAmplitude_63           ((uint32_t)0x00000500) /*!< Select max triangle amplitude of 63 */
  22. #define DAC_TriangleAmplitude_127          ((uint32_t)0x00000600) /*!< Select max triangle amplitude of 127 */
  23. #define DAC_TriangleAmplitude_255          ((uint32_t)0x00000700) /*!< Select max triangle amplitude of 255 */
  24. #define DAC_TriangleAmplitude_511          ((uint32_t)0x00000800) /*!< Select max triangle amplitude of 511 */
  25. #define DAC_TriangleAmplitude_1023         ((uint32_t)0x00000900) /*!< Select max triangle amplitude of 1023 */
  26. #define DAC_TriangleAmplitude_2047         ((uint32_t)0x00000A00) /*!< Select max triangle amplitude of 2047 */
  27. #define DAC_TriangleAmplitude_4095         ((uint32_t)0x00000B00) /*!< Select max triangle amplitude of 4095 */

  28. #define IS_DAC_LFSR_UNMASK_TRIANGLE_AMPLITUDE(VALUE) (((VALUE) == DAC_LFSRUnmask_Bit0) || \
  29.                                                       ((VALUE) == DAC_LFSRUnmask_Bits1_0) || \
  30.                                                       ((VALUE) == DAC_LFSRUnmask_Bits2_0) || \
  31.                                                       ((VALUE) == DAC_LFSRUnmask_Bits3_0) || \
  32.                                                       ((VALUE) == DAC_LFSRUnmask_Bits4_0) || \
  33.                                                       ((VALUE) == DAC_LFSRUnmask_Bits5_0) || \
  34.                                                       ((VALUE) == DAC_LFSRUnmask_Bits6_0) || \
  35.                                                       ((VALUE) == DAC_LFSRUnmask_Bits7_0) || \
  36.                                                       ((VALUE) == DAC_LFSRUnmask_Bits8_0) || \
  37.                                                       ((VALUE) == DAC_LFSRUnmask_Bits9_0) || \
  38.                                                       ((VALUE) == DAC_LFSRUnmask_Bits10_0) || \
  39.                                                       ((VALUE) == DAC_LFSRUnmask_Bits11_0) || \
  40.                                                       ((VALUE) == DAC_TriangleAmplitude_1) || \
  41.                                                       ((VALUE) == DAC_TriangleAmplitude_3) || \
  42.                                                       ((VALUE) == DAC_TriangleAmplitude_7) || \
  43.                                                       ((VALUE) == DAC_TriangleAmplitude_15) || \
  44.                                                       ((VALUE) == DAC_TriangleAmplitude_31) || \
  45.                                                       ((VALUE) == DAC_TriangleAmplitude_63) || \
  46.                                                       ((VALUE) == DAC_TriangleAmplitude_127) || \
  47.                                                       ((VALUE) == DAC_TriangleAmplitude_255) || \
  48.                                                       ((VALUE) == DAC_TriangleAmplitude_511) || \
  49.                                                       ((VALUE) == DAC_TriangleAmplitude_1023) || \
  50.                                                       ((VALUE) == DAC_TriangleAmplitude_2047) || \
  51.                                                       ((VALUE) == DAC_TriangleAmplitude_4095))
复制代码

DAC_output_buffer

  1. /** @defgroup DAC_output_buffer
  2.   * @{
  3.   */

  4. #define DAC_OutputBuffer_Enable            ((uint32_t)0x00000000)
  5. #define DAC_OutputBuffer_Disable           ((uint32_t)0x00000002)
  6. #define IS_DAC_OUTPUT_BUFFER_STATE(STATE) (((STATE) == DAC_OutputBuffer_Enable) || \
  7.                                            ((STATE) == DAC_OutputBuffer_Disable))
复制代码

DAC_Channel_selection

  1. /** @defgroup DAC_Channel_selection
  2.   * @{
  3.   */

  4. #define DAC_Channel_1                      ((uint32_t)0x00000000)
  5. #define DAC_Channel_2                      ((uint32_t)0x00000010)
  6. #define IS_DAC_CHANNEL(CHANNEL) (((CHANNEL) == DAC_Channel_1) || \
  7.                                  ((CHANNEL) == DAC_Channel_2))
复制代码

DAC_data_alignement

  1. /** @defgroup DAC_data_alignement
  2.   * @{
  3.   */

  4. #define DAC_Align_12b_R                    ((uint32_t)0x00000000)
  5. #define DAC_Align_12b_L                    ((uint32_t)0x00000004)
  6. #define DAC_Align_8b_R                     ((uint32_t)0x00000008)
  7. #define IS_DAC_ALIGN(ALIGN) (((ALIGN) == DAC_Align_12b_R) || \
  8.                              ((ALIGN) == DAC_Align_12b_L) || \
  9.                              ((ALIGN) == DAC_Align_8b_R))
复制代码

DAC_wave_generation

  1. /** @defgroup DAC_wave_generation
  2.   * @{
  3.   */

  4. #define DAC_Wave_Noise                     ((uint32_t)0x00000040)
  5. #define DAC_Wave_Triangle                  ((uint32_t)0x00000080)
  6. #define IS_DAC_WAVE(WAVE) (((WAVE) == DAC_Wave_Noise) || \
  7.                            ((WAVE) == DAC_Wave_Triangle))
复制代码

其它

  1. /** @defgroup DAC_data
  2.   * @{
  3.   */

  4. #define IS_DAC_DATA(DATA) ((DATA) <= 0xFFF0)
  5. /**
  6.   * @}
  7.   */

  8. /** @defgroup DAC_interrupts_definition
  9.   * @{
  10.   */   
  11. #define DAC_IT_DMAUDR                      ((uint32_t)0x00002000)  
  12. #define IS_DAC_IT(IT) (((IT) == DAC_IT_DMAUDR))

  13. /**
  14.   * @}
  15.   */

  16. /** @defgroup DAC_flags_definition
  17.   * @{
  18.   */

  19. #define DAC_FLAG_DMAUDR                    ((uint32_t)0x00002000)  
  20. #define IS_DAC_FLAG(FLAG) (((FLAG) == DAC_FLAG_DMAUDR))  
复制代码

03. 相关函数
  1. /*  Function used to set the DAC configuration to the default reset state *****/  
  2. void DAC_DeInit(void);

  3. /*  DAC channels configuration: trigger, output buffer, data format functions */
  4. void DAC_Init(uint32_t DAC_Channel, DAC_InitTypeDef* DAC_InitStruct);
  5. void DAC_StructInit(DAC_InitTypeDef* DAC_InitStruct);
  6. void DAC_Cmd(uint32_t DAC_Channel, FunctionalState NewState);
  7. void DAC_SoftwareTriggerCmd(uint32_t DAC_Channel, FunctionalState NewState);
  8. void DAC_DualSoftwareTriggerCmd(FunctionalState NewState);
  9. void DAC_WaveGenerationCmd(uint32_t DAC_Channel, uint32_t DAC_Wave, FunctionalState NewState);
  10. void DAC_SetChannel1Data(uint32_t DAC_Align, uint16_t Data);
  11. void DAC_SetChannel2Data(uint32_t DAC_Align, uint16_t Data);
  12. void DAC_SetDualChannelData(uint32_t DAC_Align, uint16_t Data2, uint16_t Data1);
  13. uint16_t DAC_GetDataOutputValue(uint32_t DAC_Channel);

  14. /* DMA management functions ***************************************************/
  15. void DAC_DMACmd(uint32_t DAC_Channel, FunctionalState NewState);

  16. /* Interrupts and flags management functions **********************************/
  17. void DAC_ITConfig(uint32_t DAC_Channel, uint32_t DAC_IT, FunctionalState NewState);
  18. FlagStatus DAC_GetFlagStatus(uint32_t DAC_Channel, uint32_t DAC_FLAG);
  19. void DAC_ClearFlag(uint32_t DAC_Channel, uint32_t DAC_FLAG);
  20. ITStatus DAC_GetITStatus(uint32_t DAC_Channel, uint32_t DAC_IT);
  21. void DAC_ClearITPendingBit(uint32_t DAC_Channel, uint32_t DAC_IT);
复制代码

04. 结构体封装
  1. /**
  2.   * @brief Digital to Analog Converter
  3.   */

  4. typedef struct
  5. {
  6.   __IO uint32_t CR;       /*!< DAC control register,                                    Address offset: 0x00 */
  7.   __IO uint32_t SWTRIGR;  /*!< DAC software trigger register,                           Address offset: 0x04 */
  8.   __IO uint32_t DHR12R1;  /*!< DAC channel1 12-bit right-aligned data holding register, Address offset: 0x08 */
  9.   __IO uint32_t DHR12L1;  /*!< DAC channel1 12-bit left aligned data holding register,  Address offset: 0x0C */
  10.   __IO uint32_t DHR8R1;   /*!< DAC channel1 8-bit right aligned data holding register,  Address offset: 0x10 */
  11.   __IO uint32_t DHR12R2;  /*!< DAC channel2 12-bit right aligned data holding register, Address offset: 0x14 */
  12.   __IO uint32_t DHR12L2;  /*!< DAC channel2 12-bit left aligned data holding register,  Address offset: 0x18 */
  13.   __IO uint32_t DHR8R2;   /*!< DAC channel2 8-bit right-aligned data holding register,  Address offset: 0x1C */
  14.   __IO uint32_t DHR12RD;  /*!< Dual DAC 12-bit right-aligned data holding register,     Address offset: 0x20 */
  15.   __IO uint32_t DHR12LD;  /*!< DUAL DAC 12-bit left aligned data holding register,      Address offset: 0x24 */
  16.   __IO uint32_t DHR8RD;   /*!< DUAL DAC 8-bit right aligned data holding register,      Address offset: 0x28 */
  17.   __IO uint32_t DOR1;     /*!< DAC channel1 data output register,                       Address offset: 0x2C */
  18.   __IO uint32_t DOR2;     /*!< DAC channel2 data output register,                       Address offset: 0x30 */
  19.   __IO uint32_t SR;       /*!< DAC status register,                                     Address offset: 0x34 */
  20. } DAC_TypeDef;
复制代码



收藏 评论0 发布时间:2022-6-16 17:00

举报

0个回答

所属标签

相似分享

官网相关资源

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版