/******************************************************************************* * File Name : Battery.c * Description : * Author : hido.ltd *******************************************************************************/ /******************************************************************************* * Include Files * *******************************************************************************/ #include "battery.h" #include "adc.h" #include "gpio.h" /******************************************************************************* * Macro * *******************************************************************************/ /******************************************************************************* * Type Definition * *******************************************************************************/ /******************************************************************************* * Local Variable * *******************************************************************************/ static ST_GPIO l_astBatteryPin[BATTERY_PIN_MAX]; extern ADC_HandleTypeDef hadc; /******************************************************************************* * Local Function Declaration * *******************************************************************************/ /******************************************************************************* * Local Function * *******************************************************************************/ /******************************************************************************* * Function Name : Battery_MearsEnable * Description : * Input : * Output : * Return : * Author : hido.ltd *******************************************************************************/ void Battery_MearsEnable(void) { GPIO_RESET(&l_astBatteryPin[BATTERY_PIN_MEARS_CTRL]); } /******************************************************************************* * Function Name : Battery_PinRegister * Description : * Input : * Output : * Return : * Author : hido.ltd *******************************************************************************/ void Battery_MearsDisable(void) { GPIO_SET(&l_astBatteryPin[BATTERY_PIN_MEARS_CTRL]); } /******************************************************************************* * Global Function * *******************************************************************************/ /******************************************************************************* * Function Name : Battery_GetADC * Description : »ñÈ¡µç³ØADCÖµ * Input : None * Output : None * Return : µç³ØADCÖµ * Author : hido.ltd *******************************************************************************/ void Battery_ConfigureAdcChannel(void) { ; } /******************************************************************************* * Function Name : Battery_GetADC * Description : »ñÈ¡µç³ØADCÖµ * Input : None * Output : None * Return : µç³ØADCÖµ * Author : hido.ltd *******************************************************************************/ HIDO_UINT32 Battery_GetADC(void) { HIDO_UINT32 adc_value = 0; Battery_ConfigureAdcChannel(); // Æô¶¯Ò»´Î ADC ת»» HAL_ADC_Start(&hadc); // µÈ´ýת»»Íê³É if (HAL_ADC_PollForConversion(&hadc, HAL_MAX_DELAY) == HAL_OK) { // ¶Áȡת»»½á¹û adc_value = HAL_ADC_GetValue(&hadc); } // ¹Ø±Õ ADC HAL_ADC_Stop(&hadc); return adc_value; } /******************************************************************************* * Function Name : Battery_VoltageConvert * Description : µç³Øµçѹת»» * Input : None * Output : None * Return : µç³ØADCÖµ * Author : hido.ltd *******************************************************************************/ float Battery_VoltageConvert(HIDO_UINT32 _u32ADC) { float fVoltage = (_u32ADC / 4095.0) * 3.3; fVoltage = fVoltage * 2; return fVoltage; } /******************************************************************************* * Function Name : Battery_GetVoltage * Description : »ñÈ¡µç³Øµçѹ * Input : None * Output : None * Return : µç³Øµçѹ * Author : hido.ltd *******************************************************************************/ float Battery_GetVoltage(void) { float fVoltage; Battery_MearsEnable(); HAL_Delay(100); fVoltage = Battery_VoltageConvert(Battery_GetADC()); Battery_MearsDisable(); return fVoltage; } /******************************************************************************* * Function Name : Battery_GetPercentage * Description : »ñÈ¡µç³ØÈÝÁ¿°Ù·Ö±È * Input : None * Output : None * Return : µç³ØÈÝÁ¿°Ù·Ö±È * Author : hido.ltd *******************************************************************************/ HIDO_UINT32 Battery_GetPercentage(void) { float fVoltage = 0; HIDO_UINT32 u32Percentage = 0; fVoltage = Battery_GetVoltage(); if(fVoltage < (float)3.3) { u32Percentage = 0; } else if(u32Percentage > 4.2) { u32Percentage = 100; } else { u32Percentage = (fVoltage - (float)3.3) / (float)(4.2 - 3.3)*100; } return u32Percentage; } /******************************************************************************* * Function Name : Battery_PinRegister * Description : * Input : * Output : * Return : * Author : hido.ltd *******************************************************************************/ HIDO_INT32 Battery_PinRegister(E_BatteryPin _ePin, HIDO_VOID* _pstGPIOx, HIDO_UINT16 _u16GPIOPin) { if(_ePin >= BATTERY_PIN_MAX) { return HIDO_ERR; } l_astBatteryPin[_ePin].m_pstGPIOx = (GPIO_TypeDef *)_pstGPIOx; l_astBatteryPin[_ePin].m_u16GPIOPin = _u16GPIOPin; return HIDO_OK; }