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

stm32l431RC ADC 采集Vbat 不准的问题

[复制链接]
zhaoxuji-239467 发布时间:2020-5-11 13:08
系统电池供电,3.6V电池,经过一个二极管(3A),大概是3.3V到Vbat . / Y* L% i' ~- D3 P& b6 W$ C4 c
测量电压为2.98V 左右。
0 X$ E2 r* U& ?% G* Q) w继续采用直流电源供电,当输入Vbat 3.0v 时,测量为 2.86V左右。
; W! C$ m) l" h5 k, p' Y同时还发现一些怪现象,aADCxConvertedData[0],明显比后续的采集值小很多。! B6 K: n9 x4 @& ^4 P9 i! x6 z
哪里操作有问题?
. f/ ~5 ?$ M( ^7 U! b9 ~# [" T' r% K
& d; m% F. |; y4 ^
5 D$ I: [9 A+ b. w) B% W# s

原理图

原理图
# M8 y# O) M  M" ^
" i# H8 t2 P( v
/* Definition of ADCx conversions data table size */
* E# B! ^0 F1 U. S* z/ h( u* V4 G#define ADC_CONVERTED_DATA_BUFFER_SIZE   ((uint32_t)  10)   /* Size of array aADCxConvertedData[] */, s4 Y/ v- Z" |" Y0 ~& h
static uint16_t   aADCxConvertedData[ADC_CONVERTED_DATA_BUFFER_SIZE];
+ u2 w$ J6 L3 z. ]4 `- v, _/**
: N( C' {, v) K. ^0 e. `$ B( c! @+ {, a  * @brief  Conversion complete callback in non-blocking mode.+ X$ a( x' t& O
  * @param hadc ADC handle
( S2 A8 c6 w/ s# w  H& [( @  * @retval None7 M+ g- r- P! i& Z( p8 w: H
  */5 l8 o: [' f( U2 v* S* u  }
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)5 G" S4 [+ L: f. |% D3 j
{. P, q6 u6 x2 S4 _: J( A/ W
  /* Prevent unused argument(s) compilation warning */% [! z4 {: A0 _! d2 T$ X, S6 q
        if(BinarySem_ADCHandle)% g% r' r# p! E. n4 h/ \3 d( P
                osSemaphoreRelease(BinarySem_ADCHandle);- l( C: h: {% ]* s; X1 O! Y  S
. n; R. e  D* p
  /* NOTE : This function should not be modified. When the callback is needed,
& E8 Q/ B4 A; }3 t. ?2 l            function HAL_ADC_ConvCpltCallback must be implemented in the user file.0 y7 l  K4 Q& a4 B4 x5 a7 M
   */" ?0 z& f) X9 n' J8 U
}! |- N1 Z$ ]8 Y7 w/ l6 C$ b1 C: F
/**, L$ ?' a5 I; k% t2 r& I
  * @brief  测量电池电压  d1 R; w/ x) N. [# |
  * @param[IN]  e5 l* n" _0 `
  * @retval None% [  B  u  J* V
  */
+ ^6 U# ]7 q, s+ f0 U2 E9 P: s1 P* Duint32_t getBattaryTemperatureMeasure(uint8_t bat_flag)$ |$ F' Z! v/ {
{
  r' b8 d) k# x, `/ L          ADC_ChannelConfTypeDef sConfig = {0};
& `6 q+ ?( B- I; E4 e" |( y( D( P9 B% F2 h/ n! s
          memset(aADCxConvertedData,0,sizeof(aADCxConvertedData));
$ D" B2 M# X% ~. f& p3 A7 L/ d          /* ### - 1 - Initialize ADC peripheral #################################### */) Q+ u; w* d% |, d
          hadc1.Instance          = ADC1;$ K# }# {3 B* W$ b4 V
          if (HAL_ADC_DeInit(&hadc1) != HAL_OK)
8 [5 [8 {) G! q3 Y+ j, ~' Y. f+ F' J          {
; D$ S" ]3 C, l            /* ADC de-initialization Error */
; a1 X, B; J4 I0 W8 |1 {6 @* g+ n            Error_Handler();
9 L! {' Y7 i8 x7 f          }
5 B2 Q9 z5 N, N: B" r1 T- t4 g& u; }' N
          hadc1.Init.ClockPrescaler        = ADC_CLOCK_SYNC_PCLK_DIV1;      /* Synchronous clock mode, input ADC clock divided by 2*/
3 I  t; _9 u! ]. }" T/ n9 E          hadc1.Init.Resolution            = ADC_RESOLUTION_12B;            /* 12-bit resolution for converted data */
4 t7 l, E8 r. E! J0 U9 T6 J. y          hadc1.Init.DataAlign             = ADC_DATAALIGN_RIGHT;           /* Right-alignment for converted data */
- k7 w% P8 F1 \# s          hadc1.Init.ScanConvMode          = DISABLE;                       /* Sequencer disabled (ADC conversion on only 1 channel: channel set on rank 1) */
2 F* ^7 {  V, w1 Y$ e) t5 y          hadc1.Init.EOCSelection          = ADC_EOC_SINGLE_CONV;           /* EOC flag picked-up to indicate conversion end */
8 w1 m" u1 s. j# E2 X( s          hadc1.Init.LowPowerAutoWait      = DISABLE;                       /* Auto-delayed conversion feature disabled */
5 m. E- X3 C/ Y* [. |* X4 |          hadc1.Init.ContinuousConvMode    = ENABLE;                        /* Continuous mode enabled (automatic conversion restart after each conversion) */' ~  J' r0 A7 \0 p
          hadc1.Init.NbrOfConversion       = 1;                             /* Parameter discarded because sequencer is disabled */
. R: u0 d, h: }. M1 V- g$ i          hadc1.Init.DiscontinuousConvMode = DISABLE;                       /* Parameter discarded because sequencer is disabled */) ^& x) ]8 f( J3 m, A# Q' _; y  O' y
          hadc1.Init.NbrOfDiscConversion   = 1;                             /* Parameter discarded because sequencer is disabled */
  W4 b3 E% b+ Z9 q' {          hadc1.Init.ExternalTrigConv      = ADC_SOFTWARE_START;            /* Software start to trig the 1st conversion manually, without external event */
* s' ]8 {) p7 m/ @8 w. |3 X  Q          hadc1.Init.ExternalTrigConvEdge  = ADC_EXTERNALTRIGCONVEDGE_NONE; /* Parameter discarded because software trigger chosen */; ^: I; o- S0 T8 s
          hadc1.Init.DMAContinuousRequests = ENABLE;                        /* ADC DMA continuous request to match with DMA circular mode */) u: n% c9 X$ n; s& |/ O
          hadc1.Init.Overrun               = ADC_OVR_DATA_OVERWRITTEN;      /* DR register is overwritten with the last conversion result in case of overrun */
7 l1 D3 I2 k; J! S5 s          hadc1.Init.OversamplingMode      = DISABLE;                       /* No oversampling */
. M& o+ Q% e$ i  w          /* Initialize ADC peripheral according to the passed parameters */$ t/ y3 Y) V9 P/ r; v; P" O4 U
          if (HAL_ADC_Init(&hadc1) != HAL_OK)
% h( `, V' c: D3 v: ]          {2 h; T) y5 _& D4 w
            Error_Handler();& p0 C3 s: q# D3 i5 P* l
          }/ h( S2 C+ V( A2 e7 J
" \" C$ R- i' I1 I) F

+ |1 S* r9 m. |7 |          /* ### - 2 - Start calibration ############################################ */# W  \5 L& i$ G" X! C+ }: C
          if (HAL_ADCEx_Calibration_Start(&hadc1, ADC_SINGLE_ENDED) !=  HAL_OK)
/ t/ W& x. H, z: c& a) D/ {          {% ]2 J- K  D. S3 w* m( }/ A6 H
            Error_Handler();) }/ \5 |2 Z; s9 x
          }) x8 v2 k3 x" o2 K

% f2 r8 [% d4 s; G0 M, O          /* ### - 3 - Channel configuration ######################################## */
7 v5 F5 w# L0 g1 I, t5 r          sConfig.Channel      = ADC_CHANNEL_VBAT;                /* Sampled channel number */* f7 G$ o- J( V
          sConfig.Rank         = ADC_REGULAR_RANK_1;          /* Rank of sampled channel number ADCx_CHANNEL */7 i8 l! m5 N/ N% y0 s' t% s" q1 Y
          sConfig.SamplingTime = ADC_SAMPLETIME_6CYCLES_5;   /* Sampling time (number of clock cycles unit) */
; v: {: H* d9 Q& y. @' v5 \! C8 q          sConfig.SingleDiff   = ADC_SINGLE_ENDED;            /* Single-ended input channel */) ]/ ^* \$ ?, C+ U# B
          sConfig.OffsetNumber = ADC_OFFSET_NONE;             /* No offset subtraction */
/ {: V( n2 O- Z+ B$ V2 w' H4 K          sConfig.Offset = 0;                                 /* Parameter discarded because offset correction is disabled */
% ~: F# o' `! B: l; Z0 ~4 z; b          if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
. @5 k- T$ y) @9 v5 G: X          {
* c/ h* S3 n* `" b            Error_Handler();
/ x9 T1 J8 j3 c* C' L$ w' m; _* K          }. b1 ~6 t- ^2 L1 r3 R$ E% B& H

6 D6 n8 Y) X! ~; H* P          /* ### - 4 - Start conversion in DMA mode ################################# */
8 s  ~- |0 x* ?, {- ?4 d          if (HAL_ADC_Start_DMA(&hadc1,6 {) K) u5 a9 E- c3 Q. }4 f
                                (uint32_t *)aADCxConvertedData,
7 _+ H& O: I" A4 T6 C                                ADC_CONVERTED_DATA_BUFFER_SIZE$ Q1 I/ D4 K' L2 A1 Q9 K
                               ) != HAL_OK)) D9 K9 K$ C1 g# i
          {
$ G) G6 q1 B& O& t: Y+ }            Error_Handler();8 H5 ]; s, m, A/ @. m& ]! h
          }
5 \" J7 o" ~4 j" C% s3 q          uint32_t ret = 0 ;. H0 y1 E  `. g/ v* x) i
          if(osOK == osSemaphoreWait(BinarySem_ADCHandle,10)). |7 m* F" B6 `5 w) o
          {+ {0 l4 ~4 P$ N# U. t; _, E" P
                  uint32_t adc_val = 0;//aADCxConvertedData[1];
* _& r' s* s1 p. i7 G2 |! p2 e% ?2 k                  for (int i = ADC_CONVERTED_DATA_BUFFER_SIZE/2 ; i < ADC_CONVERTED_DATA_BUFFER_SIZE ; i++)
( [& i" U3 h2 j: t) N                  {5 R  }: t8 y% z9 M8 g% F7 M
                          adc_val += aADCxConvertedData;
, l  L! s- `  f+ g5 t                  }/ ~% }2 U8 _+ I  i/ H7 t/ [# G
                  adc_val = adc_val  / (ADC_CONVERTED_DATA_BUFFER_SIZE/2);# X# D# K  F5 a; S1 y' ~
                  ret = __HAL_ADC_CALC_DATA_TO_VOLTAGE(2500,adc_val,ADC_RESOLUTION_12B);2 H' ^) W6 J) k6 J5 P
                  ret *= 3;
* g& f3 W$ s, V" k2 S# ^+ V' f; ^          }
- u# n, S- j6 \- x) P# m; s6 y) K2 t8 C
          return (ret);5 g2 Q2 M$ X* C: e/ h& u3 E  S
}
7 R9 k: T6 N" M: V( G
5 y- T2 T) `) C: g3 \" f# k! D3 W5 D5 D( \! m: q
收藏 评论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 手机版