STM32G031-使用STM32CubeIDE实现DMA处理ADC
2 l& u( t* Y+ y2 v一、USART配置
/ M' r# ^, S/ k7 ]% O
4 v0 p& I. S- f, N+ z
( V% t+ Q5 y# b( a% S8 n- i& Z) w4 \* o, I/ I3 W
二、ADC配置 ]+ A: W6 Q) J {. T) |( D
3 Q5 p4 W9 \2 o0 M$ B- H
' A- p8 F! g+ k2 h. f) r$ |) R
5 T8 D! u( u3 }8 ?6 e! j7 k: Z* C- f% h: P' J4 C
' _! B0 e" T+ f. I
( w: }9 `8 A; Z& U+ D/ s2 c
$ Q( x$ i7 P8 z& b v" p3 J4 S: {配置完成后,生成代码即可。。。8 [6 ]1 Y( i' V
. ^0 ~% z) e3 q
三、用户代码6 ~$ W4 ]- g2 _' c! g A
- /* USER CODE BEGIN Includes */
% ^4 Q1 M. D: i; W/ L - ) f& x) b3 ]0 F C0 R- |
- #include "stdio.h"
, B" C$ c: g: j- M5 G( z p2 p - /* USER CODE END Includes */* f& V! A% m( Q8 ^! |/ @
% z+ ^/ s9 C. f, J- /* USER CODE BEGIN PD */
6 j" Y9 e& I2 { - #ifdef __GNUC__
- }8 w* ?, `) Q - #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)$ P4 w, ]" o( p, k5 e- q6 v
- #else' [& p! ], ?+ v/ `* K2 c# k2 {2 J
- #define PUTCHAR_PROTOTYPE int fputc(int ch,FILE F)
) f: G" o$ P% Z% e/ G) } - #endif/ B# E0 }3 {$ w/ w/ f: g8 p" Y
9 O$ x. [' W/ v! I8 M ]0 p( x- PUTCHAR_PROTOTYPE; O! N' ^! |5 c; o5 \; P
- {! d9 z$ F2 ]$ Q5 L: @" Q
- LL_USART_TransmitData8(USART2, (uint8_t)ch);0 J6 z& H+ G0 U. X4 D$ w
- while(!LL_USART_IsActiveFlag_TXE(USART2)){}" B# L, ~ X$ m
- return ch;+ c$ V& a) Y" i. z; N7 T9 ?
- }" l! t. w) ?. Y. [7 K4 S
- /* USER CODE END PD */2 l% d) o+ `9 u$ n6 `; A, {& b
; }1 I o- X7 N7 B4 [2 W- /* USER CODE BEGIN PV */" B, f0 \2 }: @( M$ A
- uint16_t ADC_ConvertedValue[150];* L! w& v; N, X: H. W2 _, ?: T) \
- uint8_t i;/ Q& M$ I0 n, }' V w1 V
- uint32_t ad0,ad1,ad4;: |- Q4 L# ~3 V( B
- /* USER CODE END PV */
( S3 W: G: Z# ]
I! Q3 c) q) S7 P- /* USER CODE BEGIN 0 */: C" a* t; E) @8 i9 ]
- void ADC_DMA_Config_Start(void)
u A: F6 q( R. P) h7 M+ Q0 g - {+ \3 p! g" E0 B# ~8 j' n
- LL_mDelay(2); //延时2ms,避免DMA读取数据通道不对
( d, m4 e; G% [+ j - // LL_DMA_SetChannelPriorityLevel(DMA1, LL_DMA_CHANNEL_1, LL_DMA_PRIORITY_HIGH);( J) \/ J$ ~3 M- l( b% N# A; c
- LL_DMA_SetDataLength(DMA1, LL_DMA_CHANNEL_1, 150);3 i4 o# [ h3 `) ?7 M( }' I; e% _
- LL_DMA_SetMemoryAddress(DMA1, LL_DMA_CHANNEL_1,(uint32_t)ADC_ConvertedValue);
- J: t$ F7 }, e2 o; `$ q" v, q - LL_DMA_SetPeriphAddress(DMA1, LL_DMA_CHANNEL_1,LL_ADC_DMA_GetRegAddr(ADC1,LL_ADC_DMA_REG_REGULAR_DATA));
: J! V) n: x* x1 e2 G. z( m2 f3 M - LL_DMA_EnableChannel(DMA1, LL_DMA_CHANNEL_1);
8 U- r' A0 f. C7 r% N2 J - . r5 Q! ^+ N7 J
- LL_ADC_StartCalibration(ADC1);$ I/ J0 H2 q/ k) M# Y
- while(LL_ADC_IsCalibrationOnGoing(ADC1));) V8 C, m5 V5 z3 C1 C' l& S2 U
- LL_ADC_Enable(ADC1);6 {" p; A3 F; k
- LL_ADC_REG_StartConversion(ADC1);
1 m, o8 ?7 g R! v% ?# B& Q- _
1 P1 n k1 ]" s6 @$ G) n# K" s$ L- LL_ADC_REG_SetDMATransfer(ADC1, LL_ADC_REG_DMA_TRANSFER_UNLIMITED);3 n3 ~+ k- s$ v* k
- }
9 I, p! v m5 f* d6 U - / _4 x, V* x& S4 L& h( h- a$ y
- + v! [6 [) o' N
- /* USER CODE END 0 */
* e4 }/ l& M8 R9 h% c8 A* u
2 }" V8 c" G% C- H4 z) _5 j- /* USER CODE BEGIN 2 */
/ U. Q F# k/ C7 s0 r - printf("STM32CubeMX Test...\r\n"); s8 x: l7 Y5 ?/ S4 d$ ?+ k5 c
- ADC_DMA_Config_Start();
2 J$ E( n6 S6 a) M8 M. o, g - /* USER CODE END 2 */
5 U' V/ B0 H2 v - /* USER CODE BEGIN 3 */
& B' o7 N6 v8 `2 k - for(i = 0,ad0 = 0,ad1 = 0,ad4 = 0; i < 150;){' H) H; ~! H4 F8 Q P! F: E7 X" z8 B
- ad0 += ADC_ConvertedValue[i++];
) J+ W, O; n0 `0 a3 b9 j& @$ c - ad1 += ADC_ConvertedValue[i++];2 ^( ?+ i" O# V! z" g. L! b
- ad4 += ADC_ConvertedValue[i++];6 T% G1 S4 j3 Z" f% w7 o: a4 c
- }
6 `' k: x" M& y7 _ - ad0 /= 50; // 实现均匀滤波/ T- c B2 X' \1 g* n" L0 X0 I& ~
- ad1 /= 50; // 实现均匀滤波' _ e, Y8 m) ^# R1 x2 T6 A+ ~
- ad4 /= 50; // 实现均匀滤波
' {) N7 O/ n: o - // printf("ADC->CH1 Value:%lf\r\n",(float)ad0*3.3/4095);& z2 ?9 W& N. Y# j/ o% d9 d
- // printf("ADC->CH2 Value:%lf\r\n",(float)ad1*3.3/4095);
2 J6 \9 ^4 B p; N- a& x* N! a" C - // printf("ADC->CH4 Value:%lf\r\n",(float)ad4*3.3/4095);0 c8 F& q( i2 r" V; X5 n
- printf("ADC Value:%lf,%lf,%lf\r\n",(float)ad0*3.3/4095,(float)ad1*3.3/4095,(float)ad4*3.3/4095);" i9 E% o0 j" z$ y$ e* a
- }
5 r/ \% H% x% ~7 P6 k' h7 j - /* USER CODE END 3 */4 J1 G+ e+ {( f8 b; E1 k; m
- x) Y$ n* ]3 t
复制代码 8 s. S& W4 v, Y) Z M0 u; l7 E0 O
|