BMS I/Fプログラム

Dependencies:   mbed

Revision:
3:8607d7ab7f25
Parent:
2:c7ea42f45ff5
Child:
4:c5b0a318c84d
--- a/main.cpp	Fri Apr 29 08:13:52 2016 +0000
+++ b/main.cpp	Fri Apr 29 09:00:40 2016 +0000
@@ -1,5 +1,6 @@
 #include "mbed.h"
 #include "LTC6803.h"
+#define BMS_NUM 2
 
 DigitalOut myled(LED1);
 SPI spi(dp2, dp1, dp6); // mosi, miso, sclk
@@ -74,10 +75,10 @@
 
 
 int main() {
-    unsigned char cmd, tmp, datnum;
-    unsigned char dat[16], getdat[18];
+    unsigned char cmd, tmp, datnum, getpec;
+    unsigned char dat[16], getdat[18][BMS_NUM];
 //    float cellvol[4];
-    int cellvol[12];
+    int cellvol[12][BMS_NUM];
     int cellallvol;
     char str[32];
     cs = 1;
@@ -95,11 +96,13 @@
     dat[3] = 0xff;
     dat[4] = 42;
     dat[5] = 255;
-    for(int loop = 0;loop < 6; loop++){
-        spi.write(dat[loop]);
+    for(int loop_1 = 0; loop_1 < BMS_NUM; loop_1++){
+        for(int loop = 0;loop < 6; loop++){
+            spi.write(dat[loop]);
+        }
+        cmd = CalcCRCLTC6803(dat, 6);
+        spi.write(cmd);
     }
-    cmd = CalcCRCLTC6803(dat, 6);
-    spi.write(cmd);
     cs = 1;
     wait(0.001);
     while(1) {
@@ -107,34 +110,38 @@
         cmd = RDCV;
         spi.write(cmd);
         spi.write(CalcCRCLTC6803(&cmd, 1));
-        for(int loop = 0;loop < 18; loop++){
-            getdat[loop] = spi.write(0);
+        for(int bms = 0; bms < BMS_NUM; bms++){
+            for(int cell = 0;cell < 18; cell++){
+                getdat[cell][bms] = spi.write(0);
+            }
+            getpec = spi.write(0);
         }
         cs = 1;
         wait(0.001);
         cs = 0;
         pc.printf("\033[2J");
         pc.printf("\033[%d;%dH" , 0, 0);
-        cellvol[0] = (((getdat[1] & 0x0f) << 8) | getdat[0]);
-        cellvol[1] = ((getdat[2] << 4) | ((getdat[1] & 0xf0) >> 4));
-        cellvol[2] = (((getdat[4] & 0x0f) << 8) | getdat[3]);
-        cellvol[3] = ((getdat[5] << 4) | ((getdat[4] & 0xf0) >> 4));
-        cellvol[4] = (((getdat[7] & 0x0f) << 8) | getdat[6]);
-        cellvol[5] = ((getdat[8] << 4) | ((getdat[7] & 0xf0) >> 4));
-        cellvol[6] = (((getdat[10] & 0x0f) << 8) | getdat[9]);
-        cellvol[7] = ((getdat[11] << 4) | ((getdat[10] & 0xf0) >> 4));
-        cellvol[8] = (((getdat[13] & 0x0f) << 8) | getdat[12]);
-        cellvol[9] = ((getdat[14] << 4) | ((getdat[13] & 0xf0) >> 4));
-        cellvol[10] = (((getdat[16] & 0x0f) << 8) | getdat[15]);
-        cellvol[11] = ((getdat[17] << 4) | ((getdat[16] & 0xf0) >> 4));
         cellallvol = 0;
-
-        for(int loop = 0;loop < 12; loop++){
-            //表示する数値は(cellvol[loop] - 512)*1.5mV
-            pc.printf("Cell%3d = %5dmV\n", loop, ((cellvol[loop] - 512)+((cellvol[loop] - 512)/2)));
-            cellallvol += cellvol[loop];
+        for(int bms = 0; bms < BMS_NUM; bms++){
+            cellvol[0][bms] = (((getdat[1][bms] & 0x0f) << 8) | getdat[0][bms]);
+            cellvol[1][bms] = ((getdat[2][bms] << 4) | ((getdat[1][bms] & 0xf0) >> 4));
+            cellvol[2][bms] = (((getdat[4][bms] & 0x0f) << 8) | getdat[3][bms]);
+            cellvol[3][bms] = ((getdat[5][bms] << 4) | ((getdat[4][bms] & 0xf0) >> 4));
+            cellvol[4][bms] = (((getdat[7][bms] & 0x0f) << 8) | getdat[6][bms]);
+            cellvol[5][bms] = ((getdat[8][bms] << 4) | ((getdat[7][bms] & 0xf0) >> 4));
+            cellvol[6][bms] = (((getdat[10][bms] & 0x0f) << 8) | getdat[9][bms]);
+            cellvol[7][bms] = ((getdat[11][bms] << 4) | ((getdat[10][bms] & 0xf0) >> 4));
+            cellvol[8][bms] = (((getdat[13][bms] & 0x0f) << 8) | getdat[12][bms]);
+            cellvol[9][bms] = ((getdat[14][bms] << 4) | ((getdat[13][bms] & 0xf0) >> 4));
+            cellvol[10][bms] = (((getdat[16][bms] & 0x0f) << 8) | getdat[15][bms]);
+            cellvol[11][bms] = ((getdat[17][bms] << 4) | ((getdat[16][bms] & 0xf0) >> 4));
+            for(int loop = 0;loop < (12); loop++){
+                //表示する数値は(cellvol[loop] - 512)*1.5mV
+                pc.printf("Cell%2d = %5dmV\n", (loop*bms) + 1, ((cellvol[loop][bms] - 512)+((cellvol[loop][bms] - 512)/2)));
+                cellallvol += cellvol[loop][bms];
+            }
         }
-        pc.printf("CellALL = %5dmV\n", ((cellallvol - 512*12)+((cellallvol - 512*12)/2)));
+        pc.printf("CellALL = %5dmV\n", ((cellallvol - 512*(12 * BMS_NUM))+((cellallvol - 512*(12 * BMS_NUM))/2)));
 
         cmd = STCVADALL;