naoto tanaka
/
LTC6803_Eval
BMS I/Fプログラム
Diff: main.cpp
- Revision:
- 1:5690684e5460
- Parent:
- 0:2d6ffd5a4d2e
- Child:
- 2:c7ea42f45ff5
--- a/main.cpp Mon Jan 12 02:51:41 2015 +0000 +++ b/main.cpp Sun May 17 02:21:27 2015 +0000 @@ -1,11 +1,11 @@ #include "mbed.h" #include "LTC6803.h" -DigitalOut myled(P2_17); -SPI spi(P0_9, P0_8, P1_29); // mosi, miso, sclk -DigitalOut cs(P0_2); +DigitalOut myled(LED1); +SPI spi(p11, p12, p13); // mosi, miso, sclk +DigitalOut cs(p15); -Serial pc(P0_19, P0_18); +Serial pc(USBTX, USBRX); unsigned char CalcCRCLTC6803(unsigned char * dat, unsigned char numdat); @@ -76,51 +76,59 @@ int main() { unsigned char cmd, tmp, datnum; unsigned char dat[16], getdat[16]; +// float cellvol[4]; + int cellvol[4]; char str[32]; cs = 1; spi.format(8,3); - spi.frequency(50000); + spi.frequency(8000000); pc.baud(460800); + + cs = 0; cmd = WRCFG; spi.write(cmd); spi.write(CalcCRCLTC6803(&cmd, 1)); dat[0] = 0x02; - dat[1] = 0x00; - dat[2] = 0xf0; + dat[1] = 0x0f; + dat[2] = 0x00; dat[3] = 0xff; - dat[4] = 0x00; - dat[5] = 0x00; + dat[4] = 42; + dat[5] = 255; for(int loop = 0;loop < 6; loop++){ - spi.write(dat[loop]) + spi.write(dat[loop]); } cmd = CalcCRCLTC6803(dat, 6); spi.write(cmd); + cs = 1; + wait(0.001); while(1) { - pc.printf("cmd in\n"); - pc.scanf("%x", &cmd); cs = 0; - pc.printf("dat num\n"); - pc.scanf("%d", &datnum); - for(tmp = 0;tmp < datnum; tmp++){ - pc.printf("dat num[%d]\n", (tmp)); - pc.scanf("%s", str); - dat[tmp] = strtol(str, NULL, 16); - } + cmd = RDCVA; spi.write(cmd); -// pc.printf("%X\n", cmd); - cmd = CalcCRCLTC6803(&cmd, 1); - spi.write(cmd); -// pc.printf("%X\n", cmd); - for(tmp = 0; tmp < datnum; tmp++){ - getdat[tmp] = spi.write(dat[tmp]); -// pc.printf("dat[%X] = %X\n", tmp, dat[tmp]); + spi.write(CalcCRCLTC6803(&cmd, 1)); + for(int loop = 0;loop < 6; loop++){ + getdat[loop] = spi.write(0); } - cmd = CalcCRCLTC6803(dat, datnum); - spi.write(cmd); cs = 1; - for(tmp = 0; tmp < datnum; tmp++){ - pc.printf("get[%d] = %x\n", tmp, getdat[tmp]); + 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)); + + for(int loop = 0;loop < 4; loop++){ + pc.printf("Cell%d = %6d\n", loop, cellvol[loop]); } + + cmd = STCVADALL; + + spi.write(cmd); + spi.write(CalcCRCLTC6803(&cmd, 1)); + cs = 1; myled = !myled; + wait(0.4); } }