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

stm32l431RC ADC 采集Vbat 不准的问题

[复制链接]
zhaoxuji-239467 发布时间:2020-5-11 13:08
系统电池供电,3.6V电池,经过一个二极管(3A),大概是3.3V到Vbat .
6 l. J# B) M! z+ f测量电压为2.98V 左右。
/ ^0 f* g1 o+ r* j2 c# P8 J继续采用直流电源供电,当输入Vbat 3.0v 时,测量为 2.86V左右。
: r5 n, h) U$ n+ g% G同时还发现一些怪现象,aADCxConvertedData[0],明显比后续的采集值小很多。! S5 I! S& C, u# X5 z
哪里操作有问题?- m; F! O7 K! g% S1 R3 q

7 k8 V' v: c; B) a! J8 a* `$ G( k# Y

原理图

原理图

; b3 m) y  M7 N4 s! |  R2 f
0 Q, ~, `2 \: p+ E: F  q1 h% |/* Definition of ADCx conversions data table size */
# S  v. \5 }3 p! X( z9 t, b#define ADC_CONVERTED_DATA_BUFFER_SIZE   ((uint32_t)  10)   /* Size of array aADCxConvertedData[] */
( m5 r: x! Y: p1 z7 Xstatic uint16_t   aADCxConvertedData[ADC_CONVERTED_DATA_BUFFER_SIZE];0 u: r3 G& P1 {
/**6 W. o1 y4 H' o9 l4 ]0 @
  * @brief  Conversion complete callback in non-blocking mode." Q# G- d3 }% ]# {
  * @param hadc ADC handle9 c1 ]  g7 g8 v; H0 p7 d4 {8 j% U
  * @retval None
4 D0 i4 ?+ B% y  */: F  e! F0 c& {$ K# c0 |
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)/ k1 ^4 K7 V1 x" _4 \, O8 E
{" [3 O1 Y7 B5 n& L* H
  /* Prevent unused argument(s) compilation warning *// ^' h1 w. i% _' G' A' z5 k
        if(BinarySem_ADCHandle)
5 W( Q# m& ?+ I0 F                osSemaphoreRelease(BinarySem_ADCHandle);
# a# y4 ^9 v$ H6 w) b2 y" f9 w& _1 i; |& q/ ^( f. ]# f
  /* NOTE : This function should not be modified. When the callback is needed,5 [. G$ Q4 W6 n6 ^4 O- y! b
            function HAL_ADC_ConvCpltCallback must be implemented in the user file.
/ s5 N4 z' s1 V: G* g8 e$ b) {   */
( b7 [7 C; |' }+ |3 S1 R}4 o4 g- d4 I! a7 V2 {
/**, ~* e* ^/ ~( a* l* @7 \+ `
  * @brief  测量电池电压
8 m9 k# z4 g! f' [! m" g  * @param[IN], T2 p, m; A( U$ n- e
  * @retval None
+ R2 t$ D& l/ W' O! ]- z3 f  */5 x4 `1 ^4 ?! ~5 w$ H
uint32_t getBattaryTemperatureMeasure(uint8_t bat_flag)8 p2 E2 a! I! N. w. f7 I# h
{
/ I7 V$ E* w; B+ A+ e* W          ADC_ChannelConfTypeDef sConfig = {0};
# d, |: S+ X6 s) W
3 ]! t- L9 w, J          memset(aADCxConvertedData,0,sizeof(aADCxConvertedData));
6 [8 D$ Z% ^) T: }! @          /* ### - 1 - Initialize ADC peripheral #################################### */2 Y/ T3 N" c$ }
          hadc1.Instance          = ADC1;
