各位同学好, 这两天弄STM32的DAC,发现输出不了可变电压。 我是看了手册,之后借鉴了一下开发板的教程,但是没有完全依照教程来做。 因为按到手册里面有说,如果不设置TEN, 那么,在写入到:
0: DAC channel1 trigger disabled and data written into the DAC_DHRx register are
transferred one APB1 clock cycle later to the DAC_DOR1 register
我想,那就不用触发模式。直接写入数据寄存器,让它自己动去转换成模拟电压。
下面是我的程序,请帮忙看看,是啥问题,多谢!
void Init_DAC(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
DAC_InitTypeDef DAC_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
GPIO_InitStructure.GPIO_Pin =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);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);
DAC_InitStructure.DAC_Trigger = DAC_Trigger_None;
DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None;
DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable;
DAC_InitStructure.DAC_LFSRUnmask_TriangleAmplitude = DAC_TriangleAmplitude_4095;
DAC_Init(DAC_Channel_2, &DAC_InitStructure);
DAC_Cmd(DAC_Channel_2,ENABLE);
}
void Feed_DAC(unsigned int val)
{
if(val>4096)
val=4096;
DAC_SetChannel2Data(DAC_Align_12b_R,val);
}
在Main函数里,先调用Init_DAC,然后调用Feed_DAC(4096);
|