ADC+LPUART串口通讯
q3 w1 i3 K: Y& |程序介绍:给ADC的第四通道一个电压,通过AD转换,输出到串口打印信息“The current AD value = ****** V”4 b/ a* _3 Y9 D& l, \" O
3 O, } O3 s$ @& H4 d) _1 ?
调试过程中出现的问题' ]/ \+ K: s/ G, ~* x% _
因为用的内置AD,调试过程不复杂,基本一次就通了,但是串口调试的问题主要是会出现乱码情况,还有16进制显示全都是00 00 00,关于这些问题大多数都是由于波特率不匹配或者是波特率和晶振频率不匹配等。! w+ P5 W" Q2 t: d3 }" N5 r
" e5 o' h% r+ w) J
1.STM32CubeMX
6 j$ \- x2 O) w$ m2 a# d7 m% j) O; E7 {. Y# a* l
- \! w; r7 X0 y I3 a
/ Z* _0 [0 }/ c9 q; [2 M6 S+ f" p
x# H; h. Y) H9 q0 g8 t% n
/ b) Q* w' X3 h+ J1 ZSTM32CubeMX里面时钟图还是比较清晰直观的,比较容易上手,从图中可以看出这款芯片的LPUART是低功耗的,并且用的内部时钟(选择LPUART也是因为项目需求,UART类似),而ADC是内部有专用时钟,用HSI16M时钟工作 _( K4 \4 ?/ O& r* a5 F: L$ U1 P- r
* C% o! n- V+ Y0 P, i2.代码部分* B9 k' S: d# w' k7 N+ n$ M
9 E0 M( O2 m2 }# [6 A7 e' c6 h
- #include "main.h"3 P9 ?. y3 L$ |% S
- #include <stdio.h>
f2 O7 z* @" n" F - 7 I5 ?0 ~; G( j* Q4 c- k; K7 Y5 P
- ADC_HandleTypeDef hadc;
7 ^6 j" t; d1 W* z x8 L4 Y
5 ]; y2 g! ]# ^ U$ D- UART_HandleTypeDef hlpuart1; @2 z# W [ o% g8 i
W) U5 n0 }- ]9 l% @- ADC_ChannelConfTypeDef sConfig;% y$ x0 h/ S- e) l% {! Q
- ! B8 J) w& Z. C" a" Z; X2 V
- static uint32_t adc_conv_var;
3 _0 J0 x/ y% q: _8 D' ] - float temp;
# m' ]1 u% e' x - 7 `5 i+ ?& m! I4 T' {7 c- ]
- void SystemClock_Config(void);9 ?# _7 g: d( Z1 C
- static void MX_GPIO_Init(void);5 O. j- X7 c0 D/ F& y; S
- static void MX_LPUART1_UART_Init(void);
- X- n! |9 h$ S. Q8 Z/ l4 n - static void MX_ADC_Init(void);+ K4 A; ` }" B! N) s
- static void GET_ADC_(void);* v: \% L. L* l( F' o' }# Z
' ]& @! [, T8 F7 s
9 o6 E. q! P' f- L7 h7 s- int main(void)
3 c0 m2 ^8 `8 H; u) ^- f - {
, ?' c6 S6 q4 @- } - ( |8 B% Y. _3 [, z) C, h0 Q' X
- HAL_Init();
/ z' R2 L2 l! k; G: i3 q' y. x6 U* a* P - 5 Z, L: E' T, O7 b+ G6 T/ u
- SystemClock_Config();
& y9 M% n2 j1 R
i' x; ~' p' a, R( e- MX_GPIO_Init();
& g B- G0 @7 ? |& X% d4 U+ }/ G& g
/ e, O: T( u3 y2 C* p- MX_ADC_Init();
% |8 [. ^, h) V6 F! V7 } - - y" n# ~% r, }& W% W k2 o7 j
- GET_ADC_();
6 D# _" _ i& g$ b2 W/ | j -
$ x5 a5 X8 ?4 L" `' G" m+ T - if((HAL_ADC_GetState(&hadc) & HAL_ADC_STATE_REG_EOC) == HAL_ADC_STATE_REG_EOC)
, f: i9 C& {0 G% u" G+ [: z - {- p/ ]) I' n/ L6 f
- adc_conv_var =HAL_ADC_GetValue(&hadc);( A; { K1 W, J- l- o( `. {
- temp=(float)adc_conv_var*(3.3/4096); + K" K! s0 \% c3 A5 V6 C
- } ! {; Y& Y, g3 g$ q3 y9 Z2 g8 ]8 T
7 j7 G& h7 s" w9 |% i6 J8 r( H- MX_LPUART1_UART_Init();* G3 B+ X7 R* h9 j) K6 ]
- printf("\r\n The current AD value = %f V \r\n",temp); $ |4 S7 {) g" f! S* X5 Y
; z) ~ F. s; V- while (1)
% s0 J- z' W1 f, g, ~% n - {
( n* J% f2 b2 r3 e# q2 E) r - ( M) Z; U( _% E/ w
- }& _1 {* B/ E% o+ x, D; p, V. K- i
- }$ j" @) f- H9 b* }3 M* g8 j( c' }
- //printf函数重定向/ M: H$ ^; w2 E8 Q% }: S
- int fputc(int ch, FILE *f): A+ O/ z3 n( F [- J. j9 _/ [/ L" u3 T
- {8 ]2 a! z; n2 g6 a! O I
- uint8_t temp1[1]={ch};
+ s/ i( {/ k3 r, g: j - HAL_UART_Transmit(&hlpuart1, temp1, 1,0xFFFF);
8 e2 Y- |% z" p5 |7 l- Z) P - return ch;' N+ G5 L" J5 u& E: G, S& v0 k
- }0 ^8 \3 [2 {2 Q F0 Y% q
- /**
3 E& ^0 w- m( |" P# {2 R% J4 i4 ? - * @brief System Clock Configuration8 w' q8 N4 V C+ U9 h
- * @retval None
0 {( P. ^: F$ q: H4 b. r - */, i/ J/ k* W' W8 O8 d0 j
- void SystemClock_Config(void)
N* w2 W4 \) B% W; U - {+ O/ D" K) H. m8 _( @
- RCC_OscInitTypeDef RCC_OscInitStruct = {0};, G6 [/ k, r& J3 d6 k+ L9 \
- RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
1 V# a2 v# ^5 @, N$ _ - RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
6 v; e7 u% ~8 F" U - % Z# `% W, ~0 p. q9 x9 E$ q
- /** Configure the main internal regulator output voltage : f. Y3 B. A3 i1 O1 D7 y( p- _
- */: Q* t$ T; d* u: ]$ N
- __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);+ S" x6 A) v* E N4 H/ C
- /** Initializes the CPU, AHB and APB busses clocks
4 b- x+ m1 u/ E2 z - */
" `+ B: S) ~5 K/ H, ]. M/ j; u0 l - RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_MSI;
/ a; G& p7 n0 V" ^* j - RCC_OscInitStruct.MSIState = RCC_MSI_ON; g3 ~6 M2 X0 L6 u4 @/ x
- RCC_OscInitStruct.MSICalibrationValue = 0;
7 w. L ]9 i8 x8 y$ P# M$ G+ e K - RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_5;0 D/ d# A/ h! b2 g7 V6 J
- RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;/ k& e& a% d0 _! H7 I; f" ` Q# E
- if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)1 {7 v! z. ]; F) J4 m- L
- {1 r E; U Y9 ~% a
- Error_Handler();5 L$ T i, o( A) N; v; S0 N8 ^8 H" `
- }
4 Q9 I. D) F, d W - /** Initializes the CPU, AHB and APB busses clocks # c: H1 r0 T7 E. p E0 I7 E1 K& N
- */
8 O& g; l: c7 G# l; C* s( N/ u& s - RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
/ q" q- M/ W6 a. w% o, `6 k - |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
, z5 l% ?$ q) \: u/ ^+ F8 A - RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_MSI;, {% L% z" o( Y( ?/ b1 _. e
- RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
4 h! e3 f" y5 m7 _: o: m1 E - RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;% G9 N5 B) ^ U) {. Z7 P7 o
- RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
/ {) i2 F/ O) ~0 _' _
% F% P* d, A0 G% k( c; v& h- if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)8 x- F6 x6 h0 E( S- d o
- {) [3 Q1 f3 x5 g$ X& d3 O
- Error_Handler();8 S) M% k' w: J* R3 I7 R, f
- }+ A( k, L7 s3 J! s4 _* N
- PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_LPUART1;
4 `( t# Y4 f' C' k8 |$ j - PeriphClkInit.Lpuart1ClockSelection = RCC_LPUART1CLKSOURCE_PCLK1;
9 R( m. F. K& F* o- E) K6 {- c - if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)" o3 R; T. C7 D
- {
' }, ?* j3 x9 {* G/ K5 S - Error_Handler();
4 i- U0 t: r1 x8 p( t+ p2 Z6 ^ - }! M. n- U' G, }/ p0 }
- HAL_RCC_MCOConfig(RCC_MCO1, RCC_MCO1SOURCE_SYSCLK, RCC_MCODIV_1);
1 q# |" H5 s* @6 |& f# Q - }
% O: O% k- I* _& f9 G. x; d( X: Q - , d a0 t4 F! F/ `* T9 u
- /**+ M, s7 d! ^5 g. ]5 p0 F% D
- * @brief ADC Initialization Function& a$ \8 _1 O8 Q: \8 H$ w2 A' h
- * @param None
$ Y) K0 h/ \/ O/ `* x - * @retval None: c# i& Y9 \6 I
- */
t& ]" L; v9 J/ Y5 j: I; `4 V - static void MX_ADC_Init(void)
1 @1 a! `# ~% q/ D( P3 h2 m0 }- Z - {
7 v6 D9 L* A: h) @: x/ Y - ADC_ChannelConfTypeDef sConfig = {0};
1 k7 M- Q5 {- b- A
# {: x' T! J9 c$ T, Q- hadc.Instance = ADC1;
7 q+ o% V; C3 r4 A& y - hadc.Init.OversamplingMode = DISABLE;
( E$ k4 X* ]. D( U - hadc.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2;
, m$ J* c6 w4 W - hadc.Init.Resolution = ADC_RESOLUTION_12B;6 ]; n$ u9 U( ~1 @1 B, o S
- hadc.Init.SamplingTime = ADC_SAMPLETIME_7CYCLES_5;
2 L, g& `! k. H1 M% I - hadc.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD;
0 ~4 X) t& A" X- P7 Y - hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT;
^ _ t; b" X1 c! T1 | - hadc.Init.ContinuousConvMode = ENABLE;% L7 y- R7 ^4 F5 J6 X% f7 b$ r
- hadc.Init.DiscontinuousConvMode = DISABLE;
, h& f8 o$ c- o% D: | - hadc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
9 `0 f; @# q$ F9 Y5 \ - hadc.Init.ExternalTrigConv = ADC_SOFTWARE_START;
, }' Z. |4 h4 ]. i2 x s; y( z - hadc.Init.DMAContinuousRequests = DISABLE;, f' D% ?# F" M+ u6 V/ [
- hadc.Init.EOCSelection = ADC_EOC_SINGLE_CONV;" C' {2 R# E" p$ n
- hadc.Init.Overrun = ADC_OVR_DATA_OVERWRITTEN;0 f* Q8 O9 M& \/ |0 ?/ N2 _
- hadc.Init.LowPowerAutoWait = DISABLE;! @! l5 d5 ~0 S, y0 J
- hadc.Init.LowPowerFrequencyMode = DISABLE;
) I: w9 b V7 z - hadc.Init.LowPowerAutoPowerOff = DISABLE;0 D, C" L- f& t }1 r$ w
- if (HAL_ADC_Init(&hadc) != HAL_OK)
5 ?8 g& _1 D7 [& B8 y1 r) u - {2 {: L$ |' T! p6 ?
- Error_Handler();! Y4 m6 x6 c1 y# B3 J, l
- }7 U# C' _% C a& j E' r
- 7 M4 |' x+ n$ ~5 e2 N
- if (HAL_ADCEx_Calibration_Start(&hadc, ADC_SINGLE_ENDED) != HAL_OK) //启动校验功能
" [) O0 q9 C+ O: U j, e - {
; y5 S+ z$ c g5 ]* e0 m - Error_Handler();
) Q3 R# q9 a9 l- `- u - }1 V* s& g( E0 p) G& T
- }
9 L/ `- |6 \9 b+ _ - static void GET_ADC_(void)
) h4 U. b8 T* h2 e3 Z( X - {
g0 K4 z0 c: ^$ \7 X- F - sConfig.Channel = ADC_CHANNEL_4; /* Channel to be converted */
3 t! o6 U# x( V) q - sConfig.Rank = ADC_RANK_CHANNEL_NUMBER; // 设置通道+ ~( r+ a/ n( R/ J' b6 p
- if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)0 ]+ I( j! q4 N: a: r( |; f
- {
# I0 h# K J! S& s. O3 E% }& y - Error_Handler();6 T' s2 I. i4 f0 r) k/ D
- }
- u$ y: w/ M, S, V - $ z& t% u* e9 N4 A$ s
- if (HAL_ADC_Start(&hadc) != HAL_OK)
! I0 z; H% l: q* b: B4 r - {6 w( _3 R3 m' v! A! w7 C
- Error_Handler();$ A2 p& x% Z1 B: s
- }) J- d* Q& |% r! P
- HAL_ADC_PollForConversion(&hadc, 10);9 r1 S! T7 \8 P& u) a, ~+ n
- }% z) J- _* S5 w/ |7 |/ G1 ~! E
- /**
6 v2 V6 d7 P, J* `% m9 n+ i - * @brief LPUART1 Initialization Function& H# e7 B: C9 [! ^9 L% J( w
- * @param None
# i& ]* E x3 \8 c9 _' `' D - * @retval None
+ d( m9 L9 p7 e ^. S2 I# I - */* {4 r% q, m: B4 f+ u
- static void MX_LPUART1_UART_Init(void)' N0 q% u8 L" M! ^! d$ O
- {
2 }% o U( q! B5 N) _. b: ]% y - hlpuart1.Instance = LPUART1;
* K6 U- a7 f0 ? H3 Z - hlpuart1.Init.BaudRate = 128000;' [& Q/ X5 j0 L$ f( Q, `9 v
- hlpuart1.Init.WordLength = UART_WORDLENGTH_7B;0 b+ w" z0 n5 C! Q. f" [8 m
- hlpuart1.Init.StopBits = UART_STOPBITS_1;
9 F6 ^$ P) ?( l3 V# k- P" M - hlpuart1.Init.Parity = UART_PARITY_NONE;- N, ?1 `* N; Z0 c8 g. i4 C, G+ }3 i
- hlpuart1.Init.Mode = UART_MODE_TX_RX;
1 M- X0 o3 {5 ^9 C# T& |7 r( L, B - hlpuart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
5 c" p% D1 b9 z. c0 w$ B - hlpuart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;" \; _+ x9 }" B# a% }; W
- hlpuart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
6 W" ` |! O3 _7 q( S, w3 M' ?% J - if (HAL_UART_Init(&hlpuart1) != HAL_OK)
& }$ e8 Q! M3 d X, f8 n! W4 _ - {
! Q, ?% v7 R) e) u8 X# z# o' e - Error_Handler();! B' j& n/ u6 o a1 F
- }& }" s! ]7 L/ N1 _0 b7 n" H z" h
- }
* K) i, X+ M* \2 ^, y - 5 H8 n$ |& W. `3 a* ?3 A/ O
- /**8 `5 P6 A2 f+ ]& v8 j$ m
- * @brief GPIO Initialization Function
3 B" q$ Z& s: U* u C1 d - * @param None. B. b% O4 y; k
- * @retval None& n" o) A2 O( ~( U4 i, P
- */
a; b2 f. z0 S/ L: @ - static void MX_GPIO_Init(void)
. g5 H' ?" ?, A# n1 T+ ? - {
9 N, \' G' z+ C ?* \ - GPIO_InitTypeDef GPIO_InitStruct = {0};
5 _) T; P1 T7 _& j! ?
3 P# k2 m; A! Y* P1 e' Z- /* GPIO Ports Clock Enable */
5 @0 {2 C, Q8 S - __HAL_RCC_GPIOH_CLK_ENABLE();
- [$ W0 S6 u8 D0 J - __HAL_RCC_GPIOA_CLK_ENABLE();
, L$ ^( Q; y$ g0 P6 D - __HAL_RCC_GPIOC_CLK_ENABLE();
$ b+ H: T7 J: Z4 G& j C - __HAL_RCC_GPIOB_CLK_ENABLE();$ t0 O! f k/ D4 a" n2 A
, E- G$ a3 g: Z- /*Configure GPIO pin : PB13 */! ^# R7 g& C; M' y3 b7 f
- GPIO_InitStruct.Pin = GPIO_PIN_13;% w0 I" N5 E& Q* A! f0 W* v `
- GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
- @3 l0 z6 p$ M& ^5 d% m: f2 Z - GPIO_InitStruct.Pull = GPIO_NOPULL;( h+ E( c- N+ o
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
# |) u/ i: }' @7 T. i+ U; h& H1 d; W: I! r - GPIO_InitStruct.Alternate = GPIO_AF2_MCO;( W- |) r& U+ v0 X; t# Z- b
- HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
! n6 I6 T* F+ S# c - }
复制代码
$ Y: g$ T# q8 [* c, u4 a: |
! j2 \0 A. B0 `" d/ h: E j
+ k6 x; Z6 o1 l6 N' H" {! ?
& V) g) P& [ t _
1 N/ C% \6 e, W+ p |