[应用相关] Stm32f103 DAC 学习笔记

[复制链接]
988|7
 楼主| externally 发表于 2019-6-17 16:43 | 显示全部楼层 |阅读模式
最近在做电流型信号输出的项目,遇到了些问题这里把这些解决方法做一个笔记方便以后运用。在搞这个的时候因为手册这部分讲的不是很详细,所以在使用上也遇到了些阻力。
用的是64封装的芯,   此芯ADC的基准Vref+和电源是同一个端口,Vref-共用电源地。在电池输出时AD值为0时 取样电阻100欧姆 有0.66mA的电流输出,只要在初始化时只要失能端口输出缓冲,输出可到0.0025mA。OK问题就解决了。
  1. void AnalogInit(void)
  2. {
  3.     DAC_InitTypeDef  DAC_InitStructure;
  4.     GPIO_InitTypeDef GPIO_InitStructure;

  5.       RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOA, ENABLE);
  6.       /* DAC Periph clock enable */
  7.       RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);
  8.       
  9.     /* Configure DAC channe1 output pin */
  10.     GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_4;
  11.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  12.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  13.     GPIO_Init(GPIOA, &GPIO_InitStructure);

  14.     /* Configure DAC channe1 output pin */
  15.     GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_5;
  16.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  17.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  18.     GPIO_Init(GPIOA, &GPIO_InitStructure);

  19.    
  20.     /* DAC channel1 Configuration */
  21.     DAC_InitStructure.DAC_Trigger = DAC_Trigger_Software;
  22.     DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None;
  23.     DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Disable;    //输出缓冲失能   
  24.     DAC_InitStructure.DAC_LFSRUnmask_TriangleAmplitude = DAC_TriangleAmplitude_4095;

  25.     DAC_Init(DAC_Channel_1, &DAC_InitStructure);

  26.     /* DAC channel2 Configuration */
  27.     DAC_Init(DAC_Channel_2, &DAC_InitStructure);

  28.     /* Enable DAC Channel1: Once the DAC channel1 is enabled, PA.04 is
  29.     automatically connected to the DAC converter. */
  30.     DAC_Cmd(DAC_Channel_1, ENABLE);
  31.     /* Enable DAC Channel2: Once the DAC channel2 is enabled, PA.05 is
  32.     automatically connected to the DAC converter. */
  33.     DAC_Cmd(DAC_Channel_2, ENABLE);

  34. }

  35. //端口1AD值更新
  36. void DAC1_update(u16 ch1)
  37. {
  38.     ch1 = (ch1 <<4) & 0xfff0;
  39.     /* Set DAC Channel1 DHR12L register */
  40.     DAC_SetChannel1Data(DAC_Align_12b_L, ch1);

  41.     /* Start DAC Channel1 conversion by software */
  42.     DAC_SoftwareTriggerCmd(DAC_Channel_1, ENABLE);
  43. }

  44. void DAC2_update(u16 ch2)
  45. {
  46.     ch2 = (ch2 <<4) & 0xfff0;
  47.     /* Set DAC Channel2 DHR12L register */
  48.     DAC_SetChannel2Data(DAC_Align_12b_L, ch2);

  49.     /* Start DAC Channel1 conversion by software */
  50.     DAC_SoftwareTriggerCmd(DAC_Channel_2, ENABLE);
  51. }


mmuuss586 发表于 2019-6-29 22:16 | 显示全部楼层
不错,感谢分享
mmuuss586 发表于 2019-6-29 22:16 | 显示全部楼层
不错,感谢分享
xiaoqizi 发表于 2019-7-8 09:30 | 显示全部楼层
要用高精度电阻吗
磨砂 发表于 2019-7-8 09:38 | 显示全部楼层
要求不高的话 应该不用高精度的把
晓伍 发表于 2019-7-8 09:55 | 显示全部楼层
驱动能力如何
八层楼 发表于 2019-7-8 10:00 | 显示全部楼层
非常感谢楼主分享
观海 发表于 2019-7-8 10:45 | 显示全部楼层
代码写的非常整齐
您需要登录后才可以回帖 登录 | 注册

本版积分规则

10

主题

285

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部