你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

【经验分享】STM32G031 HAL库adc+DMA采集电压

[复制链接]
STMCU小助手 发布时间:2021-11-10 01:00
硬件原理图/ t$ A6 h  l- P
  E# `: g; f/ i3 S
20201216095853276.png
2 `/ w2 Q3 [6 f! r
; B4 F7 o* N/ D# C9 }
程序:& X% H: K2 z4 Q7 Q$ q! c% e

! A. K) r, \! D+ M# P& x( B! r
  1. int main(void)0 @% ~5 J+ N7 ?5 s/ J0 [4 Z
  2. {
    8 Y! o" c1 z8 }* Q
  3.     //Flash_EnableReadProtection();//RDP设置 使能读保护
    , b$ b% _! \. P: Z
  4.     HAL_Init();/ _" Z' z  ^5 G, W/ a8 _+ l1 y+ w
  5.     SystemClock_Config();
    5 e+ t( E3 Z& P: r) [1 I: v
  6.     MX_GPIO_Init();
    2 V# _- ]7 K) H! N
  7.     MX_DMA_Init();
    7 T1 R! L6 ^; Y1 M
  8.     MX_I2C1_Init();2 H5 c* o$ x" }
  9.     MX_ADC1_Init();
    ) E1 A4 O& I% b% ?: \
  10.     MX_TIM2_Init();
    - R7 M+ o  L( g( w- |) m+ Q
  11.     MX_NVIC_Init();; e* r0 h9 a- k6 J3 Y5 @% u
  12.     HAL_TIM_Base_Start_IT(&htim2);
    , e: J7 m1 M+ h
  13.     HAL_ADC_Start_DMA(&hadc1,(uint32_t *)&adcbuf,2);5 t- u. z6 O  V( p# S
  14.     while(1)% J* R# V/ J) B( T& `: U
  15.     {
    : x! S% z4 }  K5 N, C9 B. r) |' |
  16. - G8 ~' _( v) J3 U' K5 c; G' J% R

  17. 7 R0 R* F8 d- o5 `4 H& E
  18.     }/ o  q+ c& U! M7 q/ P9 _7 Y
  19. }1 W2 m% B! K" e/ q9 V  h+ B
复制代码

+ K8 Y1 V% d7 J7 r. ]% S- d" w8 b# i
  1. void MX_DMA_Init(void)
    3 v' w5 H8 L6 a
  2. {
    ' Z9 B' V; e% o7 Y

  3. 4 p# I+ E3 s0 `% {( ]& m4 V4 R
  4.   /* DMA controller clock enable */2 t# u2 z8 b, M! L0 v
  5.   __HAL_RCC_DMA1_CLK_ENABLE();
    2 [8 S  D6 S% n# G2 \& T* C

  6. 6 X1 l: t6 z4 R
  7.   /* DMA interrupt init */! X1 A! _% ?- o3 b
  8.   /* DMA1_Channel1_IRQn interrupt configuration */
    3 W; c- b" \' F+ u) O" V1 C3 ^: v: Q4 H
  9.   HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 1, 0);
    & U/ L5 }2 I! {* Y% p; x. l
  10.   HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn);
    # O2 A. [7 ~* S' v6 e1 f# z+ c

  11. - w5 q2 I, T$ |, |: H
  12. }
    6 B+ M# q7 H9 Z4 y
