说明:驱动基于STm32G031K6测试,其他型号需自行做改动。
+ F) s5 c8 ~5 W8 F& v
% q3 U) J- R( }! |. I! jADC的初始化: f7 _# i1 {, B% y# @( T7 U
2 d+ _7 G: l; M
- #define ADCIO1_IN_CHANNEL LL_ADC_CHANNEL_5" k- |8 T. z" [, u' q
- #define ADCIO2_IN_CHANNEL LL_ADC_CHANNEL_7
5 Y: `# I6 B8 H1 [1 I. [ - uint8_t STM32LLADC1Init(void)$ u7 D9 X$ ~5 k9 e
- {: e0 x- f* {$ h6 O/ Q: i
- LL_ADC_REG_InitTypeDef ADC_REG_InitStruct = {0};
7 Q! R# _8 ~$ _# |3 _4 N - LL_ADC_InitTypeDef ADC_InitStruct = {0};
: S0 z8 ?( L4 W; X+ h
- t+ D" g, I" u8 H: U: b- LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
% Z5 U" q0 h8 j/ n8 Q) w- d
0 J8 l# Z0 [: x- |# h- /* Peripheral clock enable */$ @$ ]) c ~6 q0 b0 N1 r& |4 M
- LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_ADC); //使能ADC时钟
4 ]+ g2 q# q5 U: l" c1 z# D - 7 n& n+ P9 e) ^
- LL_IOP_GRP1_EnableClock(LL_IOP_GRP1_PERIPH_GPIOA); //谁能响应ADCioGpio时钟
7 \( ^5 y. x8 q1 X+ s - /**ADC1 GPIO Configuration
' f( H9 F0 N! E6 G# k J: J - PA5 ------> ADC1_IN5+ E% l/ l' r' V+ l
- PA7 ------> ADC1_IN7
; |/ y0 m( W q2 e( A7 d - */
9 n7 r/ y/ M( A - GPIO_InitStruct.Pin = gpioADCIO1_IN_PIN; //ADC检测IO初始化, h+ e v/ X" \$ t& G
- GPIO_InitStruct.Mode = LL_GPIO_MODE_ANALOG;
9 j" N8 Z( D$ n0 `$ E( B - GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;, W) Y% e. d3 g
- LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
0 q3 n9 u8 A& ?# O# {4 k' S
4 p [. d" f" Z) u- GPIO_InitStruct.Pin = gpioADCIO2_IN_PIN;
; X4 Y; W- H [ f M4 B- S6 B - GPIO_InitStruct.Mode = LL_GPIO_MODE_ANALOG;$ l; k5 k$ U% e3 R; Z% W* U
- GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;+ B6 r* E o; H$ k! f
- LL_GPIO_Init(GPIOA, &GPIO_InitStruct);% N" u8 a C4 Z* G) x
- & i& w6 @7 g/ O9 ~
- . I ?, E6 }3 n0 Y. U2 u
- /** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion) $ j$ x! i) A: D6 u) I4 @
- */! i6 p6 y# F3 ^
- ADC_REG_InitStruct.TriggerSource = LL_ADC_REG_TRIG_SOFTWARE; //ADC定时转化触发条件来源,可以为软件触发或硬件(外部中断、定时器)触发。
1 ^* b$ l0 L0 S! g5 J3 G/ F# J9 J/ ?2 S - ADC_REG_InitStruct.SequencerLength = LL_ADC_REG_SEQ_SCAN_ENABLE_2RANKS; //ADC顺序采集通道的个数,根据自己需要的通道个数设置
" `' N/ U1 G$ ^ - ADC_REG_InitStruct.SequencerDiscont = LL_ADC_REG_SEQ_DISCONT_DISABLE; //
: r2 J1 } ?' W! A# P2 n5 N - ADC_REG_InitStruct.ContinuousMode = LL_ADC_REG_CONV_SINGLE; //采集方式,单次采集还是连续采集
; v1 j) l R# l# Q - ADC_REG_InitStruct.DMATransfer = LL_ADC_REG_DMA_TRANSFER_UNLIMITED; //使能DMA,并使用无限传输,如果DMA保存方式为循环覆盖的话才可以使用无限传输。
% D/ e. T& e" k5 `) n - ADC_REG_InitStruct.Overrun = LL_ADC_REG_OVR_DATA_OVERWRITTEN; //采集的数据循环覆盖模式 s% P# |! @# @0 i2 ^3 |$ x0 Q, M
- LL_ADC_REG_Init(ADC1, &ADC_REG_InitStruct);
; T5 m6 ^% R* K9 U6 |6 ?% t w - LL_ADC_SetOverSamplingScope(ADC1, LL_ADC_OVS_DISABLE); //失能过采样" k1 v# V" L2 S& T3 l
- LL_ADC_SetTriggerFrequencyMode(ADC1, LL_ADC_CLOCK_FREQ_MODE_HIGH); //采样时钟使用高频模式% w) @. u) _1 ]& ~6 ]# V* n
- LL_ADC_REG_SetSequencerConfigurable(ADC1, LL_ADC_REG_SEQ_CONFIGURABLE); //adc通道和硬件adc in是否强制对应,因为adc的io使用时不是连续的,所以这里使用可自定义配置" v a9 a+ p" b% X8 c5 C
- LL_ADC_SetSamplingTimeCommonChannels(ADC1, LL_ADC_SAMPLINGTIME_COMMON_1, LL_ADC_SAMPLINGTIME_160CYCLES_5); //设置通道共用取样时间,根据需要自行选择+ W6 V9 ~/ t% ?) [' J+ S
- LL_ADC_DisableIT_EOC(ADC1); //禁用通道采样结束中断1 M3 h7 }) V. \5 h! L' I
- LL_ADC_DisableIT_EOS(ADC1); //禁用序列采样结束中断,因为使用的是单次软件触发所以这里关闭这些中断
E2 g$ y) ]8 \. e' F. J% P - ADC_InitStruct.Clock = LL_ADC_CLOCK_SYNC_PCLK_DIV2; //选择采样时钟来源6 h" I# l N( n% C k; n1 V
- ADC_InitStruct.Resolution = LL_ADC_RESOLUTION_12B; //采样分辨率
( H6 c! b- y+ ?: c - ADC_InitStruct.DataAlignment = LL_ADC_DATA_ALIGN_RIGHT; //采样数据对齐方式,右对齐高位补0,左对齐低位补0.
2 i" \; @1 E0 ~ - ADC_InitStruct.LowPowerMode = LL_ADC_LP_MODE_NONE; //低功耗模式,使用DMA的话无法使用,这里关闭
0 ]4 S1 I. o; w8 D5 x `: \, { - LL_ADC_Init(ADC1, &ADC_InitStruct);
3 s- V' U; B( _) ]1 n( N: k - /** Configure Regular Channel - w: f8 }1 z8 N7 z. e
- */) f' w6 {' T4 {% g. z2 b
- //ADC_RegularChannelConfig* M: R- P, R- J4 N& g$ W
- LL_ADC_REG_SetSequencerRanks(ADC1, LL_ADC_REG_RANK_1, ADCIO1_IN_CHANNEL); //将硬件通道ADCIO1_IN_CHANNEL映射到ADC通道1' I. j: v3 u( [
- LL_ADC_SetChannelSamplingTime(ADC1, ADCIO1_IN_CHANNEL, LL_ADC_SAMPLINGTIME_COMMON_1); //设置通道采样时间, ]- Q# I1 K R( J: R
- LL_ADC_REG_SetSequencerRanks(ADC1, LL_ADC_REG_RANK_2, ADCIO2_IN_CHANNEL); //将硬件通道ADCIO1_IN_CHANNEL映射到ADC通道2
" m0 J) t: k$ J5 K. X- j - LL_ADC_SetChannelSamplingTime(ADC1, ADCIO2_IN_CHANNEL, LL_ADC_SAMPLINGTIME_COMMON_1); //设置通道采样时间8 M3 ~' ~9 o: M8 [. h( x
" a# Y8 X5 @ q- /*采样数据转换时需要参考电压,参考电压可能是变化的,所以也有可能需要采集*/% t% l: k+ r# Q5 M
- //LL_ADC_REG_SetSequencerRanks(ADC1, LL_ADC_REG_RANK_4, LL_ADC_CHANNEL_VREFINT);
3 o$ f9 i4 v4 n4 {: h. O! y, L - //LL_ADC_SetChannelSamplingTime(ADC1, LL_ADC_CHANNEL_VREFINT, LL_ADC_SAMPLINGTIME_COMMON_1);) |" T! y5 I1 B: u: ~
- return 1;4 O* x- e2 \0 Z; L4 F
- }
复制代码 E S) a1 ~; Y7 L; g; g' u: u% n
激活并校准ADC:* y5 b% t* }5 ?/ f' v( u
0 d7 Y% V6 y( w- uint8_t STM32ActivateADC1(void)* a: W, C1 i1 \" q7 h, B; X$ A7 e l& D
- {% U3 x. Z" f5 D6 R
- __IO uint32_t wait_loop_index = 0U;
% d8 q5 \' B& A$ @8 q - __IO uint32_t backup_setting_adc_dma_transfer = 0U; f) c2 p9 ?5 D& _! C- T
- uint32_t Timeout = 0U; /* Variable used for timeout management */
1 C: V3 r0 i. }- _' |* f F - /*## Operation on ADC hierarchical scope: ADC instance #####################*/& R4 C3 v, } u K1 G+ c
" S2 ?5 w1 i8 p. z# U8 x# F& G- /* Note: Hardware constraint (refer to description of the functions */% M, | a, C" K2 j# w% k8 ~, e
- /* below): */. E3 E* V" ^! c. d
- /* On this STM32 serie, setting of these features is conditioned to */! V2 n4 l$ ` v# j2 S
- /* ADC state: */! a% M7 Q0 Y9 s
- /* ADC must be disabled. */
1 `. A2 y1 i7 D8 E" M- ^- `$ ]% X - /* Note: In this example, all these checks are not necessary but are */( k7 k" h0 b& ^- q$ s$ _& b7 K# q# z
- /* implemented anyway to show the best practice usages */
& j o( H6 y- ? - /* corresponding to reference manual procedure. */
( h; O/ z# C- _1 f% ]) \ - /* Software can be optimized by removing some of these checks, if */- N6 i- `; k1 p) a. [) U
- /* they are not relevant considering previous settings and actions */
' D7 {: h0 K- c9 M1 ^# h4 N - /* in user application. */
3 W# X/ ~5 P1 H1 r# Y! d! f- f; } - if (LL_ADC_IsEnabled(ADC1) == 0)
: G- x( A% h. m0 z - {7 Y( r/ Z$ \5 s S6 a6 U2 \: m
- /* Enable ADC internal voltage regulator */
: Q$ W+ G) J8 | - LL_ADC_EnableInternalRegulator(ADC1);0 h, V0 e c0 ^; U
; A& k, B! G% s; |; L7 V* A- /* Delay for ADC internal voltage regulator stabilization. */! R% x5 O3 J: S. M" i1 F8 u
- /* Compute number of CPU cycles to wait for, from delay in us. */5 j, {+ h3 i, K+ U# U7 n- M
- /* Note: Variable divided by 2 to compensate partially */ n4 Q! j- \0 i1 I2 N g
- /* CPU processing cycles (depends on compilation optimization). */, \$ z# B6 \- S
- /* Note: If system core clock frequency is below 200kHz, wait time */
" `% @+ O4 h8 R! W# c - /* is only a few CPU processing cycles. */
+ e4 e& W% g: W# F- D - wait_loop_index = ((LL_ADC_DELAY_INTERNAL_REGUL_STAB_US * (SystemCoreClock / (100000 * 2))) / 10);
- g2 Q; i! @3 z& H - while(wait_loop_index != 0)
: w' | i1 A% I8 D - {
" v, H- `5 S7 V% m+ D) m - wait_loop_index--;- I' L a1 k8 _& Q, @# W0 g( I7 o
- }
6 q8 H5 r0 a" H7 p2 X1 x8 r; @ - ) |- `# l( \/ i6 I
- /* Disable ADC DMA transfer request during calibration */$ M$ ?5 Z9 D) I1 X7 }3 @+ v
- /* Note: Specificity of this STM32 serie: Calibration factor is */; h: w* w' Y3 ^& P; G6 E
- /* available in data register and also transfered by DMA. */
0 O6 @4 ?8 y) s# m8 g' \, x1 b) [ - /* To not insert ADC calibration factor among ADC conversion data */
; M7 f' e& E w/ u$ A - /* in DMA destination address, DMA transfer must be disabled during */0 _! z9 j# |- v2 R0 a3 y2 l
- /* calibration. */
6 k+ c! P; \" Z* _3 | - backup_setting_adc_dma_transfer = LL_ADC_REG_GetDMATransfer(ADC1);4 ]7 H# W# {" i% x+ N
- LL_ADC_REG_SetDMATransfer(ADC1, LL_ADC_REG_DMA_TRANSFER_NONE);" m. F. i7 a$ p; f% [$ B
6 f N; o) w0 U% {. _2 _/ E- /* Run ADC self calibration */
# _8 ]) r4 P3 ]1 T+ p - LL_ADC_StartCalibration(ADC1);& c; }( N y. S- p7 K+ s( E
1 h* t, f4 A5 Y" q l g* r- /* Poll for ADC effectively calibrated */
0 h) ~4 I- h s - Timeout = ADC_CALIBRATION_TIMEOUT_MS;
3 n4 Q9 Z5 v. G, {* M: M4 k6 T7 t) { - 6 e& m; q% z/ w% Y) C
- while (LL_ADC_IsCalibrationOnGoing(ADC1) != 0)1 [( B& J5 ?, @
- {
1 c# k6 p" {* F* u* Y - /* Check Systick counter flag to decrement the time-out value */
: R0 H& I+ Z/ L/ t9 g2 N% ^7 Q - if (LL_SYSTICK_IsActiveCounterFlag())
: ]! u: Z# C( u8 G. M1 A/ l: e' z' t - {
; G% v1 O" O; H! K" W - if(Timeout-- == 0)+ @/ X+ F7 c" n) M: D( T% r
- {3 d, W. M) I8 ^ ^- _' _' \% o
- return 0;
. f1 l) D* a/ [) N - }
3 W& a3 ^# J* \0 C7 w R - }- R6 f: J2 L G" O0 R# C. _
- }
( v8 L! C4 Z, M+ ~, x
) ?( v z2 j1 M9 s0 q7 n" e- /* Restore ADC DMA transfer request after calibration */
6 R& I& h u. V# ` F - LL_ADC_REG_SetDMATransfer(ADC1, backup_setting_adc_dma_transfer);( @6 ^6 u/ r' y X9 h1 f, [
! G" `8 X; [; H) L" W: Y- /* Delay between ADC end of calibration and ADC enable. */. w. M- U5 |& l ^5 w3 f# K
- /* Note: Variable divided by 2 to compensate partially */
+ p" S1 A4 X6 v7 v. P - /* CPU processing cycles (depends on compilation optimization). */
( h! u7 F8 C$ U3 |. q - wait_loop_index = (ADC_DELAY_CALIB_ENABLE_CPU_CYCLES >> 1);8 d8 c0 n' a; t: J* R& {2 }2 ~
- while(wait_loop_index != 0)% F8 r* A P9 _3 a9 e0 w
- {
' t" x/ \2 z4 f3 m$ k3 q7 L - wait_loop_index--; k3 h% n) y; V( J" R7 D( w
- }
9 g5 a1 T4 q1 i+ p% S" _; c- \( L - % l; H( v' f9 N. @
- /* Enable ADC */
) }& }# F; l8 l; @$ B - LL_ADC_Enable(ADC1);& H9 z e' e3 l2 O" A
6 w' K; y% U% C5 e4 I4 c+ k- /* Poll for ADC ready to convert */( N5 L& [0 J0 q8 h" L' [
- Timeout = ADC_ENABLE_TIMEOUT_MS;. Y. X; ]8 F8 R4 ], E$ X8 R/ `
- A# X% i* c9 t/ o, p+ Z* |- while (LL_ADC_IsActiveFlag_ADRDY(ADC1) == 0)0 I4 F. m3 V6 p# ^) |
- {; q/ I9 h6 N, K; O0 Z9 \# d. t
- /* Check Systick counter flag to decrement the time-out value */+ r* |" m% r& c3 ~3 g% I: r+ [! x% L
- if (LL_SYSTICK_IsActiveCounterFlag())2 d" o, g7 [0 D5 P
- {
0 C9 {% g) @" @' C - if(Timeout-- == 0)! c2 U4 [' E# ]/ m$ W
- {
$ k! u- J; S, s% w$ ?- H - return 0;& ?& X0 N+ t8 I z1 E: d, o6 _* e
- }
5 b! X3 Z! |5 K - }
. K) U2 k8 I. u9 P @0 c* e - }% w. h r0 m* r9 ^. @
- /* Note: ADC flag ADRDY is not cleared here to be able to check ADC */
1 A' w8 e: y. G" X$ F1 |7 ]* Q3 L - /* status afterwards. */& ^4 w$ b* h' ]9 T8 C& ~9 C
- /* This flag should be cleared at ADC Deactivation, before a new */
0 k* S/ H4 A8 }7 ] - /* ADC activation, using function "LL_ADC_ClearFlag_ADRDY()". */
2 @/ w2 h, j* k4 Z, y6 ] - }
3 I1 h' W6 [9 b4 ` - return 1;
( t' a: b" R3 @* H# ~/ k$ M. \ - }
复制代码 3 B: T* I( F( t$ M8 L0 R; U
因为ADC配置的是软件触发的单次采样,所以还需要有手动开始和结束采样的函数。3 [ U- n, b# b8 U' C9 p$ P, Q4 }
4 B' _; N; x" E7 U3 U5 H
使能ADC采样:
( J9 p1 O+ b( g( }8 N
2 } {/ U8 T( l6 I4 h- uint8_t STM32ADC1SampleEnable(void)1 I. N) ~. w1 [
- {
8 x$ S1 b) b5 d" w/ w - uint32_t Timeout = ADC_ENABLE_TIMEOUT_MS; - E% E1 V" N4 k
- LL_DMA_EnableChannel(DMA1,LL_DMA_CHANNEL_4); //先使能dma再使能adc,以防采样数据丢失
( [3 B7 H) s: g" h0 [9 g$ ^& M; { - LL_ADC_Enable(ADC1);6 T% ^, L+ d- ]# t) X
- Timeout = ADC_ENABLE_TIMEOUT_MS;
( }9 U# c2 L1 d. q/ c/ e3 q - while(LL_ADC_IsActiveFlag_ADRDY(ADC1) != SET)
/ \0 p( z; m4 b, g# o0 _8 R6 x - {: \$ M7 O4 ?0 V7 h
- if(Timeout-- == 0)
4 q5 g! e- G0 U - {
4 O) K1 j! B6 M9 e/ y - LOG(1,"STM32ADC1SampleEnable ERROR!!!");
" Z) F! b; Y3 t* F- [5 e) | - return 0;) ~$ v5 M A7 C8 B- @/ V
- }
" ^$ y; Y4 u C6 A0 ` s P - }
% l' v ~: o: V; W, M! v - LL_ADC_REG_StartConversion(ADC1);
! E* n0 U! _" U, S) U* l$ E- I* A2 z - return 1;7 D0 \4 i/ _8 n* A
- }
复制代码
( m/ z2 a5 m) Q( @, q; Y失能ADC采样:
/ A( h: {8 L' K, K2 v5 O. h& Q N! p! Z6 H8 b8 i) W! k
- uint8_t STM32ADC1SampleDisable(void)2 T( j) e9 V$ c1 t* C
- {
- u# A. ^5 r2 a$ X5 M8 z - uint32_t Timeout = ADC_DISABLE_TIMEOUT_MS; & r G! n' Z6 @0 L9 U
- LL_ADC_REG_StopConversion(ADC1);
8 X% _) y5 `/ L% V2 L& l5 l; S - while(LL_ADC_REG_IsConversionOngoing(ADC1) != 0). J9 L! [% P+ k7 m) Z# O) h
- {
c! C. W d) V8 f6 f% _4 v - if(Timeout-- == 0)9 N# H y4 K% U# `7 L; r" C; Z
- {) x% b' z% h3 o) C0 Z
- LOG(1,"STM32ADC1SampleDisenable ERROR!!!");
2 i* j n+ H4 g& k ? - return 0;7 r" S: o: y7 N2 H
- }
. ]( l Y1 c, R - }
8 b9 @- {# M" n9 _4 {, r$ y - LL_ADC_Disable(ADC1); //先关闭adc再断开dma,以防采样数据丢失# u! z% ~( x5 ]% C, y3 G# d
- LL_DMA_DisableChannel(DMA1,LL_DMA_CHANNEL_4);7 u* F: x, S% u8 {; p8 l$ g2 i
- return 1;
: b7 I6 V" i: y, U6 n# J - }
复制代码
% }. B5 Y5 f( P# `进行ADC数据采集流程(采用多次采样求平均值的方式):
# N2 d/ b1 l8 q8 g5 W0 A6 }# y& J" L- N8 Z, v, s
1、当需要开始采样前,调用一次STM32ADC1SampleEnable()函数;
6 d4 \ z6 G4 r: W8 r, F
7 J6 r2 x9 X) |( h) b5 G! l2、在一个定时函数内调用LL_ADC_REG_StartConversion(ADC1)进行数据的转换(因为使用的是单次采样,所以每次进入定时函数时都需要调用),取出DMA数据进行累加;
5 B. e4 N# _$ c2 ?' A* J
7 ?; l! e2 k! x0 |- G3、达到需要的采样次数之后,对得到的数据进行运算,并调用STM32ADC1SampleDisable()函数关闭ADC通道。# N! o+ x6 A! S
3 |2 `2 H% G9 @0 _以上方式适用于需要间隔一段时间获取一次ADC数据时使用,例如需要每隔1分钟获取电池电压时,可以进行1分钟倒计时,然后连续进行十次间隔10ms的ADC采样求平均值。) b4 g- w3 x- r$ W
' }% L. b7 W' V( @8 B v1 L! j. g; o
|