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:
- 2:03a6da61d834
- Parent:
- 1:d0662d4ffb8c
- Child:
- 3:4dc457ed63d5
diff -r d0662d4ffb8c -r 03a6da61d834 main.cpp
--- a/main.cpp Sun Dec 20 18:07:17 2020 +0000
+++ b/main.cpp Wed Dec 23 21:41:03 2020 +0000
@@ -8,47 +8,67 @@
// - - - UART CONFIGURATION - - -
-Serial bms(PA_0, PA_1, BAUDRATE);//PA_9, PA_10,250000); //UART ka BMS Slaveu
+Serial bms(PA_0, PA_1, 250000); //UART ka BMS Slaveu
Serial pc1(USBTX, USBRX, 9600);//PC_10, PC_11,9600); //UART ka PCu Serijskom monitoru
+CAN can1(PB_8, PB_9, 500000);
+CANMessage message1;
+
+
BYTE recBuff[1024];
-int recLen=0;
-int expected=0;
+volatile int recLen=0;
+volatile int expected=0;
volatile bool full = false;
-int rdLen=0;
+volatile int rdLen=0;
int counter = 0;
uint8_t pFrame1[(MAXBYTES+6)*TOTALBOARDS];
void callback() {
// Note: you need to actually read from the serial to clear the RX interrupt
- pc1.printf("* * * Uspesan PRIJEM! * * *\n");
- while(bms.readable()){
+ //pc1.printf("* * * Uspesan PRIJEM! * * *\n");
+
+ //pc1.printf("rec = %d", c);
recBuff[recLen++] = bms.getc();
- if(expected==0) expected = recBuff[0]+6; //prvi bajt je (broj data - 1), +1 device id, +2 reg address, +2 CRC
+ if(expected==0) expected = recBuff[0]+7; //prvi bajt je (broj data - 1), +1 device id, +2 reg address, +2 CRC
+ //pc1.printf("Prva rec = %d", recBuff[0]);
if(expected == recLen){
+ //pc1.printf("\n\n- - - USAO U EXPECTED - - -\n");
full = true;
rdLen = expected;
expected = 0;
recLen = 0;
}
- }
+
+
+ //full = true;
+ //recLen = 0;
+ //rdLen = 17; //samo test - nebitno koji broj
}
void waitFrame(){
while(!full);
+ //wait(2);
full=false;
+ pc1.printf("\n%d\n", rdLen);
for(int i = 0;i<rdLen;i++){
pc1.printf("%X ",recBuff[i]);
}
pc1.printf("\n\n- - - VOLTAGE - - -\n");
+ message1.id = 0x71;
+ int j = 0;
for(int i = 4; i < recBuff[0] + 4; i += 2){
int voltage = recBuff[i+1]; //LSB
voltage |= (recBuff[i]) << 8; //MSB
- double vol = ((double)voltage)/65536.0 * 5.0;
- pc1.printf("CELL[%d] = %6.f V\n", i/2-1, vol);
+ double vol = voltage*0.0001907349;
+ //double vol = ((double)voltage)/65536.0 * 5.0;
+ pc1.printf("CELL[%d] = %6.2f V\n", i/2-1, vol);
+
+ message1.data[j++] = recBuff[i];
+
}
+ can1.write(message1);
pc1.printf("\n");
}
@@ -61,24 +81,59 @@
pc1.printf("\n");
}
+
int main(){
bms.attach(&callback);
Wake79606();
+ //DigitalOut(PA_0, 0);
+ //DigitalOut(PA_1, 0);
+ //wait(1);
+ //DigitalOut(PA_0, 1);
+ //DigitalOut(PA_1, 1);
+ //bms(PA_0, PA_1, 250000);
+ bms.baud(10);
+ bms.send_break();
+ bms.baud(250000);
+ //wait_ms(500);
+ //bms.clear_break();
+
+ wait(2); //marta rekla da mozda treba da se doda wait
AutoAddress();
+
+
+ //WriteReg(0, COMM_CTRL, 0x343C, 2, FRMWRT_ALL_NR); //mask GPIO faults
+
+ wait(2);
init();
+ /*WriteReg(0, COMM_TO, 0x00, 1, FRMWRT_ALL_NR); //disable COMM timeout because printf takes a long time between reads
+ WriteReg(0, SYSFLT1_FLT_RST, 0xFFFFFF, 3, FRMWRT_ALL_NR); //reset system faults
+ 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)
+
+ //SET UP MAIN ADC
+ WriteReg(0, CELL_ADC_CTRL, 0x3F, 1, FRMWRT_ALL_NR); //enable conversions for all cells
+ WriteReg(0, CELL_ADC_CONF2, 0x08, 1, FRMWRT_ALL_NR); //set continuous ADC conversions, and set minimum conversion interval
+ WriteReg(0, CONTROL2, 0x01, 1, FRMWRT_ALL_NR); //CELL_ADC_GO = 1
+ wait_ms(5);*/
while (1) {
pc1.printf("Main Code \n");
pc1.printf("Board 0 \n");
+
+
+
wait(2);
- ReadReg(0, VCELL1H, pFrame1, 0x0C, 0, FRMWRT_SGL_R); //12 bajtova jer cita od adrese VCELL1H po dva bajta za svaki kanal (ima 6 kanala)
+ while(bms.readable()) bms.getc();
+ int rdLen = ReadReg(0, VCELL1H , pFrame1, 6 , 0, FRMWRT_ALL_R); //12 bajtova jer cita od adrese VCELL1H po dva bajta za svaki kanal (ima 6 kanala)
+
waitFrame();
+
+
//slanje zahteva za GRESKAMA
- /*ReadReg(0, 0x52, &wTemp, 2, 0); // 0ms timeout
- waitFrameResponse();*/
+ //ReadReg(0, 0x52, &wTemp, 2, 0); // 0ms timeout
+ //waitFrameResponse();
}