09 ADC acquisition and power management system (series post)
[Copy link]
This post was last edited by Qintianqintian0303 on 2022-3-22 21:44
Related articles :
【GD32L233C-START Review】02 Power-on and program download and debugging
【GD32L233C-START Review】03 LED Operation and General Timing Function
【GD32L233C-START Review】04 External Interrupt and Timer PWM
【GD32L233C-START Review】05 Serial port experience and receiving variable-length data
【GD32L233C-START Review】06 Making a GD32L233C expansion board
【GD32L233C-START Review】07 Test finished product + SPI driver TFT
【GD32L233C-START Review】08 IIC communication experience to obtain sensor data
Preface
At present, we have basically experienced all the commonly used functions, and the only thing left is ADC acquisition. Combined with the resources of the expansion board, this time we use ADC to collect the power supply voltage and combine it with the power supply status of various locations to complete the power on and off and power display, completing the most basic step of the system.
Target
Experience the ADC acquisition function and realize power on/off and power display.
analyze
The power display function is realized thanks to the ADC collecting the power voltage and USB power supply status and charging status, so that the power supply status can be divided into the following types: USB power supply and charging, USB power supply single replenishment point, battery power supply, etc.
Next is the corresponding initialization:
Step 1: USB input status pin initialization and charging status initialization;
Step 2: ADC acquisition initialization;
Step 3: Collect data and make logical judgments;
Step 4: Interface display;
ADC Configuration Code
//******************************************************************************
//* 函数名称 : gd_ADC_init
//* 函数描述 : ADC配置
//* 输入参数 :
//* 参数描述 : ADC初始化配置
//* 输出参数 : 无
//* 返回值 : 无
//******************************************************************************
void gd_ADC_init(void)
{
/* enable GPIOC clock */
rcu_periph_clock_enable(RCU_GPIOA);
/* enable ADC clock */
rcu_periph_clock_enable(RCU_ADC);;
/* config ADC clock CK_APB2/6*/
rcu_adc_clock_config(RCU_ADCCK_APB2_DIV6);
gpio_mode_set(GPIOA, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, GPIO_PIN_1);
/* ADC data alignment config */
adc_data_alignment_config(ADC_DATAALIGN_RIGHT);
/* ADC channel length config */
adc_channel_length_config(ADC_REGULAR_CHANNEL, 1U);
/* ADC trigger config */
adc_external_trigger_source_config(ADC_REGULAR_CHANNEL, ADC_EXTTRIG_REGULAR_NONE);
/* ADC external trigger config */
adc_external_trigger_config(ADC_REGULAR_CHANNEL, ENABLE);
/* enable ADC interface */
adc_enable();
DelaySysTick_ms(1U);
/* ADC calibration and reset calibration */
adc_calibration_enable();
}
Battery power collection and judgment code
//*******************************************************************************
//* 函数名称 : void Collection_SPM(void)
//* 函数描述 : 采集数据
//* 输入参数 :
//* 参数描述 :
//* 输出参数 : 无
//* 返回值 :
//*******************************************************************************
void Collection_SPM(void)
{
if(Power_collect_flag == 1)
{
ADC_Value=Power_get(1);
VOL_Value=(uint16_t)(ADC_Value*110*5/4096+30);
// if(Sign_USB==1)
// {
// Flag_USB = 1;
// }
// else
// {
// Flag_USB = 0;
// }
if(Sign_CHG==1)
{
Flag_CHG = 0;
}
else
{
Flag_CHG = 1;
}
Power_collect_flag = 0;
}
App_Battery_Judge();
}
//*******************************************************************************
//* 函数名称 : void Power_get(void)
//* 函数描述 : 采集数据
//* 输入参数 :
//* 参数描述 :
//* 输出参数 : 无
//* 返回值 :
//*******************************************************************************
uint16_t Power_get(uint8_t channel)
{
/* ADC regular channel config */
adc_regular_channel_config(0U, channel, ADC_SAMPLETIME_7POINT5);
/* ADC software trigger enable */
adc_software_trigger_enable(ADC_REGULAR_CHANNEL);
/* wait the end of conversion flag */
while(!adc_flag_get(ADC_FLAG_EOC));
/* clear the end of conversion flag */
adc_flag_clear(ADC_FLAG_EOC);
/* return regular channel sample value */
return (adc_regular_data_read());
}
//********************************************************************************/
//* 函数名称 : void App_Battery_Judge(void) */
//* 函数描述 : 电池状态判断 */
//* 输入参数 : */
//* 参数描述 : */
//* 输出参数 : 无 */
//* 返回值 : 无 */
//********************************************************************************/
void App_Battery_Judge(void)
{
// if((Flag_USB == 1)&&(Flag_CHG == 1))
// {
// State_Power_cnt = 8;
// }
if((Flag_CHG == 1))
{
State_Power_cnt = 7;
}
else if((Flag_CHG == 0)&&(VOL_Value >= 410))
{
State_Power_cnt = 6;
}
else if((Flag_CHG == 0)&&(VOL_Value >= 400)&&(VOL_Value < 410))
{
State_Power_cnt = 5;
}
else if((Flag_CHG == 0)&&(VOL_Value >= 390)&&(VOL_Value < 400))
{
State_Power_cnt = 4;
}
else if((Flag_CHG == 0)&&(VOL_Value >= 380)&&(VOL_Value < 390))
{
State_Power_cnt = 3;
}
else if((Flag_CHG == 0)&&(VOL_Value >= 370)&&(VOL_Value < 380))
{
State_Power_cnt = 2;
}
else if((Flag_CHG == 0)&&(VOL_Value < 370))
{
State_Power_cnt = 1;
}
if(State_Power_cnt >= 1 && State_Power_cnt <= 6 && SHOW_Powering_cnt == State_Power_cnt-1)
{
State_Power_cnt = SHOW_Powering_cnt;
}
}
The acquisition timing is one time per 2 seconds. The battery power does not need to be judged too frequently, but the USB status needs to be judged in a timely manner. The ADC conversion is started by software. The waiting time for the conversion to complete varies depending on the accuracy. This should be noted.
The evaluation of this development board is basically over here. I still can’t bear to destroy the evaluation of the low power consumption of the L series. For most of the current product requirements, function realization is still the first priority. The power consumption is getting lower and lower, and the homogeneity is very obvious. I also hope that domestic manufacturers will do better and better in product quality control. I wish forum friends to use it more and work smoothly!
|