STM32G031-使用STM32CubeIDE实现DMA处理ADC
0 i) A" j6 v( B& H- ]) o一、USART配置
& ^! U! ^9 |* W( j) v0 q; [" F* X2 Q# v" o
V5 t- e) y+ Q M
6 G2 j& c8 _$ K: _$ C6 K% y$ F二、ADC配置3 K! C3 d- U& F! `: ~9 [
6 |; h1 e3 W8 |
1 V- x: a9 m' l1 k
. S( `, p; z5 X6 @4 k- X: q; [1 p6 }+ j: ]2 T. v
- j& Z6 J" V m% n' p
4 {( t8 L; W. R
; I: Z6 J! L a+ J* t# Z& S配置完成后,生成代码即可。。。
2 M" V/ H1 K5 p4 E+ a% Y
: n" k: N2 X4 @8 X; o# G1 k三、用户代码5 k& V! l- j5 J
- /* USER CODE BEGIN Includes */& ^, F" q( C8 `: t# v
- 6 q4 n* H) T, M, |' O1 z
- #include "stdio.h"
: ^& l+ z0 r: {- s - /* USER CODE END Includes */
6 u4 p1 e8 s9 C4 h! c) W
6 S' w( ^+ m: B( a- /* USER CODE BEGIN PD */
: g# M! g v1 y" L/ q$ n - #ifdef __GNUC__
( j8 \$ \. I! t" m }6 T1 B& N) s9 ~ - #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)% b; g# V; L$ {3 u E% R) e$ Y" x
- #else: V" t, c, ^6 Q4 j% v; n2 b
- #define PUTCHAR_PROTOTYPE int fputc(int ch,FILE F)# t7 _; V) ?- T% C5 I6 Z
- #endif+ G$ |1 k% V! B0 F
- ' E* H5 u; M: J% m8 G
- PUTCHAR_PROTOTYPE/ Z* i+ `6 x4 A, ?; X I5 ^
- {
9 W0 {5 {# H5 {$ f# K' _ - LL_USART_TransmitData8(USART2, (uint8_t)ch);3 g2 L0 X" T. y
- while(!LL_USART_IsActiveFlag_TXE(USART2)){}
8 Q/ L- Z. B5 ]+ @ - return ch;& r* |" r) I- y1 W" s. p9 w# w
- }
- Y2 a& k. n5 T/ `0 K - /* USER CODE END PD *// b0 K7 c1 T( u" x% P
. l3 c8 J/ Y/ N- /* USER CODE BEGIN PV */
& P, H5 E9 h9 ?/ S: ^ - uint16_t ADC_ConvertedValue[150];) s Y7 k% g v3 i D; {$ ^% `
- uint8_t i;
0 l! H6 E: ~" s2 z9 X+ W: t - uint32_t ad0,ad1,ad4;7 Z( }7 i& n6 W8 c4 X
- /* USER CODE END PV */: f9 {$ g# w- F9 d
q) ~9 X" a" |: e3 j- X$ k- /* USER CODE BEGIN 0 */
; e& w/ h$ z8 s; I - void ADC_DMA_Config_Start(void)5 B' s& E* I2 O
- {# x, S+ G: y, K, q+ a
- LL_mDelay(2); //延时2ms,避免DMA读取数据通道不对6 J' T6 T( y: \" A9 p% R
- // LL_DMA_SetChannelPriorityLevel(DMA1, LL_DMA_CHANNEL_1, LL_DMA_PRIORITY_HIGH);
3 K6 D6 h. [% @! A3 W" {3 D; T - LL_DMA_SetDataLength(DMA1, LL_DMA_CHANNEL_1, 150);( X) i' J. |6 x: P
- LL_DMA_SetMemoryAddress(DMA1, LL_DMA_CHANNEL_1,(uint32_t)ADC_ConvertedValue);, K. R! ]( M: t# M5 V) Y
- LL_DMA_SetPeriphAddress(DMA1, LL_DMA_CHANNEL_1,LL_ADC_DMA_GetRegAddr(ADC1,LL_ADC_DMA_REG_REGULAR_DATA));7 q) J7 {3 Q* i& m2 L9 |
- LL_DMA_EnableChannel(DMA1, LL_DMA_CHANNEL_1);
1 i% z4 W3 Y; p7 E) a - / {5 q# J* ^3 b7 m. `; J
- LL_ADC_StartCalibration(ADC1);/ e/ i# a( H. a- j
- while(LL_ADC_IsCalibrationOnGoing(ADC1));
1 P" Z( J F' @! R! t - LL_ADC_Enable(ADC1);" b; Q0 k9 {6 v3 l2 w5 W" c3 ~9 H7 d
- LL_ADC_REG_StartConversion(ADC1);
; e; H& K0 P* j4 @ - " C, ?5 T$ ]' b9 |: @2 Z+ d1 I. U
- LL_ADC_REG_SetDMATransfer(ADC1, LL_ADC_REG_DMA_TRANSFER_UNLIMITED);
% O3 }8 U6 }+ K7 f2 A, I4 G$ P - }* X/ `7 a% }* E6 |& R6 n9 ]3 }
- / t8 a9 `; q: J& u4 x! U$ R% ~3 _
- 5 h; B$ C6 x) b, o: N7 u
- /* USER CODE END 0 */
" {* P- Y% m* y - , K1 z% ^/ C( q" [. t$ A, h
- /* USER CODE BEGIN 2 */
% G v2 {4 A% B* E( p - printf("STM32CubeMX Test...\r\n");
& j, C* W# S9 b - ADC_DMA_Config_Start();
8 N( a7 T1 ?% E - /* USER CODE END 2 */
2 k& `, C9 l1 c2 q1 x - /* USER CODE BEGIN 3 */& E2 }8 z; H; A: E4 O+ I# P2 P
- for(i = 0,ad0 = 0,ad1 = 0,ad4 = 0; i < 150;){4 G4 x1 b# C: P
- ad0 += ADC_ConvertedValue[i++];
: K5 K' {/ _" K1 W - ad1 += ADC_ConvertedValue[i++];6 O% z3 t8 I3 ], ~
- ad4 += ADC_ConvertedValue[i++];4 G$ W/ Z+ }4 w
- }
' H" f1 v8 |' V0 E$ g& | - ad0 /= 50; // 实现均匀滤波
' ~( @: x3 k3 t0 y - ad1 /= 50; // 实现均匀滤波
& d* Y2 d; J1 K7 a& [ - ad4 /= 50; // 实现均匀滤波
$ v$ ^4 F z' X - // printf("ADC->CH1 Value:%lf\r\n",(float)ad0*3.3/4095);; c) b% S3 G9 {
- // printf("ADC->CH2 Value:%lf\r\n",(float)ad1*3.3/4095);8 M5 Q( L% W" B+ k0 Z; x. U
- // printf("ADC->CH4 Value:%lf\r\n",(float)ad4*3.3/4095);
1 [# z0 w" j3 f5 k - printf("ADC Value:%lf,%lf,%lf\r\n",(float)ad0*3.3/4095,(float)ad1*3.3/4095,(float)ad4*3.3/4095);
1 ^- K- G6 w! A# [* _: D1 c) C - }" S" p- n6 Z: _. W& m
- /* USER CODE END 3 */
9 y+ F, D2 I m' l
( `1 \( C' [* K# x! ~
复制代码
7 F* S2 x* X- v7 F+ f5 \8 i |