![](/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@12:e91e44924194, 2016-07-23 (annotated)
- Committer:
- hudakz
- Date:
- Sat Jul 23 08:29:31 2016 +0000
- Revision:
- 12:e91e44924194
- Parent:
- 11:07d927da1a94
- Child:
- 13:e741def3e4ff
Updated
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hudakz | 0:c5e5d0df6f2a | 1 | /* |
hudakz | 0:c5e5d0df6f2a | 2 | * An example showing how to use the CANnucleo library: |
hudakz | 0:c5e5d0df6f2a | 3 | * |
hudakz | 6:7ff95ce72f6d | 4 | * Two affordable (less than $4 on ebay) STM32F103C8T6 boards (20kB SRAM, 64kB Flash), |
hudakz | 6:7ff95ce72f6d | 5 | * compatible with the NUCLEO-F103RB platform (20kB SRAM, 128kB Flash), |
hudakz | 6:7ff95ce72f6d | 6 | * are connected to the same CAN bus via transceivers (MCP2551 or TJA1040, or etc.). |
hudakz | 6:7ff95ce72f6d | 7 | * CAN transceivers are not part of NUCLEO boards, therefore must be added by you. |
hudakz | 6:7ff95ce72f6d | 8 | * Remember also that CAN bus (even a short one) must be terminated with 120 Ohm resitors at both ends. |
hudakz | 6:7ff95ce72f6d | 9 | * |
hudakz | 10:66da8731bdb6 | 10 | * For more details see the wiki page <https://developer.mbed.org/users/hudakz/code/CANnucleo_Hello/> |
hudakz | 6:7ff95ce72f6d | 11 | * |
hudakz | 10:66da8731bdb6 | 12 | * NOTE: If you'd like to use the official NUCLEO boards comment out line 25 |
hudakz | 6:7ff95ce72f6d | 13 | * |
hudakz | 6:7ff95ce72f6d | 14 | * The same code is used for both NUCLEO boards, but: |
hudakz | 0:c5e5d0df6f2a | 15 | * For board #1 compile the example without any change. |
hudakz | 10:66da8731bdb6 | 16 | * For board #2 comment out line 26 before compiling |
hudakz | 4:ccf4ac2deac8 | 17 | * |
hudakz | 6:7ff95ce72f6d | 18 | * Once the binaries have been downloaded to the boards reset board #1. |
hudakz | 0:c5e5d0df6f2a | 19 | * |
hudakz | 0:c5e5d0df6f2a | 20 | */ |
hudakz | 0:c5e5d0df6f2a | 21 | |
hudakz | 0:c5e5d0df6f2a | 22 | #include "mbed.h" |
hudakz | 12:e91e44924194 | 23 | #include "CANnucleo.h" |
hudakz | 0:c5e5d0df6f2a | 24 | |
hudakz | 12:e91e44924194 | 25 | //#define TARGET_STM32F103C8T6 1 // comment out this line when using official NUCLEO boards! |
hudakz | 10:66da8731bdb6 | 26 | #define BOARD1 1 // comment out this line when compiling for board #2 |
hudakz | 0:c5e5d0df6f2a | 27 | |
hudakz | 10:66da8731bdb6 | 28 | #if defined(TARGET_STM32F103C8T6) |
hudakz | 10:66da8731bdb6 | 29 | #define LED_PIN PC_13 |
hudakz | 10:66da8731bdb6 | 30 | const int OFF = 1; |
hudakz | 10:66da8731bdb6 | 31 | const int ON = 0; |
hudakz | 0:c5e5d0df6f2a | 32 | #else |
hudakz | 10:66da8731bdb6 | 33 | #define LED_PIN LED1 |
hudakz | 10:66da8731bdb6 | 34 | const int OFF = 0; |
hudakz | 10:66da8731bdb6 | 35 | const int ON = 1; |
hudakz | 0:c5e5d0df6f2a | 36 | #endif |
hudakz | 0:c5e5d0df6f2a | 37 | |
hudakz | 10:66da8731bdb6 | 38 | #if defined(BOARD1) |
hudakz | 10:66da8731bdb6 | 39 | const unsigned int RX_ID = 0x100; |
hudakz | 10:66da8731bdb6 | 40 | const unsigned int TX_ID = 0x101; |
hudakz | 6:7ff95ce72f6d | 41 | #else |
hudakz | 10:66da8731bdb6 | 42 | const unsigned int RX_ID = 0x101; |
hudakz | 10:66da8731bdb6 | 43 | const unsigned int TX_ID = 0x100; |
hudakz | 6:7ff95ce72f6d | 44 | #endif |
hudakz | 6:7ff95ce72f6d | 45 | |
hudakz | 11:07d927da1a94 | 46 | DigitalOut led(LED_PIN); |
hudakz | 11:07d927da1a94 | 47 | int ledState; |
hudakz | 11:07d927da1a94 | 48 | Timer timer; |
hudakz | 11:07d927da1a94 | 49 | CAN can(PA_11, PA_12); // CAN Rx pin name, CAN Tx pin name |
hudakz | 11:07d927da1a94 | 50 | CANMessage rxMsg; |
hudakz | 11:07d927da1a94 | 51 | CANMessage txMsg; |
hudakz | 11:07d927da1a94 | 52 | int counter = 0; |
hudakz | 0:c5e5d0df6f2a | 53 | volatile bool msgAvailable = false; |
hudakz | 0:c5e5d0df6f2a | 54 | |
hudakz | 0:c5e5d0df6f2a | 55 | /** |
hudakz | 0:c5e5d0df6f2a | 56 | * @brief 'CAN receive-complete' interrup handler. |
hudakz | 0:c5e5d0df6f2a | 57 | * @note Called on arrival of new CAN message. |
hudakz | 0:c5e5d0df6f2a | 58 | * Keep it as short as possible. |
hudakz | 0:c5e5d0df6f2a | 59 | * @param |
hudakz | 0:c5e5d0df6f2a | 60 | * @retval |
hudakz | 0:c5e5d0df6f2a | 61 | */ |
hudakz | 0:c5e5d0df6f2a | 62 | void onMsgReceived() { |
hudakz | 0:c5e5d0df6f2a | 63 | msgAvailable = true; |
hudakz | 0:c5e5d0df6f2a | 64 | } |
hudakz | 0:c5e5d0df6f2a | 65 | |
hudakz | 0:c5e5d0df6f2a | 66 | /** |
hudakz | 0:c5e5d0df6f2a | 67 | * @brief Main |
hudakz | 0:c5e5d0df6f2a | 68 | * @note |
hudakz | 0:c5e5d0df6f2a | 69 | * @param |
hudakz | 0:c5e5d0df6f2a | 70 | * @retval |
hudakz | 0:c5e5d0df6f2a | 71 | */ |
hudakz | 0:c5e5d0df6f2a | 72 | int main() { |
hudakz | 5:c6503b7ae971 | 73 | can.frequency(1000000); // set bit rate to 1Mbps |
hudakz | 12:e91e44924194 | 74 | can.attach(&onMsgReceived); // attach 'CAN receive-complete' interrupt handler |
hudakz | 6:7ff95ce72f6d | 75 | |
hudakz | 0:c5e5d0df6f2a | 76 | #if defined(BOARD1) |
hudakz | 10:66da8731bdb6 | 77 | led = ON; // turn LED on |
hudakz | 10:66da8731bdb6 | 78 | timer.start(); // start timer |
hudakz | 0:c5e5d0df6f2a | 79 | #else |
hudakz | 10:66da8731bdb6 | 80 | led = OFF; // turn LED off |
hudakz | 0:c5e5d0df6f2a | 81 | #endif |
hudakz | 0:c5e5d0df6f2a | 82 | |
hudakz | 0:c5e5d0df6f2a | 83 | while(1) { |
hudakz | 4:ccf4ac2deac8 | 84 | if(timer.read() >= 1.0) { // check for timeout |
hudakz | 0:c5e5d0df6f2a | 85 | timer.stop(); // stop timer |
hudakz | 10:66da8731bdb6 | 86 | timer.reset(); // reset timer |
hudakz | 0:c5e5d0df6f2a | 87 | counter++; // increment counter |
hudakz | 10:66da8731bdb6 | 88 | ledState = led.read(); // get led state |
hudakz | 0:c5e5d0df6f2a | 89 | txMsg.clear(); // clear Tx message storage |
hudakz | 0:c5e5d0df6f2a | 90 | txMsg.id = TX_ID; // set ID |
hudakz | 10:66da8731bdb6 | 91 | txMsg << counter; // append first data item |
hudakz | 10:66da8731bdb6 | 92 | txMsg << ledState; // append second data item (total data lenght must be <= 8 bytes!) |
hudakz | 10:66da8731bdb6 | 93 | led = OFF; // turn LED off |
hudakz | 10:66da8731bdb6 | 94 | if(can.write(txMsg)) // transmit message |
hudakz | 10:66da8731bdb6 | 95 | printf("CAN message sent\r\n"); |
hudakz | 10:66da8731bdb6 | 96 | else |
hudakz | 10:66da8731bdb6 | 97 | printf("Transmission error\r\n"); |
hudakz | 0:c5e5d0df6f2a | 98 | } |
hudakz | 0:c5e5d0df6f2a | 99 | if(msgAvailable) { |
hudakz | 0:c5e5d0df6f2a | 100 | msgAvailable = false; // reset flag for next use |
hudakz | 0:c5e5d0df6f2a | 101 | can.read(rxMsg); // read message into Rx message storage |
hudakz | 2:49c9430860d1 | 102 | printf("CAN message received:\r\n"); |
hudakz | 2:49c9430860d1 | 103 | printf(" ID = %#x\r\n", rxMsg.id); |
hudakz | 2:49c9430860d1 | 104 | printf(" Type = %d\r\n", rxMsg.type); |
hudakz | 2:49c9430860d1 | 105 | printf(" Format = %d\r\n", rxMsg.format); |
hudakz | 2:49c9430860d1 | 106 | printf(" Length = %d\r\n", rxMsg.len); |
hudakz | 2:49c9430860d1 | 107 | printf(" Data ="); |
hudakz | 2:49c9430860d1 | 108 | for(int i = 0; i < rxMsg.len; i++) |
hudakz | 2:49c9430860d1 | 109 | printf(" %x", rxMsg.data[i]); |
hudakz | 10:66da8731bdb6 | 110 | printf("\r\n"); |
hudakz | 10:66da8731bdb6 | 111 | // Filtering performed by software: |
hudakz | 12:e91e44924194 | 112 | if(rxMsg.id == RX_ID) { // See comments in CAN.cpp for filtering performed by hardware |
hudakz | 0:c5e5d0df6f2a | 113 | rxMsg >> counter; // extract first data item |
hudakz | 10:66da8731bdb6 | 114 | rxMsg >> ledState; // extract second data item |
hudakz | 1:267d6288df33 | 115 | printf("counter = %d\r\n", counter); |
hudakz | 10:66da8731bdb6 | 116 | led = ledState; // set LED |
hudakz | 10:66da8731bdb6 | 117 | timer.start(); // transmission lag |
hudakz | 0:c5e5d0df6f2a | 118 | } |
hudakz | 0:c5e5d0df6f2a | 119 | } |
hudakz | 0:c5e5d0df6f2a | 120 | } |
hudakz | 0:c5e5d0df6f2a | 121 | } |
hudakz | 7:2dce8ed51091 | 122 | |
hudakz | 12:e91e44924194 | 123 |