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

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

[复制链接]
STMCU小助手 发布时间:2021-11-10 01:00
硬件原理图
6 x% u; x/ m: J, i# q
( h; S2 r7 M; z6 p
20201216095853276.png

, m- O' H- O! c3 _/ U* Q7 K- `1 c4 f! @
程序:# b) u* d5 ~, ~; m

* D8 P# N, f, z; x2 `
  1. int main(void)4 j! \) [8 S- A5 ?4 L
  2. {
    + |: c# y& ^$ C. O2 A% O
  3.     //Flash_EnableReadProtection();//RDP设置 使能读保护# v5 u" R9 m5 S" a
  4.     HAL_Init();/ R2 j# W/ l% Q7 G  e( `
  5.     SystemClock_Config();
    ; |3 a* ]0 o" p+ t: J5 [) j
  6.     MX_GPIO_Init();: A3 C' u% t* A2 K. }! d) F9 z8 b+ I
  7.     MX_DMA_Init();
    & `. V4 l: B: M; N8 k9 S
  8.     MX_I2C1_Init();
    1 E; z- l  {$ e5 ^+ Y- a
  9.     MX_ADC1_Init();
    2 h! P6 z3 e: i( e* u4 o
  10.     MX_TIM2_Init();
    ; p5 M8 }4 h+ j8 T+ l5 W0 r  F( K, V$ M
  11.     MX_NVIC_Init();
    9 h7 J# L$ j! G% D" Z  h; d& ?  `
  12.     HAL_TIM_Base_Start_IT(&htim2);3 p' d( d$ z) b
  13.     HAL_ADC_Start_DMA(&hadc1,(uint32_t *)&adcbuf,2);& i# Z2 Y5 V% ]8 m1 T. M
  14.     while(1)/ {8 T, ]' x+ O7 b- P0 V& a* M& `
  15.     {
    # X" D, c- e- H4 F  ^& s

  16. . T9 U# u& h" r, W& X9 |7 }4 L1 N
  17. ! j+ T6 M7 w1 r
  18.     }
    . W" I+ E' h& A& s$ I& q
  19. }
    8 z* T$ Y* `$ B) q
复制代码
6 S  h; c% C% J/ Z. B! Y& h3 d4 R

8 z* H4 |' H' x/ i0 F  K2 j1 P
  1. void MX_DMA_Init(void) / V5 H+ W- e& `  l+ ?+ ^! U4 o
  2. {; d. @2 N& O6 F# A- Z+ }

  3. - w" |& o, L! C/ e
  4.   /* DMA controller clock enable */) B: m2 y9 q& Z( C" p, Y- L+ H6 A
  5.   __HAL_RCC_DMA1_CLK_ENABLE();
    " d: G: S* M% A3 D" z
  6. , ~/ q2 P3 h1 O9 n: n' w
  7.   /* DMA interrupt init */0 D% ^$ n7 o/ Z$ y
  8.   /* DMA1_Channel1_IRQn interrupt configuration */
    4 u* e4 V  T. W
  9.   HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 1, 0);
    ' Z1 E7 _2 s# i/ G; o
  10.   HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn);. N( U3 h0 y1 A! v/ Q
  11.   d& ~' v" }/ d$ Z4 F+ M
  12. }
      F2 v3 X+ C% e, |
