基于单片机Stm32f103 DAC电流输出解决方案

基于单片机Stm32f103 DAC电流输出解决方案,第1张

最近在做电流型信号输出的项目,遇到了些问题这里把这些解决方法做一个笔记方便以后运用。在搞这个的时候因为手册这部分讲的不是很详细,所以在使用上也遇到了些阻力。

用的是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

6 RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOA, ENABLE);

7 /* DAC Periph clock enable */

8 RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);

9

10 /* Configure DAC channe1 output pin */

11 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;

12 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

13 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;

14 GPIO_Init(GPIOA, &GPIO_InitStructure);

15

16 /* Configure DAC channe1 output pin */

17 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;

18 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

19 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;

20 GPIO_Init(GPIOA, &GPIO_InitStructure);

21

22

23 /* DAC channel1 ConfiguraTIon */

24 DAC_InitStructure.DAC_Trigger = DAC_Trigger_Software;

25 DAC_InitStructure.DAC_WaveGeneraTIon = DAC_WaveGeneraTIon_None;

26 DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Disable; //输出缓冲失能

27 DAC_InitStructure.DAC_LFSRUnmask_TriangleAmplitude = DAC_TriangleAmplitude_4095;

28

29 DAC_Init(DAC_Channel_1, &DAC_InitStructure);

30

31 /* DAC channel2 ConfiguraTIon */

32 DAC_Init(DAC_Channel_2, &DAC_InitStructure);

33

34 /* Enable DAC Channel1: Once the DAC channel1 is enabled, PA.04 is

35 automatically connected to the DAC converter. */

36 DAC_Cmd(DAC_Channel_1, ENABLE);

37 /* Enable DAC Channel2: Once the DAC channel2 is enabled, PA.05 is

38 automatically connected to the DAC converter. */

39 DAC_Cmd(DAC_Channel_2, ENABLE);

40

41 }

42

43 //端口1AD值更新

44 void DAC1_update(u16 ch1)

45 {

46 ch1 = (ch1 《《4) & 0xfff0;

47 /* Set DAC Channel1 DHR12L register */

48 DAC_SetChannel1Data(DAC_Align_12b_L, ch1);

49

50 /* Start DAC Channel1 conversion by software */

51 DAC_SoftwareTriggerCmd(DAC_Channel_1, ENABLE);

52 }

53

54 void DAC2_update(u16 ch2)

55 {

56 ch2 = (ch2 《《4) & 0xfff0;

57 /* Set DAC Channel2 DHR12L register */

58 DAC_SetChannel2Data(DAC_Align_12b_L, ch2);

59

60 /* Start DAC Channel1 conversion by software */

61 DAC_SoftwareTriggerCmd(DAC_Channel_2, ENABLE);

62 }

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/dianzi/2607545.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-08-09
下一篇 2022-08-09

发表评论

登录后才能评论

评论列表(0条)

保存