![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
all bms comunicating with nucleo board and balancing
Dependencies: CANnucleo LTC68041 mbed
Fork of BMS_2 by
main.cpp
- Committer:
- hudakz
- Date:
- 2015-07-24
- Revision:
- 3:464b06c16d24
- Parent:
- 2:49c9430860d1
- Child:
- 4:ccf4ac2deac8
File content as of revision 3:464b06c16d24:
/* * An example showing how to use the CANnucleo library: * * Two NUCLEO boards are connected to the same CAN bus via CAN transceivers (MCP2551 or TJA1040, or etc.). * Transceivers are not part of the NUCLEO boards, therefore must be added by you. * Remember also that CAN bus must be terminated with 120 Ohm resitors on both ends. * See <https://developer.mbed.org/users/WiredHome/notebook/can---getting-started/> * The same source code is used for both NUCLEO boards, but: * For board #1 compile the example without any change. * For board #2 set BOARD1 to 0 * * Note: * To simplify adding/getting data to/from a CAN message * inserter "<<" and extractor ">>" operators have been defined. * Please be aware that CAN message maximum data length is limited to eight bytes. * To make sure this limitation is not violated I recommend to first compile * your application with DEBUG enabled in "CAN.h" file. * Then run it and check for error messages. */ #include "mbed.h" #include "CAN.h" #define BOARD1 1 // please comment out this line when compiling for board #2 #if defined(BOARD1) #define RX_ID 0x100 #define TX_ID 0x101 #else #define RX_ID 0x101 #define TX_ID 0x100 #endif DigitalOut led(LED1); int ledTarget; Timer timer; CAN can(PA_11, PA_12); // rx, tx CANMessage rxMsg; CANMessage txMsg; long int counter = 0; volatile bool msgAvailable = false; /** * @brief 'CAN receive-complete' interrup handler. * @note Called on arrival of new CAN message. * Keep it as short as possible. * @param * @retval */ void onMsgReceived() { msgAvailable = true; } /** * @brief Main * @note * @param * @retval */ int main() { can.frequency(500000); // Initialize CAN and set bit rate to 500kbs can.attach(&onMsgReceived, CAN::RxIrq); // attach 'CAN receive complete' interrupt handler timer.reset(); #if defined(BOARD1) led = 1; timer.start(); #else led = 0; #endif while(1) { if(timer.read() >= 2.0) { // check for timeout timer.stop(); // stop timer timer.reset(); // reset timer (to avaoid repeated send) counter++; // increment counter txMsg.clear(); // clear Tx message storage txMsg.id = TX_ID; // set ID txMsg << counter; // append first data item (always make sure that CAN message total data lenght <= 8 bytes!) txMsg << led.read(); // append second data item (always make sure that CAN message total data lenght <= 8 bytes!) can.write(txMsg); // transmit message printf("CAN message sent\r\n"); led = 0; // turn off LED } if(msgAvailable) { msgAvailable = false; // reset flag for next use can.read(rxMsg); // read message into Rx message storage printf("CAN message received:\r\n"); printf(" ID = %#x\r\n", rxMsg.id); printf(" Type = %d\r\n", rxMsg.type); printf(" Format = %d\r\n", rxMsg.format); printf(" Length = %d\r\n", rxMsg.len); printf(" Data ="); for(int i = 0; i < rxMsg.len; i++) printf(" %x", rxMsg.data[i]); printf("\r\n"); if(rxMsg.id == RX_ID) { // if ID matches rxMsg >> counter; // extract first data item rxMsg >> ledTarget; // extract second data item led = ledTarget; // set LED printf("counter = %d\r\n", counter); timer.start(); } } } }