switch_test/User/dac.c

71 lines
1.7 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "basic.h"
/**
* @brief 使能DAC的时钟初始化GPIO
* @param 无
* @retval 无
*/
void DAC_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
DAC_InitTypeDef DAC_InitStructure;
/* 使能GPIOA时钟 */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
/* 使能DAC时钟 */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);
/* DAC的GPIO配置模拟输入 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* 配置DAC 通道1 */
DAC_InitStructure.DAC_Trigger = DAC_Trigger_Software; //使用软件触发
DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None; //不使用波形发生器
DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable; //使用DAC输出缓冲
DAC_Init(DAC_Channel_1, &DAC_InitStructure);
/* 配置DAC 通道2 */
DAC_Init(DAC_Channel_2, &DAC_InitStructure);
/* 配置DAC 通道1、2 */
DAC_Cmd(DAC_Channel_1, ENABLE);
DAC_Cmd(DAC_Channel_2, ENABLE);
//初始值
DAC_SetChannel1Data(DAC_Align_12b_R,0x0000);
DAC_SetChannel2Data(DAC_Align_12b_R,0x0000);
//软件触发
DAC_SoftwareTriggerCmd(DAC_Channel_1,ENABLE);
DAC_SoftwareTriggerCmd(DAC_Channel_2,ENABLE);
}
void DAC1_SetVol(float voltage)
{
uint16_t data;
data = (uint16_t)((voltage / 3.0f) * 4095);
DAC_SetChannel1Data(DAC_Align_12b_R,data);
DAC_SoftwareTriggerCmd(DAC_Channel_1,ENABLE);
}
void DAC2_SetVol(float voltage)
{
uint16_t data;
data = (uint16_t)((voltage / 3.0f) * 4095);
DAC_SetChannel2Data(DAC_Align_12b_R,data);
DAC_SoftwareTriggerCmd(DAC_Channel_2,ENABLE);
}