Просмотр исходного кода

加入温度的测量

Signed-off-by: huhui <huhui@sharkgulf.com>
huhui 5 лет назад
Родитель
Сommit
520e50266a

+ 42 - 13
Application/app/iv_measure.c

@@ -3,6 +3,8 @@
 #include "bsp/cs1180.h"
 #include "bsp/gd32_adc.h"
 #include "bsp/clock.h"
+#include "bsp/gpio.h"
+#include "bsp/temp_lookup_tab.h"
 /* measure the current & voltage for battery pack by using
  * ms5238 & cs1180(only used when bms is in small current loading)
 */
@@ -18,9 +20,10 @@ static float vim0_50x = 0.0f;
 static float vim0_now;
 #define r_resistor 1.0f // 1ºÁÅ·
 #define r_pcb_resistor 2.0f // pcb resistor
-static float r_sense = r_resistor + r_pcb_resistor;
-static float v_ref = 3300.0f; //adc ref = 3.3v
-static float max_adc = 65535.0f;
+static const float r_sense = r_resistor + r_pcb_resistor;
+static const float v_ref = 3300.0f; //adc ref = 3.3v
+static const float max_adc = 65535.0f;
+static const float small_cur_r_sense = 360.0f;//mo
 
 static void __inline__ select_gain_10x(int select){
 	if (select){
@@ -68,24 +71,50 @@ void measure_system_init(void){
 	current_calibrate();
 }
 
-float get_imon_current(bool calibration){
+/* get battery pack's current (mA) */
+float get_imon_current(int calibration){
 	float adc = adc_sample(ADC_CHAN_IMON, calibration);
-	if (adc >= 0xFFF0){
-		//overflow, use 10x
-		if (imon_gain_now == imon_gain_50x){
-			select_gain_10x(1);
-			adc = adc_sample(ADC_CHAN_IMON, calibration);
-		}
+	if (adc >= 0xFFF0){//overflow, use 10x gain
+		select_gain_10x(1);
+		adc = adc_sample(ADC_CHAN_IMON, calibration);
+	}else if (adc <= 0x1F){// is too small, select 50x gain
+		select_gain_10x(0); 
+		adc = adc_sample(ADC_CHAN_IMON, calibration);		
 	}
 	float cali_adc = ML5238_V_RSENSER(adc, vim0_now, imon_gain_now);
 
-	return (cali_adc/max_adc) * v_ref / r_sense;
+	return (cali_adc / max_adc) * v_ref / r_sense * 1000;
 }
 
-float get_vmon_voltage(int cell, bool calibration){
+/* get cell's voltage (mV) */
+float get_vmon_voltage(int cell, int calibration){
 	ml5238_select_cell_to_vmon(cell);
 	delay_us(50);
 	float adc = adc_sample(ADC_CHAN_VMON, calibration);
 
-	return adc/max_adc * v_ref;
+	return (adc / max_adc) * v_ref * 1000;
 }
+
+/* get battery pack's aux current (mA) */
+float get_small_current(int calibration){
+	float adc = adc_sample(ADC_CHAN_AUX_CURR, calibration);
+
+	return (adc / max_adc * v_ref) / small_cur_r_sense * 1000;
+}
+
+int get_pcb_temperature(int calibration){
+	TEMP_OPEN(1);
+	delay_us(100);
+	uint16_t adc = adc_sample(ADC_CHAN_TEMPERATURE_4, calibration);
+	TEMP_OPEN(0);
+	return get_temp_by_adc(adc);
+}
+
+int get_pack_temperature(int index, int calibration){
+	TEMP_OPEN(1);
+	delay_us(100);
+	uint16_t adc = adc_sample(ADC_CHAN_TEMPERATURE_1 + index, calibration);
+	TEMP_OPEN(0);
+	return get_temp_by_adc(adc);
+}
+

+ 6 - 0
Application/app/iv_measure.h

@@ -1,5 +1,11 @@
 #ifndef _IV_Measure_H__
 #define _IV_Measure_H__
+void measure_system_init(void);
+float get_imon_current(int calibration);
+float get_vmon_voltage(int cell, int calibration);
+float get_small_current(int calibration);
+int get_pcb_temperature(int calibration);
+int get_pack_temperature(int index, int calibration);
 
 #endif /* _IV_Measure_H__ */
 

+ 1 - 0
Application/bsp/gpio.h

@@ -2,6 +2,7 @@
 #define _GPIO_H__
 #include "gd32f3x0.h"
 #include "gd32f3x0_libopt.h"
+#include "bsp/shark_bsp.h"
 
 /*switch for temperature sensers */
 #define TEMP_OPEN(x) gpio_bit_write(GPIOF,GPIO_PIN_1,(bit_status)(x))

+ 149 - 0
Application/bsp/temp_lookup_tab.h

@@ -0,0 +1,149 @@
+#ifndef _Temp_Lookup_Table_H__
+#define _Temp_Lookup_Table_H__
+
+#define ZERO_OFFSET 40 //index 40 --> 0度
+/*温感查找表
+* 一共4个温感,以下表格是3个在pack中的数据,PCB上的用这个表格近视
+*/
+static const unsigned short Temp_Lookup_Table[] = 
+{
+	0xF620,		//-40[degrees C]
+	0xF582,		//-39[degrees C]
+	0xF4DC,		//-38[degrees C]
+	0xF42E,		//-37[degrees C]
+	0xF378,		//-36[degrees C]
+	0xF2B9,		//-35[degrees C]
+	0xF1F1,		//-34[degrees C]
+	0xF120,		//-33[degrees C]
+	0xF045,		//-32[degrees C]
+	0xEF61,		//-31[degrees C]
+	0xEE73,		//-30[degrees C]
+	0xED7A,		//-29[degrees C]
+	0xEC77,		//-28[degrees C]
+	0xEB69,		//-27[degrees C]
+	0xEA51,		//-26[degrees C]
+	0xE92D,		//-25[degrees C]
+	0xE7FE,		//-24[degrees C]
+	0xE6C4,		//-23[degrees C]
+	0xE57E,		//-22[degrees C]
+	0xE42C,		//-21[degrees C]
+	0xE2CF,		//-20[degrees C]
+	0xE166,		//-19[degrees C]
+	0xDFF0,		//-18[degrees C]
+	0xDE6F,		//-17[degrees C]
+	0xDCE2,		//-16[degrees C]
+	0xDB48,		//-15[degrees C]
+	0xD9A3,		//-14[degrees C]
+	0xD7F1,		//-13[degrees C]
+	0xD634,		//-12[degrees C]
+	0xD46A,		//-11[degrees C]
+	0xD295,		//-10[degrees C]
+	0xD0B4,		//-9[degrees C]
+	0xCEC8,		//-8[degrees C]
+	0xCCD1,		//-7[degrees C]
+	0xCACF,		//-6[degrees C]
+	0xC8C1,		//-5[degrees C]
+	0xC6AA,		//-4[degrees C]
+	0xC488,		//-3[degrees C]
+	0xC25D,		//-2[degrees C]
+	0xC028,		//-1[degrees C]
+	0xBDE9,		//0[degrees C]
+	0xBBA3,		//1[degrees C]
+	0xB954,		//2[degrees C]
+	0xB6FD,		//3[degrees C]
+	0xB49F,		//4[degrees C]
+	0xB23A,		//5[degrees C]
+	0xAFCF,		//6[degrees C]
+	0xAD5E,		//7[degrees C]
+	0xAAE8,		//8[degrees C]
+	0xA86D,		//9[degrees C]
+	0xA5EE,		//10[degrees C]
+	0xA36B,		//11[degrees C]
+	0xA0E5,		//12[degrees C]
+	0x9E5D,		//13[degrees C]
+	0x9BD3,		//14[degrees C]
+	0x9947,		//15[degrees C]
+	0x96BB,		//16[degrees C]
+	0x942E,		//17[degrees C]
+	0x91A2,		//18[degrees C]
+	0x8F17,		//19[degrees C]
+	0x8C8C,		//20[degrees C]
+	0x8A04,		//21[degrees C]
+	0x877E,		//22[degrees C]
+	0x84FB,		//23[degrees C]
+	0x827C,		//24[degrees C]
+	0x8000,		//25[degrees C]
+	0x7D88,		//26[degrees C]
+	0x7B16,		//27[degrees C]
+	0x78A8,		//28[degrees C]
+	0x763F,		//29[degrees C]
+	0x73DD,		//30[degrees C]
+	0x7181,		//31[degrees C]
+	0x6F2B,		//32[degrees C]
+	0x6CDC,		//33[degrees C]
+	0x6A94,		//34[degrees C]
+	0x6853,		//35[degrees C]
+	0x661A,		//36[degrees C]
+	0x63E8,		//37[degrees C]
+	0x61BF,		//38[degrees C]
+	0x5F9E,		//39[degrees C]
+	0x5D85,		//40[degrees C]
+	0x5B74,		//41[degrees C]
+	0x596C,		//42[degrees C]
+	0x576D,		//43[degrees C]
+	0x5576,		//44[degrees C]
+	0x5389,		//45[degrees C]
+	0x51A4,		//46[degrees C]
+	0x4FC8,		//47[degrees C]
+	0x4DF4,		//48[degrees C]
+	0x4C2A,		//49[degrees C]
+	0x4A69,		//50[degrees C]
+	0x48B0,		//51[degrees C]
+	0x4701,		//52[degrees C]
+	0x455A,		//53[degrees C]
+	0x43BC,		//54[degrees C]
+	0x4227,		//55[degrees C]
+	0x409A,		//56[degrees C]
+	0x3F16,		//57[degrees C]
+	0x3D9A,		//58[degrees C]
+	0x3C27,		//59[degrees C]
+	0x3ABC,		//60[degrees C]
+	0x3959,		//61[degrees C]
+	0x37FE,		//62[degrees C]
+	0x36AC,		//63[degrees C]
+	0x3561,		//64[degrees C]
+	0x341D,		//65[degrees C]
+	0x32E2,		//66[degrees C]
+	0x31AD,		//67[degrees C]
+	0x3080,		//68[degrees C]
+	0x2F5B,		//69[degrees C]
+	0x2E3C,		//70[degrees C]
+	0x2D24,		//71[degrees C]
+	0x2C13,		//72[degrees C]
+	0x2B09,		//73[degrees C]
+	0x2A05,		//74[degrees C]
+	0x2908,		//75[degrees C]
+	0x2811,		//76[degrees C]
+	0x2720,		//77[degrees C]
+	0x2635,		//78[degrees C]
+	0x2550,		//79[degrees C]
+	0x2471,		//80[degrees C]
+	0x2397,		//81[degrees C]
+	0x22C2,		//82[degrees C]
+	0x21F4,		//83[degrees C]
+	0x212A,		//84[degrees C]
+	0x2065		//85[degrees C]
+};
+
+#include "libs/shark_types.h"
+static int get_temp_by_adc(unsigned short adc){
+	int i;
+	for (i = 0; i < NELEM(Temp_Lookup_Table); i++){
+		if (adc >= Temp_Lookup_Table[i]){
+			return (i - ZERO_OFFSET);
+		}
+	}
+	return NELEM(Temp_Lookup_Table) - 1 - ZERO_OFFSET;
+}
+
+#endif /* _Temp_Lookup_Table_H__ */

+ 1 - 1
Project/BMS.uvprojx

@@ -337,7 +337,7 @@
             <v6Rtti>0</v6Rtti>
             <VariousControls>
               <MiscControls></MiscControls>
-              <Define></Define>
+              <Define>CONFIG_BOARD_SP700</Define>
               <Undefine></Undefine>
               <IncludePath>..\Librarys\CMSIS\5.7.0\Include,..\Librarys\CMSIS\GD32F3x0\Include,..\Librarys\GD32F3x0_Drivers\include,..\Application</IncludePath>
             </VariousControls>