nrf51822蓝牙之电量检测

基于nrf51822的电池检测试验,参考了百度手环部分源码。

#define BATTER_MAX_VAL 420//4200  4.2V
#define BATTER_MIN_VAL 330//3300   3.3V

#define ENABLE_CHARGING_PIN_INIT 1
#define DISABLE_CHARGING_PIN_INIT 0

static unsigned char chargeing_init=DISABLE_CHARGING_PIN_INIT;

void battery_start(void)
{
    //start evey time use ADC
    NRF_ADC->ENABLE     = ADC_ENABLE_ENABLE_Enabled;

    NRF_ADC->EVENTS_END  = 0;    // Stop any running conversions.
    NRF_ADC->TASKS_START = 1;
}
uint16_t battery_adc_read_once(void)
{
    uint32_t adc_result = 0;
    uint16_t batt_lvl_in_milli_volts=0;
    battery_start();
    nrf_delay_ms(10);
    while( !(NRF_ADC->EVENTS_END) ) {
        nrf_delay_ms(10);
    }

    NRF_ADC->EVENTS_END     = 0;
    adc_result              = NRF_ADC->RESULT;
    NRF_ADC->TASKS_STOP     = 1;

		batt_lvl_in_milli_volts = ((uint32_t)(ADC_RESULT_IN_MILLI_VOLTS(adc_result));
		return batt_lvl_in_milli_volts;
}

uint8_t Get_BatteryPercent(void)
{
	uint16_t volt=0;
	uint8_t PercentValue=0;    	
        volt=battery_adc_read_once();
	volt=volt/10;
	if (BATTER_MAX_VAL > BATTER_MIN_VAL)
	{
		if (volt < BATTER_MIN_VAL)
		{
			PercentValue = 0;

		}
		else if (volt > BATTER_MAX_VAL)
		{
			PercentValue = 100;
			//PercentValue = 99;

		}
		else
		{
			PercentValue = (uint8_t)(((uint32_t)(volt - BATTER_MIN_VAL)	*100) / (uint32_t)(BATTER_MAX_VAL - BATTER_MIN_VAL));
	
		}
	} 
	return PercentValue;
}

void battery_adc_dev_init(void)
{

    // Configure ADC
    NRF_ADC->CONFIG     = BATTERY_ADC_RESOLUTION << ADC_CONFIG_RES_Pos    |
                          ADC_CONFIG_INPSEL_AnalogInputNoPrescaling  << ADC_CONFIG_INPSEL_Pos |
                          ADC_CONFIG_REFSEL_VBG << ADC_CONFIG_REFSEL_Pos |
                          ADC_INPUT_CHANNEL << ADC_CONFIG_PSEL_Pos;

    NRF_ADC->EVENTS_END = 0;
}
//================USB charing detect==================================
uint8_t USB_detect(void)
{ 

   if(nrf_gpio_pin_read(USB_DETECT_PIN)==1)
   {
    if(chargeing_init==DISABLE_CHARGING_PIN_INIT)
    {
      nrf_gpio_cfg_input(CHARGE_PIN, NRF_GPIO_PIN_PULLUP);
      chargeing_init=ENABLE_CHARGING_PIN_INIT;
    }
   	return 1;
   }
   else
   {
    if(chargeing_init==ENABLE_CHARGING_PIN_INIT)
    {
      nrf_gpio_cfg_input(CHARGE_PIN, NRF_GPIO_PIN_PULLDOWN);  //充电引脚设置
      chargeing_init=DISABLE_CHARGING_PIN_INIT;
    }  
   return 0;
   }
}
uint8_t Charge_full_detect(void)
{ 
   if(nrf_gpio_pin_read(CHARGE_PIN)==0)
   {
   	return 1;
   }
   return 0;
}
void USB_detect_init(void)
{ 

  nrf_gpio_cfg_input(USB_DETECT_PIN, NRF_GPIO_PIN_NOPULL);    //充电检测引脚设置

}


#define ADC_INPUT_CHANNEL  ADC_CONFIG_PSEL_AnalogInput5


#define ADC_REF_VOLTAGE_IN_MILLIVOLTS        1200      (内部1.2V 设置转换单位为毫伏)                                 /**< Reference voltage (in  milli volts) used by ADC while doing conversion. */
#define ADC_PRE_SCALING_COMPENSATION         1                                         /**< The ADC is configured to use VDD with no prescaling as input. And hence the result of conversion is to be multiplied by 3 to get the actual value of the battery voltage.*/
#define ADC_HW_PRE_SCALING_COMPENSATION      4                    (电压范围为3.3--4.2  而内部电压的4倍才满足关系)


#define BATTERY_ADC_RESOLUTION  (ADC_CONFIG_RES_10bit)
#define BATTERY_ADC_DIV         (1023)


#define MIN_BAT_MV (3600)
#define CHARGER_CHECK_INTERVAL_MS (APP_TIMER_TICKS(1000, APP_TIMER_PRESCALER))                //一秒检测一次
#define BATTERY_VOLTAGE_ADJUSTMENT           60     /**< Adjustment for charging */   



#define ADC_RESULT_IN_MILLI_VOLTS(ADC_VALUE)\
        ((((ADC_VALUE) * ADC_REF_VOLTAGE_IN_MILLIVOLTS)  * ADC_PRE_SCALING_COMPENSATION * ADC_HW_PRE_SCALING_COMPENSATION)/ BATTERY_ADC_DIV)





你可能感兴趣的:(nrf51822蓝牙开发)