STM32——DAC

0.比较坑啊

  0)stm32f10x板

  1)【GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;  //模拟输入】

    引脚设置成模拟输入是为了防干扰

  2)【//DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Disable;  //DAC1输出缓存关闭 BOFF1=1】
    【 DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable;  //DAC1输出缓存打开,提高端口驱动能力 BOFF1=0】

    第二句才有一定的驱动能力,否则死活连个灯都不亮

    用另外的板子读取过值,2个代码都能输出理想电压;可第1句代码却无法驱动小灯,估计是电流过小;据说要加电压跟随器

  3)我买的板子并未直接将Vref与电源相连;需要自己去提供参考电压


1.

  1)GPIOA的时钟是在APB2,DAC的时钟则是在APB1

  2)DAC的通道引脚在电路图里有

    PA4——DAC_OUT1

    PA5——DAC_OUT2

  3)模式有8位和12位;

    为12位模式时,设置的值在4095以内

  3)流程

    设置GPIOA为模拟输出

    不使用触发功能

    不使用波形发生

    屏蔽、幅值设置

    DAC1输出缓存打开【提高端口驱动能力!】

    初始化DAC通道1

    使能DAC1

    设置DAC值

  4)重复修改值调用只用调用以下2句代码

    DAC_SetChannel1Data(DAC_Align_12b_R, Value);
    DAC_SoftwareTriggerCmd(DAC_Channel_1, ENABLE);


2.代码:PA4口输出 = 3000 / 4095 * Vref 的电压

#include <stm32f10x.h>

void GPIO_Config(void);
void DAC_Config(void);
void DAC_SetValue(int Value);
    
int main()
{
    GPIO_Config();
    DAC_Config();
    
    while(1);
}

void GPIO_Config()
{
    GPIO_InitTypeDef GPIO_InitStructure;
    
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, 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);
}

void DAC_Config()
{
    DAC_InitTypeDef DAC_InitStructure;
    
    //使能DAC通道时钟
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE ); 
    
    //不使用触发功能 TEN1=0
    DAC_InitStructure.DAC_Trigger = DAC_Trigger_None;
    //不使用波形发生
    DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None;
    //屏蔽、幅值设置
    DAC_InitStructure.DAC_LFSRUnmask_TriangleAmplitude = DAC_LFSRUnmask_Bit0;
    //DAC1输出缓存关闭 BOFF1=1
//DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Disable;
    //DAC1输出缓存打开,提高端口驱动能力 BOFF1=0
    DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable;
    //初始化DAC通道1
    DAC_Init(DAC_Channel_1, &DAC_InitStructure);
    //使能DAC1
    DAC_Cmd(DAC_Channel_1, ENABLE);
    //12位右对齐数据格式设置DAC值
    DAC_SetChannel1Data(DAC_Align_12b_R, 3000);
}

/***************************************
**
**设置通道1输出电压
**反复调用并修改数值仅调用以下2句
**主函数并未使用,备用
**
***************************************/
void DAC_SetValue(int Value)
{
    DAC_SetChannel1Data(DAC_Align_12b_R, Value);
    DAC_SoftwareTriggerCmd(DAC_Channel_1, ENABLE);
}
原文地址:https://www.cnblogs.com/maplesnow/p/5397723.html