2019 Off season development Test of CAN communication Nucleo Board STM32 F446RE

Dependencies:   mbed

Committer:
st17099ng
Date:
Thu Mar 12 15:05:59 2020 +0000
Revision:
0:bc4e55608ee9
Initial version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
st17099ng 0:bc4e55608ee9 1 #include <mbed.h>
st17099ng 0:bc4e55608ee9 2 int PT = 0;
st17099ng 0:bc4e55608ee9 3 int OLD_PT = 0;
st17099ng 0:bc4e55608ee9 4 char counter[8] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
st17099ng 0:bc4e55608ee9 5 char RcvData[8];
st17099ng 0:bc4e55608ee9 6
st17099ng 0:bc4e55608ee9 7 void CAN1_Trans(void);
st17099ng 0:bc4e55608ee9 8 void CAN2_Rcv(void);
st17099ng 0:bc4e55608ee9 9 void CHECK_PT(void);
st17099ng 0:bc4e55608ee9 10 void PT0(void);//ヒコウジョー
st17099ng 0:bc4e55608ee9 11 void PT1(void);//DJ
st17099ng 0:bc4e55608ee9 12 void PT2(void);//ワッカイッテモドッテ
st17099ng 0:bc4e55608ee9 13 void PT3(void);//ブツカッチャウ
st17099ng 0:bc4e55608ee9 14 void PT4(void);//ギャクヒコウジョー
st17099ng 0:bc4e55608ee9 15 void PT5(void);//トケイ
st17099ng 0:bc4e55608ee9 16 void PT6(void);//ハントケイ
st17099ng 0:bc4e55608ee9 17 void PT7(void);
st17099ng 0:bc4e55608ee9 18 void PT8(void);
st17099ng 0:bc4e55608ee9 19 void PT9(void);
st17099ng 0:bc4e55608ee9 20
st17099ng 0:bc4e55608ee9 21
st17099ng 0:bc4e55608ee9 22 DigitalOut GLS[] = {
st17099ng 0:bc4e55608ee9 23 DigitalOut(PC_3),
st17099ng 0:bc4e55608ee9 24 DigitalOut(PC_2),
st17099ng 0:bc4e55608ee9 25 DigitalOut(PC_13),
st17099ng 0:bc4e55608ee9 26 DigitalOut(PB_7),
st17099ng 0:bc4e55608ee9 27 DigitalOut(PA_15),
st17099ng 0:bc4e55608ee9 28 DigitalOut(PC_12),
st17099ng 0:bc4e55608ee9 29 DigitalOut(PC_11),
st17099ng 0:bc4e55608ee9 30 DigitalOut(PC_10)
st17099ng 0:bc4e55608ee9 31 };
st17099ng 0:bc4e55608ee9 32
st17099ng 0:bc4e55608ee9 33 DigitalOut YLS[] = {
st17099ng 0:bc4e55608ee9 34 DigitalOut(PC_4),
st17099ng 0:bc4e55608ee9 35 DigitalOut(PA_10),
st17099ng 0:bc4e55608ee9 36 DigitalOut(PB_3),
st17099ng 0:bc4e55608ee9 37 DigitalOut(PB_4),
st17099ng 0:bc4e55608ee9 38 DigitalOut(PB_10),
st17099ng 0:bc4e55608ee9 39 DigitalOut(PA_8),
st17099ng 0:bc4e55608ee9 40 DigitalOut(PA_9),
st17099ng 0:bc4e55608ee9 41 DigitalOut(PC_7)
st17099ng 0:bc4e55608ee9 42 };
st17099ng 0:bc4e55608ee9 43
st17099ng 0:bc4e55608ee9 44 DigitalOut RLS[] = {
st17099ng 0:bc4e55608ee9 45 DigitalOut(PA_12),
st17099ng 0:bc4e55608ee9 46 DigitalOut(PA_11),
st17099ng 0:bc4e55608ee9 47 DigitalOut(PB_12),
st17099ng 0:bc4e55608ee9 48 DigitalOut(PB_2),
st17099ng 0:bc4e55608ee9 49 DigitalOut(PB_1),
st17099ng 0:bc4e55608ee9 50 DigitalOut(PB_15),
st17099ng 0:bc4e55608ee9 51 DigitalOut(PB_14),
st17099ng 0:bc4e55608ee9 52 DigitalOut(PB_13)
st17099ng 0:bc4e55608ee9 53 };
st17099ng 0:bc4e55608ee9 54
st17099ng 0:bc4e55608ee9 55 DigitalIn SW1(PC_6);
st17099ng 0:bc4e55608ee9 56 DigitalIn SW2(PC_5);
st17099ng 0:bc4e55608ee9 57
st17099ng 0:bc4e55608ee9 58 Ticker ticker;
st17099ng 0:bc4e55608ee9 59 Serial pc(USBTX,NC,115200);
st17099ng 0:bc4e55608ee9 60 CAN can1(PB_8, PB_9,500000);
st17099ng 0:bc4e55608ee9 61 CAN can2(PB_5, PB_6,500000);
st17099ng 0:bc4e55608ee9 62 CANMessage msg1(0x114,counter,8,CANData,CANStandard);
st17099ng 0:bc4e55608ee9 63 CANMessage msg2(0x114,CANStandard);
st17099ng 0:bc4e55608ee9 64
st17099ng 0:bc4e55608ee9 65 int main(void)
st17099ng 0:bc4e55608ee9 66 {
st17099ng 0:bc4e55608ee9 67 SW1.mode(PullUp);
st17099ng 0:bc4e55608ee9 68 SW2.mode(PullUp);
st17099ng 0:bc4e55608ee9 69 ticker.attach(&CAN1_Trans, 0.1);
st17099ng 0:bc4e55608ee9 70 can2.attach(&CAN2_Rcv,CAN::RxIrq);
st17099ng 0:bc4e55608ee9 71 pc.printf("\033[2J");
st17099ng 0:bc4e55608ee9 72 while(1) {
st17099ng 0:bc4e55608ee9 73 }
st17099ng 0:bc4e55608ee9 74 }
st17099ng 0:bc4e55608ee9 75
st17099ng 0:bc4e55608ee9 76 void CAN1_Trans(void)
st17099ng 0:bc4e55608ee9 77 {
st17099ng 0:bc4e55608ee9 78 msg1.id = 0x114;
st17099ng 0:bc4e55608ee9 79 if(SW1 == 1 && SW2 == 1) {
st17099ng 0:bc4e55608ee9 80 for(uint8_t i=0; i<8; i++) {
st17099ng 0:bc4e55608ee9 81 msg1.data[i] = 0x00;
st17099ng 0:bc4e55608ee9 82 }
st17099ng 0:bc4e55608ee9 83 } else if(SW1 == 0 && SW2 == 0) {
st17099ng 0:bc4e55608ee9 84 for(uint8_t i=0; i<8; i++) {
st17099ng 0:bc4e55608ee9 85 msg1.data[i] = 0x33;
st17099ng 0:bc4e55608ee9 86 }
st17099ng 0:bc4e55608ee9 87 } else if(SW1 == 0) {
st17099ng 0:bc4e55608ee9 88 for(uint8_t i=0; i<8; i++) {
st17099ng 0:bc4e55608ee9 89 msg1.data[i] = 0x11;
st17099ng 0:bc4e55608ee9 90 }
st17099ng 0:bc4e55608ee9 91 } else if(SW2 == 0) {
st17099ng 0:bc4e55608ee9 92 for(uint8_t i=0; i<8; i++) {
st17099ng 0:bc4e55608ee9 93 msg1.data[i] = 0x22;
st17099ng 0:bc4e55608ee9 94 }
st17099ng 0:bc4e55608ee9 95 }
st17099ng 0:bc4e55608ee9 96 if(can1.write(msg1)) {
st17099ng 0:bc4e55608ee9 97 pc.printf("\033[1;1HCAN1 TRANSID: 0x%x", msg1.id);
st17099ng 0:bc4e55608ee9 98 pc.printf("\033[2;1HCAN1 DATA[0]: 0x%x", msg1.data[0]);
st17099ng 0:bc4e55608ee9 99 pc.printf("\033[3;1HCAN1 DATA[1]: 0x%x", msg1.data[1]);
st17099ng 0:bc4e55608ee9 100 pc.printf("\033[4;1HCAN1 DATA[2]: 0x%x", msg1.data[2]);
st17099ng 0:bc4e55608ee9 101 pc.printf("\033[5;1HCAN1 DATA[3]: 0x%x", msg1.data[3]);
st17099ng 0:bc4e55608ee9 102 pc.printf("\033[6;1HCAN1 DATA[4]: 0x%x", msg1.data[4]);
st17099ng 0:bc4e55608ee9 103 pc.printf("\033[7;1HCAN1 DATA[5]: 0x%x", msg1.data[5]);
st17099ng 0:bc4e55608ee9 104 pc.printf("\033[8;1HCAN1 DATA[6]: 0x%x", msg1.data[6]);
st17099ng 0:bc4e55608ee9 105 pc.printf("\033[9;1HCAN1 DATA[7]: 0x%x", msg1.data[7]);
st17099ng 0:bc4e55608ee9 106 }
st17099ng 0:bc4e55608ee9 107 }
st17099ng 0:bc4e55608ee9 108
st17099ng 0:bc4e55608ee9 109 void CAN2_Rcv(void)
st17099ng 0:bc4e55608ee9 110 {
st17099ng 0:bc4e55608ee9 111 can2.read(msg2);
st17099ng 0:bc4e55608ee9 112 pc.printf("\033[1;25HCAN2 RCVID: 0x%x\n", msg2.id);
st17099ng 0:bc4e55608ee9 113 if(msg2.id == 0x114) {
st17099ng 0:bc4e55608ee9 114 for(uint8_t i=0; i<8; i++) {
st17099ng 0:bc4e55608ee9 115 RcvData[i] = msg2.data[i];
st17099ng 0:bc4e55608ee9 116 }
st17099ng 0:bc4e55608ee9 117 if(RcvData[0] == 0x00) {
st17099ng 0:bc4e55608ee9 118 PT = 0;
st17099ng 0:bc4e55608ee9 119 CHECK_PT();
st17099ng 0:bc4e55608ee9 120 PT0();
st17099ng 0:bc4e55608ee9 121 } else if(RcvData[0] == 0x33) {
st17099ng 0:bc4e55608ee9 122 PT = 3;
st17099ng 0:bc4e55608ee9 123 CHECK_PT();
st17099ng 0:bc4e55608ee9 124 PT4();
st17099ng 0:bc4e55608ee9 125 } else if(RcvData[0] == 0x11) {
st17099ng 0:bc4e55608ee9 126 PT = 1;
st17099ng 0:bc4e55608ee9 127 CHECK_PT();
st17099ng 0:bc4e55608ee9 128 PT1();
st17099ng 0:bc4e55608ee9 129 } else if(RcvData[0] == 0x22) {
st17099ng 0:bc4e55608ee9 130 PT = 2;
st17099ng 0:bc4e55608ee9 131 CHECK_PT();
st17099ng 0:bc4e55608ee9 132 PT2();
st17099ng 0:bc4e55608ee9 133 }
st17099ng 0:bc4e55608ee9 134 OLD_PT = PT;
st17099ng 0:bc4e55608ee9 135 pc.printf("\033[2;25HCAN2 DATA[0]: 0x%x", RcvData[0]);
st17099ng 0:bc4e55608ee9 136 pc.printf("\033[3;25HCAN2 DATA[0]: 0x%x", RcvData[1]);
st17099ng 0:bc4e55608ee9 137 pc.printf("\033[4;25HCAN2 DATA[0]: 0x%x", RcvData[2]);
st17099ng 0:bc4e55608ee9 138 pc.printf("\033[5;25HCAN2 DATA[0]: 0x%x", RcvData[3]);
st17099ng 0:bc4e55608ee9 139 pc.printf("\033[6;25HCAN2 DATA[0]: 0x%x", RcvData[4]);
st17099ng 0:bc4e55608ee9 140 pc.printf("\033[7;25HCAN2 DATA[0]: 0x%x", RcvData[5]);
st17099ng 0:bc4e55608ee9 141 pc.printf("\033[8;25HCAN2 DATA[0]: 0x%x", RcvData[6]);
st17099ng 0:bc4e55608ee9 142 pc.printf("\033[9;25HCAN2 DATA[0]: 0x%x", RcvData[7]);
st17099ng 0:bc4e55608ee9 143 }
st17099ng 0:bc4e55608ee9 144 }
st17099ng 0:bc4e55608ee9 145
st17099ng 0:bc4e55608ee9 146 void CHECK_PT(void)
st17099ng 0:bc4e55608ee9 147 {
st17099ng 0:bc4e55608ee9 148 if(OLD_PT != PT) {
st17099ng 0:bc4e55608ee9 149 for(int i = 0; i<8; i++) {
st17099ng 0:bc4e55608ee9 150 GLS[i] = 0;
st17099ng 0:bc4e55608ee9 151 YLS[i] = 0;
st17099ng 0:bc4e55608ee9 152 RLS[i] = 0;
st17099ng 0:bc4e55608ee9 153 }
st17099ng 0:bc4e55608ee9 154 pc.printf("\033[2J");
st17099ng 0:bc4e55608ee9 155 }
st17099ng 0:bc4e55608ee9 156 }
st17099ng 0:bc4e55608ee9 157
st17099ng 0:bc4e55608ee9 158 void PT0(void)//ヒコウジョー
st17099ng 0:bc4e55608ee9 159 {
st17099ng 0:bc4e55608ee9 160 for(int i = 0; i<8; i++) {
st17099ng 0:bc4e55608ee9 161 GLS[i] = !GLS[i];
st17099ng 0:bc4e55608ee9 162 YLS[i] = !YLS[i];
st17099ng 0:bc4e55608ee9 163 RLS[i] = !RLS[i];
st17099ng 0:bc4e55608ee9 164 wait(0.03);
st17099ng 0:bc4e55608ee9 165 }
st17099ng 0:bc4e55608ee9 166 }
st17099ng 0:bc4e55608ee9 167
st17099ng 0:bc4e55608ee9 168 void PT1(void)//DJ
st17099ng 0:bc4e55608ee9 169 {
st17099ng 0:bc4e55608ee9 170 for(int i = 0; i < 8; i++) {
st17099ng 0:bc4e55608ee9 171 GLS[i] = 1;
st17099ng 0:bc4e55608ee9 172 YLS[i] = 1;
st17099ng 0:bc4e55608ee9 173 RLS[i] = 1;
st17099ng 0:bc4e55608ee9 174 wait(0.03);
st17099ng 0:bc4e55608ee9 175 }
st17099ng 0:bc4e55608ee9 176 for(int i = 7; i >= 0; i--) {
st17099ng 0:bc4e55608ee9 177 GLS[i] = 0;
st17099ng 0:bc4e55608ee9 178 YLS[i] = 0;
st17099ng 0:bc4e55608ee9 179 RLS[i] = 0;
st17099ng 0:bc4e55608ee9 180 wait(0.03);
st17099ng 0:bc4e55608ee9 181 }
st17099ng 0:bc4e55608ee9 182 }
st17099ng 0:bc4e55608ee9 183
st17099ng 0:bc4e55608ee9 184 void PT2(void)//ワッカイッテモドッテ
st17099ng 0:bc4e55608ee9 185 {
st17099ng 0:bc4e55608ee9 186 for(int i = 7; i >= 0; i--) {
st17099ng 0:bc4e55608ee9 187 GLS[i] = 1;
st17099ng 0:bc4e55608ee9 188 wait(0.03);
st17099ng 0:bc4e55608ee9 189 }
st17099ng 0:bc4e55608ee9 190 for(int i = 7; i >= 0; i--) {
st17099ng 0:bc4e55608ee9 191 YLS[i] = 1;
st17099ng 0:bc4e55608ee9 192 wait(0.03);
st17099ng 0:bc4e55608ee9 193 }
st17099ng 0:bc4e55608ee9 194 for(int i = 7; i >= 0; i--) {
st17099ng 0:bc4e55608ee9 195 RLS[i] = 1;
st17099ng 0:bc4e55608ee9 196 wait(0.03);
st17099ng 0:bc4e55608ee9 197 }
st17099ng 0:bc4e55608ee9 198
st17099ng 0:bc4e55608ee9 199 for(int i = 0; i < 8; i++) {
st17099ng 0:bc4e55608ee9 200 RLS[i] = 0;
st17099ng 0:bc4e55608ee9 201 wait(0.03);
st17099ng 0:bc4e55608ee9 202 }
st17099ng 0:bc4e55608ee9 203
st17099ng 0:bc4e55608ee9 204 for(int i = 0; i < 8; i++) {
st17099ng 0:bc4e55608ee9 205 YLS[i] = 0;
st17099ng 0:bc4e55608ee9 206 wait(0.03);
st17099ng 0:bc4e55608ee9 207 }
st17099ng 0:bc4e55608ee9 208 for(int i = 0; i < 8; i++) {
st17099ng 0:bc4e55608ee9 209 GLS[i] = 0;
st17099ng 0:bc4e55608ee9 210 wait(0.03);
st17099ng 0:bc4e55608ee9 211 }
st17099ng 0:bc4e55608ee9 212 }
st17099ng 0:bc4e55608ee9 213
st17099ng 0:bc4e55608ee9 214 void PT3(void)//ブツカッチャウ
st17099ng 0:bc4e55608ee9 215 {
st17099ng 0:bc4e55608ee9 216 for(int i = 7; i >= 0; i--) {
st17099ng 0:bc4e55608ee9 217 GLS[i] = 1;
st17099ng 0:bc4e55608ee9 218 RLS[7-i] = 1;
st17099ng 0:bc4e55608ee9 219 wait(0.03);
st17099ng 0:bc4e55608ee9 220 }
st17099ng 0:bc4e55608ee9 221 for(int i = 0; i < 4; i++) {
st17099ng 0:bc4e55608ee9 222 YLS[i] = 1;
st17099ng 0:bc4e55608ee9 223 YLS[7-i] = 1;
st17099ng 0:bc4e55608ee9 224 wait(0.03);
st17099ng 0:bc4e55608ee9 225 }
st17099ng 0:bc4e55608ee9 226 for(int i = 3; i >= 0; i--) {
st17099ng 0:bc4e55608ee9 227 YLS[i] = 0;
st17099ng 0:bc4e55608ee9 228 YLS[7-i] = 0;
st17099ng 0:bc4e55608ee9 229 wait(0.03);
st17099ng 0:bc4e55608ee9 230 }
st17099ng 0:bc4e55608ee9 231 for(int i = 7; i >= 0; i--) {
st17099ng 0:bc4e55608ee9 232 GLS[7-i] = 0;
st17099ng 0:bc4e55608ee9 233 RLS[i] = 0;
st17099ng 0:bc4e55608ee9 234 wait(0.03);
st17099ng 0:bc4e55608ee9 235 }
st17099ng 0:bc4e55608ee9 236 }
st17099ng 0:bc4e55608ee9 237
st17099ng 0:bc4e55608ee9 238 void PT4(void)//ギャクヒコウジョー
st17099ng 0:bc4e55608ee9 239 {
st17099ng 0:bc4e55608ee9 240 for(int i = 7; i >= 0; i--) {
st17099ng 0:bc4e55608ee9 241 GLS[i] = !GLS[i];
st17099ng 0:bc4e55608ee9 242 YLS[i] = !YLS[i];
st17099ng 0:bc4e55608ee9 243 RLS[i] = !RLS[i];
st17099ng 0:bc4e55608ee9 244 wait(0.03);
st17099ng 0:bc4e55608ee9 245 }
st17099ng 0:bc4e55608ee9 246 }
st17099ng 0:bc4e55608ee9 247
st17099ng 0:bc4e55608ee9 248 void PT5(void)//トケイ
st17099ng 0:bc4e55608ee9 249 {
st17099ng 0:bc4e55608ee9 250 for(int i = 0; i<8; i++) {
st17099ng 0:bc4e55608ee9 251 GLS[i] = 1;
st17099ng 0:bc4e55608ee9 252 wait(0.03);
st17099ng 0:bc4e55608ee9 253 }
st17099ng 0:bc4e55608ee9 254 for(int i = 0; i<8; i++) {
st17099ng 0:bc4e55608ee9 255 GLS[i] = 0;
st17099ng 0:bc4e55608ee9 256 }
st17099ng 0:bc4e55608ee9 257 }
st17099ng 0:bc4e55608ee9 258
st17099ng 0:bc4e55608ee9 259 void PT6(void)//ハントケイ
st17099ng 0:bc4e55608ee9 260 {
st17099ng 0:bc4e55608ee9 261
st17099ng 0:bc4e55608ee9 262 }