Cell voltages fork (SoC)
Dependencies: CUER_CAN CUER_DS1820 LTC2943 LTC6804 mbed PowerControl
Diff: main.cpp
- Revision:
- 34:65fd6a72106f
- Parent:
- 33:44b241c7b2c1
- Child:
- 35:be07fef5db72
--- a/main.cpp Sun Jul 16 09:54:06 2017 +0000 +++ b/main.cpp Sun Jul 16 14:37:36 2017 +0000 @@ -235,6 +235,7 @@ void reset_EEPROM(float init_SOC, float init_SOC_Percent) { char start_address_array1[2]; //Purely for testing + char start_address_array2[2]; //Purely for testing char test_float_array[10]; //Very first addresses to use char first_address[2] = {0x40,0}; //Address 0x0040 @@ -249,32 +250,81 @@ wait_ms(10); char data_out[10]; - float *fp1,*fp2; + //float *fp1,*fp2; uint16_t data_pec; + + union float2bytes { float f; char b[sizeof(float)]; }; + + float2bytes init_SOC_union; + float2bytes init_SOC_Percent_union; + + init_SOC_union.f = init_SOC; + for ( int i=0; i < sizeof(float); i++ ) { + data_out[i] = init_SOC_union.b[i]; + } + + init_SOC_Percent_union.f = init_SOC_Percent; + for ( int i=0; i < sizeof(float); i++ ) { + data_out[i+sizeof(float)] = init_SOC_Percent_union.b[i]; + } + + - fp1 = (float*)(&init_SOC); //This may be source of error, uncomfortable with this - fp2 = (float*)(&init_SOC_Percent); + //fp1 = (float*)(&init_SOC); //This may be source of error, uncomfortable with this + //fp2 = (float*)(&init_SOC_Percent); - for(int i = 0; i < 4; i++ ) { + /*for(int i = 0; i < 4; i++ ) { data_out[i] = *fp1; fp1++; } for(int j = 4; j < 7; j++ ) { data_out[j] = *fp2; fp2++; - } + }*/ data_pec = pec15_calc(8, ((uint8_t*)data_out)); // Calculate the pec and then write it to memory data_out[8] = (char)(data_pec >> 8); data_out[9] = (char)(data_pec); + + for (int i = 0; i < 10; ++i) { + printf("Data_out %d is %d \r\n", i, data_out[i]); + } + i2c_page_write(address1, 10,data_out); wait_ms(10); i2c_page_write(address2, 10,data_out); - + wait_ms(10); i2c_page_read(0x0000,2,start_address_array1); - printf("Start address (%d,%d) \r\n \r\n", start_address_array1[0], start_address_array1[1]); + wait_ms(10); + i2c_page_read(0x0002,2,start_address_array2); + printf("Start address 1 is (%x,%x) \r\n \r\n", start_address_array1[0], start_address_array1[1]); + printf("Start address 2 is (%x,%x) \r\n \r\n", start_address_array2[0], start_address_array2[1]); wait_ms(10); - i2c_page_read(0x0040,10,test_float_array); - printf("float test (%d,%d) \r\n \r\n", test_float_array[0], test_float_array[1]); + i2c_page_read(address1,10,test_float_array); + for (int i = 0; i < 10; ++i) { + printf("test_float array %d is %d \r\n", i, test_float_array[i]); + } + //uint32_t init_SOC_int = (uint32_t) test_float_array[3] << 24 | ( uint32_t)test_float_array[2] << 16 | ( uint32_t)test_float_array[1] << 8| ( uint32_t)test_float_array[0]; + //float init_SOC_float = (float) init_SOC_int; + //float *received_fp1,*received_fp2; + //received_fp1 = (float*)(&test_float_array[0]); + //received_fp2 = (float*)(&test_float_array[4]); + + float2bytes rec_init_SOC_union; + float2bytes rec_init_SOC_Percentage_union; + + + for ( int i=0; i < sizeof(float); i++ ) { + rec_init_SOC_union.b[i] = test_float_array[i]; + } + float rec_init_SOC = rec_init_SOC_union.f; + + for ( int i=0; i < sizeof(float); i++ ) { + rec_init_SOC_Percentage_union.b[i] = test_float_array[i+4]; + } + float rec_init_SOC_Percentage = rec_init_SOC_Percentage_union.f; + + printf("init SOC %f \r\n \r\n", rec_init_SOC); + printf("percentage SOC %f \r\n \r\n", rec_init_SOC_Percentage); } bool check_EEPROM_PEC( uint16_t start_address, char start_address_array[], char SOC_out[]){