![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
all bms comunicating with nucleo board and balancing
Fork of Can_sniffer_BMS_GER by
main.cpp@15:6449443e2207, 2016-07-29 (annotated)
- Committer:
- hudakz
- Date:
- Fri Jul 29 10:24:45 2016 +0000
- Revision:
- 15:6449443e2207
- Parent:
- 14:e12ebd1260b1
- Child:
- 16:a86f339d1c25
Updated
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hudakz | 0:c5e5d0df6f2a | 1 | /* |
hudakz | 13:e741def3e4ff | 2 | * An example of using CAN bus with NUCLEO boards: |
hudakz | 0:c5e5d0df6f2a | 3 | * |
hudakz | 15:6449443e2207 | 4 | * Two affordable (less than $3 on ebay) STM32F103C8T6 boards compatible with the NUCLEO-F103RB platform |
hudakz | 6:7ff95ce72f6d | 5 | * are connected to the same CAN bus via transceivers (MCP2551 or TJA1040, or etc.). |
hudakz | 6:7ff95ce72f6d | 6 | * CAN transceivers are not part of NUCLEO boards, therefore must be added by you. |
hudakz | 6:7ff95ce72f6d | 7 | * Remember also that CAN bus (even a short one) must be terminated with 120 Ohm resitors at both ends. |
hudakz | 6:7ff95ce72f6d | 8 | * |
hudakz | 13:e741def3e4ff | 9 | * For more details see the wiki page [https://developer.mbed.org/users/hudakz/code/CANnucleo_Hello/] |
hudakz | 6:7ff95ce72f6d | 10 | * |
hudakz | 13:e741def3e4ff | 11 | * NOTE: If you'd like to use the official NUCLEO boards comment out line 24 |
hudakz | 6:7ff95ce72f6d | 12 | * |
hudakz | 6:7ff95ce72f6d | 13 | * The same code is used for both NUCLEO boards, but: |
hudakz | 0:c5e5d0df6f2a | 14 | * For board #1 compile the example without any change. |
hudakz | 13:e741def3e4ff | 15 | * For board #2 comment out line 25 before compiling |
hudakz | 4:ccf4ac2deac8 | 16 | * |
hudakz | 6:7ff95ce72f6d | 17 | * Once the binaries have been downloaded to the boards reset board #1. |
hudakz | 0:c5e5d0df6f2a | 18 | * |
hudakz | 0:c5e5d0df6f2a | 19 | */ |
hudakz | 0:c5e5d0df6f2a | 20 | |
hudakz | 15:6449443e2207 | 21 | |
hudakz | 0:c5e5d0df6f2a | 22 | #include "mbed.h" |
hudakz | 0:c5e5d0df6f2a | 23 | |
hudakz | 14:e12ebd1260b1 | 24 | //#define TARGET_STM32F103C8T6 1 // comment out this line when using official NUCLEO boards! |
hudakz | 10:66da8731bdb6 | 25 | #define BOARD1 1 // comment out this line when compiling for board #2 |
hudakz | 0:c5e5d0df6f2a | 26 | |
hudakz | 10:66da8731bdb6 | 27 | #if defined(TARGET_STM32F103C8T6) |
hudakz | 10:66da8731bdb6 | 28 | #define LED_PIN PC_13 |
hudakz | 10:66da8731bdb6 | 29 | const int OFF = 1; |
hudakz | 10:66da8731bdb6 | 30 | const int ON = 0; |
hudakz | 0:c5e5d0df6f2a | 31 | #else |
hudakz | 10:66da8731bdb6 | 32 | #define LED_PIN LED1 |
hudakz | 10:66da8731bdb6 | 33 | const int OFF = 0; |
hudakz | 10:66da8731bdb6 | 34 | const int ON = 1; |
hudakz | 0:c5e5d0df6f2a | 35 | #endif |
hudakz | 0:c5e5d0df6f2a | 36 | |
hudakz | 10:66da8731bdb6 | 37 | #if defined(BOARD1) |
hudakz | 10:66da8731bdb6 | 38 | const unsigned int RX_ID = 0x100; |
hudakz | 10:66da8731bdb6 | 39 | const unsigned int TX_ID = 0x101; |
hudakz | 6:7ff95ce72f6d | 40 | #else |
hudakz | 10:66da8731bdb6 | 41 | const unsigned int RX_ID = 0x101; |
hudakz | 10:66da8731bdb6 | 42 | const unsigned int TX_ID = 0x100; |
hudakz | 6:7ff95ce72f6d | 43 | #endif |
hudakz | 6:7ff95ce72f6d | 44 | |
hudakz | 11:07d927da1a94 | 45 | DigitalOut led(LED_PIN); |
hudakz | 11:07d927da1a94 | 46 | Timer timer; |
hudakz | 15:6449443e2207 | 47 | CAN can(PA_11, PA_12); // CAN RD pin name, CAN TD pin name |
hudakz | 11:07d927da1a94 | 48 | CANMessage rxMsg; |
hudakz | 13:e741def3e4ff | 49 | char counter = 0; |
hudakz | 0:c5e5d0df6f2a | 50 | |
hudakz | 14:e12ebd1260b1 | 51 | int main() { |
hudakz | 0:c5e5d0df6f2a | 52 | #if defined(BOARD1) |
hudakz | 10:66da8731bdb6 | 53 | led = ON; // turn LED on |
hudakz | 10:66da8731bdb6 | 54 | timer.start(); // start timer |
hudakz | 0:c5e5d0df6f2a | 55 | #else |
hudakz | 10:66da8731bdb6 | 56 | led = OFF; // turn LED off |
hudakz | 0:c5e5d0df6f2a | 57 | #endif |
hudakz | 0:c5e5d0df6f2a | 58 | |
hudakz | 15:6449443e2207 | 59 | can.frequency(1000000); // set bit rate to 1Mbps |
hudakz | 15:6449443e2207 | 60 | |
hudakz | 0:c5e5d0df6f2a | 61 | while(1) { |
hudakz | 15:6449443e2207 | 62 | if(timer.read_ms() >= 1000) { // check for timeout |
hudakz | 14:e12ebd1260b1 | 63 | timer.stop(); // stop timer |
hudakz | 14:e12ebd1260b1 | 64 | timer.reset(); // reset timer |
hudakz | 14:e12ebd1260b1 | 65 | counter++; // increment counter |
hudakz | 14:e12ebd1260b1 | 66 | led = OFF; // turn LED off |
hudakz | 14:e12ebd1260b1 | 67 | if(can.write(CANMessage(TX_ID, &counter, 1))) // transmit message |
hudakz | 15:6449443e2207 | 68 | printf("CAN message sent\r\n\r\n"); |
hudakz | 10:66da8731bdb6 | 69 | else |
hudakz | 15:6449443e2207 | 70 | printf("Transmission error\r\n\r\n"); |
hudakz | 0:c5e5d0df6f2a | 71 | } |
hudakz | 15:6449443e2207 | 72 | if(can.read(rxMsg)) { // check for new message |
hudakz | 15:6449443e2207 | 73 | printf("CAN message received:\r\n"); // print message details |
hudakz | 15:6449443e2207 | 74 | printf(" ID = 0x%.3x\r\n", rxMsg.id); |
hudakz | 2:49c9430860d1 | 75 | printf(" Type = %d\r\n", rxMsg.type); |
hudakz | 2:49c9430860d1 | 76 | printf(" Format = %d\r\n", rxMsg.format); |
hudakz | 2:49c9430860d1 | 77 | printf(" Length = %d\r\n", rxMsg.len); |
hudakz | 2:49c9430860d1 | 78 | printf(" Data ="); |
hudakz | 2:49c9430860d1 | 79 | for(int i = 0; i < rxMsg.len; i++) |
hudakz | 14:e12ebd1260b1 | 80 | printf(" 0x%.2x", rxMsg.data[i]); |
hudakz | 10:66da8731bdb6 | 81 | printf("\r\n"); |
hudakz | 13:e741def3e4ff | 82 | |
hudakz | 13:e741def3e4ff | 83 | if(rxMsg.id == RX_ID) { |
hudakz | 14:e12ebd1260b1 | 84 | counter = rxMsg.data[0]; // set counter |
hudakz | 15:6449443e2207 | 85 | printf("counter = %d\r\n\r\n", counter); // print counter |
hudakz | 15:6449443e2207 | 86 | led = ON; // turn LED on |
hudakz | 14:e12ebd1260b1 | 87 | timer.start(); // transmission lag |
hudakz | 0:c5e5d0df6f2a | 88 | } |
hudakz | 0:c5e5d0df6f2a | 89 | } |
hudakz | 0:c5e5d0df6f2a | 90 | } |
hudakz | 0:c5e5d0df6f2a | 91 | } |
hudakz | 7:2dce8ed51091 | 92 | |
hudakz | 12:e91e44924194 | 93 |