Nagito Sukegawa
/
Test_CAN_F446re
2019 Off season development Test of CAN communication Nucleo Board STM32 F446RE
main.cpp@0:79abbe95bdcd, 2020-03-12 (annotated)
- Committer:
- st17099ng
- Date:
- Thu Mar 12 14:24:05 2020 +0000
- Revision:
- 0:79abbe95bdcd
Initial version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
st17099ng | 0:79abbe95bdcd | 1 | /** NucleoF446re CAN1,CAN2 TEST CODE **/ |
st17099ng | 0:79abbe95bdcd | 2 | /** 2019/11/27 Coding by Nagito Sukegawa **/ |
st17099ng | 0:79abbe95bdcd | 3 | /** 2019/11/28 Add can2Rcv Function **/ |
st17099ng | 0:79abbe95bdcd | 4 | /** 参考元 福島B Coding by YutaTogashi,@nitfc_togatogah URL:https://os.mbed.com/users/YutaTogashi/code/robocon2019_AutoRobot_mbed_testVer/ **/ |
st17099ng | 0:79abbe95bdcd | 5 | |
st17099ng | 0:79abbe95bdcd | 6 | #include "mbed.h" |
st17099ng | 0:79abbe95bdcd | 7 | |
st17099ng | 0:79abbe95bdcd | 8 | void can1Trans(void); |
st17099ng | 0:79abbe95bdcd | 9 | void can2Rcv(void); |
st17099ng | 0:79abbe95bdcd | 10 | char counter[8] = {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07}; |
st17099ng | 0:79abbe95bdcd | 11 | char RcvData[8]; |
st17099ng | 0:79abbe95bdcd | 12 | Ticker ticker; |
st17099ng | 0:79abbe95bdcd | 13 | DigitalOut myled(LED1); |
st17099ng | 0:79abbe95bdcd | 14 | CAN can1(PB_8, PB_9,500000); //データ吐く方//D15,D14 |
st17099ng | 0:79abbe95bdcd | 15 | CAN can2(PB_5, PB_6,500000); //データ受ける方//D4,D10 |
st17099ng | 0:79abbe95bdcd | 16 | Serial pc(USBTX,NC,115200); |
st17099ng | 0:79abbe95bdcd | 17 | CANMessage msg1(0x456,counter,8,CANData,CANStandard); |
st17099ng | 0:79abbe95bdcd | 18 | CANMessage msg2(0x539,CANStandard); |
st17099ng | 0:79abbe95bdcd | 19 | |
st17099ng | 0:79abbe95bdcd | 20 | |
st17099ng | 0:79abbe95bdcd | 21 | int main() |
st17099ng | 0:79abbe95bdcd | 22 | { |
st17099ng | 0:79abbe95bdcd | 23 | ticker.attach(&can1Trans, 0.05); |
st17099ng | 0:79abbe95bdcd | 24 | can2.attach(&can2Rcv,CAN::RxIrq); |
st17099ng | 0:79abbe95bdcd | 25 | pc.printf("\033[2J"); |
st17099ng | 0:79abbe95bdcd | 26 | while(1) { |
st17099ng | 0:79abbe95bdcd | 27 | // pc.printf("TEST_CAN\n"); |
st17099ng | 0:79abbe95bdcd | 28 | wait(0.01); |
st17099ng | 0:79abbe95bdcd | 29 | } |
st17099ng | 0:79abbe95bdcd | 30 | } |
st17099ng | 0:79abbe95bdcd | 31 | |
st17099ng | 0:79abbe95bdcd | 32 | |
st17099ng | 0:79abbe95bdcd | 33 | /** CAN1送信関数 **/ |
st17099ng | 0:79abbe95bdcd | 34 | void can1Trans(void) |
st17099ng | 0:79abbe95bdcd | 35 | { |
st17099ng | 0:79abbe95bdcd | 36 | if(counter[0] % 0x10 == 0) { |
st17099ng | 0:79abbe95bdcd | 37 | msg1.id = 0x456; |
st17099ng | 0:79abbe95bdcd | 38 | for(uint8_t i=0; i<8; i++) { |
st17099ng | 0:79abbe95bdcd | 39 | msg1.data[i] = counter[i]++; |
st17099ng | 0:79abbe95bdcd | 40 | } |
st17099ng | 0:79abbe95bdcd | 41 | } else { |
st17099ng | 0:79abbe95bdcd | 42 | msg1.id = 0x500; |
st17099ng | 0:79abbe95bdcd | 43 | for(uint8_t i=0; i<8; i++) { |
st17099ng | 0:79abbe95bdcd | 44 | msg1.data[i] = counter[i]++; |
st17099ng | 0:79abbe95bdcd | 45 | } |
st17099ng | 0:79abbe95bdcd | 46 | } |
st17099ng | 0:79abbe95bdcd | 47 | if(can1.write(msg1)) { |
st17099ng | 0:79abbe95bdcd | 48 | myled = !myled; |
st17099ng | 0:79abbe95bdcd | 49 | pc.printf("\033[1;1HCAN1 TRANSID: 0x%x", msg1.id); |
st17099ng | 0:79abbe95bdcd | 50 | pc.printf("\033[2;1HCAN1 DATA[0]: 0x%x", msg1.data[0]); |
st17099ng | 0:79abbe95bdcd | 51 | pc.printf("\033[3;1HCAN1 DATA[1]: 0x%x", msg1.data[1]); |
st17099ng | 0:79abbe95bdcd | 52 | pc.printf("\033[4;1HCAN1 DATA[2]: 0x%x", msg1.data[2]); |
st17099ng | 0:79abbe95bdcd | 53 | pc.printf("\033[5;1HCAN1 DATA[3]: 0x%x", msg1.data[3]); |
st17099ng | 0:79abbe95bdcd | 54 | pc.printf("\033[6;1HCAN1 DATA[4]: 0x%x", msg1.data[4]); |
st17099ng | 0:79abbe95bdcd | 55 | pc.printf("\033[7;1HCAN1 DATA[5]: 0x%x", msg1.data[5]); |
st17099ng | 0:79abbe95bdcd | 56 | pc.printf("\033[8;1HCAN1 DATA[6]: 0x%x", msg1.data[6]); |
st17099ng | 0:79abbe95bdcd | 57 | pc.printf("\033[9;1HCAN1 DATA[7]: 0x%x", msg1.data[7]); |
st17099ng | 0:79abbe95bdcd | 58 | } |
st17099ng | 0:79abbe95bdcd | 59 | if(counter[0] >= 0xF0) |
st17099ng | 0:79abbe95bdcd | 60 | pc.printf("\033[2J"); |
st17099ng | 0:79abbe95bdcd | 61 | } |
st17099ng | 0:79abbe95bdcd | 62 | |
st17099ng | 0:79abbe95bdcd | 63 | /** CAN2受信関数 **/ |
st17099ng | 0:79abbe95bdcd | 64 | void can2Rcv(void) |
st17099ng | 0:79abbe95bdcd | 65 | { |
st17099ng | 0:79abbe95bdcd | 66 | can2.read(msg2); |
st17099ng | 0:79abbe95bdcd | 67 | pc.printf("\033[1;25HCAN2 RCVID: 0x%x\n", msg2.id); |
st17099ng | 0:79abbe95bdcd | 68 | if(msg2.id == 0x539) { |
st17099ng | 0:79abbe95bdcd | 69 | for(uint8_t i=0; i<8; i++) { |
st17099ng | 0:79abbe95bdcd | 70 | RcvData[i] = msg2.data[i]; |
st17099ng | 0:79abbe95bdcd | 71 | } |
st17099ng | 0:79abbe95bdcd | 72 | pc.printf("\033[2;25HCAN2 DATA[0]: 0x%x", RcvData[0]); |
st17099ng | 0:79abbe95bdcd | 73 | pc.printf("\033[3;25HCAN2 DATA[0]: 0x%x", RcvData[1]); |
st17099ng | 0:79abbe95bdcd | 74 | pc.printf("\033[4;25HCAN2 DATA[0]: 0x%x", RcvData[2]); |
st17099ng | 0:79abbe95bdcd | 75 | pc.printf("\033[5;25HCAN2 DATA[0]: 0x%x", RcvData[3]); |
st17099ng | 0:79abbe95bdcd | 76 | pc.printf("\033[6;25HCAN2 DATA[0]: 0x%x", RcvData[4]); |
st17099ng | 0:79abbe95bdcd | 77 | pc.printf("\033[7;25HCAN2 DATA[0]: 0x%x", RcvData[5]); |
st17099ng | 0:79abbe95bdcd | 78 | pc.printf("\033[8;25HCAN2 DATA[0]: 0x%x", RcvData[6]); |
st17099ng | 0:79abbe95bdcd | 79 | pc.printf("\033[9;25HCAN2 DATA[0]: 0x%x", RcvData[7]); |
st17099ng | 0:79abbe95bdcd | 80 | myled = !myled; |
st17099ng | 0:79abbe95bdcd | 81 | } |
st17099ng | 0:79abbe95bdcd | 82 | } |