复制代码
9 F. p4 z) X% o) }
. l6 O/ P' {4 i5 m! @6 }3 Z
  1. uint16_t adcbuf[2];//0,电池电压, 1,温度ADC: g" O+ u# n; B6 V& P1 Q* {1 S

  2. * \4 c8 P# v) `% I2 g7 Z) M( [
  3. ADC_HandleTypeDef hadc1;: i3 l2 k7 v' Q! o! Q, e( T$ v9 U
  4. DMA_HandleTypeDef hdma_adc1;" K4 y# w) H# S# s: z" V& g! R

  5. 4 U! `& |! W) T
  6. void MX_ADC1_Init(void)
    / t- E) l9 s0 a
  7. {4 D6 ~/ b/ e0 |% j0 ]
  8.   ADC_AnalogWDGConfTypeDef AnalogWDGConfig = {0};
    " o! k* K( [) ^  r$ K2 ^
  9.   ADC_ChannelConfTypeDef sConfig = {0};
    4 ^1 A5 P) ?* c/ u# L7 x
  10. ( Z* x' [- ^+ C
  11.   /** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion) 2 E" `9 L6 e! ]$ Q1 H. \
  12.   */
    - a/ w8 N1 J. @; R, k) A3 C
  13.   hadc1.Instance = ADC1;
    8 H+ c1 |5 l6 z% f
  14.   hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;1 ~$ _* @+ e. Y
  15.   hadc1.Init.Resolution = ADC_RESOLUTION_12B;
    ) q7 h" u/ F: L& S1 r
  16.   hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;8 s, L$ k) ~) b3 j( A3 Y1 e
  17.   //hadc1.Init.ScanConvMode = ADC_SCAN_ENABLE;//STM32CubeMX生成,只能使用通道 0-14,不能使用通道 15,16,17,18;! ]$ z# N- D9 Q8 B8 }
  18.   hadc1.Init.ScanConvMode = ADC_SCAN_SEQ_FIXED;3 Z3 h$ E$ w; q! y2 q
  19.   hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
    / j3 ]$ B! U& i- B2 I0 P
  20.   hadc1.Init.LowPowerAutoWait = DISABLE;
    ( G5 N5 T( ]7 k: ^2 M  J2 B6 N' Z
  21.   hadc1.Init.LowPowerAutoPowerOff = DISABLE;3 A, G2 Y1 v3 M" w% y8 c! \
  22.   hadc1.Init.ContinuousConvMode = ENABLE;
    2 t, `# i3 Q, u) a/ f) P
  23.   hadc1.Init.NbrOfConversion = 2;
    ! Z) _0 }1 q! S% c3 S" K( p
  24.   //hadc1.Init.DiscontinuousConvMode = DISABLE;//STM32CubeMX生成,只能使用通道 0-14,不能使用通道 15,16,17,18;6 M8 t: p  d5 K0 A3 n
  25.   hadc1.Init.DiscontinuousConvMode = ENABLE;
    4 R" ^$ m  n; Z: h5 r
  26.   hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;8 i8 Y; J$ ^! ~8 H, L: A* `
  27.   hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;- }! i- ?  @( ~' S& a  X8 }! x
  28.   hadc1.Init.DMAContinuousRequests = ENABLE;
    % L$ L/ i1 o- F9 Y
  29.   hadc1.Init.Overrun = ADC_OVR_DATA_PRESERVED;
    # t- B- z6 d0 A; Y6 h
  30.   hadc1.Init.SamplingTimeCommon1 = ADC_SAMPLETIME_160CYCLES_5;5 I. D& O9 k" s" W! ^
  31.   hadc1.Init.SamplingTimeCommon2 = ADC_SAMPLETIME_160CYCLES_5;
    ' f! F) P# B3 _% i1 n$ F/ ?* Q$ w
  32.   hadc1.Init.OversamplingMode = DISABLE;
    5 Y: l+ A# H$ L& z
  33.   hadc1.Init.TriggerFrequencyMode = ADC_TRIGGER_FREQ_HIGH;) C3 H& H8 V( ]) P- L! {" k& R
  34.   if (HAL_ADC_Init(&hadc1) != HAL_OK)
    $ L8 s+ `8 T8 _5 T; d
  35.   {2 L( |0 S: K* q1 K- J, [/ H
  36.     Error_Handler();" @% u4 B& y( j1 H  E3 q
  37.   }
    ( I. P9 ~' ~4 H& I: D$ A
  38.   /** Configure Analog WatchDog 2
    1 H4 d9 ]! ^1 s* L% \7 }& x6 m& L
  39.   */
    $ h, C) F) T' g2 L, H3 R# P
  40.   AnalogWDGConfig.WatchdogMode = ADC_ANALOGWATCHDOG_SINGLE_REG;8 d! o9 K4 o+ Y& c" m8 N
  41.   if (HAL_ADC_AnalogWDGConfig(&hadc1, &AnalogWDGConfig) != HAL_OK)  M: D  Q2 Z# L9 i/ k0 Z0 ?
  42.   {
    8 N% }) }. t- Z# u4 n5 Y, H
  43.     Error_Handler();6 G5 M3 g8 C& S3 ~
  44.   }- V+ N! x! U+ e3 q3 T
  45.   /** Configure Regular Channel + B) v+ Z; J% v& f7 F
  46.   */4 d8 }7 _$ z  \& U& w& c8 `
  47.   sConfig.Channel = ADC_CHANNEL_8;: D, p- o. L, m, ]. h
  48.   sConfig.Rank = ADC_REGULAR_RANK_1;7 i2 n4 x; t& Y' c5 Y, [+ I
  49.   sConfig.SamplingTime = ADC_SAMPLINGTIME_COMMON_1;9 k. B+ I. T2 a9 Y  m
  50.   if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
    $ c- b$ I  x0 k( f/ k) s1 o
  51.   {
    3 p2 ~; z. s& p' |4 m/ k
  52.     Error_Handler();
    , x) G2 r0 {0 t3 ~+ w
  53.   }3 q6 |# q5 t0 W1 t8 w7 l  b) U
  54.   /** Configure Regular Channel
    , u+ F# ?2 R# B3 B( J( v5 E& Q
  55.   */
    ! k4 J5 H# ~1 s$ g! o- k
  56.   sConfig.Channel = ADC_CHANNEL_15;& |/ P6 g& v% z
  57.   sConfig.Rank = ADC_REGULAR_RANK_2;  o  ^4 |" \9 q  R
  58.   sConfig.SamplingTime = ADC_SAMPLINGTIME_COMMON_2;
    + l0 |0 i2 Q9 e9 t
  59.   if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)( ~9 a$ z+ A! s$ Q3 t; n
  60.   {
    5 j& j- V; R# X2 Y+ K  B% m6 u" z
  61.     Error_Handler();
    " R* Z, a; |# G: |) |5 U  {
  62.   }
      C( ~/ O' P2 U$ o

  63. 4 n# L6 ^5 {9 U2 |, V% q" Z
  64. }( E1 e: W1 {( _

  65. 4 x! C  K. S* U6 l( P; k4 f
  66. void HAL_ADC_MspInit(ADC_HandleTypeDef* adcHandle)" u( w; g% q. T9 z4 f( w
  67. {2 Y. ^' S' a4 P' V5 r8 F8 Q6 M* P
  68. ! C$ U! d8 F7 ^3 G1 Z9 \
  69.   GPIO_InitTypeDef GPIO_InitStruct = {0};% {. a) i1 v- L
  70.   if(adcHandle->Instance==ADC1)
    : [! Q4 n5 _0 L
  71.   {) C% r* f6 X4 n  ^- E2 V
  72.   /* USER CODE BEGIN ADC1_MspInit 0 */
    # r+ V+ I/ h$ ]+ O$ I" R& a& |. ?
  73. 8 O  Z6 b; Q" R  G) }7 r
  74.   /* USER CODE END ADC1_MspInit 0 */! m* b2 h* B7 B" G7 ]
  75.     /* ADC1 clock enable */; n. `9 q: ^; B1 Z, W) o% h
  76.     __HAL_RCC_ADC_CLK_ENABLE();% |2 {8 P4 C( ]  b3 w$ E
  77. 6 W1 M) U: J, t9 Y
  78.     __HAL_RCC_GPIOB_CLK_ENABLE();; X" v# \0 v! U; S: E) G
  79.     __HAL_RCC_GPIOA_CLK_ENABLE();
    , w- D3 p- Y2 e- d3 J- g# `
  80.     /**ADC1 GPIO Configuration   
    , }/ A6 O' W* s$ q7 i
  81.     PB0     ------> ADC1_IN8
    : q! b4 R" e4 D* n" t* z
  82.     PA11 [PA9]     ------> ADC1_IN15 5 M7 M, P9 }. j* A
  83.     */
    ( I0 e$ Z8 ^) J3 i! j. g% [! a9 [
  84.     GPIO_InitStruct.Pin = GPIO_PIN_0;
    ' ^8 p; h" U+ H, T
  85.     GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
    $ N3 u, [! Z2 G1 U
  86.     GPIO_InitStruct.Pull = GPIO_NOPULL;
    " R0 Y% D6 ?3 h2 Y  j. z9 g
  87.     HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);' J# S( ~! I" u7 U% K$ O3 B. x

  88.   X* V9 a* x# h7 q/ U  E, e6 z
  89.     GPIO_InitStruct.Pin = GPIO_PIN_11;
    . {" V' ^% R6 k6 ^# w
  90.     GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
    " m$ b0 B" h+ `* E4 f  R6 N. V
  91.     GPIO_InitStruct.Pull = GPIO_NOPULL;2 K# J/ y6 t2 }8 u1 h# y) M" d
  92.     HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);. z% I* G6 p) F- Z

  93. + n% G$ g$ J' z; w) @
  94.     /* ADC1 DMA Init */
    ( f: s/ Y: y# t% U1 j6 E! @- \
  95.     /* ADC1 Init */9 _- }1 C' j% ?* f0 |% |
  96.     hdma_adc1.Instance = DMA1_Channel1;0 y/ S+ H& G1 b
  97.     hdma_adc1.Init.Request = DMA_REQUEST_ADC1;1 L; s( v8 v- F$ x4 ~7 h" O
  98.     hdma_adc1.Init.Direction = DMA_PERIPH_TO_MEMORY;0 Q, @7 g5 e0 J& L, p; g
  99.     hdma_adc1.Init.PeriphInc = DMA_PINC_DISABLE;
    $ W( k9 g( }7 n5 q) l, O
  100.     hdma_adc1.Init.MemInc = DMA_MINC_ENABLE;
    ( X5 [0 E+ o0 i! \
  101.     hdma_adc1.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD;
    - G4 |9 m+ b7 B
  102.     hdma_adc1.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;
    / E7 g  W6 S0 G# p
  103.     hdma_adc1.Init.Mode = DMA_CIRCULAR;
    ! ]5 z5 o! Y0 T
  104.     hdma_adc1.Init.Priority = DMA_PRIORITY_HIGH;
    $ C+ O, ]; {. `# P
  105.     if (HAL_DMA_Init(&hdma_adc1) != HAL_OK)- p6 j0 S1 i$ O
  106.     {
    2 _7 c% M& c1 v; `! }6 w0 o
  107.       Error_Handler();  ^& ?$ Z- }* G# ]* Q% @- F9 o
  108.     }
    6 ~5 p! R1 T8 W" L4 ^) N- v
  109. / @7 [) V3 w! l3 f6 D4 p+ x
  110.     __HAL_LINKDMA(adcHandle,DMA_Handle,hdma_adc1);
    + o3 W, l) S8 h/ e9 k0 K" A5 x

  111. # p3 V2 T) A( f: ]
  112.   /* USER CODE BEGIN ADC1_MspInit 1 */2 g4 ~$ Z; M# b/ U& h; E# Z: c
  113. # L0 `: A8 t& G$ `
  114.   /* USER CODE END ADC1_MspInit 1 */
    8 [' v' p; U: v% q
  115.   }6 n7 u6 d  |1 x" {3 u3 Z% j/ n# q
  116. }
复制代码

& O& b! q3 ~( H分压公式:U1/U2=R1/R20 c) {/ S5 e* A/ t7 C7 j7 V% e
9 X6 W* V( R" ?: Q$ Z
  1. #define R0  220//分压电阻阻值
    . b; S2 v0 a! P" Z8 h' a
  2. #define R1  100//分压电阻阻值
    : [7 {, U7 e6 [7 n) U) l5 z
  3. #define ref_vol 2800//参考电压3 r  {! S5 r4 ]; H

  4. " ]/ E! a4 ~- u5 h$ r4 S3 H- {
  5. " N1 C; f, _. ]
  6. static uint16_t get_volt(uint16_t advalue)
    ' n) {0 Y* T, K* a; |! m9 L
  7. {: C, Q2 j8 A3 T
  8.     return advalue * ref_vol / 0xfff * (R0 + R1) / R0;9 u1 z( |0 d: B- `4 I3 t
  9. }
复制代码

" _( l; r! d/ m3 u( k
- h3 a" {. I" }/ c( C+ ~0 N, T3 ]! K( S
收藏 评论0 发布时间:2021-11-10 01:00

举报

0个回答

所属标签

相似分享

关于意法半导体
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
招聘信息
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
关注我们
st-img 微信公众号
st-img 手机版