你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

stm32l431RC ADC 采集Vbat 不准的问题

[复制链接]
zhaoxuji-239467 发布时间:2020-5-11 13:08
系统电池供电,3.6V电池,经过一个二极管(3A),大概是3.3V到Vbat .
: E5 P# M( R+ q3 |. m$ Y$ b) `测量电压为2.98V 左右。! V* r, l3 [( T+ K# A: P% r
继续采用直流电源供电,当输入Vbat 3.0v 时,测量为 2.86V左右。
8 U. r5 {4 q$ W: b/ U6 P同时还发现一些怪现象,aADCxConvertedData[0],明显比后续的采集值小很多。
1 \5 `' U5 T% X# x3 z哪里操作有问题?) x5 S, C$ I2 E  `- ^( Y: X/ V6 y

# X" l" w2 |, }* W; ]* H; U/ l2 O* i/ ]+ E

原理图

原理图
% |9 t- }; z8 ^7 v2 M" [4 B

  p  _  M* F- e. H1 [/ ]/* Definition of ADCx conversions data table size */$ ?) L; r+ G" J/ h0 Q
#define ADC_CONVERTED_DATA_BUFFER_SIZE   ((uint32_t)  10)   /* Size of array aADCxConvertedData[] */
7 K. b, e* Y  _3 H+ Y, m+ J0 Gstatic uint16_t   aADCxConvertedData[ADC_CONVERTED_DATA_BUFFER_SIZE];, x( j' o0 Y7 r8 q+ b9 O* S
/**  k) r! Q1 c. N2 u( p
  * @brief  Conversion complete callback in non-blocking mode.. t' h/ [1 K# f; J
  * @param hadc ADC handle
$ B9 D" c- [) {/ ^; D) v9 o  * @retval None
5 I6 K2 n- t" O8 T+ x; Q  */7 @4 d& M# E- u1 r8 L" ]
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
- R. ~; ?( ]3 L9 c- ]{
; m. c4 Z( j# Z' p  A+ ~  /* Prevent unused argument(s) compilation warning */
5 [# V: f: Y9 [7 U; G; U- y# c5 i        if(BinarySem_ADCHandle)
7 ?. D# `" D  n1 w/ @. j8 v                osSemaphoreRelease(BinarySem_ADCHandle);( R% X; h9 U) H9 ^/ {

+ i+ v( {( [4 c$ N- s  /* NOTE : This function should not be modified. When the callback is needed,
$ G- U! L9 D$ D            function HAL_ADC_ConvCpltCallback must be implemented in the user file.; i9 N# i, Z7 z6 H( p6 p
   */; T# U; _2 B- {
}
1 t/ a$ w. V1 k$ w* O/**" a) o8 u! L% T9 b) p- h
  * @brief  测量电池电压
3 f; Y* ]+ d7 o9 v  * @param[IN]0 h4 A5 q, U8 d+ Y! n$ J7 W4 e
  * @retval None: Z# q1 F# x8 a
  */
6 Y; y( X6 a% z* Xuint32_t getBattaryTemperatureMeasure(uint8_t bat_flag)  w2 y) C0 @9 b1 @
{
" T* Y* w( k; w+ b* z5 v& \          ADC_ChannelConfTypeDef sConfig = {0};% n7 U- B9 [$ k0 Q
$ A, m* `: S4 K  R; K1 U. [$ b
          memset(aADCxConvertedData,0,sizeof(aADCxConvertedData));
6 x: f' k, a+ M, f6 {          /* ### - 1 - Initialize ADC peripheral #################################### */
+ h/ N* ^3 v! n  N0 L7 i          hadc1.Instance          = ADC1;$ E" e& u8 d' K/ {% N
          if (HAL_ADC_DeInit(&hadc1) != HAL_OK), \* T0 D* l/ B" C* q
          {" \; Q1 A8 A" _  |- |
            /* ADC de-initialization Error */
, Y5 s+ P2 U0 m( p* l  z            Error_Handler();
5 q# ]  k$ R$ g& |: l" A          }
( R3 n4 _# b7 K  f5 u- }0 N3 q5 G& X& g( C2 r! E/ s  Z
          hadc1.Init.ClockPrescaler        = ADC_CLOCK_SYNC_PCLK_DIV1;      /* Synchronous clock mode, input ADC clock divided by 2*/
8 z/ j  }4 \: w" j: L; o          hadc1.Init.Resolution            = ADC_RESOLUTION_12B;            /* 12-bit resolution for converted data */
' n9 D. _0 Q% I* ~          hadc1.Init.DataAlign             = ADC_DATAALIGN_RIGHT;           /* Right-alignment for converted data */# c& q9 i- c9 N3 |
          hadc1.Init.ScanConvMode          = DISABLE;                       /* Sequencer disabled (ADC conversion on only 1 channel: channel set on rank 1) */9 L* q# O9 z8 U
          hadc1.Init.EOCSelection          = ADC_EOC_SINGLE_CONV;           /* EOC flag picked-up to indicate conversion end */" l9 A' ?! z' ^& Q4 I! D
          hadc1.Init.LowPowerAutoWait      = DISABLE;                       /* Auto-delayed conversion feature disabled */* w5 T+ b! u# d2 v# d, o
          hadc1.Init.ContinuousConvMode    = ENABLE;                        /* Continuous mode enabled (automatic conversion restart after each conversion) */4 g' s: Y* {" i  s) Z1 G
          hadc1.Init.NbrOfConversion       = 1;                             /* Parameter discarded because sequencer is disabled */
) L$ r, a. d. c. S  p          hadc1.Init.DiscontinuousConvMode = DISABLE;                       /* Parameter discarded because sequencer is disabled */1 c- o, T  K' t* ?# t+ l
          hadc1.Init.NbrOfDiscConversion   = 1;                             /* Parameter discarded because sequencer is disabled */
2 a9 S$ z) P) \! W          hadc1.Init.ExternalTrigConv      = ADC_SOFTWARE_START;            /* Software start to trig the 1st conversion manually, without external event */
$ @8 X7 G/ v8 e& O          hadc1.Init.ExternalTrigConvEdge  = ADC_EXTERNALTRIGCONVEDGE_NONE; /* Parameter discarded because software trigger chosen */$ f! ]6 S1 ^6 ?
          hadc1.Init.DMAContinuousRequests = ENABLE;                        /* ADC DMA continuous request to match with DMA circular mode */; _+ b0 @$ z2 g
          hadc1.Init.Overrun               = ADC_OVR_DATA_OVERWRITTEN;      /* DR register is overwritten with the last conversion result in case of overrun */
/ j" d1 B$ [3 ^& B5 e4 g$ Q8 s" ^! O          hadc1.Init.OversamplingMode      = DISABLE;                       /* No oversampling */+ k: f0 d) O; T5 E  \6 u7 @5 t2 z
          /* Initialize ADC peripheral according to the passed parameters */
0 h% b) |0 |& w# |/ ^) y) e' A+ r/ }          if (HAL_ADC_Init(&hadc1) != HAL_OK)
5 X; W# R* ]! {6 G6 S          {
1 {; o* L* _4 c            Error_Handler();
& f. \& C/ {5 R% C. ~; ]5 x          }
+ c# k' ?- Z3 q3 ], I' D/ |7 s% U9 F

3 s/ N6 T% i! T5 r) `9 M, O; s9 T1 I          /* ### - 2 - Start calibration ############################################ */
! }$ g. P# r7 M/ x; y          if (HAL_ADCEx_Calibration_Start(&hadc1, ADC_SINGLE_ENDED) !=  HAL_OK)
& K% }; ~9 o* [$ `- j! j% ~4 L          {0 q1 G7 _4 ?, f9 f3 K
            Error_Handler();9 }" C8 U& v$ P5 \# ~0 y: i
          }
, D/ ]. x% w/ B" ]9 f4 R
) Y# z$ H3 T% k9 X$ H6 E* D; {          /* ### - 3 - Channel configuration ######################################## */1 s6 A* c: G& g! l. y: D
          sConfig.Channel      = ADC_CHANNEL_VBAT;                /* Sampled channel number */
, Z3 x4 v+ c& C. N. p3 B          sConfig.Rank         = ADC_REGULAR_RANK_1;          /* Rank of sampled channel number ADCx_CHANNEL */% O) R3 B6 T+ [, V
          sConfig.SamplingTime = ADC_SAMPLETIME_6CYCLES_5;   /* Sampling time (number of clock cycles unit) */
5 E5 n/ n  t8 F$ b# d" ~, Y          sConfig.SingleDiff   = ADC_SINGLE_ENDED;            /* Single-ended input channel */( h/ H, ?2 l: k' w; n8 ]! U& P
          sConfig.OffsetNumber = ADC_OFFSET_NONE;             /* No offset subtraction */, u3 g3 p, q  T3 t
          sConfig.Offset = 0;                                 /* Parameter discarded because offset correction is disabled */9 b- X1 D0 s9 T
          if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
& J6 n) S- P5 [" E6 |! f! H! z          {
. C4 T: ^' }6 u4 j' b7 c            Error_Handler();
- F0 y9 f- ]8 x7 J          }
( I) ~5 W& u/ s) H( I6 V( K" p
$ G* P% m( b/ z5 d2 e$ X          /* ### - 4 - Start conversion in DMA mode ################################# */
4 x, i  j% f9 v4 M          if (HAL_ADC_Start_DMA(&hadc1,2 ~+ C2 O- f/ u: i  p$ G  i/ D
                                (uint32_t *)aADCxConvertedData,
  {: z2 I$ O1 P. x                                ADC_CONVERTED_DATA_BUFFER_SIZE
" g! \6 F+ X) m% B3 `                               ) != HAL_OK)
  |1 l0 ?9 _8 J9 o          {$ V: K5 a% K1 N  b2 I' L. p& r4 }
            Error_Handler();
+ E3 c/ s) j# s! U% A          }" l% {) n6 D' M" q
          uint32_t ret = 0 ;& j) V6 l5 _9 I/ p
          if(osOK == osSemaphoreWait(BinarySem_ADCHandle,10))
5 K" ]- S& D$ V' i          {
% |$ v& D9 ?: L% I! L' U' I                  uint32_t adc_val = 0;//aADCxConvertedData[1];$ W- {2 i: W/ `- p& @$ _
                  for (int i = ADC_CONVERTED_DATA_BUFFER_SIZE/2 ; i < ADC_CONVERTED_DATA_BUFFER_SIZE ; i++)
9 r. l( Q. V2 `9 C. t# u                  {
, \' a: w% u) a                          adc_val += aADCxConvertedData;. |9 O) ?: L' Q. E' o: p$ i! b
                  }
+ k7 B" j* c7 a+ R$ D+ [                  adc_val = adc_val  / (ADC_CONVERTED_DATA_BUFFER_SIZE/2);
, P5 r5 Y" P5 |! y                  ret = __HAL_ADC_CALC_DATA_TO_VOLTAGE(2500,adc_val,ADC_RESOLUTION_12B);' Q( N, Z2 K$ h- _4 ~1 L6 |9 z
                  ret *= 3;
' R0 {5 U7 q3 U, Z" B          }' c$ z- B( Z1 d, K

9 S2 z5 b" [/ b7 w          return (ret);( M3 \2 i" W5 y2 s5 Y. s' |
}
3 f2 i  j4 F0 H% h  ?
, o$ ^! p, L  F  h6 j* u
, ^: f6 @. n" ~
收藏 评论1 发布时间:2020-5-11 13:08

举报

1个回答
zhaoxuji-239467 回答时间:2020-5-14 09:13:46
通过两天调试,讲采样周期设置长一些,比较接近了。要大于 ADC_SAMPLETIME_12CYCLES_5  。技术支持一直没响应。

所属标签

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
人形机器人运动控制、感知与智能配电
半导体创新技术与应用方向
EE架构与软件定义汽车
12V/48V 汽车智能配电(SPD)
区域控制单元(ZCU)与分区架构
关注我们
st-img 微信公众号
st-img 手机版