系统电池供电,3.6V电池,经过一个二极管(3A),大概是3.3V到Vbat . 测量电压为2.98V 左右。0 ~" R4 ?+ m7 d 继续采用直流电源供电,当输入Vbat 3.0v 时,测量为 2.86V左右。7 A" L5 F$ i! N5 Z 同时还发现一些怪现象,aADCxConvertedData[0],明显比后续的采集值小很多。; n0 E% e5 f! u6 ~' A; b9 k 哪里操作有问题? 2 S2 @4 O3 O- x, w) B& ^3 N 9 r3 i; }: t/ U9 | åçå¾ /* Definition of ADCx conversions data table size */1 f f% N% k# { #define ADC_CONVERTED_DATA_BUFFER_SIZE ((uint32_t) 10) /* Size of array aADCxConvertedData[] */ static uint16_t aADCxConvertedData[ADC_CONVERTED_DATA_BUFFER_SIZE]; /**8 m/ q1 R3 x1 C2 Z, [* ~ * @brief Conversion complete callback in non-blocking mode. * @param hadc ADC handle * @retval None */% z. t* E8 H8 e: {- v3 _ void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc) { /* Prevent unused argument(s) compilation warning */8 v8 R3 D! ?6 T9 H" I if(BinarySem_ADCHandle) osSemaphoreRelease(BinarySem_ADCHandle);( U5 D, F1 r, H4 d /* NOTE : This function should not be modified. When the callback is needed," P ^9 |, W' d& j/ ~0 V function HAL_ADC_ConvCpltCallback must be implemented in the user file. */) G% \8 k l5 `/ n1 V1 i } /** * @brief 测量电池电压 * @param[IN] * @retval None */' Q' P4 ]* F2 L/ q$ e, v uint32_t getBattaryTemperatureMeasure(uint8_t bat_flag) { ADC_ChannelConfTypeDef sConfig = {0}; memset(aADCxConvertedData,0,sizeof(aADCxConvertedData));: R7 k9 W- C8 `6 l! d7 z' Y /* ### - 1 - Initialize ADC peripheral #################################### */ hadc1.Instance = ADC1;5 [. j( G% s8 f7 l7 T& L! E2 |! ? if (HAL_ADC_DeInit(&hadc1) != HAL_OK)2 M; `/ {8 M8 x {. }& H' ^% T' R1 S; X /* ADC de-initialization Error */ J* I. L# r3 H7 @+ O3 Y. ^3 U Error_Handler();+ S3 D9 I( ^1 U }* h2 O6 h" q# N' v+ t 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 */7 u" c) b8 T4 j hadc1.Init.LowPowerAutoWait = DISABLE; /* Auto-delayed conversion feature disabled */9 l6 X& d- E$ D o$ ]; g. ^ U+ r hadc1.Init.ContinuousConvMode = ENABLE; /* Continuous mode enabled (automatic conversion restart after each conversion) */ hadc1.Init.NbrOfConversion = 1; /* Parameter discarded because sequencer is disabled */+ j9 m* J3 A) q! t hadc1.Init.DiscontinuousConvMode = DISABLE; /* Parameter discarded because sequencer is disabled */ 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 */) X" X3 K. c* Y% w hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; /* Parameter discarded because software trigger chosen */2 ?6 s, i E; M hadc1.Init.DMAContinuousRequests = ENABLE; /* ADC DMA continuous request to match with DMA circular mode */ 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 */ if (HAL_ADC_Init(&hadc1) != HAL_OK)+ n9 `8 j7 z! a6 |8 M" t { Error_Handler(); }$ |+ A& O2 X3 O" O3 v 7 T/ |1 C2 d; C6 R /* ### - 2 - Start calibration ############################################ */ if (HAL_ADCEx_Calibration_Start(&hadc1, ADC_SINGLE_ENDED) != HAL_OK)' d4 X- k' n& X { Error_Handler(); U4 ~ \3 Z" r) J1 p }' D; R1 N% ~ Z$ J6 w) l6 x * ]8 _& t6 W3 b! p8 R+ ]3 E /* ### - 3 - Channel configuration ######################################## */" \2 P; G$ r |' b sConfig.Channel = ADC_CHANNEL_VBAT; /* Sampled channel number */ sConfig.Rank = ADC_REGULAR_RANK_1; /* Rank of sampled channel number ADCx_CHANNEL */ sConfig.SamplingTime = ADC_SAMPLETIME_6CYCLES_5; /* Sampling time (number of clock cycles unit) */ sConfig.SingleDiff = ADC_SINGLE_ENDED; /* Single-ended input channel */: k/ H6 ?7 ?: p: z2 E* D* D0 e3 W! Y 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();( T4 x0 W# U( R9 r- g } /* ### - 4 - Start conversion in DMA mode ################################# */ {& m* j0 O- D: f# o! X% a if (HAL_ADC_Start_DMA(&hadc1, (uint32_t *)aADCxConvertedData, ADC_CONVERTED_DATA_BUFFER_SIZE. x) t5 |7 ^2 d: y; u ) != HAL_OK) {1 u O6 V2 M4 T- _3 {2 j% n Error_Handler();5 _8 u; ~: T! L5 x2 f$ K } uint32_t ret = 0 ;5 C% N. m: u4 f. s5 S if(osOK == osSemaphoreWait(BinarySem_ADCHandle,10)) {" U" i2 r* [/ G+ S. L uint32_t adc_val = 0;//aADCxConvertedData[1];% C5 ~) [$ Q! _ for (int i = ADC_CONVERTED_DATA_BUFFER_SIZE/2 ; i < ADC_CONVERTED_DATA_BUFFER_SIZE ; i++)$ R: G9 k$ e( V ]/ b- Z { adc_val += aADCxConvertedData;" [/ D5 N, u9 b }# L7 Z3 t+ S, i9 B$ V6 M3 z2 k adc_val = adc_val / (ADC_CONVERTED_DATA_BUFFER_SIZE/2);" w; M" c% `' x) H* i3 x ret = __HAL_ADC_CALC_DATA_TO_VOLTAGE(2500,adc_val,ADC_RESOLUTION_12B);! j/ { h; t! f% O ret *= 3;8 S1 \$ ?" Q# O/ I9 o; L w; e }" F' B& s2 T* G& Z- V" w! I/ r & q3 R H! N9 H# a6 u4 ^' Q* E$ @ return (ret); } 5 N5 [0 j' u; }% ^+ x$ o, T/ a( ] |
通过两天调试,讲采样周期设置长一些,比较接近了。要大于 ADC_SAMPLETIME_12CYCLES_5 。技术支持一直没响应。 |