Adds Balancing function (on the top of SPI_I2C_Parser.cpp) and changes main function adding a test case for balancing to show that it is working as intended.
Fork of BMS_BMUCore_cellvoltage_Mrinank by
Revision 13:499c1e2da85b, committed 2016-12-01
- Comitter:
- maxv008
- Date:
- Thu Dec 01 15:15:13 2016 +0000
- Parent:
- 12:f6ff8ddea5ab
- Commit message:
- Added working function that enables and disables S pins on the CMU
Changed in this revision
diff -r f6ff8ddea5ab -r 499c1e2da85b SPI_I2C_Parser.cpp --- a/SPI_I2C_Parser.cpp Sun Nov 27 11:51:16 2016 +0000 +++ b/SPI_I2C_Parser.cpp Thu Dec 01 15:15:13 2016 +0000 @@ -9,6 +9,69 @@ I2C i2c(p9, p10); + + +/** +Sets the balancing transistors by adjusting the configuration states of the +LTC6804. This version of the function writes all 12 states for a chosen +transistor. If other forms end up being more useful I will add other overloaded +versions. + +@param uint8_t total_ic Total LTC6804s in the setup (required for configuration writing +functions). + +@param uint8_t ic The specific IC to write to (can be changed in favor of a +states array of size 12*total_ic if preferred) + +@param uint8_t states[12] For Sn in S1-S12 set states[n-1] to 1 to enable +balancing and 0 to disable balancing. +*/ +void LTC6804_balance(uint8_t total_ic, uint8_t ic, uint8_t states[12]) +{ + uint8_t r_config[total_ic][8]; + uint8_t w_config[total_ic][6]; + wake_LTC6804(); + wait_us(330); + LTC6804_rdcfg(total_ic, r_config); + + for (int i=0; i<8; i++) { + printf("TEST %d config \r\n", (uint8_t)r_config[0][i]); + } + + uint8_t cfgr4 = 0; //This entire configuration is DCC states + uint8_t cfgr5 = r_config[ic][5]; + + for(int i = 0; i < 8; i++) + { + //Note: This disgusting thing is written by someone who has not used c++ in a long time + cfgr4 = states[i] ? cfgr4 | (1u << i) : cfgr4 & ~(1u << i); + } + + for(int i = 8; i < 12; i++) + { + cfgr5 = states[i] ? cfgr5 | (1u << (i-8)) : cfgr5 & ~(1u << (i-8)); + } + + printf("cfgr4 %d \r\n", (uint8_t)cfgr4); + printf("cfgr5 %d \r\n", (uint8_t)cfgr5); + for(int i =0 ; i < total_ic; i++) + { + for(int j = 0; j < 6; j++) + { + w_config[i][j] = r_config[i][j]; + } + } + w_config[ic][4] = cfgr4; + w_config[ic][5] = cfgr5; + + wake_LTC6804(); + wait_us(330); + LTC6804_wrcfg(total_ic,w_config); //Make sure this is written in the write order +} + + + + void wake_LTC6804() { spi.format(8,3);//All data transfer on LTC6804 occur in byte groups. LTC6820 set up such that POL=1 and PHA=3, this corresponds to mode 3 in mbed library. spi.frequency(spiBitrate);
diff -r f6ff8ddea5ab -r 499c1e2da85b SPI_I2C_Parser.h --- a/SPI_I2C_Parser.h Sun Nov 27 11:51:16 2016 +0000 +++ b/SPI_I2C_Parser.h Thu Dec 01 15:15:13 2016 +0000 @@ -124,5 +124,6 @@ int8_t LTC6804_rdcfg(uint8_t total_ic, uint8_t r_config[][8]); int8_t LTC6804_rdstat(uint8_t total_ic, uint8_t r_config[][8]); void LTC6804_wrcfg(uint8_t total_ic,uint8_t config[][6]); +void LTC6804_balance(uint8_t total_ic, uint8_t ic, uint8_t states[12]); #endif \ No newline at end of file
diff -r f6ff8ddea5ab -r 499c1e2da85b main.cpp --- a/main.cpp Sun Nov 27 11:51:16 2016 +0000 +++ b/main.cpp Thu Dec 01 15:15:13 2016 +0000 @@ -142,10 +142,12 @@ LTC6804_init(MD_FAST, DCP_DISABLED, CELL_CH_ALL, AUX_CH_VREF2); //LTC6804_init(MD_NORMAL,DCP_DISABLED,CELL_CH_ALL,AUX_CH_ALL); - + bool swap = 1; + wake_LTC6804(); + LTC6804_wrcfg(1,write); while(1) { wake_LTC6804();//ensures CMU's are in ready state and wakes it up from low power mode - LTC6804_wrcfg(1,write); + //LTC6804_wrcfg(1,write); wait_us(330); LTC6804_rdcfg(1,read); wait_us(330); @@ -163,6 +165,16 @@ printf("Reading %d mV \r\n", (uint16_t)cellcodes[0][i]); } printf("\r\n"); + + //Test Balancing Code + + uint8_t states[12]; + for(int i = 0; i < 12; i++) + { + states[i] = swap ^ (i % 2); + } + LTC6804_balance(1,0,states); + swap = !swap; } }