Motion_EC_Stm32_archived/Middlewares/Coulomb/coulomb.c

74 lines
3.4 KiB
C
Raw Normal View History

2020-04-28 18:44:48 +08:00
/*
* @Description: file content
* @Author: CK.Zh
* @Date: 2020-04-27 18:01:58
* @LastEditors: CK.Zh
* @LastEditTime: 2020-04-28 16:42:03
*/
2020-04-27 18:19:02 +08:00
#include "coulomb.h"
2020-04-28 18:44:48 +08:00
coulomb_t coulomb;
//bool is_coulomb_ready()
//{
//// HAL_I2C_IsDeviceReady(hi2c1,COULOMB_ADDR);
//}
2020-04-27 18:19:02 +08:00
2020-04-28 18:44:48 +08:00
static HAL_StatusTypeDef coulomb_write_reg(uint8_t addr,uint8_t* pData)
2020-04-27 18:19:02 +08:00
{
2020-04-28 18:44:48 +08:00
return HAL_I2C_Mem_Write(&hi2c1,COULOMB_ADDR ,addr,1,pData, 1, 10);
2020-04-27 18:19:02 +08:00
}
2020-04-28 18:44:48 +08:00
static uint16_t coulomb_uint8_to_uint16(uint8_t *pData,uint8_t msb,uint8_t lsb)
2020-04-27 18:19:02 +08:00
{
2020-04-28 18:44:48 +08:00
return ((pData[msb]<<8) | pData[lsb]);
2020-04-27 18:19:02 +08:00
}
2020-04-28 18:44:48 +08:00
HAL_StatusTypeDef coulomb_read_status()
2020-04-27 18:19:02 +08:00
{
2020-04-28 18:44:48 +08:00
uint8_t ctl_reg_set = 0xEc;
coulomb_write_reg(LTC2943_CONTROL_REG,&ctl_reg_set);
2020-04-27 18:19:02 +08:00
2020-04-28 18:44:48 +08:00
uint8_t pData[0x18];
HAL_StatusTypeDef ret;
2020-04-27 18:19:02 +08:00
2020-04-28 18:44:48 +08:00
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;
2020-04-27 18:19:02 +08:00
}
2020-04-28 18:44:48 +08:00
//
//bool coulomb_read_alcc()
//{
// return HAL_GPIO_ReadPin(COULOMB_ALCC_GPIO_Port,COULOMB_ALCC_Pin);
//}