123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246 |
- /*********************************************************************
- *
- * 文件名: User_Battery.c
- * 功能描述:设备电池电量管理
- * 作者: 陈俊超
- * 时间:2020-11-20
- *
- ********************************************************************/
- #include "nrf.h"
- #include "nrf_drv_saadc.h"
- #include "nrf_drv_ppi.h"
- #include "nrf_drv_timer.h"
- #include "app_timer.h"
- #include "User_Battery.h"
- /********************** 变量区 **************************************/
- static nrf_saadc_value_t m_buffer_Voltage[BatteryVoltageNumber];
- static uint16_t Battery_ADC =0;//电池ADC值
- #if (PCB_VERSION ==0)
- static uint8_t ADC_Channel =5;
- #elif (PCB_VERSION ==1)
- static uint8_t ADC_Channel =3;
- #elif (PCB_VERSION ==2)
- static uint8_t ADC_Channel =0;
- #elif (PCB_VERSION ==3)
- static uint8_t ADC_Channel =3;
- #endif
- /********************** 函数功能区 **************************************/
- /**********************************************************
- * 函数名字:bubble_sort
- * 函数作用:冒泡排序
- * 函数参数:a:需要排列的数组
- * n: 数组长度
- * 函数返回值:无
- ***********************************************************/
- void bubble_sort(uint16_t a[], uint16_t n)
- {
- int i,j;
- uint16_t temp;
- for (j=0;j<n-1;j++){
- for (i=0;i<n-1-j;i++){
- if(a[i]>a[i+1]){
- temp=a[i];
- a[i]=a[i+1];
- a[i+1]=temp;
- }
- }
- }
- }
- int filter(int data)
- {
- #define filter_len 10
- static int buffer[filter_len] = {0};
- static int* buffer_p = (int*)buffer + (filter_len - 1);
- static int sum = (filter_len >> 1);
- sum -= *buffer_p;
- *buffer_p-- = data;
- sum += data;
- if (buffer_p < buffer)
- {
- buffer_p += filter_len;
- }
- return sum / filter_len;
- }
- short filter_mid_averange(short* p)
- {
- int i;
- short buf[filter_mid_averange_number];
- for(i=0;i<filter_mid_averange_number;i++){
- buf[i] = p[i];
- }
- bubble_sort(buf,filter_mid_averange_number);
- return buf[filter_mid_averange_number>>1];
- }
- short filter_mid(short val)
- {
- static short buf[filter_mid_averange_number];
- static int dex = 0;
- buf[dex] = val;
- if(++dex>=filter_mid_averange_number) dex = 0;
- return filter_mid_averange(buf);
- }
- /**********************************************************
- * 函数名字:saadc_callback
- * 函数作用:adc 事件回调
- * 函数参数:p_event:事件类型
- * 函数返回值:无
- ***********************************************************/
- static void saadc_callback(nrf_drv_saadc_evt_t const * p_event)
- {
- if (p_event->type == NRF_DRV_SAADC_EVT_DONE){
- ret_code_t err_code = nrf_drv_saadc_buffer_convert(m_buffer_Voltage, BatteryVoltageNumber);
- APP_ERROR_CHECK(err_code);
- }
- }
- /**********************************************************
- * 函数名字:saadc_init
- * 函数作用:Adc模块功能初始化
- * 函数参数:无
- * 函数返回值:无
- ***********************************************************/
- static void saadc_init(void)
- {
- ret_code_t err_code;
- nrf_gpio_cfg_input(PIN_ADC_IN,NRF_GPIO_PIN_NOPULL);//设置ADC引脚为浮空
- #if (PCB_VERSION ==0)
- nrf_saadc_channel_config_t channel_config =
- NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(ADC_Channel+1);
- #elif (PCB_VERSION ==1)
- nrf_saadc_channel_config_t channel_config =
- NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(ADC_Channel+1);
- #else
- nrf_saadc_channel_config_t channel_config =
- NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(ADC_Channel+1);
- #endif
- //单次阻塞式读取,不需要回调
- //err_code = nrf_drv_saadc_init(NULL, saadc_callback);
- err_code = nrf_drv_saadc_init(NULL, NULL);
- APP_ERROR_CHECK(err_code);
- //初始化通道0
- err_code = nrf_drv_saadc_channel_init(ADC_Channel, &channel_config);
- APP_ERROR_CHECK(err_code);
- }
- void Quick_GetBatteryPersent(void)
- {
- for(uint8_t temp =0;temp<filter_mid_averange_number;temp++)
- {
- GetBatteryVoltage_adc();
- nrf_delay_ms(20);
- }
- }
- /**********************************************************
- * 函数名字:GetBatteryCurrentPersen
- * 函数作用:获取电池电量百分比
- * 函数参数:adcvalue :读取到ADC值
- * 函数返回值:电量值
- ***********************************************************/
- uint8_t GetBatteryCurrentPersent()
- {
- #if 0
- const unsigned short list[3][12]={
- {100,90,80,70,60,50,40,30,20,10,5,0},
- {4200,4060,3980,3870,3820,3790,3770,3740,3680,3450,3000},
- {678,651,640,629,621,610,606,603,600,586,548,470}
- };
- if(Battery_ADC>list[2][0])return 100;
- if(Battery_ADC<=list[2][11])return 0;
- for(int i=0;i<11;i++)
- {
- if((Battery_ADC > list[2][i+1]) && (Battery_ADC <= list[2][i]))
- {
- return (list[0][i]-list[0][i+1])*(Battery_ADC-list[2][i+1])/(list[2][i]-list[2][i+1]) + list[0][i+1];
- }
- }
- #endif
- #if 1
- const unsigned short list[3][3]={
- {100,8,1},
- {4000,3300,2960},
- {683,565,507}
- };
-
- static short adcvalue_old=1000;
- short adcvalue_n=Battery_ADC;
- #if 1
- if((Battery_ADC - adcvalue_old > 0)&&(Battery_ADC - adcvalue_old < 5))adcvalue_n=adcvalue_old;
- else adcvalue_old=Battery_ADC;
- #endif
- if(adcvalue_n>list[2][0])return 100;
- if(adcvalue_n<=list[2][2])return 0;
- for(int i=0;i<3;i++)
- {
- if((adcvalue_n > list[2][i+1]) && (adcvalue_n <= list[2][i]))
- {
- return (list[0][i]-list[0][i+1])*(adcvalue_n-list[2][i+1])/(list[2][i]-list[2][i+1]) + list[0][i+1];
- }
- }
- #endif
- return 100;
- }
- /**********************************************************
- * 函数名字:StartBattery_Filter
- * 函数作用:获取电池的ADC值,并且使用冒泡排序过滤数据
- * 函数参数:无
- * 函数返回值:无
- ***********************************************************/
- void StartBattery_Filter(void)
- {
- nrf_saadc_value_t Batterysample;
- ret_code_t err_code = nrfx_saadc_sample_convert(ADC_Channel, &Batterysample);
- Battery_ADC = filter(filter_mid(Batterysample));
- #if DEBUG_EN
- //NRF_LOG_INFO("sample:%d\n",Battery_ADC*3600/1024);
- #endif
- }
- /**********************************************************
- * 函数名字:GetBatteryVoltage
- * 函数作用:获取电池的ADC值,并且使用冒泡排序过滤数据
- * 函数参数:无
- * 函数返回值:无
- ***********************************************************/
- uint16_t GetBatteryVoltage_adc(void)
- {
- return Battery_ADC;
- }
- /**********************************************************
- * 函数名字:User_SAADC_DisOrEnable
- * 函数作用:SAADC的打开和关闭
- * 函数参数:无
- * 函数返回值:无
- ***********************************************************/
- void User_SAADC_DisOrEnable(bool value)
- {
- static bool Saadc_Open_Flag =false;
- uint32_t err_code;
- if(value == Saadc_Open_Flag)return;
- if(value){
- saadc_init();
- }
- else{
- err_code = nrfx_saadc_channel_uninit(ADC_Channel);
- nrfx_saadc_uninit();
- }
- Saadc_Open_Flag = value;
- }
|