Ultima versão da banca de ensaios BMS Fev2017
Dependencies: CANnucleo LTC68041 mbed
Fork of BMS_4 by
main.cpp@3:464b06c16d24, 2015-07-24 (annotated)
- Committer:
- hudakz
- Date:
- Fri Jul 24 00:46:47 2015 +0000
- Revision:
- 3:464b06c16d24
- Parent:
- 2:49c9430860d1
- Child:
- 4:ccf4ac2deac8
bug in setting LED status fixed
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 | 2:49c9430860d1 | 4 | * Two NUCLEO boards are connected to the same CAN bus via CAN transceivers (MCP2551 or TJA1040, or etc.). |
hudakz | 0:c5e5d0df6f2a | 5 | * Transceivers are not part of the NUCLEO boards, therefore must be added by you. |
hudakz | 0:c5e5d0df6f2a | 6 | * Remember also that CAN bus must be terminated with 120 Ohm resitors on both ends. |
hudakz | 0:c5e5d0df6f2a | 7 | * See <https://developer.mbed.org/users/WiredHome/notebook/can---getting-started/> |
hudakz | 0:c5e5d0df6f2a | 8 | * The same source code is used for both NUCLEO boards, but: |
hudakz | 0:c5e5d0df6f2a | 9 | * For board #1 compile the example without any change. |
hudakz | 0:c5e5d0df6f2a | 10 | * For board #2 set BOARD1 to 0 |
hudakz | 0:c5e5d0df6f2a | 11 | * |
hudakz | 0:c5e5d0df6f2a | 12 | * Note: |
hudakz | 0:c5e5d0df6f2a | 13 | * To simplify adding/getting data to/from a CAN message |
hudakz | 0:c5e5d0df6f2a | 14 | * inserter "<<" and extractor ">>" operators have been defined. |
hudakz | 0:c5e5d0df6f2a | 15 | * Please be aware that CAN message maximum data length is limited to eight bytes. |
hudakz | 0:c5e5d0df6f2a | 16 | * To make sure this limitation is not violated I recommend to first compile |
hudakz | 0:c5e5d0df6f2a | 17 | * your application with DEBUG enabled in "CAN.h" file. |
hudakz | 0:c5e5d0df6f2a | 18 | * Then run it and check for error messages. |
hudakz | 0:c5e5d0df6f2a | 19 | */ |
hudakz | 0:c5e5d0df6f2a | 20 | |
hudakz | 0:c5e5d0df6f2a | 21 | #include "mbed.h" |
hudakz | 0:c5e5d0df6f2a | 22 | #include "CAN.h" |
hudakz | 0:c5e5d0df6f2a | 23 | |
hudakz | 3:464b06c16d24 | 24 | #define BOARD1 1 // please comment out this line when compiling for board #2 |
hudakz | 0:c5e5d0df6f2a | 25 | |
hudakz | 0:c5e5d0df6f2a | 26 | #if defined(BOARD1) |
hudakz | 0:c5e5d0df6f2a | 27 | #define RX_ID 0x100 |
hudakz | 0:c5e5d0df6f2a | 28 | #define TX_ID 0x101 |
hudakz | 0:c5e5d0df6f2a | 29 | #else |
hudakz | 0:c5e5d0df6f2a | 30 | #define RX_ID 0x101 |
hudakz | 0:c5e5d0df6f2a | 31 | #define TX_ID 0x100 |
hudakz | 0:c5e5d0df6f2a | 32 | #endif |
hudakz | 0:c5e5d0df6f2a | 33 | |
hudakz | 0:c5e5d0df6f2a | 34 | DigitalOut led(LED1); |
hudakz | 3:464b06c16d24 | 35 | int ledTarget; |
hudakz | 0:c5e5d0df6f2a | 36 | Timer timer; |
hudakz | 0:c5e5d0df6f2a | 37 | CAN can(PA_11, PA_12); // rx, tx |
hudakz | 0:c5e5d0df6f2a | 38 | CANMessage rxMsg; |
hudakz | 0:c5e5d0df6f2a | 39 | CANMessage txMsg; |
hudakz | 2:49c9430860d1 | 40 | long int counter = 0; |
hudakz | 0:c5e5d0df6f2a | 41 | volatile bool msgAvailable = false; |
hudakz | 0:c5e5d0df6f2a | 42 | |
hudakz | 0:c5e5d0df6f2a | 43 | /** |
hudakz | 0:c5e5d0df6f2a | 44 | * @brief 'CAN receive-complete' interrup handler. |
hudakz | 0:c5e5d0df6f2a | 45 | * @note Called on arrival of new CAN message. |
hudakz | 0:c5e5d0df6f2a | 46 | * Keep it as short as possible. |
hudakz | 0:c5e5d0df6f2a | 47 | * @param |
hudakz | 0:c5e5d0df6f2a | 48 | * @retval |
hudakz | 0:c5e5d0df6f2a | 49 | */ |
hudakz | 0:c5e5d0df6f2a | 50 | void onMsgReceived() { |
hudakz | 0:c5e5d0df6f2a | 51 | msgAvailable = true; |
hudakz | 0:c5e5d0df6f2a | 52 | } |
hudakz | 0:c5e5d0df6f2a | 53 | |
hudakz | 0:c5e5d0df6f2a | 54 | /** |
hudakz | 0:c5e5d0df6f2a | 55 | * @brief Main |
hudakz | 0:c5e5d0df6f2a | 56 | * @note |
hudakz | 0:c5e5d0df6f2a | 57 | * @param |
hudakz | 0:c5e5d0df6f2a | 58 | * @retval |
hudakz | 0:c5e5d0df6f2a | 59 | */ |
hudakz | 0:c5e5d0df6f2a | 60 | int main() { |
hudakz | 0:c5e5d0df6f2a | 61 | can.frequency(500000); // Initialize CAN and set bit rate to 500kbs |
hudakz | 0:c5e5d0df6f2a | 62 | can.attach(&onMsgReceived, CAN::RxIrq); // attach 'CAN receive complete' interrupt handler |
hudakz | 0:c5e5d0df6f2a | 63 | timer.reset(); |
hudakz | 0:c5e5d0df6f2a | 64 | #if defined(BOARD1) |
hudakz | 2:49c9430860d1 | 65 | led = 1; |
hudakz | 2:49c9430860d1 | 66 | timer.start(); |
hudakz | 0:c5e5d0df6f2a | 67 | #else |
hudakz | 2:49c9430860d1 | 68 | led = 0; |
hudakz | 0:c5e5d0df6f2a | 69 | #endif |
hudakz | 0:c5e5d0df6f2a | 70 | |
hudakz | 0:c5e5d0df6f2a | 71 | while(1) { |
hudakz | 0:c5e5d0df6f2a | 72 | if(timer.read() >= 2.0) { // check for timeout |
hudakz | 0:c5e5d0df6f2a | 73 | timer.stop(); // stop timer |
hudakz | 0:c5e5d0df6f2a | 74 | timer.reset(); // reset timer (to avaoid repeated send) |
hudakz | 0:c5e5d0df6f2a | 75 | counter++; // increment counter |
hudakz | 0:c5e5d0df6f2a | 76 | txMsg.clear(); // clear Tx message storage |
hudakz | 0:c5e5d0df6f2a | 77 | txMsg.id = TX_ID; // set ID |
hudakz | 0:c5e5d0df6f2a | 78 | txMsg << counter; // append first data item (always make sure that CAN message total data lenght <= 8 bytes!) |
hudakz | 0:c5e5d0df6f2a | 79 | txMsg << led.read(); // append second data item (always make sure that CAN message total data lenght <= 8 bytes!) |
hudakz | 0:c5e5d0df6f2a | 80 | can.write(txMsg); // transmit message |
hudakz | 0:c5e5d0df6f2a | 81 | printf("CAN message sent\r\n"); |
hudakz | 2:49c9430860d1 | 82 | led = 0; // turn off LED |
hudakz | 0:c5e5d0df6f2a | 83 | } |
hudakz | 0:c5e5d0df6f2a | 84 | if(msgAvailable) { |
hudakz | 0:c5e5d0df6f2a | 85 | msgAvailable = false; // reset flag for next use |
hudakz | 0:c5e5d0df6f2a | 86 | can.read(rxMsg); // read message into Rx message storage |
hudakz | 2:49c9430860d1 | 87 | printf("CAN message received:\r\n"); |
hudakz | 2:49c9430860d1 | 88 | printf(" ID = %#x\r\n", rxMsg.id); |
hudakz | 2:49c9430860d1 | 89 | printf(" Type = %d\r\n", rxMsg.type); |
hudakz | 2:49c9430860d1 | 90 | printf(" Format = %d\r\n", rxMsg.format); |
hudakz | 2:49c9430860d1 | 91 | printf(" Length = %d\r\n", rxMsg.len); |
hudakz | 2:49c9430860d1 | 92 | printf(" Data ="); |
hudakz | 2:49c9430860d1 | 93 | for(int i = 0; i < rxMsg.len; i++) |
hudakz | 2:49c9430860d1 | 94 | printf(" %x", rxMsg.data[i]); |
hudakz | 2:49c9430860d1 | 95 | printf("\r\n"); |
hudakz | 0:c5e5d0df6f2a | 96 | if(rxMsg.id == RX_ID) { // if ID matches |
hudakz | 0:c5e5d0df6f2a | 97 | rxMsg >> counter; // extract first data item |
hudakz | 3:464b06c16d24 | 98 | rxMsg >> ledTarget; // extract second data item |
hudakz | 3:464b06c16d24 | 99 | led = ledTarget; // set LED |
hudakz | 1:267d6288df33 | 100 | printf("counter = %d\r\n", counter); |
hudakz | 0:c5e5d0df6f2a | 101 | timer.start(); |
hudakz | 0:c5e5d0df6f2a | 102 | } |
hudakz | 0:c5e5d0df6f2a | 103 | } |
hudakz | 0:c5e5d0df6f2a | 104 | } |
hudakz | 0:c5e5d0df6f2a | 105 | } |
hudakz | 0:c5e5d0df6f2a | 106 | |
hudakz | 0:c5e5d0df6f2a | 107 |