dac.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /* 包含头文件-----------------------------------------------------------------*/
  2. #include "dac.h"
  3. /* 私有宏定义-----------------------------------------------------------------*/
  4. /* 私有类型定义---------------------------------------------------------------*/
  5. /* 私有变量-------------------------------------------------------------------*/
  6. /* 全局变量-------------------------------------------------------------------*/
  7. /* 私有函数原型---------------------------------------------------------------*/
  8. /**
  9. * @brief DAC通道1初始化
  10. * @note PA4引脚输出
  11. * @param None
  12. * @retval None
  13. */
  14. void dac_ch1_config(void)
  15. {
  16. DAC_InitTypeDef DAC_InitStructure;
  17. GPIO_InitTypeDef GPIO_InitStructure;
  18. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); /* 开启GPIO外设相关时钟 */
  19. RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE); /* 开启DAC时钟 */
  20. /* DAC输出引脚的GPIO配置 */
  21. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; /* 设置引脚号 */
  22. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; /* 设置引脚模式 模拟输入 */
  23. GPIO_Init(GPIOA, &GPIO_InitStructure); /* 初始化GPIO */
  24. DAC_InitStructure.DAC_Trigger = DAC_Trigger_Software; /* 软件触发 */
  25. DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None; /* 不使用波形发生 */
  26. DAC_InitStructure.DAC_LFSRUnmask_TriangleAmplitude = DAC_LFSRUnmask_Bit0; /* LFSR掩码0 */
  27. DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Disable; /* 失能输出缓冲 */
  28. DAC_Init(DAC_Channel_1, &DAC_InitStructure); /* 初始化DAC通道1 */
  29. DAC_Cmd(DAC_Channel_1, ENABLE); /* 开启DAC通道1 */
  30. /* 默认12bit右对齐输出0 */
  31. DAC_SetChannel1Data(DAC_Align_12b_R ,0);
  32. DAC_SoftwareTriggerCmd(DAC_Channel_1, ENABLE);
  33. }
  34. /**
  35. * @brief DAC通道2初始化
  36. * @note PA5引脚输出
  37. * @param None
  38. * @retval None
  39. */
  40. void dac_ch2_config(void)
  41. {
  42. DAC_InitTypeDef DAC_InitStructure;
  43. GPIO_InitTypeDef GPIO_InitStructure;
  44. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); /* 开启GPIO外设相关时钟 */
  45. RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE); /* 开启DAC时钟 */
  46. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; /* 设置引脚号 */
  47. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; /* 设置引脚模式 */
  48. GPIO_Init(GPIOA, &GPIO_InitStructure); /* 初始化GPIO */
  49. DAC_InitStructure.DAC_Trigger = DAC_Trigger_Software; /* 软件触发 */
  50. DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None; /* 不使用波形发生 */
  51. DAC_InitStructure.DAC_LFSRUnmask_TriangleAmplitude = DAC_LFSRUnmask_Bit0; /* LFSR掩码0 */
  52. DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Disable; /* 失能输出缓冲 */
  53. DAC_Init(DAC_Channel_2, &DAC_InitStructure); /* 初始化DAC通道1 */
  54. DAC_Cmd(DAC_Channel_2, ENABLE); /* 开启DAC通道1 */
  55. /* 默认12bit右对齐输出0 */
  56. DAC_SetChannel2Data(DAC_Align_12b_R ,0);
  57. DAC_SoftwareTriggerCmd(DAC_Channel_2, ENABLE);
  58. }
  59. /**
  60. * @brief DAC输出
  61. * @param dacChx : DAC输出通道 @ref dac_output_chx
  62. * @arg DAC_OUTPUT_CH1 : DAC输出通道1-PA4
  63. * @arg DAC_OUTPUT_CH2 : DAC输出通道2-PA5
  64. * @param value : DAC输出值
  65. * @retval None
  66. */
  67. void dac_output(uint8_t dacChx, uint16_t value)
  68. {
  69. if(DAC_OUTPUT_CH1 == dacChx)
  70. {
  71. DAC_SetChannel1Data(DAC_Align_12b_R ,value);
  72. DAC_SoftwareTriggerCmd(DAC_Channel_1, ENABLE);
  73. }
  74. else if(DAC_OUTPUT_CH2 == dacChx)
  75. {
  76. DAC_SetChannel2Data(DAC_Align_12b_R ,value);
  77. DAC_SoftwareTriggerCmd(DAC_Channel_2, ENABLE);
  78. }
  79. }