3135 views|8 replies

5998

Posts

6

Resources
The OP
 

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!

This post is from GD32 MCU

Latest reply

The original poster uses voltage to judge the power level. Is there a linear relationship between power level and battery voltage or is there another algorithm? Can you give me some scientific information?  Details Published on 2022-3-28 09:49
Personal signature

在爱好的道路上不断前进,在生活的迷雾中播撒光引

 

6555

Posts

0

Resources
2
 

About [GD32L233C-START review, the author has tested to the ninth article, awesome

This post is from GD32 MCU

Comments

In fact, it is the realization of one function at a time  Details Published on 2022-3-24 16:47
 
 
 

706

Posts

0

Resources
3
 

The host is good, awesome

This post is from GD32 MCU

Comments

Thank you, let's encourage each other  Details Published on 2022-3-24 16:47
 
 
 

6742

Posts

2

Resources
4
 

Looking forward to the final demo video~

This post is from GD32 MCU

Comments

Record it later. It is actually a simple function implementation.  Details Published on 2022-3-24 16:48
 
 
 

5998

Posts

6

Resources
5
 
Jacktang posted on 2022-3-23 07:27 About [GD32L233C-START review, the author has tested the ninth one, awesome

In fact, it is the realization of one function at a time

This post is from GD32 MCU
 
Personal signature

在爱好的道路上不断前进,在生活的迷雾中播撒光引

 
 

5998

Posts

6

Resources
6
 
Fred_1977 posted on 2022-3-23 13:28 The host is great, awesome

Thank you, let's encourage each other

This post is from GD32 MCU
 
Personal signature

在爱好的道路上不断前进,在生活的迷雾中播撒光引

 
 

5998

Posts

6

Resources
7
 
wangerxian posted on 2022-3-24 14:11 Looking forward to the final demo effect video~

Record it later. It is actually a simple function implementation.

This post is from GD32 MCU
 
Personal signature

在爱好的道路上不断前进,在生活的迷雾中播撒光引

 
 

6818

Posts

11

Resources
8
 
The original poster uses voltage to judge the power level. Is there a linear relationship between power level and battery voltage or is there another algorithm? Can you give me some scientific information?
This post is from GD32 MCU

Comments

The simple voltage and power are linearly related. If you want to be more precise, you can check the battery curve to find the point.  Details Published on 2022-3-29 08:38
 
 
 

5998

Posts

6

Resources
9
 
lugl4313820 posted on 2022-3-28 09:49 The OP uses voltage to judge the power level. Is the power level linearly related to the battery voltage or is there another algorithm? Can you explain it in detail?

The simple voltage and power are linearly related. If you want to be more precise, you can check the battery curve to find the point.

This post is from GD32 MCU
 
Personal signature

在爱好的道路上不断前进,在生活的迷雾中播撒光引

 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list