STM32的DAC輸出驅動電壓
阿新 • • 發佈:2019-01-30
目的
專案中的模組對輸出電壓的改變來控制功率、所以考慮用stm32內部的DAC來輸出電壓作為驅動。
原始碼
#include "dac.h" void Dac1_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; DAC_InitTypeDef DAC_InitType; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE ); RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE ); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_SetBits(GPIOA,GPIO_Pin_4) ; DAC_InitType.DAC_Trigger=DAC_Trigger_Software; DAC_InitType.DAC_WaveGeneration=DAC_WaveGeneration_None; DAC_InitType.DAC_LFSRUnmask_TriangleAmplitude=DAC_LFSRUnmask_Bit0; //DAC_InitType.DAC_OutputBuffer=DAC_OutputBuffer_Disable ; DAC_InitType.DAC_OutputBuffer=DAC_OutputBuffer_Enable ; DAC_Init(DAC_Channel_1,&DAC_InitType); DAC_Cmd(DAC_Channel_1, ENABLE); //ʹÄÜDAC1 DAC_SetChannel1Data(DAC_Align_12b_R, 0); } void Dac1_Set_Vol(uint16_t vol) { uint16_t temp; temp = (uint16_t) ((vol/3.3) * 4096); DAC_SetChannel1Data(DAC_Align_12b_R,temp); DAC_SoftwareTriggerCmd(DAC_Channel_1,ENABLE); } void DAC_OutVoltage(float valtage) { uint16_t data; data = (uint16_t)((valtage/3.3) *4096); DAC_SetChannel1Data(DAC_Align_12b_R,data); DAC_SoftwareTriggerCmd(DAC_Channel_1,ENABLE); }
在實現輸出的電壓後、用萬用表測量。輸出的電壓雖然正確。但用在模組上發現驅動能力不夠
//DAC_InitType.DAC_OutputBuffer=DAC_OutputBuffer_Disable ;
正是由於DAC_OutputBuffer沒有使能!。。。。