Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: main.cpp
- Revision:
- 3:a1368cd4b0a9
- Parent:
- 2:f6f76dde7e1d
- Child:
- 4:e57b023e41f3
diff -r f6f76dde7e1d -r a1368cd4b0a9 main.cpp --- a/main.cpp Sat Oct 26 05:24:17 2019 +0000 +++ b/main.cpp Sat Nov 30 07:24:22 2019 +0000 @@ -34,6 +34,10 @@ void wakeup(); void cell_read(); void spi_error(); +void ic_check(); +void spi_check(); +void print_CAN(uint8_t datalog_en); +void print_math(); //char get_char(); //void read_config_data(uint8_t cfg_data[][6], uint8_t nIC); @@ -44,7 +48,7 @@ configure the software. ***********************************************************/ -const uint8_t TOTAL_IC = 1;//!<number of ICs in the daisy chain +const uint8_t TOTAL_IC = 2;//!<number of ICs in the daisy chain char ui_buffer[UI_BUFFER_SIZE]; @@ -69,7 +73,7 @@ const uint8_t MEASURE_AUX = DISABLED; // This is ENABLED or DISABLED const uint8_t MEASURE_STAT = DISABLED; //This is ENABLED or DISABLED const uint8_t PRINT_PEC = DISABLED; //This is ENABLED or DISABLED - +short n =0; // Read data from the serial interface into the ui_buffer buffer uint8_t read_data(); @@ -77,19 +81,15 @@ // Read a float value from the serial interface float read_float(); -// Read an integer from the serial interface. -// The routine can recognize Hex, Decimal, Octal, or Binary -// Example: -// Hex: 0x11 (0x prefix) -// Decimal: 17 -// Octal: O21 (leading letter O prefix) -// Binary: B10001 (leading letter B prefix) int32_t read_int(); int kaisuu; -// Read a string from the serial interface. Returns a pointer to the ui_buffer. +unsigned short BAT_MIN, BAT_MAX, BAT_AVG; +unsigned long BAT_SUM; +unsigned short BAT_CELL[23]; + char *read_string(); -// Read a character from the serial interface + int8_t read_char(); /************************************ @@ -105,63 +105,80 @@ cell_asic bms_ic[TOTAL_IC]; -/*!********************************************************************* - \brief main loop -***********************************************************************/ int main(void) { uint32_t user_command; pc.baud(115200); + //spi_enable(); + //LTC681x_init_cfg(TOTAL_IC, bms_ic); + //LTC6811_reset_crc_count(TOTAL_IC,bms_ic); + //LTC6811_init_reg_limits(TOTAL_IC,bms_ic); + //ic_check(); + //wakeup(); + + + //print_menu(); + + + while(1) { + + + unsigned short BAT_MIN = 0; + unsigned short BAT_MAX = 0; + //spi_error(); + //pc.printf("check 00\n"); + //while(!pc.readable()) { + //wait(0.3); + // pc.printf("check 001\n"); + //} // Check for user input + //pc.printf("check 01\n"); + //user_command = read_int(); + //run_command(user_command); + + ic_check(); + } +} +void ic_check(){ //1 + + pc.baud(115200); spi_enable(); LTC681x_init_cfg(TOTAL_IC, bms_ic); LTC6811_reset_crc_count(TOTAL_IC,bms_ic); LTC6811_init_reg_limits(TOTAL_IC,bms_ic); - wakeup(); - //print_menu(); - - - while(1) { - - spi_error(); - //pc.printf("check 00\n"); - //while(!pc.readable()) { - wait(0.3); - // pc.printf("check 001\n"); - //} // Check for user input - //pc.printf("check 01\n"); - user_command = read_int(); - run_command(user_command); - - } -} - -void wakeup(){ - int countup; - int8_t error = 0; - uint32_t conv_time = 0; - - wakeup_sleep(TOTAL_IC); LTC6811_wrcfg(TOTAL_IC,bms_ic); print_config(); - - wait(0.05); - + spi_check(); + + } + +void spi_check(){ //2 + int countup; + int8_t error = 0; + wakeup_sleep(TOTAL_IC); error = LTC6811_rdcfg(TOTAL_IC,bms_ic); check_error(error); print_rxconfig(); - wait(0.05); + spi_error(); + + } + + +void wakeup(){ //3 + int countup; + int8_t error = 0; + uint32_t conv_time = 0; - wakeup_sleep(TOTAL_IC); + + wakeup_sleep(TOTAL_IC); LTC6811_adcv(ADC_CONVERSION_MODE,ADC_DCP,CELL_CH_TO_CONVERT); conv_time = LTC6811_pollAdc(); - pc.printf("cell conversion completed in:"); - pc.printf("%.1f",((float)conv_time/1000)); - pc.printf("mS\n"); - - + // pc.printf("cell conversion completed in:"); + //pc.printf("%.1f",((float)conv_time/1000)); + //pc.printf("mS\n"); + cell_read(); } @@ -169,214 +186,120 @@ int8_t error = 0; int countup; - + /* if(error = -1 ){ - for(countup = 0; countup <= 100; countup++){ - wakeup(); - if(error = 1){ + for(countup = 0; countup <= 2; countup++){ + spi_check(); + if(error =1){ break; + } + } } - } + if(error = 1){ + wakeup(); } - if(error = 1){ - cell_read(); - } + */ + + wakeup(); + } - -/*!***************************************** - \brief executes the user command -*******************************************/ + +void cell_read(){ //4 電圧読み取り -void run_command(uint32_t cmd) -{ int8_t error = 0; uint32_t conv_time = 0; -// uint32_t user_command; int8_t readIC=0; - char input = 0; - - cell_read(); - - switch (cmd) { - - - case 3: // Start GPIO ADC Measurement - wakeup_sleep(TOTAL_IC); - LTC6811_adax(ADC_CONVERSION_MODE , AUX_CH_TO_CONVERT); - LTC6811_pollAdc(); - pc.printf("aux conversion completed\n"); - pc.printf("\n"); - break; - - case 4: // Read AUX Voltage Registers - wakeup_sleep(TOTAL_IC); - error = LTC6811_rdaux(0,TOTAL_IC,bms_ic); // Set to read back all aux registers - check_error(error); - print_aux(DATALOG_DISABLED); - break; - - case 5: // Start Status ADC Measurement - wakeup_sleep(TOTAL_IC); - LTC6811_adstat(ADC_CONVERSION_MODE, STAT_CH_TO_CONVERT); - LTC6811_pollAdc(); - pc.printf("stat conversion completed\n"); - pc.printf("\n"); - break; - - case 6: // Read Status registers - wakeup_sleep(TOTAL_IC); - error = LTC6811_rdstat(0,TOTAL_IC,bms_ic); // Set to read back all aux registers + wakeup_sleep(TOTAL_IC); + error = LTC6811_rdcv(0, TOTAL_IC,bms_ic); check_error(error); - print_stat(); - break; - - case 7: // Loop Measurements - pc.printf("transmit 'm' to quit\n"); - wakeup_sleep(TOTAL_IC); - LTC6811_wrcfg(TOTAL_IC,bms_ic); - while (input != 'm') { - //if (pc.readable()) { - input = read_char(); - //} - - measurement_loop(DATALOG_DISABLED); - - wait_ms(MEASUREMENT_LOOP_TIME); - } - //print_menu(); - break; - - case 8: // Run open wire self test - print_pec(); - - break; - - case 9: // Read in raw configuration data - LTC6811_reset_crc_count(TOTAL_IC,bms_ic); - break; - - case 10: // Run the ADC/Memory Self Test - wakeup_sleep(TOTAL_IC); - error = LTC6811_run_cell_adc_st(CELL,ADC_CONVERSION_MODE,bms_ic); - pc.printf("%d", error); - pc.printf(" : errors detected in Digital Filter and CELL Memory\n"); - - wakeup_sleep(TOTAL_IC); - error = LTC6811_run_cell_adc_st(AUX,ADC_CONVERSION_MODE, bms_ic); - pc.printf("%d",error); - pc.printf(" : errors detected in Digital Filter and AUX Memory\n"); - - wakeup_sleep(TOTAL_IC); - error = LTC6811_run_cell_adc_st(STAT,ADC_CONVERSION_MODE, bms_ic); - pc.printf("%d",error); - pc.printf(" : errors detected in Digital Filter and STAT Memory\n"); - //print_menu(); - break; - - case 11: // Enable a discharge transistor - pc.printf("Please enter the Spin number\n"); - readIC = (int8_t)read_int(); - LTC6811_set_discharge(readIC,TOTAL_IC,bms_ic); - wakeup_sleep(TOTAL_IC); - LTC6811_wrcfg(TOTAL_IC,bms_ic); - print_config(); - break; + //print_cells(DATALOG_DISABLED); + print_CAN(DATALOG_DISABLED); + + ic_check(); + } + + - case 12: // Clear all discharge transistors - clear_discharge(TOTAL_IC,bms_ic); - wakeup_sleep(TOTAL_IC); - LTC6811_wrcfg(TOTAL_IC,bms_ic); - print_config(); - break; - - case 13: // Clear all ADC measurement registers - wakeup_sleep(TOTAL_IC); - LTC6811_clrcell(); - LTC6811_clraux(); - LTC6811_clrstat(); - pc.printf("All Registers Cleared\n"); - break; - - case 14: // Run the Mux Decoder Self Test - wakeup_sleep(TOTAL_IC); - LTC6811_diagn(); - wait_ms(5); - error = LTC6811_rdstat(0,TOTAL_IC,bms_ic); // Set to read back all aux registers - check_error(error); - error = 0; - for (int ic = 0; - ic<TOTAL_IC; - ic++) { - if (bms_ic[ic].stat.mux_fail[0] != 0) error++; - } - if (error==0) pc.printf("Mux Test: PASS\n"); - else pc.printf("Mux Test: FAIL\n"); - - break; - - case 15: // Run ADC Overlap self test - wakeup_sleep(TOTAL_IC); - error = (int8_t)LTC6811_run_adc_overlap(TOTAL_IC,bms_ic); - if (error==0) pc.printf("Overlap Test: PASS\n"); - else pc.printf("Overlap Test: FAIL\n"); - break; - - case 16: // Run ADC Redundancy self test - wakeup_sleep(TOTAL_IC); - error = LTC6811_run_adc_redundancy_st(ADC_CONVERSION_MODE,AUX,TOTAL_IC, bms_ic); - pc.printf("%d",error); - pc.printf(" : errors detected in AUX Measurement\n"); - - wakeup_sleep(TOTAL_IC); - error = LTC6811_run_adc_redundancy_st(ADC_CONVERSION_MODE,STAT,TOTAL_IC, bms_ic); - pc.printf("%d",error); - pc.printf(" : errors detected in STAT Measurement\n"); - break; - - case 17: - LTC6811_run_openwire(TOTAL_IC, bms_ic); - print_open(); - break; - - case 18: //Datalog print option Loop Measurements - pc.printf("transmit 'm' to quit\n"); - wakeup_sleep(TOTAL_IC); - LTC6811_wrcfg(TOTAL_IC,bms_ic); - while (input != 'm') { - //if (pc.readable()) { - input = read_char(); - //} - - measurement_loop(DATALOG_ENABLED); - - wait_ms(MEASUREMENT_LOOP_TIME); - } - //print_menu(); - break; - - case 'm': //prints menu - //print_menu(); - break; - - default: - pc.printf("Incorrect Option\n"); - break; - } + +void print_CAN(uint8_t datalog_en){ + + short n =0; + long BAT_SUM = 0; + unsigned short BAT_MIN = 55000; + //unsigned short BTA_MAX = 0; + + for (int current_ic = 0 ; current_ic < TOTAL_IC; current_ic++) { + + //if (datalog_en == 0) { + pc.printf("IC%d, ", current_ic+1); + //for(n = 0; n <= 12; n++){ + for (int i=0; i < bms_ic[0].ic_reg.cell_channels; i++) { + + BAT_CELL[n] = bms_ic[current_ic].cells.c_codes[i]; + + + pc.printf("C%d:", i+1); + pc.printf("%.4f, ",bms_ic[current_ic].cells.c_codes[i]*0.0001); + BAT_SUM = bms_ic[current_ic].cells.c_codes[i] + BAT_SUM; + + if(bms_ic[current_ic].cells.c_codes[i] > 10000){ + if(bms_ic[current_ic].cells.c_codes[i] < BAT_MIN){ + BAT_MIN = bms_ic[current_ic].cells.c_codes[i]; + } + } + //unsigned short BTA_MAX = 0; + if( bms_ic[current_ic].cells.c_codes[i] > BAT_MAX){ + BAT_MAX = bms_ic[current_ic].cells.c_codes[i]; + } + + + + + + // BAT_SUM = BAT_CELL[n] + BAT_SUM; + // if(bms_ic[current_ic].cells.c_codes[i] < BAT_MIN){ + // BAT_MIN = BAT_CELL[n]; + // } } -void cell_read(){ + + + + + + //BAT_CELL[n] = bms_ic[current_ic].cells.c_codes[i]*0.0001; + // BAT_CELL[n] = BAT_CELL[n]*0.0001; + // pc.printf("C%d:", i+1); + // pc.printf("%.4f, ",bms_ic[current_ic].cells.c_codes[i]); + // pc.printf("%d, ",BAT_CELL[n]); + //} + // } - int8_t error = 0; - uint32_t conv_time = 0; - - int8_t readIC=0; - wakeup_sleep(TOTAL_IC); - error = LTC6811_rdcv(0, TOTAL_IC,bms_ic); // Set to read back all cell voltage registers - check_error(error); - print_cells(DATALOG_DISABLED); - + // else { + // pc.printf("Cells, "); + // for (int i=0; i<bms_ic[0].ic_reg.cell_channels; i++) { + // pc.printf("%.4f, ",bms_ic[current_ic].cells.c_codes[i]*0.0001); + + //} + // } } + + //print_math(); + pc.printf("\n"); + pc.printf("SUM"); + pc.printf("%f, ",BAT_SUM*0.0001); + BAT_AVG = BAT_SUM / 20; + pc.printf("AVG"); + pc.printf("%.4f, ",BAT_AVG*0.0001); + pc.printf("MIN"); + pc.printf("%.4f, ",BAT_MIN*0.0001); + pc.printf("MAX"); + pc.printf("%.4f, ",BAT_MAX*0.0001); + pc.printf("\n"); + pc.printf("\n"); + + } void measurement_loop(uint8_t datalog_en) @@ -432,10 +355,6 @@ } - -/*!********************************* - \brief Prints the main menu -***********************************/ /* void print_menu() { @@ -455,27 +374,29 @@ pc.printf("\n"); } */ -/*!************************************************************ - \brief Prints cell voltage codes to the serial port - *************************************************************/ + void print_cells(uint8_t datalog_en) { for (int current_ic = 0 ; current_ic < TOTAL_IC; current_ic++) { if (datalog_en == 0) { pc.printf("IC%d, ", current_ic+1); - for (int i=0; i<bms_ic[0].ic_reg.cell_channels; i++) { + for (int i=0; i < bms_ic[0].ic_reg.cell_channels; i++) { pc.printf("C%d:", i+1); pc.printf("%.4f, ", bms_ic[current_ic].cells.c_codes[i]*0.0001); - } + } + pc.printf("\n"); - } else { + } + + else { pc.printf("Cells, "); for (int i=0; i<bms_ic[0].ic_reg.cell_channels; i++) { - pc.printf("%.4f, ",bms_ic[current_ic].cells.c_codes[i]*0.0001); + pc.printf("%.4f, ",bms_ic[current_ic].cells.c_codes[i]*0.0001); + } } - + } pc.printf("\n"); } @@ -546,31 +467,31 @@ { int cfg_pec; - pc.printf("Written Configuration: \n"); + //pc.printf("Written Configuration: \n"); for (int current_ic = 0; current_ic<TOTAL_IC; current_ic++) { - pc.printf(" IC "); - pc.printf("%d", current_ic+1); - pc.printf(": "); - pc.printf("0x"); + //pc.printf(" IC "); + //pc.printf("%d", current_ic+1); + // pc.printf(": "); + //pc.printf("0x"); serial_print_hex(bms_ic[current_ic].config.tx_data[0]); - pc.printf(", 0x"); + // pc.printf(", 0x"); serial_print_hex(bms_ic[current_ic].config.tx_data[1]); - pc.printf(", 0x"); + //pc.printf(", 0x"); serial_print_hex(bms_ic[current_ic].config.tx_data[2]); - pc.printf(", 0x"); + //pc.printf(", 0x"); serial_print_hex(bms_ic[current_ic].config.tx_data[3]); - pc.printf(", 0x"); + // pc.printf(", 0x"); serial_print_hex(bms_ic[current_ic].config.tx_data[4]); - pc.printf(", 0x"); + // pc.printf(", 0x"); serial_print_hex(bms_ic[current_ic].config.tx_data[5]); - pc.printf(", Calculated PEC: 0x"); + //pc.printf(", Calculated PEC: 0x"); cfg_pec = pec15_calc(6,&bms_ic[current_ic].config.tx_data[0]); serial_print_hex((uint8_t)(cfg_pec>>8)); - pc.printf(", 0x"); + //pc.printf(", 0x"); serial_print_hex((uint8_t)(cfg_pec)); - pc.printf("\n"); + // pc.printf("\n"); } - pc.printf("\n"); + // pc.printf("\n"); } /*!***************************************************************** @@ -579,27 +500,27 @@ *******************************************************************/ void print_rxconfig() { - pc.printf("Received Configuration "); + //pc.printf("Received Configuration "); for (int current_ic=0; current_ic<TOTAL_IC; current_ic++) { - pc.printf(" IC "); - pc.printf("%d", current_ic+1); - pc.printf(": 0x"); + // pc.printf(" IC "); + // pc.printf("%d", current_ic+1); + //pc.printf(": 0x"); serial_print_hex(bms_ic[current_ic].config.rx_data[0]); - pc.printf(", 0x"); + //pc.printf(", 0x"); serial_print_hex(bms_ic[current_ic].config.rx_data[1]); - pc.printf(", 0x"); + //pc.printf(", 0x"); serial_print_hex(bms_ic[current_ic].config.rx_data[2]); - pc.printf(", 0x"); + //pc.printf(", 0x"); serial_print_hex(bms_ic[current_ic].config.rx_data[3]); - pc.printf(", 0x"); + //pc.printf(", 0x"); serial_print_hex(bms_ic[current_ic].config.rx_data[4]); - pc.printf(", 0x"); + // pc.printf(", 0x"); serial_print_hex(bms_ic[current_ic].config.rx_data[5]); - pc.printf(", Received PEC: 0x"); + //pc.printf(", Received PEC: 0x"); serial_print_hex(bms_ic[current_ic].config.rx_data[6]); - pc.printf(", 0x"); + //pc.printf(", 0x"); serial_print_hex(bms_ic[current_ic].config.rx_data[7]); - pc.printf("\n"); + // pc.printf("\n"); } pc.printf("\n"); } @@ -616,10 +537,12 @@ void serial_print_hex(uint8_t data) { + /* if (data < 16) { - pc.printf("0x0%X", data); + //pc.printf("0x0%X", data); } else - pc.printf("0x%X", data); + pc.printf("0x%X", data); + */ } //Function to check error flag and print PEC error message