![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Nucleo by Cach
Dependencies: C12832 CANnucleo LM75B mbed
Fork of CANnucleo_Hello by
main.cpp@10:d295b0df4b82, 2016-01-29 (annotated)
- Committer:
- jcourtois
- Date:
- Fri Jan 29 16:58:28 2016 +0000
- Revision:
- 10:d295b0df4b82
- Parent:
- 7:2dce8ed51091
Test
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 | 6:7ff95ce72f6d | 10 | * For more details see the wiki page <https://developer.mbed.org/users/hudakz/code/CAN_Nucleo_Hello/> |
hudakz | 6:7ff95ce72f6d | 11 | * |
hudakz | 6:7ff95ce72f6d | 12 | * NOTE: If you'd like to use the official NUCLEO-F103RB boards |
hudakz | 6:7ff95ce72f6d | 13 | * comment out the line #define TARGET_STM32F103C8T6 1 |
hudakz | 6:7ff95ce72f6d | 14 | * |
hudakz | 6:7ff95ce72f6d | 15 | * The same code is used for both NUCLEO boards, but: |
hudakz | 0:c5e5d0df6f2a | 16 | * For board #1 compile the example without any change. |
hudakz | 4:ccf4ac2deac8 | 17 | * For board #2 comment out the line #define BOARD1 1 before compiling |
hudakz | 4:ccf4ac2deac8 | 18 | * |
hudakz | 6:7ff95ce72f6d | 19 | * Once the binaries have been downloaded to the boards reset board #1. |
hudakz | 0:c5e5d0df6f2a | 20 | * |
hudakz | 0:c5e5d0df6f2a | 21 | */ |
hudakz | 0:c5e5d0df6f2a | 22 | |
hudakz | 0:c5e5d0df6f2a | 23 | #include "mbed.h" |
hudakz | 0:c5e5d0df6f2a | 24 | #include "CAN.h" |
jcourtois | 10:d295b0df4b82 | 25 | #include "C12832.h" |
jcourtois | 10:d295b0df4b82 | 26 | #include "LM75B.h" |
hudakz | 0:c5e5d0df6f2a | 27 | |
hudakz | 6:7ff95ce72f6d | 28 | #define BOARD1 1 // comment out this line when compiling for board #2 |
hudakz | 0:c5e5d0df6f2a | 29 | |
hudakz | 0:c5e5d0df6f2a | 30 | #if defined(BOARD1) |
hudakz | 0:c5e5d0df6f2a | 31 | #define RX_ID 0x100 |
jcourtois | 10:d295b0df4b82 | 32 | #define TX_ID 0x7FF |
hudakz | 0:c5e5d0df6f2a | 33 | #else |
hudakz | 0:c5e5d0df6f2a | 34 | #define RX_ID 0x101 |
jcourtois | 10:d295b0df4b82 | 35 | #define TX_ID 0x7FF |
hudakz | 0:c5e5d0df6f2a | 36 | #endif |
hudakz | 0:c5e5d0df6f2a | 37 | |
hudakz | 6:7ff95ce72f6d | 38 | // See wiki page <https://developer.mbed.org/users/hudakz/code/CAN_Nucleo_Hello/> |
hudakz | 7:2dce8ed51091 | 39 | //#define TARGET_STM32F103C8T6 1 // comment out this line if you'd like to use the official NUCLEO-F103RB boards |
hudakz | 6:7ff95ce72f6d | 40 | |
hudakz | 6:7ff95ce72f6d | 41 | #if defined(TARGET_STM32F103C8T6) |
hudakz | 6:7ff95ce72f6d | 42 | DigitalOut led(PC_13); |
hudakz | 6:7ff95ce72f6d | 43 | #else |
hudakz | 6:7ff95ce72f6d | 44 | DigitalOut led(LED1); |
hudakz | 6:7ff95ce72f6d | 45 | #endif |
hudakz | 6:7ff95ce72f6d | 46 | |
jcourtois | 10:d295b0df4b82 | 47 | DigitalIn fire(D4); |
jcourtois | 10:d295b0df4b82 | 48 | C12832 lcd(D11, D13, D12, D7, D10); |
jcourtois | 10:d295b0df4b82 | 49 | LM75B sensor(D14,D15); |
jcourtois | 10:d295b0df4b82 | 50 | AnalogIn pot1 (A0); |
jcourtois | 10:d295b0df4b82 | 51 | AnalogIn pot2 (A1); |
jcourtois | 10:d295b0df4b82 | 52 | PwmOut r (D5); |
jcourtois | 10:d295b0df4b82 | 53 | PwmOut g (D8); |
jcourtois | 10:d295b0df4b82 | 54 | PwmOut b (D9); |
jcourtois | 10:d295b0df4b82 | 55 | |
hudakz | 5:c6503b7ae971 | 56 | int ledReceived; |
hudakz | 0:c5e5d0df6f2a | 57 | Timer timer; |
hudakz | 5:c6503b7ae971 | 58 | CAN can(PA_11, PA_12); // CAN Rx pin name, CAN Tx pin name, Automatic recovery from bus-off state enabled by default |
hudakz | 0:c5e5d0df6f2a | 59 | CANMessage rxMsg; |
hudakz | 0:c5e5d0df6f2a | 60 | CANMessage txMsg; |
hudakz | 5:c6503b7ae971 | 61 | int counter = 0; |
hudakz | 0:c5e5d0df6f2a | 62 | volatile bool msgAvailable = false; |
hudakz | 0:c5e5d0df6f2a | 63 | |
hudakz | 0:c5e5d0df6f2a | 64 | /** |
hudakz | 0:c5e5d0df6f2a | 65 | * @brief 'CAN receive-complete' interrup handler. |
hudakz | 0:c5e5d0df6f2a | 66 | * @note Called on arrival of new CAN message. |
hudakz | 0:c5e5d0df6f2a | 67 | * Keep it as short as possible. |
hudakz | 0:c5e5d0df6f2a | 68 | * @param |
hudakz | 0:c5e5d0df6f2a | 69 | * @retval |
hudakz | 0:c5e5d0df6f2a | 70 | */ |
jcourtois | 10:d295b0df4b82 | 71 | |
hudakz | 0:c5e5d0df6f2a | 72 | void onMsgReceived() { |
hudakz | 0:c5e5d0df6f2a | 73 | msgAvailable = true; |
hudakz | 0:c5e5d0df6f2a | 74 | } |
hudakz | 0:c5e5d0df6f2a | 75 | |
hudakz | 0:c5e5d0df6f2a | 76 | /** |
hudakz | 0:c5e5d0df6f2a | 77 | * @brief Main |
hudakz | 0:c5e5d0df6f2a | 78 | * @note |
hudakz | 0:c5e5d0df6f2a | 79 | * @param |
hudakz | 0:c5e5d0df6f2a | 80 | * @retval |
hudakz | 0:c5e5d0df6f2a | 81 | */ |
hudakz | 0:c5e5d0df6f2a | 82 | int main() { |
hudakz | 5:c6503b7ae971 | 83 | can.frequency(1000000); // set bit rate to 1Mbps |
hudakz | 5:c6503b7ae971 | 84 | can.attach(&onMsgReceived, CAN::RxIrq); // attach 'CAN receive-complete' interrupt handler |
jcourtois | 10:d295b0df4b82 | 85 | char buff_2[8]; |
hudakz | 0:c5e5d0df6f2a | 86 | #if defined(BOARD1) |
hudakz | 6:7ff95ce72f6d | 87 | #if defined(TARGET_STM32F103C8T6) |
hudakz | 6:7ff95ce72f6d | 88 | led = 0; // turn LED on |
hudakz | 6:7ff95ce72f6d | 89 | #else |
hudakz | 6:7ff95ce72f6d | 90 | led = 1; // turn LED on |
hudakz | 6:7ff95ce72f6d | 91 | #endif |
hudakz | 2:49c9430860d1 | 92 | timer.start(); |
hudakz | 0:c5e5d0df6f2a | 93 | #else |
hudakz | 6:7ff95ce72f6d | 94 | #if defined(TARGET_STM32F103C8T6) |
hudakz | 6:7ff95ce72f6d | 95 | led = 1; // turn LED off |
hudakz | 6:7ff95ce72f6d | 96 | #else |
hudakz | 6:7ff95ce72f6d | 97 | led = 0; // turn LED off |
hudakz | 6:7ff95ce72f6d | 98 | #endif |
hudakz | 0:c5e5d0df6f2a | 99 | #endif |
jcourtois | 10:d295b0df4b82 | 100 | lcd.cls(); |
hudakz | 0:c5e5d0df6f2a | 101 | while(1) { |
jcourtois | 10:d295b0df4b82 | 102 | wait (0.1); |
jcourtois | 10:d295b0df4b82 | 103 | lcd.cls(); |
jcourtois | 10:d295b0df4b82 | 104 | lcd.locate(0,3); |
jcourtois | 10:d295b0df4b82 | 105 | lcd.printf("%d %d %d",char(sensor.temp()),char(pot1*255.0), char(pot2*255.0)); |
jcourtois | 10:d295b0df4b82 | 106 | lcd.locate(0,14); |
jcourtois | 10:d295b0df4b82 | 107 | lcd.printf("I:%dF:%dl:%d Data:", rxMsg.id, rxMsg.type, rxMsg.len); |
jcourtois | 10:d295b0df4b82 | 108 | for(int i = 0; i < rxMsg.len; i++) |
jcourtois | 10:d295b0df4b82 | 109 | { |
jcourtois | 10:d295b0df4b82 | 110 | lcd.printf("%x", buff_2[i]); |
jcourtois | 10:d295b0df4b82 | 111 | } |
jcourtois | 10:d295b0df4b82 | 112 | if(fire) { // check for timeout |
hudakz | 0:c5e5d0df6f2a | 113 | timer.stop(); // stop timer |
hudakz | 0:c5e5d0df6f2a | 114 | timer.reset(); // reset timer (to avaoid repeated send) |
hudakz | 0:c5e5d0df6f2a | 115 | counter++; // increment counter |
hudakz | 0:c5e5d0df6f2a | 116 | txMsg.clear(); // clear Tx message storage |
hudakz | 0:c5e5d0df6f2a | 117 | txMsg.id = TX_ID; // set ID |
jcourtois | 10:d295b0df4b82 | 118 | txMsg << char(sensor.temp()); // append first data item (make sure that CAN message total data lenght <= 8 bytes!) |
jcourtois | 10:d295b0df4b82 | 119 | txMsg << char(0x00); |
jcourtois | 10:d295b0df4b82 | 120 | txMsg << char(pot1*255.0); // append second data item (make sure that CAN message total data lenght <= 8 bytes!) |
jcourtois | 10:d295b0df4b82 | 121 | txMsg << char(pot2*255.0); |
hudakz | 0:c5e5d0df6f2a | 122 | can.write(txMsg); // transmit message |
hudakz | 0:c5e5d0df6f2a | 123 | printf("CAN message sent\r\n"); |
hudakz | 6:7ff95ce72f6d | 124 | |
jcourtois | 10:d295b0df4b82 | 125 | |
jcourtois | 10:d295b0df4b82 | 126 | |
hudakz | 6:7ff95ce72f6d | 127 | #if defined(TARGET_STM32F103C8T6) |
hudakz | 6:7ff95ce72f6d | 128 | led = 1; // turn LED off |
hudakz | 6:7ff95ce72f6d | 129 | #else |
hudakz | 6:7ff95ce72f6d | 130 | led = 0; // turn LED off |
hudakz | 6:7ff95ce72f6d | 131 | #endif |
jcourtois | 10:d295b0df4b82 | 132 | while(fire); |
hudakz | 0:c5e5d0df6f2a | 133 | } |
hudakz | 0:c5e5d0df6f2a | 134 | if(msgAvailable) { |
hudakz | 0:c5e5d0df6f2a | 135 | msgAvailable = false; // reset flag for next use |
hudakz | 0:c5e5d0df6f2a | 136 | can.read(rxMsg); // read message into Rx message storage |
hudakz | 2:49c9430860d1 | 137 | printf("CAN message received:\r\n"); |
hudakz | 2:49c9430860d1 | 138 | printf(" ID = %#x\r\n", rxMsg.id); |
hudakz | 2:49c9430860d1 | 139 | printf(" Type = %d\r\n", rxMsg.type); |
hudakz | 2:49c9430860d1 | 140 | printf(" Format = %d\r\n", rxMsg.format); |
hudakz | 2:49c9430860d1 | 141 | printf(" Length = %d\r\n", rxMsg.len); |
jcourtois | 10:d295b0df4b82 | 142 | printf(" Data ="); |
jcourtois | 10:d295b0df4b82 | 143 | lcd.locate(0,14); |
jcourtois | 10:d295b0df4b82 | 144 | lcd.printf("I:%dF:%dl:%d", rxMsg.id, rxMsg.type, rxMsg.len); |
jcourtois | 10:d295b0df4b82 | 145 | |
hudakz | 2:49c9430860d1 | 146 | for(int i = 0; i < rxMsg.len; i++) |
jcourtois | 10:d295b0df4b82 | 147 | { |
jcourtois | 10:d295b0df4b82 | 148 | lcd.printf(" %x", rxMsg.data[i]); |
jcourtois | 10:d295b0df4b82 | 149 | buff_2[i] = rxMsg.data[i]; |
jcourtois | 10:d295b0df4b82 | 150 | } |
jcourtois | 10:d295b0df4b82 | 151 | r = (rxMsg.data[0]/255.0); |
jcourtois | 10:d295b0df4b82 | 152 | g = (rxMsg.data[1]/255.0); |
jcourtois | 10:d295b0df4b82 | 153 | b = (rxMsg.data[2]/255.0); |
jcourtois | 10:d295b0df4b82 | 154 | if(rxMsg.data[0] == 1) lcd.cls(); |
hudakz | 2:49c9430860d1 | 155 | printf("\r\n"); |
hudakz | 0:c5e5d0df6f2a | 156 | if(rxMsg.id == RX_ID) { // if ID matches |
hudakz | 0:c5e5d0df6f2a | 157 | rxMsg >> counter; // extract first data item |
hudakz | 5:c6503b7ae971 | 158 | rxMsg >> ledReceived; // extract second data item |
hudakz | 5:c6503b7ae971 | 159 | led = ledReceived; // set LED |
hudakz | 1:267d6288df33 | 160 | printf("counter = %d\r\n", counter); |
hudakz | 0:c5e5d0df6f2a | 161 | timer.start(); |
hudakz | 0:c5e5d0df6f2a | 162 | } |
hudakz | 0:c5e5d0df6f2a | 163 | } |
hudakz | 0:c5e5d0df6f2a | 164 | } |
hudakz | 0:c5e5d0df6f2a | 165 | } |
hudakz | 7:2dce8ed51091 | 166 |