#include "dac_cfg.h" #include "gpio_cfg.h" #include "base/delay.h" /* @brief 默认初始化 */ void DAC_Definit(void) { GPIO_InitTypeDef GPIO_InitStructure; DAC_InitTypeDef DAC_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE); GPIO_InitStructure.GPIO_Pin = C_H_DAC_Pin|C_M_DAC_Pin; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; GPIO_Init(GPIOA, &GPIO_InitStructure); DAC_DeInit(); DAC_InitStructure.DAC_Trigger = DAC_Trigger_Software; DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None; DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable; DAC_Init(DAC_Channel_1, &DAC_InitStructure); DAC_Init(DAC_Channel_2, &DAC_InitStructure); DAC_Cmd(DAC_Channel_1, ENABLE); DAC_Cmd(DAC_Channel_2, ENABLE); C_H_DAC_Set(4000); /* configure the DAC1 */ C_M_DAC_Set(4000); } /* @brief 设置高电平电压 @param dac_val DAC0输出值 0-4096 */ void C_H_DAC_Set(uint16_t dac_val) { uint16_t read=0; read=DAC_GetDataOutputValue(DAC_Channel_1); if(dac_val==read) return; DAC_SetChannel1Data(DAC_Align_12b_R, dac_val); do{ DAC_SoftwareTriggerCmd(DAC_Channel_1,ENABLE); read=DAC_GetDataOutputValue(DAC_Channel_1); }while(read!=dac_val); } /* @brief 设置高电平电压 @param dac_val DAC1输出值 0-4096 */ void C_M_DAC_Set(uint16_t dac_val) { uint16_t read=0; read=DAC_GetDataOutputValue(DAC_Channel_2); if(dac_val==read) return; DAC_SetChannel2Data(DAC_Align_12b_R, dac_val); do{ DAC_SoftwareTriggerCmd(DAC_Channel_2,ENABLE); delay_us(2); read=DAC_GetDataOutputValue(DAC_Channel_2); }while(read!=dac_val); }