/* * @Description: file content * @Author: CK.Zh * @Date: 2020-04-27 18:01:58 * @LastEditors: CK.Zh * @LastEditTime: 2020-04-28 16:42:03 */ #include "coulomb.h" coulomb_t coulomb; //bool is_coulomb_ready() //{ //// HAL_I2C_IsDeviceReady(hi2c1,COULOMB_ADDR); //} static HAL_StatusTypeDef coulomb_write_reg(uint8_t addr,uint8_t* pData) { return HAL_I2C_Mem_Write(&hi2c1,COULOMB_ADDR ,addr,1,pData, 1, 10); } static uint16_t coulomb_uint8_to_uint16(uint8_t *pData,uint8_t msb,uint8_t lsb) { return ((pData[msb]<<8) | pData[lsb]); } HAL_StatusTypeDef coulomb_read_status() { uint8_t ctl_reg_set = 0xEc; coulomb_write_reg(LTC2943_CONTROL_REG,&ctl_reg_set); uint8_t pData[0x18]; HAL_StatusTypeDef ret; HAL_I2C_Mem_Read(&hi2c1,COULOMB_ADDR ,0,1,&pData, 1, 10); ret = HAL_I2C_Mem_Read(&hi2c1,COULOMB_ADDR ,0,0x18,&pData[1], 0x18, 100); if(ret == HAL_OK ) { coulomb.raw.status = pData[LTC2943_STATUS_REG]; coulomb.raw.control = pData[LTC2943_CONTROL_REG]; coulomb.raw.accumulated_charge =coulomb_uint8_to_uint16(pData,LTC2943_ACCUM_CHARGE_MSB_REG,LTC2943_ACCUM_CHARGE_LSB_REG); coulomb.raw.charge_threshold_H = coulomb_uint8_to_uint16(pData,LTC2943_CHARGE_THRESH_HIGH_MSB_REG,LTC2943_CHARGE_THRESH_HIGH_LSB_REG); coulomb.raw.charge_threshold_L = coulomb_uint8_to_uint16(pData,LTC2943_CHARGE_THRESH_LOW_MSB_REG,LTC2943_CHARGE_THRESH_LOW_LSB_REG); coulomb.raw.voltage = coulomb_uint8_to_uint16(pData,LTC2943_VOLTAGE_MSB_REG,LTC2943_VOLTAGE_LSB_REG); coulomb.raw.voltage_threshold_H = coulomb_uint8_to_uint16(pData,LTC2943_VOLTAGE_THRESH_HIGH_MSB_REG,LTC2943_VOLTAGE_THRESH_HIGH_LSB_REG); coulomb.raw.voltage_threshold_L = coulomb_uint8_to_uint16(pData,LTC2943_VOLTAGE_THRESH_LOW_MSB_REG,LTC2943_VOLTAGE_THRESH_LOW_LSB_REG); coulomb.raw.current = coulomb_uint8_to_uint16(pData,LTC2943_CURRENT_MSB_REG,LTC2943_CURRENT_LSB_REG); coulomb.raw.current_threshold_H = coulomb_uint8_to_uint16(pData,LTC2943_CURRENT_THRESH_HIGH_MSB_REG,LTC2943_CURRENT_THRESH_HIGH_LSB_REG); coulomb.raw.current_threshold_L = coulomb_uint8_to_uint16(pData,LTC2943_CURRENT_THRESH_LOW_MSB_REG,LTC2943_CURRENT_THRESH_LOW_LSB_REG); coulomb.raw.temperature = coulomb_uint8_to_uint16(pData,LTC2943_TEMPERATURE_MSB_REG,LTC2943_TEMPERATURE_LSB_REG); coulomb.raw.temperature_threshold_H = pData[LTC2943_TEMPERATURE_THRESH_HIGH_REG]; coulomb.raw.temperature_threshold_L = pData[LTC2943_TEMPERATURE_THRESH_LOW_REG]; coulomb.actual.status.current_alret = ((coulomb.raw.status>>6) & 0x01) ? true : false; coulomb.actual.status.accumulated_charge_alert = ((coulomb.raw.status>>5) & 0x01) ? true : false; coulomb.actual.status.temperature_alret = ((coulomb.raw.status>>4) & 0x01) ? true : false; coulomb.actual.status.charge_alert_H = ((coulomb.raw.status>>3) & 0x01) ? true : false; coulomb.actual.status.charge_alert_L = ((coulomb.raw.status>>2) & 0x01) ? true : false; coulomb.actual.status.voltage_alert = ((coulomb.raw.status>>1) & 0x01) ? true : false; coulomb.actual.status.uvlo_alert = ( coulomb.raw.status & 0x01) ? true : false; coulomb.actual.voltage = coulomb.raw.voltage / 65535.0 *23.6; //电压转换 coulomb.actual.current = (coulomb.raw.current - 32767) / 32767.0 * 30 ; //电流转换 // coulomb.actual.temperature = coulomb.raw.temperature / 65535 *23.6; //温度转换 } return ret; } // //bool coulomb_read_alcc() //{ // return HAL_GPIO_ReadPin(COULOMB_ALCC_GPIO_Port,COULOMB_ALCC_Pin); //}