系统电池供电,3.6V电池,经过一个二极管(3A),大概是3.3V到Vbat . / Y* L% i' ~- D3 P& b6 W$ C4 c 测量电压为2.98V 左右。 继续采用直流电源供电,当输入Vbat 3.0v 时,测量为 2.86V左右。 同时还发现一些怪现象,aADCxConvertedData[0],明显比后续的采集值小很多。! B6 K: n9 x4 @& ^4 P9 i! x6 z 哪里操作有问题? åçå¾ " i# H8 t2 P( v /* Definition of ADCx conversions data table size */ #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]; /** * @brief Conversion complete callback in non-blocking mode.+ X$ a( x' t& O * @param hadc ADC handle * @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, 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 */ uint32_t getBattaryTemperatureMeasure(uint8_t bat_flag)$ |$ F' Z! v/ { { ADC_ChannelConfTypeDef sConfig = {0}; ( D( P9 B% F2 h/ n! s memset(aADCxConvertedData,0,sizeof(aADCxConvertedData)); /* ### - 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) { /* ADC de-initialization Error */ Error_Handler(); } : B" r1 T- t4 g& u; }' N hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV1; /* Synchronous clock mode, input ADC clock divided by 2*/ hadc1.Init.Resolution = ADC_RESOLUTION_12B; /* 12-bit resolution for converted data */ hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT; /* Right-alignment for converted data */ hadc1.Init.ScanConvMode = DISABLE; /* Sequencer disabled (ADC conversion on only 1 channel: channel set on rank 1) */ hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV; /* EOC flag picked-up to indicate conversion end */ hadc1.Init.LowPowerAutoWait = DISABLE; /* Auto-delayed conversion feature disabled */ 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 */ 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 */ hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START; /* Software start to trig the 1st conversion manually, without external event */ 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 */ hadc1.Init.OversamplingMode = DISABLE; /* No oversampling */ /* 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) {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 /* ### - 2 - Start calibration ############################################ */# W \5 L& i$ G" X! C+ }: C if (HAL_ADCEx_Calibration_Start(&hadc1, ADC_SINGLE_ENDED) != HAL_OK) {% ]2 J- K D. S3 w* m( }/ A6 H Error_Handler();) }/ \5 |2 Z; s9 x }) x8 v2 k3 x" o2 K /* ### - 3 - Channel configuration ######################################## */ 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) */ sConfig.SingleDiff = ADC_SINGLE_ENDED; /* Single-ended input channel */) ]/ ^* \$ ?, C+ U# B sConfig.OffsetNumber = ADC_OFFSET_NONE; /* No offset subtraction */ sConfig.Offset = 0; /* Parameter discarded because offset correction is disabled */ if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) { Error_Handler(); }. b1 ~6 t- ^2 L1 r3 R$ E% B& H /* ### - 4 - Start conversion in DMA mode ################################# */ if (HAL_ADC_Start_DMA(&hadc1,6 {) K) u5 a9 E- c3 Q. }4 f (uint32_t *)aADCxConvertedData, ADC_CONVERTED_DATA_BUFFER_SIZE$ Q1 I/ D4 K' L2 A1 Q9 K ) != HAL_OK)) D9 K9 K$ C1 g# i { Error_Handler();8 H5 ]; s, m, A/ @. m& ]! h } 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]; for (int i = ADC_CONVERTED_DATA_BUFFER_SIZE/2 ; i < ADC_CONVERTED_DATA_BUFFER_SIZE ; i++) {5 R }: t8 y% z9 M8 g% F7 M adc_val += aADCxConvertedData; }/ ~% }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; } ; s6 y) K2 t8 C return (ret);5 g2 Q2 M$ X* C: e/ h& u3 E S } : g3 \" f# k! D3 W5 D5 D( \! m: q |
通过两天调试,讲采样周期设置长一些,比较接近了。要大于 ADC_SAMPLETIME_12CYCLES_5 。技术支持一直没响应。 |