说明:驱动基于STm32G031K6测试,其他型号需自行做改动。& k- b; t! s9 Y
1 M" ^/ q0 y; P# j
ADC的初始化:
3 A" e/ m6 b4 |, X0 j# U, A
/ g8 L# k% U) b- #define ADCIO1_IN_CHANNEL LL_ADC_CHANNEL_5) a" _ U$ A6 L( F L) y
- #define ADCIO2_IN_CHANNEL LL_ADC_CHANNEL_72 X: W, \* c- t* d
- uint8_t STM32LLADC1Init(void)# i r* B& O8 }. l! T) Z1 m
- {
" P( `+ q; A( y+ h, | - LL_ADC_REG_InitTypeDef ADC_REG_InitStruct = {0};
1 H, j- O1 K* ]8 d - LL_ADC_InitTypeDef ADC_InitStruct = {0};
! {7 h W6 T- h0 j1 N! \. R7 M - 9 u; s/ Z4 X3 t' X: y' X7 y1 @
- LL_GPIO_InitTypeDef GPIO_InitStruct = {0};) E! b% }! t5 A1 `6 e- Z5 i4 x
8 z4 y ?/ S7 P4 P0 ]" @- /* Peripheral clock enable */. }% G) y2 |$ J$ P4 ^
- LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_ADC); //使能ADC时钟& ]% p" i# _0 [! k# V% o3 x
- 8 n: `! F4 G5 p7 K
- LL_IOP_GRP1_EnableClock(LL_IOP_GRP1_PERIPH_GPIOA); //谁能响应ADCioGpio时钟3 M s5 d5 Y# y
- /**ADC1 GPIO Configuration 0 b4 l2 [0 v0 U b' k2 a
- PA5 ------> ADC1_IN5
0 A0 V+ i! a6 P3 T1 G7 R1 M. u - PA7 ------> ADC1_IN7 5 H8 K1 g0 ?" c8 t6 G* _
- */3 L# T* M6 T6 F
- GPIO_InitStruct.Pin = gpioADCIO1_IN_PIN; //ADC检测IO初始化
* }) ]6 F: \; U9 r - GPIO_InitStruct.Mode = LL_GPIO_MODE_ANALOG;
6 _- W g% r7 r2 M) R - GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
2 D3 J2 r6 p9 o& k1 @' A$ F - LL_GPIO_Init(GPIOA, &GPIO_InitStruct);4 B0 Z8 ~/ X7 w7 H! @8 H
9 x' |. {* x0 @8 {) b5 h1 o5 y- GPIO_InitStruct.Pin = gpioADCIO2_IN_PIN;4 S* k0 g1 i( H# e/ r2 ?: h/ ?9 b8 N Y
- GPIO_InitStruct.Mode = LL_GPIO_MODE_ANALOG;
( t# k: w! n7 T - GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;+ {4 |% @+ r- ^9 K' p1 p
- LL_GPIO_Init(GPIOA, &GPIO_InitStruct);/ ^* k; d+ `- l% X- f
. I: @+ @, d6 u# T0 O0 o
# R! {+ Q% k& c' O/ S: n- /** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
0 T% M5 |% u1 `* r6 s# A - */4 \0 ^/ t( B( ?( I N' {+ i
- ADC_REG_InitStruct.TriggerSource = LL_ADC_REG_TRIG_SOFTWARE; //ADC定时转化触发条件来源,可以为软件触发或硬件(外部中断、定时器)触发。
3 o9 o, U1 V$ _: k" `$ ^1 B - ADC_REG_InitStruct.SequencerLength = LL_ADC_REG_SEQ_SCAN_ENABLE_2RANKS; //ADC顺序采集通道的个数,根据自己需要的通道个数设置( X; n$ a& n/ T9 y* Y' {" u. d+ ]
- ADC_REG_InitStruct.SequencerDiscont = LL_ADC_REG_SEQ_DISCONT_DISABLE; //
0 R, a- b$ V, ^, u0 x - ADC_REG_InitStruct.ContinuousMode = LL_ADC_REG_CONV_SINGLE; //采集方式,单次采集还是连续采集
! u4 u. x: C/ v M - ADC_REG_InitStruct.DMATransfer = LL_ADC_REG_DMA_TRANSFER_UNLIMITED; //使能DMA,并使用无限传输,如果DMA保存方式为循环覆盖的话才可以使用无限传输。
) ~8 M8 r' |# t' W; ] - ADC_REG_InitStruct.Overrun = LL_ADC_REG_OVR_DATA_OVERWRITTEN; //采集的数据循环覆盖模式
2 R; m* O! i! t, D) z9 F - LL_ADC_REG_Init(ADC1, &ADC_REG_InitStruct);
# c" ^* E& C1 |! T# p5 x6 A" @ - LL_ADC_SetOverSamplingScope(ADC1, LL_ADC_OVS_DISABLE); //失能过采样) Z- ]- ^7 F, k1 E% n, Q# \
- LL_ADC_SetTriggerFrequencyMode(ADC1, LL_ADC_CLOCK_FREQ_MODE_HIGH); //采样时钟使用高频模式6 N: V, r0 A9 f
- LL_ADC_REG_SetSequencerConfigurable(ADC1, LL_ADC_REG_SEQ_CONFIGURABLE); //adc通道和硬件adc in是否强制对应,因为adc的io使用时不是连续的,所以这里使用可自定义配置
. F- d8 ?, J' |1 V7 G; Q x - LL_ADC_SetSamplingTimeCommonChannels(ADC1, LL_ADC_SAMPLINGTIME_COMMON_1, LL_ADC_SAMPLINGTIME_160CYCLES_5); //设置通道共用取样时间,根据需要自行选择
4 C" c9 R" R' I2 L: b3 X. P - LL_ADC_DisableIT_EOC(ADC1); //禁用通道采样结束中断, E& T/ ]2 j: O3 |# I
- LL_ADC_DisableIT_EOS(ADC1); //禁用序列采样结束中断,因为使用的是单次软件触发所以这里关闭这些中断
3 }7 E% E+ h4 t - ADC_InitStruct.Clock = LL_ADC_CLOCK_SYNC_PCLK_DIV2; //选择采样时钟来源2 B# e, F5 e* n1 H- A0 r
- ADC_InitStruct.Resolution = LL_ADC_RESOLUTION_12B; //采样分辨率
' M4 h- k0 F( Z$ m& ^0 q - ADC_InitStruct.DataAlignment = LL_ADC_DATA_ALIGN_RIGHT; //采样数据对齐方式,右对齐高位补0,左对齐低位补0.
# I; T3 f _3 m& Z4 W+ O. D; E: J - ADC_InitStruct.LowPowerMode = LL_ADC_LP_MODE_NONE; //低功耗模式,使用DMA的话无法使用,这里关闭
4 k7 I- v8 ?* a* e0 E1 D# j7 F4 r - LL_ADC_Init(ADC1, &ADC_InitStruct);
3 j2 K% Y, O( H6 E) d; ^ - /** Configure Regular Channel 6 C" {# B6 L, k3 V* O: |
- */
" \2 c, ?3 s. b- J; K - //ADC_RegularChannelConfig& \) H. k4 L. l& f
- LL_ADC_REG_SetSequencerRanks(ADC1, LL_ADC_REG_RANK_1, ADCIO1_IN_CHANNEL); //将硬件通道ADCIO1_IN_CHANNEL映射到ADC通道1
- c7 p7 v; b( ^ - LL_ADC_SetChannelSamplingTime(ADC1, ADCIO1_IN_CHANNEL, LL_ADC_SAMPLINGTIME_COMMON_1); //设置通道采样时间3 a1 G9 `% q. l C: U6 {
- LL_ADC_REG_SetSequencerRanks(ADC1, LL_ADC_REG_RANK_2, ADCIO2_IN_CHANNEL); //将硬件通道ADCIO1_IN_CHANNEL映射到ADC通道2' f \0 X( j+ O0 l* x; X! S, W
- LL_ADC_SetChannelSamplingTime(ADC1, ADCIO2_IN_CHANNEL, LL_ADC_SAMPLINGTIME_COMMON_1); //设置通道采样时间: E$ R% w& ?4 I
% n0 Y- ?8 p& W2 g6 w. Z0 K- /*采样数据转换时需要参考电压,参考电压可能是变化的,所以也有可能需要采集*/
0 Z; p7 W& z0 G% X - //LL_ADC_REG_SetSequencerRanks(ADC1, LL_ADC_REG_RANK_4, LL_ADC_CHANNEL_VREFINT);2 y! K1 {7 `# Y8 |( T
- //LL_ADC_SetChannelSamplingTime(ADC1, LL_ADC_CHANNEL_VREFINT, LL_ADC_SAMPLINGTIME_COMMON_1);
9 o! e% L: i( A9 B& ?' a2 J0 N& N* u - return 1;
4 f# n k9 S8 s* C W9 P - }
复制代码 & e8 @/ l7 K8 L4 s. w: W
激活并校准ADC:
9 N+ i2 `" ?, E" D
5 n) n+ p! n* S- uint8_t STM32ActivateADC1(void)/ f& @8 k2 v& N& X6 h3 a$ V
- {
$ Y' Y, }/ O9 f! t - __IO uint32_t wait_loop_index = 0U;% {1 \, R6 \$ Q9 F: b7 P
- __IO uint32_t backup_setting_adc_dma_transfer = 0U;+ E! C$ V7 M- b* M; \& g: f6 n
- uint32_t Timeout = 0U; /* Variable used for timeout management */
6 C$ K$ u& ?% X5 g# U4 e7 c% D - /*## Operation on ADC hierarchical scope: ADC instance #####################*/
- I. T# m( p0 T3 A - ! S+ ?1 Y6 A4 M Y
- /* Note: Hardware constraint (refer to description of the functions */4 C9 D0 l6 A6 x9 h
- /* below): */
g5 X# z/ L( A H6 U - /* On this STM32 serie, setting of these features is conditioned to */* i! O3 y4 o4 H/ r1 j
- /* ADC state: */
' Z5 `' n$ ~/ R6 W2 l4 y3 M# c - /* ADC must be disabled. */
" [: c# E2 G( m! M% T8 G - /* Note: In this example, all these checks are not necessary but are */+ D' T$ x4 V8 P- T0 L+ x" n
- /* implemented anyway to show the best practice usages */
9 o( `% C- c! ~% V7 o, F6 q - /* corresponding to reference manual procedure. */
' ]2 t* S! V( D# G k - /* Software can be optimized by removing some of these checks, if */
! _9 e u4 M" M, U" H4 V - /* they are not relevant considering previous settings and actions */. o P% c! m+ Z) ~3 H" Z
- /* in user application. */& T2 Z! \2 A$ d
- if (LL_ADC_IsEnabled(ADC1) == 0)* h3 B3 A' O, A; N+ Z
- {: d8 T$ q0 Z) o# Z( j6 e! r, f
- /* Enable ADC internal voltage regulator */
: L2 ?$ B4 u9 g: a7 r; G - LL_ADC_EnableInternalRegulator(ADC1);$ C, ]) V2 b; N7 I( b
- $ d& e6 Q& u o; A" r- m
- /* Delay for ADC internal voltage regulator stabilization. */
5 n! ?4 r$ W t! O - /* Compute number of CPU cycles to wait for, from delay in us. */" T0 L, Z% ^+ u6 U( ^7 l; e
- /* Note: Variable divided by 2 to compensate partially */0 z* ?& C5 l$ F- U
- /* CPU processing cycles (depends on compilation optimization). *// }5 o9 |5 g8 `8 V0 [
- /* Note: If system core clock frequency is below 200kHz, wait time */& E e: x0 F1 Q* _* ]# d% F
- /* is only a few CPU processing cycles. */5 f" K2 u: b6 Q5 \1 j% D$ `$ H, w8 t
- wait_loop_index = ((LL_ADC_DELAY_INTERNAL_REGUL_STAB_US * (SystemCoreClock / (100000 * 2))) / 10);
9 H% ?1 U0 i5 Y' w/ ?' ~ - while(wait_loop_index != 0), P2 I7 T; s4 k" \! Q8 l
- {1 J. Q% H1 x$ x, ~
- wait_loop_index--;6 U" x+ e" Q! h. P1 V" P. C
- }7 G/ a' [: i3 P" }, Y
# u7 q/ N9 H: Q( g& G8 d) E, b- /* Disable ADC DMA transfer request during calibration */
+ W. e+ C7 |( N! F# w" }0 \ - /* Note: Specificity of this STM32 serie: Calibration factor is */
) P2 i& M7 e, u3 k) T# v0 ~3 x+ t - /* available in data register and also transfered by DMA. */
( c+ @) h* z2 z+ P; \! b$ Q - /* To not insert ADC calibration factor among ADC conversion data */
- Q2 E& b ^( A - /* in DMA destination address, DMA transfer must be disabled during */3 F) {# H! H; I9 O1 I0 I+ @
- /* calibration. */
& A: i( n+ E- J$ N! ~9 ] - backup_setting_adc_dma_transfer = LL_ADC_REG_GetDMATransfer(ADC1);7 V4 h1 D) z9 X' Y) U/ z: Q2 r
- LL_ADC_REG_SetDMATransfer(ADC1, LL_ADC_REG_DMA_TRANSFER_NONE);6 ]% I% `4 L7 C5 g- M1 z
- + X9 Z9 ~4 m( L6 n
- /* Run ADC self calibration */( E, d- f5 o z1 z* E& `3 |, _
- LL_ADC_StartCalibration(ADC1);
. ?% ^& |# P% E
! v5 j4 A) C- A+ e- /* Poll for ADC effectively calibrated */
9 \. `' K4 N; X - Timeout = ADC_CALIBRATION_TIMEOUT_MS;
g" W/ \+ Z0 L0 P0 m
" O! d% ]+ Z" u/ C1 g' Z+ G- while (LL_ADC_IsCalibrationOnGoing(ADC1) != 0)+ u' X* O/ h- ]' n- Z
- {
& r" L- g* y6 G+ R2 E2 `* e6 k - /* Check Systick counter flag to decrement the time-out value */! G3 X5 W( ?" n/ L3 E1 r7 y
- if (LL_SYSTICK_IsActiveCounterFlag())
$ e. A) S! ]& x" L$ B& x - {8 m* U- A. I) G! ]$ O
- if(Timeout-- == 0)
; ^, B: v& ?. q* k0 Z1 d0 z - {
, X# s [+ G' F- P# Q6 o* h - return 0;
5 Q9 k: l! d/ V8 R! z; t - }! y% X, u* M8 P3 s M
- }
" b, Z5 K3 K0 g - }
( j6 C, K6 `& q' [) F - 2 u5 C& H' F2 f( X( Q
- /* Restore ADC DMA transfer request after calibration */$ A N+ C* C2 H* @2 ^" C) M9 g% P
- LL_ADC_REG_SetDMATransfer(ADC1, backup_setting_adc_dma_transfer);
9 x- w+ ~( @! V - . R0 S* P' e( F! p8 M: F$ j' L3 J
- /* Delay between ADC end of calibration and ADC enable. */
) w! m& @6 M9 O* @ - /* Note: Variable divided by 2 to compensate partially */' G9 K0 G3 U2 @- |1 r- F" [8 |& d
- /* CPU processing cycles (depends on compilation optimization). */
" X2 m. _5 E: j& U5 e/ j+ m1 S - wait_loop_index = (ADC_DELAY_CALIB_ENABLE_CPU_CYCLES >> 1);/ _4 J3 x1 f5 R% |, ^- S4 T9 i/ |8 t
- while(wait_loop_index != 0); U! K( t6 y3 e$ z7 o9 W+ j
- {6 i4 F" K; m/ E2 d7 M7 R# j* \7 `% r
- wait_loop_index--;
( I; Q* ~& j/ S2 f# f! Z1 e - }
4 I) E: }' L. l' R - + U7 ~2 w8 R& F/ G0 V6 y4 A/ y
- /* Enable ADC */# ]3 m Z8 {' t3 n& Y' ^+ j' J
- LL_ADC_Enable(ADC1);
9 o" Y* x( `' A4 ?3 j - ) J: X C4 b! m. Z$ q& {& E
- /* Poll for ADC ready to convert */+ w- n5 z% U P4 W
- Timeout = ADC_ENABLE_TIMEOUT_MS;
9 t# h; z9 i+ j: h) t4 O9 j - 4 m* [ F8 M& a4 r& z* w
- while (LL_ADC_IsActiveFlag_ADRDY(ADC1) == 0)
* S2 e5 n; D' c$ z: c - {
3 G4 t8 F7 Z* s a7 \1 W% F - /* Check Systick counter flag to decrement the time-out value */2 ^7 i& x7 r' u6 l* q$ w
- if (LL_SYSTICK_IsActiveCounterFlag())
% R) R# S5 ?. G. R/ k5 {' h - {
6 \' O7 F5 K" e" W - if(Timeout-- == 0)1 l5 q, Q" D6 ]/ X, |
- {
4 @- \3 V* j# P! ^; C - return 0;% F- b$ j9 `3 M8 |+ c8 L9 @
- }4 c' w( @: J8 O' ]
- }
9 r$ g/ N+ O5 ]9 ]1 j6 \- d - }( w) T' `: C' G5 ^* M6 j
- /* Note: ADC flag ADRDY is not cleared here to be able to check ADC */' Q! ~- m6 S7 {) O' M
- /* status afterwards. */
, b6 S4 L2 V( E* [# K# t - /* This flag should be cleared at ADC Deactivation, before a new */- `0 `$ |# m0 l
- /* ADC activation, using function "LL_ADC_ClearFlag_ADRDY()". */
. r D, `* m& u1 p/ X" @5 k - }, n+ x- d' A# S$ ~' _+ @* H: g+ D [
- return 1;# w4 |9 w* Z7 ~/ ]( A4 j9 J. C ?: m
- }
复制代码 " g2 P, {- v. V) c$ }7 C6 d' n8 r
因为ADC配置的是软件触发的单次采样,所以还需要有手动开始和结束采样的函数。5 U3 L& ~& b! r7 h. _- d
' s% q! g! K& B/ F# M: s/ h6 w1 V0 Y使能ADC采样:. Q7 _0 ]: c+ f- H; H
1 Q J; m! Y" y0 d/ o" |3 N1 F
- uint8_t STM32ADC1SampleEnable(void)
% s% z) P, o P# K9 W5 b* B j - { W, x" |# q8 j% P3 W! f( W' M
- uint32_t Timeout = ADC_ENABLE_TIMEOUT_MS; 5 K' ]: \# W4 C3 y3 d: x
- LL_DMA_EnableChannel(DMA1,LL_DMA_CHANNEL_4); //先使能dma再使能adc,以防采样数据丢失& Q! u% [7 S# K) }. c
- LL_ADC_Enable(ADC1);
. t& a7 M# K& b - Timeout = ADC_ENABLE_TIMEOUT_MS;
7 J, B% H% I1 v' O3 \ - while(LL_ADC_IsActiveFlag_ADRDY(ADC1) != SET)
) g$ c2 s" G3 \* L) P - {. C( ^# _+ {- `
- if(Timeout-- == 0)5 ]- l O/ }) d9 L$ e9 ~
- {
" `; ]( g- N0 m8 p: p7 J5 E - LOG(1,"STM32ADC1SampleEnable ERROR!!!");
8 h6 o: [4 T- n* c - return 0;
) T& ?$ _% k: @( C' C - }4 r, H7 N, M8 n0 _% P1 U
- }+ J c3 W; n, }1 I1 r5 T
- LL_ADC_REG_StartConversion(ADC1);7 l+ L! o& R2 _& M$ t
- return 1;8 x2 x. R& x1 s% [+ B
- }
复制代码
- `0 O1 ]2 Y' ]9 A失能ADC采样:
) E% H; p9 A5 J$ A0 p$ Y1 x7 _- R* z9 u
- uint8_t STM32ADC1SampleDisable(void)
) h/ E! i4 L% g% f- Z" t6 D - {
! A* H& ?8 n$ W/ @: M, D - uint32_t Timeout = ADC_DISABLE_TIMEOUT_MS;
: Q! t" ~7 n7 E$ N! k+ q - LL_ADC_REG_StopConversion(ADC1);- I/ t; b% E$ G. x5 t$ P F
- while(LL_ADC_REG_IsConversionOngoing(ADC1) != 0)
+ U. t* P1 Z5 |( ^ - {
! e0 q/ y4 R! `" r - if(Timeout-- == 0)* _0 p; H5 p4 }+ X5 t! i
- {1 k1 u* g" G9 P
- LOG(1,"STM32ADC1SampleDisenable ERROR!!!");# ] ?8 H0 |& V
- return 0;7 N/ S) U$ R9 D; P
- }; c4 q/ v6 b7 Y* c
- }
4 @% v0 [) p: j5 x" Y; t# D - LL_ADC_Disable(ADC1); //先关闭adc再断开dma,以防采样数据丢失
8 H' f& M$ L$ s+ A7 X7 P8 p( M - LL_DMA_DisableChannel(DMA1,LL_DMA_CHANNEL_4);& R0 l9 s- @" i& F$ X3 H
- return 1;( `, j3 q- e) q
- }
复制代码 3 o) ^9 T# U0 Y: v. Q/ H8 ]2 o& [
进行ADC数据采集流程(采用多次采样求平均值的方式):
/ V) T3 A, i7 P0 `' d( C) t9 F7 d0 A' l7 p1 o: `- @: D0 N8 M
1、当需要开始采样前,调用一次STM32ADC1SampleEnable()函数;5 Y$ N$ i& ?8 O+ V
. r( E2 c' Z9 R( e _
2、在一个定时函数内调用LL_ADC_REG_StartConversion(ADC1)进行数据的转换(因为使用的是单次采样,所以每次进入定时函数时都需要调用),取出DMA数据进行累加;
! l9 k# |; D8 n% v; R- S! ~7 i8 m; ?( g# X* g5 s+ R
3、达到需要的采样次数之后,对得到的数据进行运算,并调用STM32ADC1SampleDisable()函数关闭ADC通道。
$ |% F+ L2 \6 s' l& [( `+ h) N
& N. m6 a6 g, q9 f, t& O+ R以上方式适用于需要间隔一段时间获取一次ADC数据时使用,例如需要每隔1分钟获取电池电压时,可以进行1分钟倒计时,然后连续进行十次间隔10ms的ADC采样求平均值。
% B W, L8 D, E" U5 h$ z. h7 }. ~+ l3 A6 _/ q
+ U# b3 A8 P/ ^; d9 d
|