复制代码
7 O" n$ D% Z0 n" j9 R/ k
3 _9 N! P0 Y% g, b) n
  1. uint16_t adcbuf[2];//0,电池电压, 1,温度ADC
    ; u3 t* O0 |( |3 X

  2. / q: b0 I5 Z# R
  3. ADC_HandleTypeDef hadc1;
    " A4 P) r2 _' O- i4 e# }
  4. DMA_HandleTypeDef hdma_adc1;
    . a7 z0 m# a2 P0 t( e: o' w

  5. 9 M- D( h8 H! F# m8 x4 \
  6. void MX_ADC1_Init(void)& M7 V- B8 Y: q5 g. V5 }0 u, T/ \4 B
  7. {
    , K! J& x8 y" S! {, n9 u2 t0 c
  8.   ADC_AnalogWDGConfTypeDef AnalogWDGConfig = {0};+ a5 q6 J# P9 N& \4 i3 d5 t
  9.   ADC_ChannelConfTypeDef sConfig = {0};5 y7 X$ I* z% v7 @

  10.   x1 W9 V& _! y% c
  11.   /** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
    & ?) o! k: U+ ]- Q0 E0 [
  12.   */
    * C# ~# r5 P2 V# c! l( r
  13.   hadc1.Instance = ADC1;
    " {* o, }" Z7 ]% W" k) f+ r& A- y  M
  14.   hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;9 ~0 e  ~' d; U/ c  V
  15.   hadc1.Init.Resolution = ADC_RESOLUTION_12B;
    + [. s/ @* Z  w( L+ }' ^
  16.   hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
    % `% _0 m% t  Q
  17.   //hadc1.Init.ScanConvMode = ADC_SCAN_ENABLE;//STM32CubeMX生成,只能使用通道 0-14,不能使用通道 15,16,17,18;3 r3 q( v- w8 t, ~2 E1 s  x0 D
  18.   hadc1.Init.ScanConvMode = ADC_SCAN_SEQ_FIXED;  L, g) V  A3 V( d; l8 n* T
  19.   hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;2 G1 _  d4 `' l" A
  20.   hadc1.Init.LowPowerAutoWait = DISABLE;$ z) A. i6 c! {. Z) B6 ^
  21.   hadc1.Init.LowPowerAutoPowerOff = DISABLE;
    ! p# `8 b3 f. e. @1 T  [) U
  22.   hadc1.Init.ContinuousConvMode = ENABLE;. h& c+ C  ~2 t* W8 n2 S# ~
  23.   hadc1.Init.NbrOfConversion = 2;. g8 r+ p: b  r# t
  24.   //hadc1.Init.DiscontinuousConvMode = DISABLE;//STM32CubeMX生成,只能使用通道 0-14,不能使用通道 15,16,17,18;
    4 D4 ^; W3 C: K% `4 Z/ l9 x
  25.   hadc1.Init.DiscontinuousConvMode = ENABLE;
    1 V3 P- h( v& y! E) Z9 F* J0 N5 [0 m
  26.   hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;5 k2 `# P/ D# A
  27.   hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;' u$ A) P2 r" [1 I) R, ]9 E, }
  28.   hadc1.Init.DMAContinuousRequests = ENABLE;
    7 r& ^1 Z! c' }: J% B! M
  29.   hadc1.Init.Overrun = ADC_OVR_DATA_PRESERVED;
    7 R# Z4 f* |* [3 x/ Z& H; Z: ]: B
  30.   hadc1.Init.SamplingTimeCommon1 = ADC_SAMPLETIME_160CYCLES_5;
    ! K* m# B  |7 L. ?
  31.   hadc1.Init.SamplingTimeCommon2 = ADC_SAMPLETIME_160CYCLES_5;
    / L. I  w) k5 X
  32.   hadc1.Init.OversamplingMode = DISABLE;. @8 b& X! s" o" M' |, `
  33.   hadc1.Init.TriggerFrequencyMode = ADC_TRIGGER_FREQ_HIGH;
    ! P, C/ @' M" y1 u3 E( D5 b
  34.   if (HAL_ADC_Init(&hadc1) != HAL_OK)
    0 R+ B; S( E3 q
  35.   {5 j, P$ @! Y1 E1 ~. D) s
  36.     Error_Handler();
    8 n3 s/ X; y& N+ ?' ]6 H
  37.   }
    8 b6 f, J. i7 ]. _) }
  38.   /** Configure Analog WatchDog 2 - d) \/ U3 O3 ~" U+ p7 o
  39.   */1 L' L* N8 f9 ]6 G' V9 b
  40.   AnalogWDGConfig.WatchdogMode = ADC_ANALOGWATCHDOG_SINGLE_REG;/ E* v: r5 q" u$ e: c
  41.   if (HAL_ADC_AnalogWDGConfig(&hadc1, &AnalogWDGConfig) != HAL_OK)
    ! D% s) O4 u$ b6 z$ `1 z9 Q
  42.   {2 _# |  ^2 |9 E  p  {9 V" B( U
  43.     Error_Handler();
    " j2 e9 r# C1 x9 Z  D" O$ D& E0 @
  44.   }4 b# [, a: ~6 i$ ^+ D# x1 M
  45.   /** Configure Regular Channel $ q9 P7 r6 @/ F
  46.   */
    ) H9 n: C9 ]& M
  47.   sConfig.Channel = ADC_CHANNEL_8;& q" O" T' w% s% U! }! Y
  48.   sConfig.Rank = ADC_REGULAR_RANK_1;
    7 Q$ `" H; u, y2 y1 K' r& I/ ^
  49.   sConfig.SamplingTime = ADC_SAMPLINGTIME_COMMON_1;
    5 v  X: ?& j5 i2 a
  50.   if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK). f- i- N2 n( l/ {
  51.   {
    . r+ M+ W! `' q- n
  52.     Error_Handler();0 Z$ _1 N' o6 p" v; @
  53.   }
      X. f% k9 R5 E: r: Z; u4 O
  54.   /** Configure Regular Channel $ E5 D" P  h4 p1 V
  55.   */
    1 C% _! \: ^$ S
  56.   sConfig.Channel = ADC_CHANNEL_15;5 c) T, _$ a* [! b, S$ v
  57.   sConfig.Rank = ADC_REGULAR_RANK_2;
    4 t' i. K* K. a: P$ O& j% Z2 A3 F
  58.   sConfig.SamplingTime = ADC_SAMPLINGTIME_COMMON_2;" |" J: n% a2 i: ?6 _
  59.   if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
    # u! {3 h, V7 Z% ?, @' B2 y
  60.   {2 b4 F1 a. g) `  s% w* X4 S
  61.     Error_Handler();3 @3 h- L" n" [) n
  62.   }
    , e) L8 b' S/ ?$ {8 v  {1 s

  63. ( X/ e3 }$ }* a/ T4 z- V
  64. }7 X. {( m- y% Z7 k; ~

  65. 6 K7 ]( T' N2 J" [* Y
  66. void HAL_ADC_MspInit(ADC_HandleTypeDef* adcHandle)* f7 N0 e3 U. N7 U; p% e1 L* p
  67. {
    ' W9 e3 ?" i! Y

  68. - v$ S, I  A: N" W& V
  69.   GPIO_InitTypeDef GPIO_InitStruct = {0};$ Z- X$ ~( v" A2 @# Y9 F7 V
  70.   if(adcHandle->Instance==ADC1)7 z" U) D' M# ~5 s% k/ o: m
  71.   {1 K, I9 ~: X! a! O# F
  72.   /* USER CODE BEGIN ADC1_MspInit 0 */5 j6 h" T7 h* i

  73. $ b& q8 M- o0 V! k7 v
  74.   /* USER CODE END ADC1_MspInit 0 */
      r, e7 V& t" K+ S8 M  M
  75.     /* ADC1 clock enable */) g- _( X* G2 f2 I+ o' q
  76.     __HAL_RCC_ADC_CLK_ENABLE();$ v! S, b" ]0 O

  77. # c$ ~# O+ I, D+ d* s
  78.     __HAL_RCC_GPIOB_CLK_ENABLE();
    ! n& z& S! J7 Q
  79.     __HAL_RCC_GPIOA_CLK_ENABLE();& e3 j1 L1 F) ^
  80.     /**ADC1 GPIO Configuration    " i: O4 U; E- \
  81.     PB0     ------> ADC1_IN8" ]& k8 m; U$ r$ U5 _
  82.     PA11 [PA9]     ------> ADC1_IN15 7 D& i! `5 U8 D/ [$ R$ s2 s
  83.     */
    ) u5 l; X5 ?+ K% m
  84.     GPIO_InitStruct.Pin = GPIO_PIN_0;+ I3 j* d, w  J1 D$ B7 m
  85.     GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;' H# D+ E" \6 ^# Y3 Q
  86.     GPIO_InitStruct.Pull = GPIO_NOPULL;6 P! [8 B) V! }; i0 o
  87.     HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);0 v  R) F, u5 Z
  88. # E5 e: H' _) ~! t' P! j2 n
  89.     GPIO_InitStruct.Pin = GPIO_PIN_11;' A$ u( `6 n3 y8 O4 R0 _$ M8 R9 e* C
  90.     GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;8 V* v: t3 v! v; k3 S# V6 T6 P
  91.     GPIO_InitStruct.Pull = GPIO_NOPULL;3 Z7 V0 i: }! \3 U% X# y+ J# ^
  92.     HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
    $ T  w" F! C7 t. u7 L! R& q9 `  H% z
  93. 5 Z9 @4 |! V8 M( k4 g
  94.     /* ADC1 DMA Init */, y& g1 \2 s2 N" [0 N: u( ]& ]
  95.     /* ADC1 Init */- U* |. [) k/ I: A1 d7 }( H) }
  96.     hdma_adc1.Instance = DMA1_Channel1;# x3 B' i3 X2 k5 ]; E
  97.     hdma_adc1.Init.Request = DMA_REQUEST_ADC1;
    4 q2 M0 S+ `2 P) c& B- r4 n5 W; J0 N
  98.     hdma_adc1.Init.Direction = DMA_PERIPH_TO_MEMORY;$ S" J+ N1 ~1 q, V6 I3 i
  99.     hdma_adc1.Init.PeriphInc = DMA_PINC_DISABLE;
    ; w+ q% n3 c) s. I! R
  100.     hdma_adc1.Init.MemInc = DMA_MINC_ENABLE;1 C1 }" Z. }" }+ S) _' K4 M" `
  101.     hdma_adc1.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD;6 C& u6 u1 I" ^' r  k9 `& K
  102.     hdma_adc1.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;4 o$ b+ x7 }2 u7 \, B( R7 m) s
  103.     hdma_adc1.Init.Mode = DMA_CIRCULAR;
    4 k% B4 {, ^- t% G, T0 B, {( X
  104.     hdma_adc1.Init.Priority = DMA_PRIORITY_HIGH;
    9 w& E6 E2 T- G- {# v
  105.     if (HAL_DMA_Init(&hdma_adc1) != HAL_OK)
    ; D+ ~- K/ \# i& j. ~7 p  a
  106.     {: R* ^  g5 C' `) c- z& u  x0 @
  107.       Error_Handler();5 v. [5 F+ a; @
  108.     }& S: c  J+ t2 M2 K- R* G

  109. % |$ z4 W* D4 m5 k+ M5 Q
  110.     __HAL_LINKDMA(adcHandle,DMA_Handle,hdma_adc1);2 ~$ }, c9 L' }3 d8 F
  111. % W# ^' D; c: K) F: n, ~8 a
  112.   /* USER CODE BEGIN ADC1_MspInit 1 */! V2 K/ F9 T; d7 |/ ^) Z
  113. . s2 n' J$ U2 ]: |' c
  114.   /* USER CODE END ADC1_MspInit 1 */
    7 v. l5 T2 N7 h6 Z$ B% I* I
  115.   }
    - O1 `' ^  i9 O& O9 h/ c
  116. }
复制代码
/ o+ i& d0 r* S/ l3 W0 `8 u# b
分压公式:U1/U2=R1/R2
! a$ g3 W$ b7 @9 X5 o' P
2 g7 K9 i: P3 m1 s( ]& [5 C+ U
  1. #define R0  220//分压电阻阻值
    : k0 ?4 [/ c8 s: T( E5 R
  2. #define R1  100//分压电阻阻值4 @2 o8 x! J4 f; z$ Z. `+ r
  3. #define ref_vol 2800//参考电压. r; Y$ o+ P7 z
  4. 0 [1 K9 ?, y; {5 D
  5. 7 ^: F) |8 G/ F  d* N
  6. static uint16_t get_volt(uint16_t advalue)" R3 C/ }! Q/ w+ o! J8 L1 w4 z, R
  7. {
      S; ?% X: V! h6 Q8 K9 b- N7 u
  8.     return advalue * ref_vol / 0xfff * (R0 + R1) / R0;
    - {$ `' X# m4 G  W4 d$ R
  9. }
复制代码
# Q& ^. c' Q: r: E! ]
0 O2 U5 M1 E1 i

8 n. ^) _% _: [6 ~$ z! \9 l; u
收藏 评论0 发布时间:2021-11-10 01:00

举报

0个回答

所属标签

相似分享

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