AppTaskUart0.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. /*
  2. * AppTaskUart0.c
  3. *
  4. * Created on: 2022年2月22日
  5. * Author: QiXiang_CHENJIE
  6. */
  7. #include "AppTaskUart0.h"
  8. #ifdef APP_UART0_ENABLE
  9. static process_Uart0 gProcess_Uart0_Task = PROCESS_UART_STATE_IDLE;
  10. #define PROC_UART0_STATE_SWITCH(a) (gProcess_Uart0_Task = a)
  11. bool bmsCellInfoDecode(uint8 *dataPtr);
  12. bool bmsOtherInfoDecode(uint8 *dataPtr);
  13. void battDataCalFunc(void);
  14. void Uart0Task(void *pvParameters)
  15. {
  16. (void)pvParameters;
  17. UartQueryType Uart0AskMsg;
  18. uint16 Reg_Num = 0;
  19. uint16 RegAddrBegin = 0;
  20. uint16 CRC_chk_buffer = 0;
  21. uint16 pReadLen = 0;
  22. UartAnsType UartAnsData;
  23. static uint8 writecounter = 0;
  24. uint16 UartNoDataReadCounter = 0;
  25. while (1)
  26. {
  27. switch (gProcess_Uart0_Task)
  28. {
  29. case PROCESS_UART_STATE_IDLE:
  30. {
  31. vTaskDelay(pdMS_TO_TICKS(10));
  32. if ((TimerCounter % 1000) == 0 && 1)
  33. {
  34. PROC_UART0_STATE_SWITCH(PROCESS_UART_STATE_READ);
  35. }
  36. else if (TimerCounter % 100 == 0)
  37. {
  38. PROC_UART0_STATE_SWITCH(PROCESS_UART_STATE_WRITE);
  39. }
  40. break;
  41. }
  42. case PROCESS_UART_STATE_READ:
  43. {
  44. static uint16 readCnt = 0;
  45. /*电压电流数据读取(第一次读取)*/
  46. uint8 *UartDataRecv = NULL;
  47. pReadLen = 0;
  48. Dio_FlipChannel(DioConf_DioChannel_PTE1_GPIO_OUT_MCU_LED2);
  49. RegAddrBegin = 0x00;
  50. Reg_Num =AppDataInfo.BattCellCount + 3;
  51. Uart0AskMsg.Bms_Address = BMS_ADDRESS_CODE;
  52. Uart0AskMsg.Bms_Funcode = UART_READ_CODE;
  53. Uart0AskMsg.Reg_Begin_H = RegAddrBegin>>8;
  54. Uart0AskMsg.Reg_Begin_L = RegAddrBegin;
  55. Uart0AskMsg.Reg_Num_H = Reg_Num >> 8;
  56. Uart0AskMsg.Reg_Num_L = Reg_Num;
  57. CRC_chk_buffer = crc_chk((uint8 *)&Uart0AskMsg, 6);
  58. Uart0AskMsg.CRC_L = CRC_chk_buffer;
  59. Uart0AskMsg.CRC_H = CRC_chk_buffer >> 8;
  60. /*RS485查询命令需要使能*/
  61. UART_Send_Data(UART_LPUART0, (uint8 *)&Uart0AskMsg, sizeof(Uart0AskMsg), 1000);
  62. uint16 ReadLen1 = 0;
  63. uint16 ReadLenTar = 0;
  64. uint8 ReadDelayCounter = 0;
  65. ReadLenTar = (Reg_Num)*2+5;
  66. memset((uint8 *)&UartAnsData,0x00,sizeof(UartAnsData));
  67. while(1)
  68. {
  69. UART_Receive_Data(UART_LPUART0, &UartDataRecv, &pReadLen, 100);
  70. if(pReadLen>2)
  71. {
  72. memcpy((uint8 *)&UartAnsData+ReadLen1,UartDataRecv,pReadLen);
  73. ReadLen1 = ReadLen1 + pReadLen;
  74. }
  75. else
  76. {
  77. ReadDelayCounter++;
  78. }
  79. if(ReadLen1>=ReadLenTar)
  80. {
  81. ReadDelayCounter = 0;
  82. pReadLen = ReadLen1;
  83. ReadLen1 = 0;
  84. break;
  85. }
  86. if(ReadDelayCounter>=20)
  87. {
  88. ReadDelayCounter = 0;
  89. pReadLen = 0;
  90. ReadLen1 = 0;
  91. break;
  92. }
  93. if(UartDataRecv!=NULL)
  94. {
  95. free(UartDataRecv);
  96. }
  97. UartDataRecv=NULL;
  98. }
  99. if (pReadLen > 0)
  100. {
  101. uint16 CrcChkGet = 0xffff;
  102. uint16 CrcChkCal = 0x0000;
  103. CrcChkCal = crc_chk((uint8 *)&UartAnsData, pReadLen-2);
  104. CrcChkGet = ((uint16)(UartAnsData.Data[pReadLen-1-3])<<8) | ((uint16)(UartAnsData.Data[pReadLen-2-3]));
  105. if(CrcChkCal==CrcChkGet)
  106. {
  107. bmsCellInfoDecode(UartAnsData.Data);
  108. UartNoDataReadCounter = 0;
  109. }
  110. else
  111. {
  112. UartNoDataReadCounter++;
  113. }
  114. }
  115. else
  116. {
  117. UartNoDataReadCounter++;
  118. }
  119. /*温度及其他状态信息读取(第二次读取)*/
  120. pReadLen = 0;
  121. RegAddrBegin = 0x03 + AppDataInfo.BattCellCount;
  122. Reg_Num = 0x1B + AppDataInfo.BattTempCount; //读取的寄存器数据,暂定,分开读取
  123. Uart0AskMsg.Bms_Address = BMS_ADDRESS_CODE;
  124. Uart0AskMsg.Bms_Funcode = UART_READ_CODE;
  125. Uart0AskMsg.Reg_Begin_H = RegAddrBegin>>8;
  126. Uart0AskMsg.Reg_Begin_L = RegAddrBegin;
  127. Uart0AskMsg.Reg_Num_H = Reg_Num >> 8;
  128. Uart0AskMsg.Reg_Num_L = Reg_Num;
  129. CRC_chk_buffer = crc_chk((uint8 *)&Uart0AskMsg, 6);
  130. Uart0AskMsg.CRC_L = CRC_chk_buffer;
  131. Uart0AskMsg.CRC_H = CRC_chk_buffer >> 8;
  132. /*RS485查询命令需要使能*/
  133. UART_Send_Data(UART_LPUART0, (uint8 *)&Uart0AskMsg, sizeof(Uart0AskMsg), 1000);
  134. memset((uint8 *)&UartAnsData,0x00,sizeof(UartAnsData));
  135. ReadLenTar = (Reg_Num)*2+5;
  136. while(1)
  137. {
  138. UART_Receive_Data(UART_LPUART0, &UartDataRecv, &pReadLen, 100);
  139. if(pReadLen>2)
  140. {
  141. memcpy((uint8 *)&UartAnsData+ReadLen1,UartDataRecv,pReadLen);
  142. ReadLen1 = ReadLen1 + pReadLen;
  143. }
  144. else
  145. {
  146. ReadDelayCounter++;
  147. }
  148. if(ReadLen1>=ReadLenTar)
  149. {
  150. ReadDelayCounter = 0;
  151. pReadLen = ReadLen1;
  152. ReadLen1 = 0;
  153. break;
  154. }
  155. if(ReadDelayCounter>=20)
  156. {
  157. ReadDelayCounter = 0;
  158. pReadLen = 0;
  159. ReadLen1 = 0;
  160. break;
  161. }
  162. if(UartDataRecv!=NULL)
  163. {
  164. free(UartDataRecv);
  165. }
  166. UartDataRecv=NULL;
  167. }
  168. if (pReadLen > 0)
  169. {
  170. uint16 CrcChkGet = 0xffff;
  171. uint16 CrcChkCal = 0x0000;
  172. CrcChkCal = crc_chk((uint8 *)&UartAnsData, pReadLen-2);
  173. CrcChkGet = ((uint16)(UartAnsData.Data[pReadLen-1-3])<<8) | ((uint16)(UartAnsData.Data[pReadLen-2-3]));
  174. if(CrcChkCal==CrcChkGet)
  175. {
  176. bmsOtherInfoDecode(UartAnsData.Data);
  177. UartNoDataReadCounter = 0;
  178. }
  179. else
  180. {
  181. UartNoDataReadCounter++;
  182. }
  183. }
  184. else
  185. {
  186. UartNoDataReadCounter++;
  187. }
  188. //没有读到数据时的默认值
  189. if(UartNoDataReadCounter>10)
  190. {
  191. memset((uint8 *)&UartAnsData,0x00,sizeof(UartAnsData));
  192. bmsCellInfoDecode(UartAnsData.Data);
  193. bmsOtherInfoDecode(UartAnsData.Data);
  194. PutErrorNum(ErrorArray, sizeof(ErrorArray)/2, 1);
  195. vTaskDelay(pdMS_TO_TICKS(5000));
  196. }
  197. if(readCnt%60==0)
  198. {
  199. /**电表数据读取*/
  200. for(uint8 i=0;i<2;i++)
  201. {
  202. /*电表参数读取*/
  203. static uint16 pt[2] = {0};
  204. static uint16 ct[2] = {0};
  205. pReadLen = 0;
  206. uint8 *UartRecvPtr=NULL;
  207. RegAddrBegin = 0x03;
  208. Reg_Num = 0x02;
  209. Uart0AskMsg.Bms_Address = METER1_ADDRESS_CODE+i;//第一次读02,第二次读03
  210. Uart0AskMsg.Bms_Funcode = UART_READ_CODE;
  211. Uart0AskMsg.Reg_Begin_H = RegAddrBegin>>8;
  212. Uart0AskMsg.Reg_Begin_L = RegAddrBegin;
  213. Uart0AskMsg.Reg_Num_H = Reg_Num >> 8;
  214. Uart0AskMsg.Reg_Num_L = Reg_Num;
  215. CRC_chk_buffer = crc_chk((uint8 *)&Uart0AskMsg, 6);
  216. Uart0AskMsg.CRC_L = CRC_chk_buffer;
  217. Uart0AskMsg.CRC_H = CRC_chk_buffer >> 8;
  218. UART_Send_Data(UART_LPUART0, (uint8 *)&Uart0AskMsg, sizeof(Uart0AskMsg), 100);
  219. UART_Receive_Data(UART_LPUART0, &UartRecvPtr, &pReadLen, 1000);
  220. //电表数据解析
  221. if (pReadLen > 0)
  222. {
  223. uint16 CrcChkGet = 0xffff;
  224. uint16 CrcChkCal = 0x0000;
  225. CrcChkCal = crc_chk(UartRecvPtr, pReadLen-2);
  226. CrcChkGet = ((uint16)(*(UartRecvPtr+pReadLen-1))<<8) | ((uint16)(*(UartRecvPtr+pReadLen-2)));
  227. if(CrcChkCal==CrcChkGet)
  228. {
  229. pt[i] = ((uint16)(*(UartRecvPtr+3))<<8) | ((uint16)(*(UartRecvPtr+4)));
  230. ct[i] = ((uint16)(*(UartRecvPtr+5))<<8) | ((uint16)(*(UartRecvPtr+6)));
  231. }
  232. }
  233. if(UartRecvPtr!=NULL)
  234. {
  235. free(UartRecvPtr);
  236. }
  237. UartRecvPtr=NULL;
  238. /*电表电量数据读取*/
  239. pReadLen = 0;
  240. RegAddrBegin = 0x3F;
  241. Reg_Num = 0x04;
  242. Uart0AskMsg.Bms_Address = METER1_ADDRESS_CODE+i;//第一次读02,第二次读03
  243. Uart0AskMsg.Bms_Funcode = UART_READ_CODE;
  244. Uart0AskMsg.Reg_Begin_H = RegAddrBegin>>8;
  245. Uart0AskMsg.Reg_Begin_L = RegAddrBegin;
  246. Uart0AskMsg.Reg_Num_H = Reg_Num >> 8;
  247. Uart0AskMsg.Reg_Num_L = Reg_Num;
  248. CRC_chk_buffer = crc_chk((uint8 *)&Uart0AskMsg, 6);
  249. Uart0AskMsg.CRC_L = CRC_chk_buffer;
  250. Uart0AskMsg.CRC_H = CRC_chk_buffer >> 8;
  251. UART_Send_Data(UART_LPUART0, (uint8 *)&Uart0AskMsg, sizeof(Uart0AskMsg), 100);
  252. UART_Receive_Data(UART_LPUART0, &UartRecvPtr, &pReadLen, 1000);
  253. //电表数据解析
  254. if (pReadLen > 0)
  255. {
  256. uint16 CrcChkGet = 0xffff;
  257. uint16 CrcChkCal = 0x0000;
  258. CrcChkCal = crc_chk(UartRecvPtr, pReadLen-2);
  259. CrcChkGet = ((uint16)(*(UartRecvPtr+pReadLen-1))<<8) | ((uint16)(*(UartRecvPtr+pReadLen-2)));
  260. if(CrcChkCal==CrcChkGet)
  261. {
  262. //meterAllPwr[i] = pt[i]*ct[i]*(((uint32)(*(UartRecvPtr+3))<<24)|((uint32)(*(UartRecvPtr+4))<<16)|((uint32)(*(UartRecvPtr+5))<<8) | ((uint32)(*(UartRecvPtr+6))));
  263. meterPosPwr[i] = pt[i]*ct[i]*((uint32)(*(UartRecvPtr+3))<<24)|((uint32)(*(UartRecvPtr+4))<<16)|((uint32)(*(UartRecvPtr+5))<<8) | ((uint32)(*(UartRecvPtr+6)));
  264. meterNegPwr[i] = pt[i]*ct[i]*((uint32)(*(UartRecvPtr+7))<<24)|((uint32)(*(UartRecvPtr+8))<<16)|((uint32)(*(UartRecvPtr+9))<<8) | ((uint32)(*(UartRecvPtr+10)));
  265. meterPosPwr[i] = meterPosPwr[i] * 100;
  266. meterNegPwr[i] = meterNegPwr[i] * 100;
  267. meterAllPwr[i] = meterPosPwr[i] + meterNegPwr[i];
  268. }
  269. }
  270. if(UartRecvPtr!=NULL)
  271. {
  272. free(UartRecvPtr);
  273. }
  274. UartRecvPtr=NULL;
  275. }
  276. }
  277. readCnt++;
  278. PROC_UART0_STATE_SWITCH(PROCESS_UART_STATE_IDLE);
  279. break;
  280. }
  281. case PROCESS_UART_STATE_WRITE:
  282. {
  283. if(battSeparateEnable)//最多重新写5次
  284. {
  285. uint16 RegAddress = 0;
  286. uint16 CRC_chk_buffer = 0;
  287. uint8 WriteData[2] = {0x00,0x00};
  288. if(!battSeparateCtlState)
  289. {
  290. setbit(WriteData[0],7);
  291. }
  292. else
  293. {
  294. clrbit(WriteData[0],7);
  295. }
  296. RegAddress = 0x1B + AppDataInfo.BattCellCount + AppDataInfo.BattTempCount;
  297. UartWriteMsgType Uart_Write_Msg;
  298. Uart_Write_Msg.Bms_Address = BMS_ADDRESS_CODE;
  299. Uart_Write_Msg.Bms_Funcode = UART_WRITE_CODE;
  300. Uart_Write_Msg.Reg_Begin_H = RegAddress >> 8;
  301. Uart_Write_Msg.Reg_Begin_L = RegAddress;
  302. Uart_Write_Msg.Reg_Num_H = 0x00;
  303. Uart_Write_Msg.Reg_Num_L = 0x01;
  304. Uart_Write_Msg.Data_Count = 0x02; //要写入的字节数
  305. memcpy(Uart_Write_Msg.Data, WriteData, 2);
  306. CRC_chk_buffer = crc_chk((uint8 *)&Uart_Write_Msg, sizeof(Uart_Write_Msg) - 2);
  307. Uart_Write_Msg.CRC_L = CRC_chk_buffer;
  308. Uart_Write_Msg.CRC_H = CRC_chk_buffer >> 8;
  309. uint8 *UartRecvPtr = NULL;
  310. UART_Query_Data(UART_LPUART0,UART_LPUART0,(uint8 *)&Uart_Write_Msg,sizeof(Uart_Write_Msg),&UartRecvPtr, &pReadLen,500);
  311. if (pReadLen>3&&*(UartRecvPtr+1) == 0x10)
  312. {
  313. writecounter = 0;
  314. battSeparateEnable = 0;
  315. }
  316. else
  317. {
  318. writecounter++;
  319. }
  320. if(UartRecvPtr != NULL)
  321. {
  322. free(UartRecvPtr);
  323. }
  324. UartRecvPtr = NULL;
  325. }
  326. if(writecounter>=5)
  327. {
  328. battSeparateEnable = 0;
  329. writecounter = 0;
  330. }
  331. PROC_UART0_STATE_SWITCH(PROCESS_UART_STATE_IDLE);
  332. break;
  333. }
  334. case PROCESS_UART_STATE_SLEEP:
  335. {
  336. break;
  337. }
  338. default:
  339. break;
  340. }
  341. }
  342. }
  343. bool bmsCellInfoDecode(uint8 *dataPtr) //根据串口协议确定的解码函数,电压电流解码
  344. {
  345. uint8 i = 0;
  346. sint8 BattCurrentNegFlag = 1;
  347. uint16 Batt_current;
  348. uint8 BATT_CELL_VOL_NUM = 0, BATT_TEMP_NUM = 0;
  349. uint32 Battsumvoltage = 0;
  350. BATT_CELL_VOL_NUM = (dataPtr[(0x00) * 2] << 8) | (dataPtr[(0x00) * 2 + 1]);
  351. BATT_TEMP_NUM = ((dataPtr[(0x01) * 2] << 8) | (dataPtr[(0x01) * 2 + 1])) - BmsOtherTemp;
  352. if (BATT_CELL_VOL_NUM != AppDataInfo.BattCellCount || BATT_TEMP_NUM != AppDataInfo.BattTempCount)
  353. {
  354. BATT_CELL_VOL_NUM = AppDataInfo.BattCellCount;
  355. BATT_TEMP_NUM = AppDataInfo.BattTempCount;
  356. }
  357. Batt_current = (dataPtr[(0x02 + BATT_CELL_VOL_NUM) * 2] << 8) | (dataPtr[(0x02 + BATT_CELL_VOL_NUM) * 2 + 1]);
  358. Batt_current = Batt_current*100;//平峰协议单位是1A
  359. //原始数据:充电为负,放电为正
  360. if (Batt_current < 0x8000) //充电过程
  361. {
  362. if (Batt_current > 0x8000) // 数据为负
  363. {
  364. //求补码,结果为负
  365. Batt_current = (uint16)((uint16)(~(Batt_current)) + 1);
  366. Batt_current = Batt_current / 10;
  367. BattCurrentNegFlag = -1;
  368. }
  369. else
  370. {
  371. //源码,结果为负
  372. Batt_current = Batt_current / 10;
  373. BattCurrentNegFlag = -1;
  374. }
  375. }
  376. else //放电过程
  377. {
  378. if (Batt_current > 0x8000) // 数据为负
  379. {
  380. //求补码,结果为正
  381. Batt_current = (uint16)((uint16)(~(Batt_current)) + 1);
  382. Batt_current = Batt_current / 10;
  383. BattCurrentNegFlag = 1;
  384. }
  385. else
  386. {
  387. //源码,结果为正
  388. Batt_current = Batt_current / 10;
  389. BattCurrentNegFlag = 1;
  390. }
  391. }
  392. battI = Batt_current * BattCurrentNegFlag + 0x2710;
  393. // sint16 BattI_Temp = 0;//做电压补偿需要的参数
  394. // BattI_Temp = Batt_current * BattCurrentNegFlag;
  395. for (i = 0; i < BATT_CELL_VOL_NUM; i++)
  396. {
  397. //battCellU[i] = ((dataPtr[(0x02 + i) * 2] << 8) | dataPtr[(0x02 + i) * 2 + 1]) + (INT16)(cmnm_R_voloffset[i] * BattI_Temp / 10000);//不使用电压补偿
  398. battCellU[i] = ((dataPtr[(0x02 + i) * 2] << 8) | dataPtr[(0x02 + i) * 2 + 1]);
  399. Battsumvoltage = Battsumvoltage + battCellU[i];
  400. }
  401. avrgCellVol = Battsumvoltage / BATT_CELL_VOL_NUM;
  402. battPackVol = Battsumvoltage/100;
  403. battDataCalFunc();
  404. return true;
  405. }
  406. bool bmsOtherInfoDecode(uint8 *dataPtr)//其他数据解析
  407. {
  408. uint8 TEMP_NUM = 0, temp = 0;
  409. sint8 OtherInfoAddrOffset = -3;//其他数据解析的起始位置改变,需要向前减3
  410. TEMP_NUM = AppDataInfo.BattTempCount + BmsOtherTemp;
  411. bmsHwVersion = dataPtr[(0x08 + OtherInfoAddrOffset + TEMP_NUM) * 2 + 1];
  412. bmsSwVersion = dataPtr[(0x08 + OtherInfoAddrOffset + TEMP_NUM) * 2];
  413. temp = ((dataPtr[(0x09 + OtherInfoAddrOffset + TEMP_NUM) * 2 + 1]) >> 1) & 0x03;
  414. battMOSSwitchState = ((temp & 0x01) << 1) | ((temp & 0x02) >> 1);
  415. battSOC = dataPtr[(0x0B + OtherInfoAddrOffset + TEMP_NUM) * 2 + 1];
  416. battSOH = dataPtr[(0x0C + OtherInfoAddrOffset + TEMP_NUM) * 2 + 1];
  417. Battdesigncap = (dataPtr[(0x0E + OtherInfoAddrOffset + TEMP_NUM) * 2]) << 24 | (dataPtr[(0x0E + OtherInfoAddrOffset + TEMP_NUM) * 2 + 1]) << 16 | (dataPtr[(0x0F + OtherInfoAddrOffset + TEMP_NUM) * 2]) << 8 | (dataPtr[(0x0F + OtherInfoAddrOffset + TEMP_NUM) * 2 + 1]);
  418. // battPackVol = ((dataPtr[(0x18 + OtherInfoAddrOffset + TEMP_NUM) * 2]) << 8 | (dataPtr[(0x18 + OtherInfoAddrOffset + TEMP_NUM) * 2 + 1])) / 10; // uint 100mV
  419. maxCellVol = (dataPtr[(0x19 + OtherInfoAddrOffset + TEMP_NUM) * 2] << 8) | dataPtr[(0x19 + OtherInfoAddrOffset + TEMP_NUM) * 2 + 1];
  420. minCellVol = (dataPtr[(0x1A + OtherInfoAddrOffset + TEMP_NUM) * 2] << 8) | dataPtr[(0x1A + OtherInfoAddrOffset + TEMP_NUM) * 2 + 1];
  421. for (uint8 i = 0; i < AppDataInfo.BattTempCount; i++)
  422. {
  423. battCellTemp[i] = dataPtr[(0x06 + OtherInfoAddrOffset + i) * 2 + 1];
  424. }
  425. battWorkState = (dataPtr[(0x03 + OtherInfoAddrOffset) * 2 + 1]) & 0x03; //电池状态(原始数据),0表示静置,1表示放电,2表示充电
  426. //状态量上传
  427. battHeatState = getbit(dataPtr[(0x09 + OtherInfoAddrOffset + TEMP_NUM) * 2 + 1],5);
  428. battFanState = getbit(dataPtr[(0x09 + OtherInfoAddrOffset + TEMP_NUM) * 2 + 1],3);
  429. battRelayState = getbit(dataPtr[(0x09 + OtherInfoAddrOffset + TEMP_NUM) * 2 + 1],4);
  430. battConverState = getbit(dataPtr[(0x09 + OtherInfoAddrOffset + TEMP_NUM) * 2 + 1],6);
  431. battSeparateState = !getbit(dataPtr[(0x1B + OtherInfoAddrOffset + TEMP_NUM) * 2],7);
  432. if (battSeparateState==1)
  433. {
  434. battMOSSwitchState = battMOSSwitchState | (0x01 << 2);
  435. }
  436. else
  437. {
  438. battMOSSwitchState = battMOSSwitchState | (0x00 << 2);
  439. }
  440. //故障状态映关系(先完成告警状态简单上传)
  441. uint8 ErrorArrayTemp[32] ={0};
  442. uint8 ErrorArrayNumTemp[32]={81,87,83,89,97,99,132,131,134,84,90,82,88,135,136,98,100};
  443. ErrorArrayTemp[0] = getbit(dataPtr[(0x03 + OtherInfoAddrOffset) * 2],0);//电芯电压过高切断告警-0-81
  444. ErrorArrayTemp[1] = getbit(dataPtr[(0x03 + OtherInfoAddrOffset) * 2],1);//总压过高切断告警-1-87
  445. ErrorArrayTemp[2] = getbit(dataPtr[(0x04 + OtherInfoAddrOffset) * 2 + 1],0);//电芯电压过低切断告警2-83
  446. ErrorArrayTemp[3] = getbit(dataPtr[(0x04 + OtherInfoAddrOffset) * 2 + 1],1);//总压过低切断告警3-89
  447. ErrorArrayTemp[4] = getbit(dataPtr[(0x04 + OtherInfoAddrOffset) * 2],4);//单节温度过高切断4-97
  448. ErrorArrayTemp[5] = getbit(dataPtr[(0x04 + OtherInfoAddrOffset) * 2],5);//单节温度过低切断5-99
  449. ErrorArrayTemp[6] = getbit(dataPtr[(0x05 + OtherInfoAddrOffset) * 2 + 1],1);//放电电流切断6-132
  450. ErrorArrayTemp[7] = getbit(dataPtr[(0x05 + OtherInfoAddrOffset) * 2 + 1],2);//充电电流切断7-131
  451. ErrorArrayTemp[8] = getbit(dataPtr[(0x05 + OtherInfoAddrOffset) * 2 + 1],3);//BTU连接告警8-134
  452. ErrorArrayTemp[9] = getbit(dataPtr[(0x0A + OtherInfoAddrOffset + TEMP_NUM) * 2 + 1],0);//单节电压下限告警9-84
  453. ErrorArrayTemp[10] = getbit(dataPtr[(0x0A + OtherInfoAddrOffset + TEMP_NUM) * 2 + 1],1);//组电压下限告警10-90
  454. ErrorArrayTemp[11] = getbit(dataPtr[(0x0A + OtherInfoAddrOffset + TEMP_NUM) * 2 + 1],2);//单节电压上限告警11-82
  455. ErrorArrayTemp[12] = getbit(dataPtr[(0x0A + OtherInfoAddrOffset + TEMP_NUM) * 2 + 1],3);//组单节电压上限告警12-88
  456. ErrorArrayTemp[13] = getbit(dataPtr[(0x0A + OtherInfoAddrOffset + TEMP_NUM) * 2 + 1],4);//放电电流告警13-135
  457. ErrorArrayTemp[14] = getbit(dataPtr[(0x0A + OtherInfoAddrOffset + TEMP_NUM) * 2 + 1],5);//充电电流告警14-136
  458. ErrorArrayTemp[15] = getbit(dataPtr[(0x0A + OtherInfoAddrOffset + TEMP_NUM) * 2 + 1],6);//单节温度上限告警15-98
  459. ErrorArrayTemp[16] = getbit(dataPtr[(0x0A + OtherInfoAddrOffset + TEMP_NUM) * 2 + 1],7);//单节温度下线告警16-100
  460. for(uint8 i=0,j=0;(i<32&&j<5);i++)
  461. {
  462. if(ErrorArrayTemp[i]==1)
  463. {
  464. PutErrorNum(ErrorArray, sizeof(ErrorArray)/2, ErrorArrayNumTemp[i]);
  465. j++;
  466. }
  467. }
  468. return true;
  469. }
  470. void battDataCalFunc(void)
  471. {
  472. static uint32 timercounterCal = 0;
  473. uint16 timediff = 0;
  474. float battCapTempOnce = 0;
  475. static float battCapTemp = 0;
  476. static float battEnrgTemp = 0;
  477. timediff = TimerCounter - timercounterCal;
  478. timercounterCal = TimerCounter;
  479. if(timediff==0 ||timediff>10000)
  480. {
  481. return;
  482. }
  483. if(battI>10000)
  484. {
  485. battCapTempOnce = ((battI - 10000)/10.0)*(timediff/1000/3600.0);
  486. battCapTemp = battCapTempOnce + battCapTemp;
  487. battEnrgTemp = (battCapTempOnce*battPackVol)/10000.0 + battEnrgTemp;
  488. if(battEnrgTemp>1)
  489. {
  490. AppConfigInfo.AppDataInfo.battDischrgAccCap = AppConfigInfo.AppDataInfo.battDischrgAccCap + (uint32)battCapTemp;
  491. AppConfigInfo.AppDataInfo.battDischrgAccEnrg = AppConfigInfo.AppDataInfo.battDischrgAccEnrg + (uint32)battEnrgTemp;
  492. AppConfigInfo.AppDataInfo.battCycleTimes = AppConfigInfo.AppDataInfo.battDischrgAccEnrg/49;
  493. AppConfigInfo.appSaveFlg = true;
  494. battEnrgTemp = 0;
  495. battCapTemp = 0;
  496. }
  497. }
  498. }
  499. #endif