#include "dac.h"
void Dac1_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
DAC_InitTypeDef DAC_InitType;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);//使能GPIOA時鐘
RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);//使能DAC時鐘
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;//模擬輸入
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;//下拉
GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化
DAC_InitType.DAC_Trigger=DAC_Trigger_None; //不使用觸發(fā)功能 TEN1=0
DAC_InitType.DAC_WaveGeneration=DAC_WaveGeneration_None;//不使用波形發(fā)生
DAC_InitType.DAC_LFSRUnmask_TriangleAmplitude=DAC_LFSRUnmask_Bit0;//屏蔽、幅值設置
DAC_InitType.DAC_OutputBuffer=DAC_OutputBuffer_Enable ; //DAC1輸出緩存關(guān)閉 BOFF1=1
DAC_Init(DAC_Channel_1,&DAC_InitType); //初始化DAC通道1
DAC_Cmd(DAC_Channel_1, ENABLE); //使能DAC通道1
DAC_SetChannel1Data(DAC_Align_12b_R, 0); //12位右對齊數(shù)據(jù)格式設置DAC值
}
//設置通道1輸出電壓
//vol:0~3300,代表0~3.3V
void Dac1_Set_Vol(u16 vol)
{
double temp=vol;
temp/=1000;
temp=temp*4096/3.3;
DAC_SetChannel1Data(DAC_Align_12b_R,temp);//12位右對齊數(shù)據(jù)格式設置DAC值
}
|