![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
BMS I/Fプログラム
main.cpp
- Committer:
- NT32
- Date:
- 2016-04-29
- Revision:
- 2:c7ea42f45ff5
- Parent:
- 1:5690684e5460
- Child:
- 3:8607d7ab7f25
File content as of revision 2:c7ea42f45ff5:
#include "mbed.h" #include "LTC6803.h" DigitalOut myled(LED1); SPI spi(dp2, dp1, dp6); // mosi, miso, sclk DigitalOut cs(dp4); Serial pc(dp16, dp15); unsigned char CalcCRCLTC6803(unsigned char * dat, unsigned char numdat); static unsigned char crc8_table[256]; // 8-bit table for PEC calc static int made_table = 0; // table made flag unsigned char packet[18]={0}; // used for PEC calc unsigned char PECbyte; // PEC of a byte unsigned char PECpacket; // PEC of packet unsigned char PECpacketREAD; // value that PECpacket should be as read from 6803 static void init_crc8(); void setPECbyte(unsigned char m); void setPECpacket(unsigned char np); static void init_crc8() { int z,j; unsigned char cr; if (!made_table) { for (z = 0; z < 256; z ++) { cr = z; for (j = 0; j < 8; j ++) { cr = (cr << 1) ^ ((cr & 0x80) ? 0x07 : 0); } crc8_table[z] = cr & 0xFF; } made_table = 1; } } void setPECbyte(unsigned char m) { PECbyte = 0x41; // initialize PECbyte if (!made_table) { init_crc8(); } PECbyte = crc8_table[(PECbyte) ^ m]; } void setPECpacket(unsigned char np) { // np is number of bytes currently in packet[] int z; PECpacket = 0x41; // initialize PECpacket if (!made_table) { init_crc8(); } for (z = 0; z < np; z ++) { PECpacket = crc8_table[(PECpacket) ^ packet[z]]; } } unsigned char CalcCRCLTC6803(unsigned char * dat, unsigned char numdat){ unsigned char pec; char loop, outerloop; datainput in; in.byte = 0; pec = CRCDEFAULT; for(outerloop = 0; outerloop < numdat; outerloop++){ for(loop = 8; loop > 0; loop--){ in.in.b0 = (dat[outerloop] >> (loop - 1)) ^ ((pec >> 7)); in.in.b1 = in.in.b0 ^ (pec); in.in.b2 = in.in.b0 ^ (pec >> 1); pec = pec << 1; pec = (pec & 0xF8) | (in.byte & 0x07); } } return pec; } int main() { unsigned char cmd, tmp, datnum; unsigned char dat[16], getdat[18]; // float cellvol[4]; int cellvol[12]; int cellallvol; char str[32]; cs = 1; spi.format(8,3); spi.frequency(500000); pc.baud(460800); cs = 0; cmd = WRCFG; spi.write(cmd); spi.write(CalcCRCLTC6803(&cmd, 1)); dat[0] = 0x02; dat[1] = 0x00; dat[2] = 0x00; dat[3] = 0xff; dat[4] = 42; dat[5] = 255; for(int loop = 0;loop < 6; loop++){ spi.write(dat[loop]); } cmd = CalcCRCLTC6803(dat, 6); spi.write(cmd); cs = 1; wait(0.001); while(1) { cs = 0; cmd = RDCV; spi.write(cmd); spi.write(CalcCRCLTC6803(&cmd, 1)); for(int loop = 0;loop < 18; loop++){ getdat[loop] = 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]; } pc.printf("CellALL = %5dmV\n", ((cellallvol - 512*12)+((cellallvol - 512*12)/2))); cmd = STCVADALL; spi.write(cmd); spi.write(CalcCRCLTC6803(&cmd, 1)); cs = 1; myled = !myled; wait(0.4); } }