" n& q: J& k1 M. U( ?          if (HAL_ADC_DeInit(&hadc1) != HAL_OK)+ x. Y5 R: |; l4 d+ H7 @+ I
          {; G0 c7 ~) s9 P: D! Z5 w9 Q/ S, i% `
            /* ADC de-initialization Error */  {& z2 z5 W2 q5 P8 y
            Error_Handler();, B$ n3 C" n7 R, v/ H
          }
/ }5 B9 Z) s4 S
" K& ]! ~7 \1 X2 o) T( O          hadc1.Init.ClockPrescaler        = ADC_CLOCK_SYNC_PCLK_DIV1;      /* Synchronous clock mode, input ADC clock divided by 2*/
# n' x; b& L1 M: h9 u          hadc1.Init.Resolution            = ADC_RESOLUTION_12B;            /* 12-bit resolution for converted data */
. J/ E' t2 ~4 {/ S$ [4 `9 j* r          hadc1.Init.DataAlign             = ADC_DATAALIGN_RIGHT;           /* Right-alignment for converted data */
/ i/ W  u& n1 g3 Y1 ^          hadc1.Init.ScanConvMode          = DISABLE;                       /* Sequencer disabled (ADC conversion on only 1 channel: channel set on rank 1) */
. z, N9 w0 @* g7 `& I4 B. m          hadc1.Init.EOCSelection          = ADC_EOC_SINGLE_CONV;           /* EOC flag picked-up to indicate conversion end */1 F5 b1 R5 S* L" n; c: [- u6 }' f& U. }
          hadc1.Init.LowPowerAutoWait      = DISABLE;                       /* Auto-delayed conversion feature disabled */
# V+ C* C# _0 R- z% l6 M& C* j          hadc1.Init.ContinuousConvMode    = ENABLE;                        /* Continuous mode enabled (automatic conversion restart after each conversion) */
; \8 X' h/ z0 H9 N0 {          hadc1.Init.NbrOfConversion       = 1;                             /* Parameter discarded because sequencer is disabled */4 _- e* ~* ?% {. z3 b. [
          hadc1.Init.DiscontinuousConvMode = DISABLE;                       /* Parameter discarded because sequencer is disabled */0 i9 o% M; W  c  y2 S7 ]
          hadc1.Init.NbrOfDiscConversion   = 1;                             /* Parameter discarded because sequencer is disabled */
2 r0 D: ^* [/ h$ Z1 p' l. ^# ?          hadc1.Init.ExternalTrigConv      = ADC_SOFTWARE_START;            /* Software start to trig the 1st conversion manually, without external event */
* T1 r8 a" N/ q/ u6 w* R          hadc1.Init.ExternalTrigConvEdge  = ADC_EXTERNALTRIGCONVEDGE_NONE; /* Parameter discarded because software trigger chosen */
( B- x0 J, ^7 O, [- D0 l) H0 t6 a1 J          hadc1.Init.DMAContinuousRequests = ENABLE;                        /* ADC DMA continuous request to match with DMA circular mode */
% Y$ G0 V  I3 a+ ]5 T2 K' Z          hadc1.Init.Overrun               = ADC_OVR_DATA_OVERWRITTEN;      /* DR register is overwritten with the last conversion result in case of overrun */4 p; }+ }7 ]  B$ A) a
          hadc1.Init.OversamplingMode      = DISABLE;                       /* No oversampling */+ |! ?5 J* e: `9 K! q+ e
          /* Initialize ADC peripheral according to the passed parameters */* U* `8 D. D* c/ ?$ ?6 a
          if (HAL_ADC_Init(&hadc1) != HAL_OK)
, ]% j; n+ g8 O8 h( Y0 S  J8 T          {/ L9 K  l- u, g6 M- J
            Error_Handler();
4 K9 V3 h4 ?3 }  N3 K* l, h$ X          }, `" l( n7 j* X. T, S
: p6 u, i5 D7 g4 e8 i
, H) M* G; D& c/ t6 J4 D
          /* ### - 2 - Start calibration ############################################ */0 a) `& ~. m9 `6 `: d; N+ R% {
          if (HAL_ADCEx_Calibration_Start(&hadc1, ADC_SINGLE_ENDED) !=  HAL_OK)
6 e( R. H' v2 _. G2 E          {
4 y) E- F# I4 t: E# F; r            Error_Handler();
: p1 r8 ]  v$ Y: E: c          }
! H+ Q" x# E) J# M$ e% t- C
5 y1 X2 Q8 _8 l: F/ m) ~          /* ### - 3 - Channel configuration ######################################## */" R/ {# H$ V; F5 q, W
          sConfig.Channel      = ADC_CHANNEL_VBAT;                /* Sampled channel number */
; _# x8 W, c: F, a          sConfig.Rank         = ADC_REGULAR_RANK_1;          /* Rank of sampled channel number ADCx_CHANNEL */
1 u5 P: J6 X; j( u0 t          sConfig.SamplingTime = ADC_SAMPLETIME_6CYCLES_5;   /* Sampling time (number of clock cycles unit) */
! Z6 O/ b) k. p8 f8 B          sConfig.SingleDiff   = ADC_SINGLE_ENDED;            /* Single-ended input channel */
) {$ K+ V6 w5 A9 n4 c& U" c# [          sConfig.OffsetNumber = ADC_OFFSET_NONE;             /* No offset subtraction */
% z; C/ R( v' }$ v4 O" U) J          sConfig.Offset = 0;                                 /* Parameter discarded because offset correction is disabled */
7 n7 G6 z+ ?9 j4 _9 r( J% Q; L          if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
4 q2 H$ A  I: u( y) _' w( ]          {( q: G/ d4 E2 W
            Error_Handler();6 c/ g0 d$ v( W) z4 G1 f
          }
: C% P3 b  m) [7 L, D( s  v" ?/ f
: S1 t. o& b; B$ A' t% ~% O          /* ### - 4 - Start conversion in DMA mode ################################# */3 `; Z* s1 S/ C2 _% J& f5 O
          if (HAL_ADC_Start_DMA(&hadc1,
4 _. u! `' t3 a, O8 O) i                                (uint32_t *)aADCxConvertedData,5 h% W+ o$ h) z
                                ADC_CONVERTED_DATA_BUFFER_SIZE
  T; Z( \- R( V' z                               ) != HAL_OK)
5 [0 v5 i& T" e+ u/ B4 Q          {8 S- Y, E8 o% u* F! M/ G: e
            Error_Handler();! @4 {4 x% Z% p& ~
          }
# R4 H4 L* p, [. j' y          uint32_t ret = 0 ;8 Y+ X- X4 d0 J6 _$ v5 F
          if(osOK == osSemaphoreWait(BinarySem_ADCHandle,10))( D$ x% t1 ~( O7 ]3 i
          {( K* ^. S1 r1 e
                  uint32_t adc_val = 0;//aADCxConvertedData[1];
, e" a' \& N- T& N! H                  for (int i = ADC_CONVERTED_DATA_BUFFER_SIZE/2 ; i < ADC_CONVERTED_DATA_BUFFER_SIZE ; i++)9 D3 \: M& j1 O9 d* u
                  {& B# u: ?/ Q5 Q0 G  z& _8 W
                          adc_val += aADCxConvertedData;
" b6 L" {# j1 d                  }$ Y% z0 d, p+ \7 a" T6 q9 f
                  adc_val = adc_val  / (ADC_CONVERTED_DATA_BUFFER_SIZE/2);) j: b5 g; n& f5 r: i
                  ret = __HAL_ADC_CALC_DATA_TO_VOLTAGE(2500,adc_val,ADC_RESOLUTION_12B);4 ~1 d8 A) L9 R  K) x' N
                  ret *= 3;$ ]- [3 P2 M, a+ w
          }
! A, c/ d7 X6 ]9 e1 Z7 |6 X6 z6 a3 J) g1 U$ z
          return (ret);. p6 s! p) s9 }' D1 d/ D3 `4 Z0 O6 S
}
) c+ f2 I/ r2 o) h( F
, ~) N1 s* r) I$ b4 T4 g' r+ R" i. ]' X% ?, f* n5 T# G
收藏 评论1 发布时间:2020-5-11 13:08

举报

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

所属标签

相似分享

关于意法半导体
我们是谁
投资者关系
意法半导体可持续发展举措
创新和工艺
招聘信息
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
关注我们
st-img 微信公众号
st-img 手机版