comparison Small_CPU/Src/batteryGasGauge.c @ 416:bcf447646e07

Merged in Ideenmodellierer/ostc4/Improment_NVM (pull request #37) Improment NVM
author heinrichsweikamp <bitbucket@heinrichsweikamp.com>
date Wed, 15 Jan 2020 10:53:15 +0000
parents 2fc08a0d1ec3
children aa286a4926c2
comparison
equal deleted inserted replaced
403:6f30f2011667 416:bcf447646e07
26 #include "batteryGasGauge.h" 26 #include "batteryGasGauge.h"
27 #include "baseCPU2.h" 27 #include "baseCPU2.h"
28 #include "stm32f4xx_hal.h" 28 #include "stm32f4xx_hal.h"
29 #include "i2c.h" 29 #include "i2c.h"
30 30
31 static float battery_f_voltage = 0; 31 static float battery_f_voltage = 6.0; /* max assumed voltage */
32 static float battery_f_charge_percent = 0; 32 static float battery_f_charge_percent = 0;
33 33
34 #define BGG_BATTERY_OFFSET (26123) //; 65536-(3,35Ah/0,085mAh) 34 #define BGG_BATTERY_OFFSET (26123) //; 65536-(3,35Ah/0,085mAh)
35 #define BGG_BATTERY_DIVIDER (394) //; 3,35Ah/0,085mAh/100 [%] 35 #define BGG_BATTERY_DIVIDER (394) //; 3,35Ah/0,085mAh/100 [%]
36 36
115 115
116 float battery_f_voltage_local; 116 float battery_f_voltage_local;
117 float battery_f_charge_percent_local; 117 float battery_f_charge_percent_local;
118 118
119 uint8_t bufferReceive[10]; 119 uint8_t bufferReceive[10];
120 I2C_Master_Receive( DEVICE_BATTERYGAUGE, bufferReceive, 10); 120
121 if(I2C_Master_Receive(DEVICE_BATTERYGAUGE, bufferReceive, 10) == HAL_OK)
122 {
123 battery_f_voltage_local = (float)(bufferReceive[8] * 256);
124 battery_f_voltage_local += (float)(bufferReceive[9]);
125 battery_f_voltage_local *= (float)6 / (float)0xFFFF;
126
127 // max/full: 0.085 mAh * 1 * 65535 = 5570 mAh
128 battery_f_charge_percent_local = (float)(bufferReceive[2] * 256);
129 battery_f_charge_percent_local += (float)(bufferReceive[3]);
130 battery_f_charge_percent_local -= BGG_BATTERY_OFFSET;
131 battery_f_charge_percent_local /= BGG_BATTERY_DIVIDER;
121 132
122 battery_f_voltage_local = (float)(bufferReceive[8] * 256); 133 if(battery_f_charge_percent_local < 0)
123 battery_f_voltage_local += (float)(bufferReceive[9]); 134 battery_f_charge_percent_local = 0;
124 battery_f_voltage_local *= (float)6 / (float)0xFFFF;
125 135
126 // max/full: 0.085 mAh * 1 * 65535 = 5570 mAh 136 battery_f_voltage = battery_f_voltage_local;
127 battery_f_charge_percent_local = (float)(bufferReceive[2] * 256); 137 battery_f_charge_percent = battery_f_charge_percent_local;
128 battery_f_charge_percent_local += (float)(bufferReceive[3]); 138 }
129 battery_f_charge_percent_local -= BGG_BATTERY_OFFSET;
130 battery_f_charge_percent_local /= BGG_BATTERY_DIVIDER;
131
132 if(battery_f_charge_percent_local < 0)
133 battery_f_charge_percent_local = 0;
134
135 battery_f_voltage = battery_f_voltage_local;
136 battery_f_charge_percent = battery_f_charge_percent_local;
137 } 139 }
138 140
139 141
140 void battery_gas_gauge_set_charge_full(void) 142 void battery_gas_gauge_set_charge_full(void)
141 { 143 {