返回列表 回复 发帖

[原创] 萧星的地盘---stm32 DAC

stm32的dac 可配置成 12位和8位数据模式  并分为左右对齐  拥有两个 独立的转换通道  有各自的转换模块  可以dma传输
  ok  下面我们根据程序来看一下dac的使用  当然硬件还有很多注意的 希望有经验的网友不吝赐教
本帖最后由 萧星 于 2009-5-31 15:10 编辑

rcc  不再讲了  基本配置  到72m
/* DAC channel1 Configuration */
  DAC_InitStructure.DAC_Trigger = DAC_Trigger_T2_TRGO;
                               设置触发模式为tim2 更新触发 在这里对Tenx 也做了设置 看寄存器 dac_CR
  DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_Triangle;
选择wave【】 三角波使能
  DAC_InitStructure.DAC_LFSRUnmask_TriangleAmplitude = DAC_TriangleAmplitude_2047;
幅度为 2047
  DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Disable;
  输出缓冲禁止
  DAC_Init(DAC_Channel_1, &DAC_InitStructure);
   配置dac1主要是配置寄存器dac_CR

  
  /* DAC channel2 Configuration */
  DAC_InitStructure.DAC_LFSRUnmask_TriangleAmplitude = DAC_TriangleAmplitude_1023;
  DAC_Init(DAC_Channel_2, &DAC_InitStructure);
设置DAC2为不同的幅度
  /* Enable DAC Channel1 */
  DAC_Cmd(DAC_Channel_1, ENABLE);

  /* Enable DAC Channel2 */
  DAC_Cmd(DAC_Channel_2, ENABLE);
使能两个通道 通过设置cr寄存器的相关位
  /* Set DAC dual channel DHR12RD register */
  DAC_SetDualChannelData(DAC_Align_12b_R, 0x100, 0x7f);
设置转换值
待续。。。。 大家节日快乐
因为上面 设置了触发功能  所以我们还要配置 tim2为比较 触发输出
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);

  /* TIM2 TRGO selection */
  TIM_SelectOutputTrigger(TIM2, TIM_TRGOSource_Update);
选择位 比较  触发输出模式
关于dac用到的 pa4 pa5 的配置  为了避免寄生的干扰和额定的功耗 两个io最好配置为模拟输入
void GPIO_Configuration(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;

  /* Once the DAC channel is enabled, the corresponding GPIO pin is automatically
     connected to the DAC converter. In order to avoid parasitic consumption,
     the GPIO pin should be configured in analog */
  GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_4 | GPIO_Pin_5;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
}
ok 给出完整main.c
main.rar (2.27 KB)
如果想要输出诸如 正弦波之类的  最好用dma了   而且选择

   DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None;
我正搞这个,请问你有DAC的中文固件库介绍吗,能否发一份给我,邮箱:604302559@qq.com
mark。。。。。。。。。。。。。。。。。。。。。。
返回列表