Battery Management System LV - 2020/2021

Dependencies:   mbed

main.cpp

Committer:
minamax
Date:
2020-12-20
Revision:
1:d0662d4ffb8c
Child:
2:03a6da61d834

File content as of revision 1:d0662d4ffb8c:

#include "mbed.h"
#include "bq79606.h"

// - - - PIN CONFIGURATION - - -

DigitalIn bmsFault(PB_4);
DigitalOut bmsWakeUp(PB_5);

// - - - UART CONFIGURATION - - -

Serial bms(PA_0, PA_1, BAUDRATE);//PA_9, PA_10,250000); //UART ka BMS Slaveu
Serial pc1(USBTX, USBRX, 9600);//PC_10, PC_11,9600); //UART ka PCu Serijskom monitoru

BYTE recBuff[1024];
int recLen=0;
int expected=0;
volatile bool full = false;
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()){
        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 == recLen){
            full = true;
            rdLen = expected;
            expected = 0;
            recLen = 0;
        }
    }
}

void waitFrame(){
    while(!full);
    full=false;
    for(int i = 0;i<rdLen;i++){
        pc1.printf("%X ",recBuff[i]);
    }
    
    pc1.printf("\n\n- - - VOLTAGE - - -\n"); 
    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);
    }
      
    pc1.printf("\n");
}
void waitFrameResponse(){
    while(!full);
    full = false;
    for(int i = 0;i < rdLen; i++){
        pc1.printf("%X ", recBuff[i]);
    }
    pc1.printf("\n");
}

int main(){
    bms.attach(&callback);
    Wake79606();
    AutoAddress();
    init();
    
    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)
        waitFrame();
        
        //slanje zahteva za GRESKAMA
        /*ReadReg(0, 0x52, &wTemp, 2, 0); // 0ms timeout
        waitFrameResponse();*/
        

    }
    
    
    
}