STM32F103VCT6自带两个12位DAC,DAC的转换速度一直没有查到,网上有人说是1MHZ的频率,那就是1us了。ADC的转换时间在56MHZ工作频率下为1us,在72MHZ工作频率下为1.17us。如果AD和DA有对称关系的话,那么很可能跟ADC的时间相同。(仅作分析用!)
DAC于我,有两个用途:输出波形和输出固定电压。先来说说前者的配置。
第一个参数:触发方式,DAC_InitStructure.DAC_Trigger。可选的外部触发源一共有八个。六个是定时器触发:TIM2,TIM4,TIM5,TIM6,TIM7和TIM8。剩下两个分别是:EXTI线路9和软件触发。如果采用定时器触发的话,就还要再编写相应的定时器函数,这个倒不是很复杂,和定时器的编写方式类似。如:DAC_InitStructure.DAC_Trigger = DAC_Trigger_T6_TRGO;//选择定时器6作外部触发源
void TIM_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Period = 0x85;
TIM_TimeBaseStructure.TIM_Prescaler = 0x0;
TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM6, &TIM_TimeBaseStructure);
TIM_SelectOutputTrigger(TIM6, TIM_TRGOSource_Update);
}
输出信号频率计算:假设输出一个6个点的锯齿波波形,则其频率为:(72MHZ/(0x85+1))*6=89.552KHZ.注意:如果prescaler不为0,则时钟还要再除以(prescaler+1)
第二个参数:DAC_InitStructure.DAC_WaveGeneration。顾名思义:波形发生器。STM32内部集成了两个幅度可调的波形发生器,可以产生三角波和噪声波。如果我们使用自定义的缓冲区的话,就不需要配置这个参数或者使其为DAC_WaveGeneration_None,但为了避免别人误以为你忘了配置这个参数,所以还是多写一句吧,养成好的习惯。
第三个参数:DAC_InitStructure.DAC_OutputBuffer,即是否使用输出缓存。输出缓存的功能主要用来减小输出阻抗,是STM32的DAC无需外部运放就可以直接驱动负载。这里一般设置成DAC_OutputBuffer_Disable,即关闭外部缓存。这是为保险起见,万一驱动能力不够强(本人也没试过@_@),那调试的时间就又多了一些。
参数的配置就这么多了,算得上STM32中最简单的外设配置之一了!
一般来说,用DAC输出波形,传输的数据比较多,所以采用DMA传输可以节省CPU的开支。所以在调用DAC_DMACmd(DAC_Channel_1, ENABLE);函数后别忘了配置DMA函数。详细的配置说明就不写了,这里把代码贴出来:
void DMA_Configuration(void)
{
DMA_InitTypeDef DMA_InitStructure;
DMA_DeInit(DMA2_Channel3);
DMA_InitStructure.DMA_PeripheralBaseAddr = DAC_DHR12R1_Address;
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&FIR_OutPutValueTab;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
DMA_InitStructure.DMA_BufferSize = 200;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA2_Channel3, &DMA_InitStructure);
DMA_Cmd(DMA2_Channel3, ENABLE);
DMA_ITConfig(DMA2_Channel3,DMA_IT_TC,ENABLE);
}
注意红色字体写的那行代码,当我们传输多个数据时,需要将其设置为连续转换!其余就不赘述了。
接下来谈谈DAC固定电压的配置。
STM32的DAC固定电压配置和上面的波形输出配置相似,不同的地方在于它要多调用一个函数:DAC_SoftwareTriggerCmd(DAC_Channel_1,ENABLE);这样才会输出固定的电平。
具体配置如下:
void DAC_VOLTAGE_Configuration(void)
{
DAC_InitTypeDef DAC_InitStructure;
DAC_DeInit();
DAC_InitStructure.DAC_Trigger = DAC_Trigger_None;
DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None;
DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Disable;
DAC_Init(DAC_Channel_1, &DAC_InitStructure);
//
DAC_Cmd(DAC_Channel_1, ENABLE);
DAC_SoftwareTriggerCmd(DAC_Channel_1,ENABLE);
}
配置完成后,调用DAC_SetChannel1Data(DAC_Align_12b_R,4000);函数就可以发送数据了!!极其方便。比起5615,0832之流不知简便了多少!有两点要注意:1、该函数的第一个参数DAC_Align_12b_R不可依据设置ADC外设地址的方式重写这个地址,因为这个参数是不带基址的,而我们重写地址后一半会把基址算进去,这样就不对了!(笔者就曾经卡在这里!看来凡事不可先入为主,否则问题迟早会发生。)2、每次改变发送数据时,要同时调用以下两个函数:
DAC_SetChannel1Data(DAC_Align_12b_R,4000);
DAC_SoftwareTriggerCmd(DAC_Channel_1,ENABLE);
至于为什么,我的理解是:因为DAC改变数据后并不会立即写入寄存器,需要一次更新,由于不是通过定时器触发来更新,因此就只能通过调用函数的方法来更新。
=======================================================================================================
1、简述:
12位数字输入,电压输出,DAC可以配置为8位或12位模式。有2个输出通道。在双DAC模式下,两个通道可以独立地工作。
特殊功能: 噪声波形生成,三角波形生成,外部触发转换,双DAC同时或者分别转换;每个通道都有DMA功能;
参考电压:2.4V~ 3.3V
因为一旦使能DAC通道,那么相应的GPIO便自动地连接到DAC转换器,因此,模拟输出前应该设置成模拟输入,目的是避免寄生的干扰和额外的功耗;
2、函数介绍:
(1)缺省函数
DAC_Init();
(2)DAC_Init(DAC_Channel_1,&DAC_InitStructure);初始化函数
DAC_InitTypeDef DAC_InitStructure;
初始化的设置有四个:
❶DAC_Trigger:指定所选DAC的触发源,有9个触发源可以选择:
DAC_Trigger_None
DAC_Trigger_T6_TRGO
DAC_Trigger_T8_TRGO
DAC_Trigger_T7_TRGO
DAC_Trigger_T5_TRGO
DAC_Trigger_T2_TRGO
DAC_Trigger_T4_TRGO
DAC_Trigger_Ext_IT9
DAC_Trigger_Software
❷DAC_WaveGeneration :指定DAC是否产生噪声波形,三角波或者不产生以上波形
DAC_WaveGeneration_None
DAC_WaveGeneration_Noise
DAC_WaveGeneration_Triangle
❸DAC_LFSRUnmask_TriangleAmplitude:如果选择了产生噪声波形或者三角波,那么这里可以选择噪声波形的LFSRUnMask屏蔽位(不晓得是什么东西) 或者三角波的最大幅度
❹DAC_OutputBuffer:指定输出是否要缓冲一下
DAC_OutputBuffer_Enable
DAC_OutputBuffer_Disable
(3)DAC_Cmd(DAC_Channel_1,EANBLE); DAC通道使能函数
(4)DAC_DMACmd(DAC_Channel_1,ENABLE); 如果要用到DMA到DAC那么就要使能哪个DAC通道
(5)DAC_SoftwareTriggerCmd(DAC_Channel_1, ENABLE);软件触发使能函数,一般单通道时候使用
(6)DAC_DualSoftwareTriggerCmd(ENABLE);如果要同时控制两个DAC通道,那么就要使能此函数
(7)DAC_Wave GenerationCmd(DAC_Channel_1, DAC_Wave_Noise, ENABLE);波形发生使能函数,可以选择哪个通道以及噪声或者三角波波形
(8)DAC_SetChannel1Data(DAC_Align_12b_R, 0x500);
选择通道1的数据位数和对齐方式,以及具体输出值。三种对齐数据位方式:
DAC_Align_12b_R 12位右对齐,数值小于4096
DAC_Align_12b_L 12位左对齐,数值小于0XxFFF0
DAC_Align_8b_R 8位右对齐,数值小于256
(9)DAC_SetChannel2Data(DAC_Align_12b_L, 0x8880);同理(8)
(10)DAC_SetDualChannelData(DAC_Align_8b_R, 0x40, 0xF1);同理(8)
(11)u16 DataValue;
DataValue = DAC_GetDataOutputValue(DAC_Channel_1); 得到当前DAC输出的值
3、具体应用:
3.1 产生一个直流电压:
GPIOA_04 是DAC1 , GPIOA_05 是DAC2
1、设置GPIOA_04 或05 为模拟输入;
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOA, &GPIO_InitStructure)
2、开启GPIOA和DAC的时钟。注意GPIOA的时钟是在APB2,而DAC的时钟则是在APB1
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);
3、配置DAC输出直流电压
DAC_InitTypeDef DAC_InitStructure;
DAC_InitStructure.DAC_Trigger = DAC_Trigger_None;//选择DAC的触发输出,当产生波形时可用定时器触发
DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None;
DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Disable;
DAC_Init(DAC_Channel_1, &DAC_InitStructure);
DAC_Cmd(DAC_Channel_1, ENABLE);
DAC_SetChannel1Data(DAC_Align_12b_R,4095);//我的板子实际输出的是3.265v
3.2 产生三角波
在上述代码基础上加入下面代码,其中部分代码替代
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Period = 0xF;
TIM_TimeBaseStructure.TIM_Prescaler = 0xF;
TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_SelectOutputTrigger(TIM2, TIM_TRGOSource_Update);
/*
TIM_SelectOutputTrigger()函数到底是干嘛的?定时器触发输出,比如用作触发另一个定时器,触发AD转换等,触发其他外设
*/
DAC_InitStructure.DAC_Trigger = DAC_Trigger_T2_TRGO;
DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_Triangle;
DAC_InitStructure.DAC_LFSRUnmask_TriangleAmplitude = DAC_TriangleAmplitude_2047;
3.3 利用DMA产生正弦波(DAC2)和直流电压值(DAC1)
uint16_t g_Wave[128];
uint16_t DAC1_Value[1];
const uint16_t SineData[32] = {
2047, 2447, 2831, 3185, 3498, 3750, 3939, 4056, 4095, 4056,
3939, 3750, 3495, 3185, 2831, 2447, 2047, 1647, 1263, 909,
599, 344, 155, 38, 0, 38, 155, 344, 599, 909, 1263, 1647};
void RCC_DAC_Config(void)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA2,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
}
void GPIO_DAC_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_4|GPIO_Pin_5;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOA,&GPIO_InitStructure);
}
void DAC_Config(void)
{
uint32_t i=0;
DMA_InitTypeDef DMA_InitStructure;
DAC_InitTypeDef DAC_InitStructure;
DAC1_Value[0]=4096*0.612;
for(i=0;i<32;i++)
{
//g_Wave[i]=DAC2_SineWave[i] *0.606; //Vpp =2.0V
g_Wave[i]=SineData[i] *0.606; //Vpp =2.0V
}
RCC_DAC_Config();
GPIO_DAC_Config();
TIM_PrescalerConfig(TIM2,0,TIM_PSCReloadMode_Update);
TIM_SetAutoreload(TIM2, 22);
TIM_SelectOutputTrigger(TIM2, TIM_TRGOSource_Update);
DAC_InitStructure.DAC_Trigger = DAC_Trigger_T2_TRGO;
DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None;
DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Disable;
#if 0
DAC_Init(DAC_Channel_1, &DAC_InitStructure);
DMA_DeInit(DMA2_Channel3);
DMA_InitStructure.DMA_PeripheralBaseAddr = DAC_DHR12R1_Address;
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&DAC1_Value;
DMA_InitStructure.DMA_BufferSize = 1;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA2_Channel3, &DMA_InitStructure);
DMA_Cmd(DMA2_Channel3, ENABLE);
DAC_Cmd(DAC_Channel_1, ENABLE);
DAC_DMACmd(DAC_Channel_1, ENABLE);
TIM_Cmd(TIM2, ENABLE);
#else
DAC_Init(DAC_Channel_2, &DAC_InitStructure);
DMA_DeInit(DMA2_Channel4);
DMA_InitStructure.DMA_PeripheralBaseAddr = DAC_DHR12R2_Address;
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&g_Wave;
DMA_InitStructure.DMA_BufferSize = 32;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA2_Channel4,&DMA_InitStructure);
DMA_Cmd(DMA2_Channel4,ENABLE);
DAC_Cmd(DAC_Channel_2,ENABLE);
DAC_DMACmd(DAC_Channel_2,ENABLE);
TIM_Cmd(TIM2,ENABLE);
#endif
}