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.
main.cpp
00001 #include "mbed.h" 00002 #include "bq79606.h" 00003 00004 // - - - PIN CONFIGURATION - - - 00005 00006 //DigitalIn bmsFault(PB_4); 00007 DigitalOut bmsWakeUp(PB_4); 00008 DigitalOut LV_ON(PC_6); 00009 00010 // - - - UART CONFIGURATION - - - 00011 Serial bms(PC_12, PD_2, 250000); //UART ka BMS Slaveu 00012 Serial pc1(USBTX, USBRX, 9600); 00013 //Serial pc1(PC_10, PC_11, 9600);//PC_10, PC_11,9600); //UART ka PCu Serijskom monitoru 00014 00015 CAN can1(PB_8, PB_9, 500000); 00016 CANMessage message1; 00017 00018 00019 BYTE recBuff[1024]; 00020 volatile int recLen=0; 00021 volatile int expected=0; 00022 volatile bool full = false; 00023 volatile int rdLen=0; 00024 int counter = 0; 00025 volatile int devStat = 0; 00026 volatile int cbRun = 1; 00027 volatile int cbDone = 0; 00028 00029 uint8_t pFrame1[(MAXBYTES+6)*TOTALBOARDS]; 00030 00031 void callback() { 00032 // Note: you need to actually read from the serial to clear the RX interrupt 00033 //pc1.printf("* * * Uspesan PRIJEM! * * *\n"); 00034 00035 //pc1.printf("rec = %d", c); 00036 recBuff[recLen++] = bms.getc(); 00037 if(expected==0) expected = recBuff[0]+7; //prvi bajt je (broj data - 1), +1 device id, +2 reg address, +2 CRC 00038 //pc1.printf("Prva rec = %d", recBuff[0]); 00039 if(expected == recLen){ 00040 //pc1.printf("\n\n- - - USAO U EXPECTED - - -\n"); 00041 full = true; 00042 rdLen = expected; 00043 expected = 0; 00044 recLen = 0; 00045 } 00046 00047 00048 //full = true; 00049 //recLen = 0; 00050 //rdLen = 17; //samo test - nebitno koji broj 00051 } 00052 00053 void waitFrame(){ 00054 while(!full); 00055 //wait(2); 00056 full=false; 00057 pc1.printf("\n%d\n", rdLen); 00058 for(int i = 0;i<rdLen;i++){ 00059 pc1.printf("%X ",recBuff[i]); 00060 } 00061 00062 pc1.printf("\n\n- - - VOLTAGE - - -\n"); 00063 message1.id = 0x71; 00064 int j = 0; 00065 for(int i = 4; i < recBuff[0] + 4; i += 2){ 00066 int voltage = recBuff[i+1]; //LSB 00067 voltage |= (recBuff[i]) << 8; //MSB 00068 double vol = voltage*0.0001907349; 00069 //double vol = ((double)voltage)/65536.0 * 5.0; 00070 pc1.printf("CELL[%d] = %6.2f V\n", i/2-1, vol); 00071 00072 message1.data[j++] = recBuff[i]; 00073 00074 } 00075 can1.write(message1); 00076 00077 pc1.printf("\n"); 00078 } 00079 00080 00081 void waitFrameTemp(){ 00082 while(!full); 00083 //wait(2); 00084 full=false; 00085 pc1.printf("****** TEMPERATURA *****"); 00086 pc1.printf("\n%d\n", rdLen); 00087 for(int i = 0;i<rdLen;i++){ 00088 pc1.printf("%X ",recBuff[i]); 00089 } 00090 int voltage = recBuff[5]; //LSB 00091 voltage |= (recBuff[4]) << 8; //MSB 00092 double vol = voltage*0.0001907349; 00093 //double vol = ((double)voltage)/65536.0 * 5.0; 00094 pc1.printf("temp1 = %f V\n", vol); 00095 00096 } 00097 void waitFrameResponse(){ 00098 while(!full); 00099 full = false; 00100 for(int i = 0;i < rdLen; i++){ 00101 pc1.printf("%X ", recBuff[i]); 00102 } 00103 pc1.printf("\n"); 00104 } 00105 void cellBalanceStart() 00106 { 00107 cbRun = 1; 00108 cbDone = 0; 00109 WriteReg(0, CB_CONFIG, 0xFA, 1, FRMWRT_ALL_NR); // Odds then Evens, continue regardless of fault condition, 30sec, seconds 00110 00111 WriteReg(0, CB_DONE_THRESHOLD, 0x5F, 1, FRMWRT_ALL_NR); // Thresh hold set to value 3.6V, CBDONE comparators enabled 00112 //Enabling the CBDONE voltage threshold overrides the OVUV function and pauses it. 00113 00114 WriteReg(0, CB_CELL1_CTRL, 0xBC, 1, FRMWRT_ALL_NR);//cell 1- 1 minute balance timer 00115 WriteReg(0, CB_CELL2_CTRL, 0xBC, 1, FRMWRT_ALL_NR);//cell 2- 1 minute balance timer 00116 WriteReg(0, CB_CELL3_CTRL, 0xBC, 1, FRMWRT_ALL_NR);//cell 3- 1 minute balance timer 00117 WriteReg(0, CB_CELL4_CTRL, 0xBC, 1, FRMWRT_ALL_NR);//cell 3- 1 minute balance timer 00118 00119 WriteReg(0, CONTROL2, 0x30, 1, FRMWRT_ALL_NR);//BAL_GO set to 1, and TSREF enabled 00120 wait_us(100); 00121 pc1.printf("Setupovano balansiranje\n"); 00122 while (cbRun) 00123 { 00124 ReadReg(0, DEV_STAT, pFrame1, 1 , 0, FRMWRT_ALL_R); 00125 wait(1); 00126 devStat = recBuff[4]; 00127 cbRun = (devStat & 0x10) >> 4; 00128 wait_us(500); 00129 if (!cbRun) 00130 { 00131 pc1.printf("DEV STAT = %d\n", devStat); 00132 pc1.printf("CBRUN = %d\n", cbRun); 00133 //wait(10); 00134 } 00135 } 00136 00137 while(!cbDone) 00138 { 00139 ReadReg(0, DEV_STAT, pFrame1, 1 , 0, FRMWRT_ALL_R); 00140 wait(1); 00141 devStat = recBuff[4]; 00142 cbDone = (devStat & 0x40) >> 6; 00143 wait_us(500); 00144 if (cbDone) 00145 { 00146 pc1.printf("DEV STAT = %d\n", devStat); 00147 pc1.printf("CBDONE = %d\n", cbDone); 00148 //wait(10); 00149 } 00150 } 00151 // Cleanup 00152 WriteReg(0, CONTROL2, 0x00, 1, FRMWRT_ALL_NR);//Reset 00153 WriteReg(0, CB_DONE_THRESHOLD, 0x20, 1, FRMWRT_ALL_NR); 00154 } 00155 int main(){ 00156 pc1.printf("Main ulazak\n"); 00157 bms.attach(&callback); 00158 pc1.printf("Main after attacha\n"); 00159 Wake79606(); 00160 pc1.printf("woken up\n"); 00161 //DigitalOut(PA_0, 0); 00162 //DigitalOut(PA_1, 0); 00163 //wait(1); 00164 //DigitalOut(PA_0, 1); 00165 //DigitalOut(PA_1, 1); 00166 //bms(PA_0, PA_1, 250000); 00167 bms.baud(10); 00168 bms.send_break(); 00169 bms.baud(250000); 00170 //wait_ms(500); 00171 //bms.clear_break(); 00172 00173 wait(2); //marta rekla da mozda treba da se doda wait 00174 AutoAddress(); 00175 00176 00177 //WriteReg(0, COMM_CTRL, 0x343C, 2, FRMWRT_ALL_NR); //mask GPIO faults 00178 00179 wait(2); 00180 init(); 00181 /*WriteReg(0, COMM_TO, 0x00, 1, FRMWRT_ALL_NR); //disable COMM timeout because printf takes a long time between reads 00182 WriteReg(0, SYSFLT1_FLT_RST, 0xFFFFFF, 3, FRMWRT_ALL_NR); //reset system faults 00183 WriteReg(0, SYSFLT1_FLT_MSK, 0xFFFFFF, 3, FRMWRT_ALL_NR); //mask system faults (so we can test boards and not worry about triggering these faults accidentally) 00184 00185 //SET UP MAIN ADC 00186 WriteReg(0, CELL_ADC_CTRL, 0x3F, 1, FRMWRT_ALL_NR); //enable conversions for all cells 00187 WriteReg(0, CELL_ADC_CONF2, 0x08, 1, FRMWRT_ALL_NR); //set continuous ADC conversions, and set minimum conversion interval 00188 WriteReg(0, CONTROL2, 0x01, 1, FRMWRT_ALL_NR); //CELL_ADC_GO = 1 00189 wait_ms(5);*/ 00190 00191 //bmsWakeUp = 0; 00192 pc1.printf("Pre while-a\n"); 00193 LV_ON = 1; 00194 while (1) { 00195 pc1.printf("Main Code \n"); 00196 00197 pc1.printf("Board 0 \n"); 00198 00199 00200 00201 00202 wait(2); 00203 //while(bms.readable()) bms.getc(); 00204 int rdLen = ReadReg(0, VCELL1H , pFrame1, 6 , 0, FRMWRT_ALL_R); //6 bajtova jer cita od adrese VCELL1H po dva bajta za svaki kanal (ima 3 kanala) 00205 00206 00207 00208 waitFrame(); 00209 00210 WriteReg(0, CONTROL2, 0x02, 1, FRMWRT_ALL_NR); 00211 ReadReg(0, AUX_GPIO1H, pFrame1, 2 , 0, FRMWRT_ALL_R); 00212 00213 waitFrameTemp(); 00214 00215 cellBalanceStart(); 00216 00217 //slanje zahteva za GRESKAMA 00218 //ReadReg(0, 0x52, &wTemp, 2, 0); // 0ms timeout 00219 //waitFrameResponse(); 00220 00221 00222 } 00223 00224 00225 00226 }
Generated on Sun Jul 17 2022 02:07:50 by
1.7.2