外部电池电压检测

static void battery_gpio_config(void)
{
  /* enable GPIOC clock */
    rcu_periph_clock_enable(BATTERY_RCC);
    /* enable ADC clock */
    rcu_periph_clock_enable(RCU_ADC);
    /* config ADC clock */
    rcu_adc_clock_config(RCU_ADCCK_APB2_DIV6);  //AHB=108M  APB2 = AHB/2=54M   RCU_ADCCK_APB2_DIV6=APB2/6=9M
    /* config the GPIO as analog mode */
    gpio_mode_set(BATTERY_PORT, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, BATTERY_PIN);
}


//++ADC 的模拟输入通道 16,通道 17 和通道 18 分别连接到温度传感器,VREFINT 和 VBAT/2 模拟输入
static void vbat_adc_config( void )
{
    /* ADC channel length config */
    adc_channel_length_config( ADC_INSERTED_CHANNEL, 2 );

//    /* ADC temperature sensor channel config */
//    adc_inserted_channel_config( 0, ADC_CHANNEL_16, ADC_SAMPLETIME_239POINT5 );
//    /* ADC internal reference voltage channel config */
    adc_inserted_channel_config( 0, ADC_CHANNEL_17, ADC_SAMPLETIME_239POINT5 );
    /* ADC bat channel config */
    adc_inserted_channel_config( 1, BATTERY_AD_CHANNEL, ADC_SAMPLETIME_239POINT5 );

    /* ADC trigger config */
    adc_external_trigger_source_config( ADC_INSERTED_CHANNEL, ADC_EXTTRIG_INSERTED_NONE );
    /* ADC data alignment config */
    adc_data_alignment_config( ADC_DATAALIGN_RIGHT );
    /* ADC SCAN function enable */
    adc_special_function_config( ADC_SCAN_MODE, ENABLE );
    /* ADC temperature and Vrefint enable */
    adc_tempsensor_vrefint_enable();
    adc_vbat_enable();
    adc_external_trigger_config( ADC_INSERTED_CHANNEL, ENABLE );

    /* enable ADC interface */
    adc_enable();
    TimerDelayMS(1);

    /* ADC calibration and reset calibration */
    adc_calibration_enable();
}

static u32 adc_get_vbat_value( void )
{
  u32 voltage = 0;
    u32 temp0,temp1;

  adc_software_trigger_enable( ADC_INSERTED_CHANNEL );
  while ( !adc_flag_get( ADC_FLAG_EOIC ) );   
  adc_flag_clear( ADC_FLAG_EOIC );
            
    temp0 = ADC_IDATA0;
    temp1 = ADC_IDATA1;    
    voltage = temp1*12*122/temp0;  //--对应电池实际电压*1000
  return voltage;
}

你可能感兴趣的:(驱动)