|
系统电池供电,3.6V电池,经过一个二极管(3A),大概是3.3V到Vbat . 测量电压为2.98V 左右。! V* r, l3 [( T+ K# A: P% r 继续采用直流电源供电,当输入Vbat 3.0v 时,测量为 2.86V左右。 同时还发现一些怪现象,aADCxConvertedData[0],明显比后续的采集值小很多。 哪里操作有问题?) x5 S, C$ I2 E `- ^( Y: X/ V6 y * H; U/ l2 O* i/ ]+ E åçå¾
/* 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[] */ static 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 * @retval None */7 @4 d& M# E- u1 r8 L" ] void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc) { /* Prevent unused argument(s) compilation warning */ if(BinarySem_ADCHandle) osSemaphoreRelease(BinarySem_ADCHandle);( R% X; h9 U) H9 ^/ { /* NOTE : This function should not be modified. When the callback is needed, function HAL_ADC_ConvCpltCallback must be implemented in the user file.; i9 N# i, Z7 z6 H( p6 p */; T# U; _2 B- { } /**" a) o8 u! L% T9 b) p- h * @brief 测量电池电压 * @param[IN]0 h4 A5 q, U8 d+ Y! n$ J7 W4 e * @retval None: Z# q1 F# x8 a */ uint32_t getBattaryTemperatureMeasure(uint8_t bat_flag) w2 y) C0 @9 b1 @ { ADC_ChannelConfTypeDef sConfig = {0};% n7 U- B9 [$ k0 Q $ A, m* `: S4 K R; K1 U. [$ b memset(aADCxConvertedData,0,sizeof(aADCxConvertedData)); /* ### - 1 - Initialize ADC peripheral #################################### */ 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 */ Error_Handler(); } 5 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*/ hadc1.Init.Resolution = ADC_RESOLUTION_12B; /* 12-bit resolution for converted data */ 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 */ 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 */ 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 */$ 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 */ 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 */ if (HAL_ADC_Init(&hadc1) != HAL_OK) { Error_Handler(); } ' D/ |7 s% U9 F /* ### - 2 - Start calibration ############################################ */ if (HAL_ADCEx_Calibration_Start(&hadc1, ADC_SINGLE_ENDED) != HAL_OK) {0 q1 G7 _4 ?, f9 f3 K Error_Handler();9 }" C8 U& v$ P5 \# ~0 y: i } /* ### - 3 - Channel configuration ######################################## */1 s6 A* c: G& g! l. y: D sConfig.Channel = ADC_CHANNEL_VBAT; /* Sampled channel number */ 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) */ 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) { Error_Handler(); } /* ### - 4 - Start conversion in DMA mode ################################# */ if (HAL_ADC_Start_DMA(&hadc1,2 ~+ C2 O- f/ u: i p$ G i/ D (uint32_t *)aADCxConvertedData, ADC_CONVERTED_DATA_BUFFER_SIZE ) != HAL_OK) {$ V: K5 a% K1 N b2 I' L. p& r4 } Error_Handler(); }" l% {) n6 D' M" q uint32_t ret = 0 ;& j) V6 l5 _9 I/ p if(osOK == osSemaphoreWait(BinarySem_ADCHandle,10)) { 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++) { adc_val += aADCxConvertedData;. |9 O) ?: L' Q. E' o: p$ i! b } adc_val = adc_val / (ADC_CONVERTED_DATA_BUFFER_SIZE/2); 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; }' c$ z- B( Z1 d, K return (ret);( M3 \2 i" W5 y2 s5 Y. s' | } |
通过两天调试,讲采样周期设置长一些,比较接近了。要大于 ADC_SAMPLETIME_12CYCLES_5 。技术支持一直没响应。 |
微信公众号
手机版