From bfa947a1d4027203c993ebde6701bc9962d739f2 Mon Sep 17 00:00:00 2001 From: ThinkPad Date: Wed, 29 Apr 2020 17:49:27 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=20=E5=BA=93=E4=BB=91?= =?UTF-8?q?=E8=AE=A1=E5=8F=82=E6=95=B0=E8=AF=BB=E5=86=99=E5=92=8C=E5=8F=82?= =?UTF-8?q?=E6=95=B0=E8=BD=AC=E6=8D=A2=E7=9A=84=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Core/Src/freertos.c | 33 ++-- Middlewares/Coulomb/coulomb.c | 330 ++++++++++++++++++++++++++++------ Middlewares/Coulomb/coulomb.h | 83 ++++++--- 3 files changed, 352 insertions(+), 94 deletions(-) diff --git a/Core/Src/freertos.c b/Core/Src/freertos.c index 9c9b0a0..0b91631 100644 --- a/Core/Src/freertos.c +++ b/Core/Src/freertos.c @@ -190,25 +190,16 @@ void StartLedBlinkTask(void *argument) /* Infinite loop */ for(;;) { - if(NaviKit.system_runing == true) - {//整板开机状态 - HAL_GPIO_TogglePin(LED_RUN_GPIO_Port,LED_RUN_Pin); - osDelay(100); + if(HAL_GPIO_ReadPin(LED_RUN_GPIO_Port,LED_RUN_Pin) == GPIO_PIN_SET) + {//LED处于熄灭状态 + osDelay(NaviKit.system_runing ? 400 : 2000); + HAL_GPIO_WritePin(LED_RUN_GPIO_Port,LED_RUN_Pin,GPIO_PIN_RESET);//点亮 } else - {//整板关机状态 - if(HAL_GPIO_ReadPin(LED_RUN_GPIO_Port,LED_RUN_Pin) == GPIO_PIN_SET) - {//LED处于熄灭状态 - HAL_GPIO_WritePin(LED_RUN_GPIO_Port,LED_RUN_Pin,GPIO_PIN_RESET); - } - else - {//LED处于点亮状态 - HAL_GPIO_WritePin(LED_RUN_GPIO_Port,LED_RUN_Pin,GPIO_PIN_SET); - osDelay(2000); - } - + {//LED处于点亮状态 + osDelay(20); + HAL_GPIO_WritePin(LED_RUN_GPIO_Port,LED_RUN_Pin,GPIO_PIN_SET);//熄灭 } - osDelay(50); } /* USER CODE END StartLedBlinkTask */ } @@ -317,10 +308,18 @@ void StartButtonDetect(void *argument) void StartCoulombRead(void *argument) { /* USER CODE BEGIN StartCoulombRead */ + coulomb_write_config_init(); + coulomb_write_config_actual_to_raw(); + coulomb_write_config(); +//读寄存器方法 + + /* Infinite loop */ for(;;) { - coulomb_read_status(); + coulomb_read_status_and_config(); + coulomb_read_status_raw_to_actual(); + coulomb_read_config_raw_to_actual(); if(NaviKit.system_runing) osDelay(500); else diff --git a/Middlewares/Coulomb/coulomb.c b/Middlewares/Coulomb/coulomb.c index 250d2c1..8053239 100644 --- a/Middlewares/Coulomb/coulomb.c +++ b/Middlewares/Coulomb/coulomb.c @@ -3,71 +3,299 @@ * @Author: CK.Zh * @Date: 2020-04-27 18:01:58 * @LastEditors: CK.Zh - * @LastEditTime: 2020-04-28 16:42:03 + * @LastEditTime: 2020-04-29 17:49:01 */ #include "coulomb.h" coulomb_t coulomb; -//bool is_coulomb_ready() -//{ -//// HAL_I2C_IsDeviceReady(hi2c1,COULOMB_ADDR); -//} + +//静态方法,检测设备是否就绪 +static HAL_StatusTypeDef is_coulomb_ready() +{ + return HAL_I2C_IsDeviceReady(&hi2c1,COULOMB_ADDR,3,20); +} +//静态方法,写寄存器 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) +//静态方法,读寄存器 +static HAL_StatusTypeDef coulomb_read_reg(uint8_t addr,uint8_t* pData) { - return ((pData[msb]<<8) | pData[lsb]); + return HAL_I2C_Mem_Read(&hi2c1,COULOMB_ADDR ,addr,1,pData, 1, 10); } -HAL_StatusTypeDef coulomb_read_status() +/** + * @description: 静态方法,字节拼接 + * @param pData 数组指针 + * @param msbIndex 高字节在数组中的位置 + * @param lsbIndex 低字节在数组中的位置 + * @return: + */ +static uint16_t uint8_to_uint16(uint8_t *pData,uint8_t msbIndex,uint8_t lsbIndex) { - uint8_t ctl_reg_set = 0xEc; - coulomb_write_reg(LTC2943_CONTROL_REG,&ctl_reg_set); + return ((pData[msbIndex]<<8) | pData[lsbIndex]); +} +/** + * @description: 静态方法,字节拆解 + * @param data + * @return: + */ +static uint8_t uint16_to_uint8_msb(uint16_t data) +{ + return ((data>>8) & 0xff); +} +/** + * @description: 静态方法,字节拆解 + * @param data + * @return: + */ +static uint8_t uint16_to_uint8_lsb(uint16_t data) +{ + return ((data ) & 0xff); +} +/** + * @description: 初始化配置参数(寄存器上电后的缺省值) + * @param {type} + * @return: + */ +// void coulomb_write_config_init_default() +// { +// coulomb.write.raw.control = 0xEC; +// coulomb.write.raw.accumulated_charge = 0x7fff; +// coulomb.write.raw.charge_threshold_H = 0xffff; +// coulomb.write.raw.charge_threshold_L = 0x0000; +// coulomb.write.raw.voltage_threshold_H = 0xffff; +// coulomb.write.raw.voltage_threshold_L = 0x0000; +// coulomb.write.raw.current_threshold_H = 0xffff; +// coulomb.write.raw.current_threshold_L = 0x0000; +// coulomb.write.raw.temperature_threshold_H = 0xff; +// coulomb.write.raw.temperature_threshold_L = 0x00; +// } +/** + * @description: 初始化配置参数 + * @param {type} + * @return: + */ +void coulomb_write_config_init() +{ + //控制寄存器配置 + coulomb.write.actual.control.adc_mode = 0b00; + coulomb.write.actual.control.prescaler = 0b111; + coulomb.write.actual.control.alcc_configure = 0b10; + coulomb.write.actual.control.shutdown = 0b00; - uint8_t pData[0x18]; - HAL_StatusTypeDef ret; + //剩余容量重写入 + coulomb.write.actual.accumulated_charge = 2; - 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); + //充电阈值上限 + coulomb.write.actual.charge_threshold_H = 20; + //充电阈值下限 + coulomb.write.actual.charge_threshold_L = 1; - if(ret == HAL_OK ) + //电压阈值上限 + coulomb.write.actual.voltage_threshold_H = 15; + //电压阈值下限 + coulomb.write.actual.voltage_threshold_L = 9.5; + + //电流阈值上限 + coulomb.write.actual.current_threshold_H = 10; + //电流阈值下限 + coulomb.write.actual.current_threshold_L = -10; + + //温度阈值上限 + coulomb.write.actual.temperature_threshold_H = 60; + //温度阈值下限 + coulomb.write.actual.temperature_threshold_L = -15; +} +/** + * @description: 将准备写入的目标参数转化成寄存器格式 + * @param {type} + * @return: + */ +void coulomb_write_config_actual_to_raw() +{ + //控制寄存器 + coulomb.write.raw.control = (coulomb.write.actual.control.adc_mode << 6) | \ + (coulomb.write.actual.control.prescaler << 3) | \ + (coulomb.write.actual.control.alcc_configure << 1) | \ + (coulomb.write.actual.control.shutdown ) ; + //累计电量 + coulomb.write.raw.accumulated_charge = (uint16_t)((coulomb.write.actual.accumulated_charge / 30.0) * 32767 + 32767); + + //充电阈值上限 + coulomb.write.raw.charge_threshold_H = (uint16_t)((coulomb.write.actual.charge_threshold_H / 30.0) * 32767 + 32767); + //充电阈值下限 + coulomb.write.raw.charge_threshold_L = (uint16_t)((coulomb.write.actual.charge_threshold_L / 30.0) * 32767 + 32767); + + //电压阈值上限 + coulomb.write.raw.voltage_threshold_H = (uint16_t)((coulomb.write.actual.voltage_threshold_H / 23.6) * 65535); + //电压阈值下限 + coulomb.write.raw.voltage_threshold_L = (uint16_t)((coulomb.write.actual.voltage_threshold_L / 23.6) * 65535); + + //电流阈值上限 + coulomb.write.raw.current_threshold_H = (uint16_t)((coulomb.write.actual.current_threshold_H / 30.0) * 32767 + 32767); + //电流阈值下限 + coulomb.write.raw.current_threshold_L = (uint16_t)((coulomb.write.actual.current_threshold_L / 30.0) * 32767 + 32767); + + //温度阈值上限 + coulomb.write.raw.temperature_threshold_H = ((uint16_t)((coulomb.write.actual.temperature_threshold_H + 273.15) / 510.0 * 65535)) & 0xFF00;//清空低字节 + //温度阈值下限 + coulomb.write.raw.temperature_threshold_L = ((uint16_t)((coulomb.write.actual.temperature_threshold_L + 273.15) / 510.0 * 65535)) & 0xFF00;//清空低字节 +} +/** + * @description: 写入寄存器配置 + * @param {type} + * @return: + */ +bool coulomb_write_config() +{ + uint8_t writeData[COULOMB_REG_NUM] = {0}; + + //控制寄存器 + writeData[LTC2943_CONTROL_REG] = coulomb.write.raw.control; + + //累计电量 + writeData[LTC2943_ACCUM_CHARGE_MSB_REG] = uint16_to_uint8_msb(coulomb.write.raw.accumulated_charge); + writeData[LTC2943_ACCUM_CHARGE_LSB_REG] = uint16_to_uint8_lsb(coulomb.write.raw.accumulated_charge); + + //充电阈值上限 + writeData[LTC2943_CHARGE_THRESH_HIGH_MSB_REG] = uint16_to_uint8_msb(coulomb.write.raw.charge_threshold_H); + writeData[LTC2943_CHARGE_THRESH_HIGH_LSB_REG] = uint16_to_uint8_lsb(coulomb.write.raw.charge_threshold_H); + //充电阈值下限 + writeData[LTC2943_CHARGE_THRESH_LOW_MSB_REG] = uint16_to_uint8_msb(coulomb.write.raw.charge_threshold_L); + writeData[LTC2943_CHARGE_THRESH_LOW_LSB_REG] = uint16_to_uint8_lsb(coulomb.write.raw.charge_threshold_L); + + //电压阈值上限 + writeData[LTC2943_VOLTAGE_THRESH_HIGH_MSB_REG] = uint16_to_uint8_msb(coulomb.write.raw.voltage_threshold_H); + writeData[LTC2943_VOLTAGE_THRESH_HIGH_LSB_REG] = uint16_to_uint8_lsb(coulomb.write.raw.voltage_threshold_H); + //电压阈值下限 + writeData[LTC2943_VOLTAGE_THRESH_LOW_MSB_REG] = uint16_to_uint8_msb(coulomb.write.raw.voltage_threshold_L); + writeData[LTC2943_VOLTAGE_THRESH_LOW_LSB_REG] = uint16_to_uint8_lsb(coulomb.write.raw.voltage_threshold_L); + + //电流阈值上限 + writeData[LTC2943_CURRENT_THRESH_HIGH_MSB_REG] = uint16_to_uint8_msb(coulomb.write.raw.current_threshold_H); + writeData[LTC2943_CURRENT_THRESH_HIGH_LSB_REG] = uint16_to_uint8_lsb(coulomb.write.raw.current_threshold_H); + //电流阈值下限 + writeData[LTC2943_CURRENT_THRESH_LOW_MSB_REG] = uint16_to_uint8_msb(coulomb.write.raw.current_threshold_L); + writeData[LTC2943_CURRENT_THRESH_LOW_LSB_REG] = uint16_to_uint8_lsb(coulomb.write.raw.current_threshold_L); + + //温度阈值上限 + writeData[LTC2943_TEMPERATURE_THRESH_HIGH_REG] = uint16_to_uint8_msb(coulomb.write.raw.temperature_threshold_H); + //温度阈值下限 + writeData[LTC2943_TEMPERATURE_THRESH_LOW_REG] = uint16_to_uint8_msb(coulomb.write.raw.temperature_threshold_L); + + coulomb.ready = (is_coulomb_ready()==HAL_OK) ? true : false; + if(coulomb.ready) { - 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; //温度转换 + coulomb_write_reg(LTC2943_CONTROL_REG,&writeData[LTC2943_CONTROL_REG]); + coulomb_write_reg(LTC2943_ACCUM_CHARGE_MSB_REG,&writeData[LTC2943_ACCUM_CHARGE_MSB_REG]); + coulomb_write_reg(LTC2943_ACCUM_CHARGE_LSB_REG,&writeData[LTC2943_ACCUM_CHARGE_LSB_REG]); + coulomb_write_reg(LTC2943_CHARGE_THRESH_HIGH_MSB_REG,&writeData[LTC2943_CHARGE_THRESH_HIGH_MSB_REG]); + coulomb_write_reg(LTC2943_CHARGE_THRESH_HIGH_LSB_REG,&writeData[LTC2943_CHARGE_THRESH_HIGH_LSB_REG]); + coulomb_write_reg(LTC2943_CHARGE_THRESH_LOW_MSB_REG,&writeData[LTC2943_CHARGE_THRESH_LOW_MSB_REG]); + coulomb_write_reg(LTC2943_CHARGE_THRESH_LOW_LSB_REG,&writeData[LTC2943_CHARGE_THRESH_LOW_LSB_REG]); + coulomb_write_reg(LTC2943_VOLTAGE_THRESH_HIGH_MSB_REG,&writeData[LTC2943_VOLTAGE_THRESH_HIGH_MSB_REG]); + coulomb_write_reg(LTC2943_VOLTAGE_THRESH_HIGH_LSB_REG,&writeData[LTC2943_VOLTAGE_THRESH_HIGH_LSB_REG]); + coulomb_write_reg(LTC2943_VOLTAGE_THRESH_LOW_MSB_REG,&writeData[LTC2943_VOLTAGE_THRESH_LOW_MSB_REG]); + coulomb_write_reg(LTC2943_VOLTAGE_THRESH_LOW_LSB_REG,&writeData[LTC2943_VOLTAGE_THRESH_LOW_LSB_REG]); + coulomb_write_reg(LTC2943_CURRENT_THRESH_HIGH_MSB_REG,&writeData[LTC2943_CURRENT_THRESH_HIGH_MSB_REG]); + coulomb_write_reg(LTC2943_CURRENT_THRESH_HIGH_LSB_REG,&writeData[LTC2943_CURRENT_THRESH_HIGH_LSB_REG]); + coulomb_write_reg(LTC2943_CURRENT_THRESH_LOW_MSB_REG,&writeData[LTC2943_CURRENT_THRESH_LOW_MSB_REG]); + coulomb_write_reg(LTC2943_CURRENT_THRESH_LOW_LSB_REG,&writeData[LTC2943_CURRENT_THRESH_LOW_LSB_REG]); + coulomb_write_reg(LTC2943_TEMPERATURE_THRESH_HIGH_REG,&writeData[LTC2943_TEMPERATURE_THRESH_HIGH_REG]); + coulomb_write_reg(LTC2943_TEMPERATURE_THRESH_LOW_REG,&writeData[LTC2943_TEMPERATURE_THRESH_LOW_REG]); } - return ret; + return coulomb.ready; +} +/** + * @description: 将读取到的 状态类的 寄存器数据转换成真实数据 + * @param {type} + * @return: + */ +void coulomb_read_status_raw_to_actual() +{ + //status 转换到 对应位 + coulomb.read.actual.status.current_alret = ((coulomb.read.raw.status>>6) & 0b00000001) ? true : false; + coulomb.read.actual.status.accumulated_charge_alert = ((coulomb.read.raw.status>>5) & 0b00000001) ? true : false; + coulomb.read.actual.status.temperature_alret = ((coulomb.read.raw.status>>4) & 0b00000001) ? true : false; + coulomb.read.actual.status.charge_alert_H = ((coulomb.read.raw.status>>3) & 0b00000001) ? true : false; + coulomb.read.actual.status.charge_alert_L = ((coulomb.read.raw.status>>2) & 0b00000001) ? true : false; + coulomb.read.actual.status.voltage_alert = ((coulomb.read.raw.status>>1) & 0b00000001) ? true : false; + coulomb.read.actual.status.uvlo_alert = ( coulomb.read.raw.status & 0b00000001) ? true : false; + + //剩余电量转换 + coulomb.read.actual.accumulated_charge = (coulomb.read.raw.accumulated_charge - 32767) / 32767.0 * 30 ; + //电压转换 + coulomb.read.actual.voltage = coulomb.read.raw.voltage / 65535.0 *23.6; + //电流转换 + coulomb.read.actual.current = (coulomb.read.raw.current - 32767) / 32767.0 * 30 ; + //温度转换 + coulomb.read.actual.temperature = (coulomb.read.raw.temperature / 65535.0 * 510 ) - 273.15; +} +/** + * @description: 将读取到的 配置类的 寄存器数据转换成真实参数 + * @param {type} + * @return: + */ +void coulomb_read_config_raw_to_actual() +{ + //control寄存器转换到对应位 + coulomb.read.actual.control.adc_mode = ((coulomb.read.raw.control >> 6) & 0b00000011); + coulomb.read.actual.control.prescaler = ((coulomb.read.raw.control >> 3) & 0b00000111); + coulomb.read.actual.control.alcc_configure = ((coulomb.read.raw.control >> 1) & 0b00000011); + coulomb.read.actual.control.shutdown = ( coulomb.read.raw.control & 0b00000001); + + //阈值累计充电转换 + coulomb.read.actual.charge_threshold_H = (coulomb.read.raw.charge_threshold_H - 32767) / 32767.0 * 30; + coulomb.read.actual.charge_threshold_L = (coulomb.read.raw.charge_threshold_L - 32767) / 32767.0 * 30; + //电压阈值转换 + coulomb.read.actual.voltage_threshold_H = coulomb.read.raw.voltage_threshold_H / 65535.0 *23.6; + coulomb.read.actual.voltage_threshold_L = coulomb.read.raw.voltage_threshold_L / 65535.0 *23.6; + //电流阈值转换 + coulomb.read.actual.current_threshold_H = (coulomb.read.raw.current_threshold_H - 32767) / 32767.0 * 30 ; + coulomb.read.actual.current_threshold_L = (coulomb.read.raw.current_threshold_L - 32767) / 32767.0 * 30 ; + //温度阈值转换 + coulomb.read.actual.temperature_threshold_H = (coulomb.read.raw.temperature_threshold_H / 65535.0 * 510 ) - 273.15; + coulomb.read.actual.temperature_threshold_L = (coulomb.read.raw.temperature_threshold_L / 65535.0 * 510 ) - 273.15; +} +//读取原始寄存器数据 +/** + * @description: 读取状态类和配置类的寄存器(即所有寄存器) + * @param {type} + * @return: + */ +bool coulomb_read_status_and_config() +{ + uint8_t readData[COULOMB_REG_NUM]; + coulomb.ready = (is_coulomb_ready()==HAL_OK) ? true : false; + if(coulomb.ready) + { + HAL_I2C_Mem_Read(&hi2c1,COULOMB_ADDR ,0,1,readData, 1, 10); + HAL_I2C_Mem_Read(&hi2c1,COULOMB_ADDR ,0,COULOMB_REG_NUM,&readData[1], COULOMB_REG_NUM, 100); + + coulomb.read.raw.status = readData[LTC2943_STATUS_REG]; + coulomb.read.raw.control = readData[LTC2943_CONTROL_REG]; + + coulomb.read.raw.accumulated_charge = uint8_to_uint16(readData,LTC2943_ACCUM_CHARGE_MSB_REG,LTC2943_ACCUM_CHARGE_LSB_REG); + coulomb.read.raw.charge_threshold_H = uint8_to_uint16(readData,LTC2943_CHARGE_THRESH_HIGH_MSB_REG,LTC2943_CHARGE_THRESH_HIGH_LSB_REG); + coulomb.read.raw.charge_threshold_L = uint8_to_uint16(readData,LTC2943_CHARGE_THRESH_LOW_MSB_REG,LTC2943_CHARGE_THRESH_LOW_LSB_REG); + + coulomb.read.raw.voltage = uint8_to_uint16(readData,LTC2943_VOLTAGE_MSB_REG,LTC2943_VOLTAGE_LSB_REG); + coulomb.read.raw.voltage_threshold_H = uint8_to_uint16(readData,LTC2943_VOLTAGE_THRESH_HIGH_MSB_REG,LTC2943_VOLTAGE_THRESH_HIGH_LSB_REG); + coulomb.read.raw.voltage_threshold_L = uint8_to_uint16(readData,LTC2943_VOLTAGE_THRESH_LOW_MSB_REG,LTC2943_VOLTAGE_THRESH_LOW_LSB_REG); + + coulomb.read.raw.current = uint8_to_uint16(readData,LTC2943_CURRENT_MSB_REG,LTC2943_CURRENT_LSB_REG); + coulomb.read.raw.current_threshold_H = uint8_to_uint16(readData,LTC2943_CURRENT_THRESH_HIGH_MSB_REG,LTC2943_CURRENT_THRESH_HIGH_LSB_REG); + coulomb.read.raw.current_threshold_L = uint8_to_uint16(readData,LTC2943_CURRENT_THRESH_LOW_MSB_REG,LTC2943_CURRENT_THRESH_LOW_LSB_REG); + + coulomb.read.raw.temperature = uint8_to_uint16(readData,LTC2943_TEMPERATURE_MSB_REG,LTC2943_TEMPERATURE_LSB_REG); + coulomb.read.raw.temperature_threshold_H = (readData[LTC2943_TEMPERATURE_THRESH_HIGH_REG]<<8) | 0x00;//补充低字节 + coulomb.read.raw.temperature_threshold_L = (readData[LTC2943_TEMPERATURE_THRESH_LOW_REG]<<8) | 0x00;//补充低字节 + } + return coulomb.ready; +} + +bool coulomb_read_alcc() +{ + return HAL_GPIO_ReadPin(COULOMB_ALCC_GPIO_Port,COULOMB_ALCC_Pin); } -// -//bool coulomb_read_alcc() -//{ -// return HAL_GPIO_ReadPin(COULOMB_ALCC_GPIO_Port,COULOMB_ALCC_Pin); -//} diff --git a/Middlewares/Coulomb/coulomb.h b/Middlewares/Coulomb/coulomb.h index a1cee81..dd7afc4 100644 --- a/Middlewares/Coulomb/coulomb.h +++ b/Middlewares/Coulomb/coulomb.h @@ -12,6 +12,7 @@ #include "i2c.h" #include "stm32f1xx_hal.h" +#define COULOMB_REG_NUM 0x18 #define COULOMB_ADDR (0x64 << 1) //device address //#define COULOMB_WRITE_CMD 0x00 //coulomb write command //#define COULOMB_READ_CMD 0x01 //coulomb read command @@ -41,14 +42,13 @@ #define LTC2943_TEMPERATURE_THRESH_HIGH_REG 0x16 #define LTC2943_TEMPERATURE_THRESH_LOW_REG 0x17 -typedef struct -{ - struct - {//原始数据 + +typedef struct +{//原始数据(寄存器数据) uint8_t status; uint8_t control; - uint16_t accumulated_charge; + uint16_t accumulated_charge; //剩余电量 uint16_t charge_threshold_H; //充电阈值上限 uint16_t charge_threshold_L; //充电阈值下限 @@ -61,11 +61,11 @@ typedef struct uint16_t current_threshold_L; //电流阈值下限 uint16_t temperature; //温度 - uint8_t temperature_threshold_H; //温度阈值上限 - uint8_t temperature_threshold_L; //温度阈值下限 - }raw; - struct - {//实际数据(转换后) + uint16_t temperature_threshold_H; //温度阈值上限(只有高字节有效) + uint16_t temperature_threshold_L; //温度阈值下限(只有高字节有效) +}ltc2943_raw_t; +typedef struct +{//实际数据(转换后,物理量纲) struct { bool current_alret; @@ -76,29 +76,60 @@ typedef struct bool voltage_alert; bool uvlo_alert; }status; - float accumulated_charge; - float charge_threshold_H; //充电阈值上限 - float charge_threshold_L; //充电阈值下限 + struct + { + uint8_t adc_mode; + uint8_t prescaler; + uint8_t alcc_configure; + bool shutdown; + }control; + float accumulated_charge; //Ah 剩余容量 + float charge_threshold_H; //Ah 充电阈值上限 + float charge_threshold_L; //Ah 充电阈值下限 - float voltage; //电压 - float voltage_threshold_H; //电压阈值上限 - float voltage_threshold_L; //电压阈值下限 + float voltage; //V 电压 + float voltage_threshold_H; //V 电压阈值上限 + float voltage_threshold_L; //V 电压阈值下限 - float current; //电流 - float current_threshold_H; //电流阈值上限 - float current_threshold_L; //电流阈值下限 + float current; //A 电流 + float current_threshold_H; //A 电流阈值上限 + float current_threshold_L; //A 电流阈值下限 - float temperature; //温度 - float temperature_threshold_H; //温度阈值上限 - float temperature_threshold_L; //温度阈值下限 - }actual; + float temperature; //℃ 温度 + float temperature_threshold_H; //℃ 温度阈值上限 + float temperature_threshold_L; //℃ 温度阈值下限 +}ltc2943_actual_t; + + +typedef struct +{ + ltc2943_raw_t raw; + ltc2943_actual_t actual; +}ltc2943_t; + +typedef struct +{ + bool ready; //设备是否就绪 + ltc2943_t write; + ltc2943_t read; }coulomb_t; extern coulomb_t coulomb; -//bool is_coulomb_ready(); +//静态方法 +static HAL_StatusTypeDef is_coulomb_ready(); static HAL_StatusTypeDef coulomb_write_reg(uint8_t addr,uint8_t* pData); static HAL_StatusTypeDef coulomb_read_reg(uint8_t addr,uint8_t* pData); -HAL_StatusTypeDef coulomb_read_status(); -//bool coulomb_read_alcc(); +static uint16_t uint8_to_uint16(uint8_t *pData,uint8_t msbIndex,uint8_t lsbIndex); +static uint8_t uint16_to_uint8_msb(uint16_t data); + +//写配置方法 +void coulomb_write_config_init(); +void coulomb_write_config_actual_to_raw(); +bool coulomb_write_config(); +//读寄存器方法 +bool coulomb_read_status_and_config(); +void coulomb_read_status_raw_to_actual(); +void coulomb_read_config_raw_to_actual(); +bool coulomb_read_alcc(); #endif /* _COULOMB_H_ */