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

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include <mbed.h>
00002 int PT = 0;
00003 int OLD_PT = 0;
00004 char counter[8] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
00005 char RcvData[8];
00006 
00007 void CAN1_Trans(void);
00008 void CAN2_Rcv(void);
00009 void CHECK_PT(void);
00010 void PT0(void);//ヒコウジョー
00011 void PT1(void);//DJ
00012 void PT2(void);//ワッカイッテモドッテ
00013 void PT3(void);//ブツカッチャウ
00014 void PT4(void);//ギャクヒコウジョー
00015 void PT5(void);//トケイ
00016 void PT6(void);//ハントケイ
00017 void PT7(void);
00018 void PT8(void);
00019 void PT9(void);
00020 
00021 
00022 DigitalOut GLS[] = {
00023     DigitalOut(PC_3),
00024     DigitalOut(PC_2),
00025     DigitalOut(PC_13),
00026     DigitalOut(PB_7),
00027     DigitalOut(PA_15),
00028     DigitalOut(PC_12),
00029     DigitalOut(PC_11),
00030     DigitalOut(PC_10)
00031 };
00032 
00033 DigitalOut YLS[] = {
00034     DigitalOut(PC_4),
00035     DigitalOut(PA_10),
00036     DigitalOut(PB_3),
00037     DigitalOut(PB_4),
00038     DigitalOut(PB_10),
00039     DigitalOut(PA_8),
00040     DigitalOut(PA_9),
00041     DigitalOut(PC_7)
00042 };
00043 
00044 DigitalOut RLS[] = {
00045     DigitalOut(PA_12),
00046     DigitalOut(PA_11),
00047     DigitalOut(PB_12),
00048     DigitalOut(PB_2),
00049     DigitalOut(PB_1),
00050     DigitalOut(PB_15),
00051     DigitalOut(PB_14),
00052     DigitalOut(PB_13)
00053 };
00054 
00055 DigitalIn SW1(PC_6);
00056 DigitalIn SW2(PC_5);
00057 
00058 Ticker ticker;
00059 Serial pc(USBTX,NC,115200);
00060 CAN can1(PB_8, PB_9,500000);
00061 CAN can2(PB_5, PB_6,500000);
00062 CANMessage msg1(0x114,counter,8,CANData,CANStandard);
00063 CANMessage msg2(0x114,CANStandard);
00064 
00065 int main(void)
00066 {
00067     SW1.mode(PullUp);
00068     SW2.mode(PullUp);
00069     ticker.attach(&CAN1_Trans, 0.1);
00070     can2.attach(&CAN2_Rcv,CAN::RxIrq);
00071     pc.printf("\033[2J");
00072     while(1) {
00073     }
00074 }
00075 
00076 void CAN1_Trans(void)
00077 {
00078     msg1.id = 0x114;
00079     if(SW1 == 1 && SW2 == 1) {
00080         for(uint8_t i=0; i<8; i++) {
00081             msg1.data[i] = 0x00;
00082         }
00083     } else if(SW1 == 0 && SW2 == 0) {
00084         for(uint8_t i=0; i<8; i++) {
00085             msg1.data[i] = 0x33;
00086         }
00087     } else if(SW1 == 0) {
00088         for(uint8_t i=0; i<8; i++) {
00089             msg1.data[i] = 0x11;
00090         }
00091     } else if(SW2 == 0) {
00092         for(uint8_t i=0; i<8; i++) {
00093             msg1.data[i] = 0x22;
00094         }
00095     }
00096     if(can1.write(msg1)) {
00097         pc.printf("\033[1;1HCAN1 TRANSID: 0x%x", msg1.id);
00098         pc.printf("\033[2;1HCAN1 DATA[0]: 0x%x", msg1.data[0]);
00099         pc.printf("\033[3;1HCAN1 DATA[1]: 0x%x", msg1.data[1]);
00100         pc.printf("\033[4;1HCAN1 DATA[2]: 0x%x", msg1.data[2]);
00101         pc.printf("\033[5;1HCAN1 DATA[3]: 0x%x", msg1.data[3]);
00102         pc.printf("\033[6;1HCAN1 DATA[4]: 0x%x", msg1.data[4]);
00103         pc.printf("\033[7;1HCAN1 DATA[5]: 0x%x", msg1.data[5]);
00104         pc.printf("\033[8;1HCAN1 DATA[6]: 0x%x", msg1.data[6]);
00105         pc.printf("\033[9;1HCAN1 DATA[7]: 0x%x", msg1.data[7]);
00106     }
00107 }
00108 
00109 void CAN2_Rcv(void)
00110 {
00111     can2.read(msg2);
00112     pc.printf("\033[1;25HCAN2 RCVID: 0x%x\n", msg2.id);
00113     if(msg2.id == 0x114) {
00114         for(uint8_t i=0; i<8; i++) {
00115             RcvData[i] = msg2.data[i];
00116         }
00117         if(RcvData[0] == 0x00) {
00118             PT = 0;
00119             CHECK_PT();
00120             PT0();
00121         } else if(RcvData[0] == 0x33) {
00122             PT = 3;
00123             CHECK_PT();
00124             PT4();
00125         } else if(RcvData[0] == 0x11) {
00126             PT = 1;
00127             CHECK_PT();
00128             PT1();
00129         } else if(RcvData[0] == 0x22) {
00130             PT = 2;
00131             CHECK_PT();
00132             PT2();
00133         }
00134         OLD_PT = PT;
00135         pc.printf("\033[2;25HCAN2 DATA[0]: 0x%x", RcvData[0]);
00136         pc.printf("\033[3;25HCAN2 DATA[0]: 0x%x", RcvData[1]);
00137         pc.printf("\033[4;25HCAN2 DATA[0]: 0x%x", RcvData[2]);
00138         pc.printf("\033[5;25HCAN2 DATA[0]: 0x%x", RcvData[3]);
00139         pc.printf("\033[6;25HCAN2 DATA[0]: 0x%x", RcvData[4]);
00140         pc.printf("\033[7;25HCAN2 DATA[0]: 0x%x", RcvData[5]);
00141         pc.printf("\033[8;25HCAN2 DATA[0]: 0x%x", RcvData[6]);
00142         pc.printf("\033[9;25HCAN2 DATA[0]: 0x%x", RcvData[7]);
00143     }
00144 }
00145 
00146 void CHECK_PT(void)
00147 {
00148     if(OLD_PT != PT) {
00149         for(int i = 0; i<8; i++) {
00150             GLS[i] = 0;
00151             YLS[i] = 0;
00152             RLS[i] = 0;
00153         }
00154         pc.printf("\033[2J");
00155     }
00156 }
00157 
00158 void PT0(void)//ヒコウジョー
00159 {
00160     for(int i = 0; i<8; i++) {
00161         GLS[i] = !GLS[i];
00162         YLS[i] = !YLS[i];
00163         RLS[i] = !RLS[i];
00164         wait(0.03);
00165     }
00166 }
00167 
00168 void PT1(void)//DJ
00169 {
00170     for(int i = 0; i < 8; i++) {
00171         GLS[i] = 1;
00172         YLS[i] = 1;
00173         RLS[i] = 1;
00174         wait(0.03);
00175     }
00176     for(int i = 7; i >= 0; i--) {
00177         GLS[i] = 0;
00178         YLS[i] = 0;
00179         RLS[i] = 0;
00180         wait(0.03);
00181     }
00182 }
00183 
00184 void PT2(void)//ワッカイッテモドッテ
00185 {
00186     for(int i = 7; i >= 0; i--) {
00187         GLS[i] = 1;
00188         wait(0.03);
00189     }
00190     for(int i = 7; i >= 0; i--) {
00191         YLS[i] = 1;
00192         wait(0.03);
00193     }
00194     for(int i = 7; i >= 0; i--) {
00195         RLS[i] = 1;
00196         wait(0.03);
00197     }
00198 
00199     for(int i = 0; i < 8; i++) {
00200         RLS[i] = 0;
00201         wait(0.03);
00202     }
00203 
00204     for(int i = 0; i < 8; i++) {
00205         YLS[i] = 0;
00206         wait(0.03);
00207     }
00208     for(int i = 0; i < 8; i++) {
00209         GLS[i] = 0;
00210         wait(0.03);
00211     }
00212 }
00213 
00214 void PT3(void)//ブツカッチャウ
00215 {
00216     for(int i = 7; i >= 0; i--) {
00217         GLS[i] = 1;
00218         RLS[7-i]  = 1;
00219         wait(0.03);
00220     }
00221     for(int i = 0; i < 4; i++) {
00222         YLS[i] = 1;
00223         YLS[7-i] = 1;
00224         wait(0.03);
00225     }
00226     for(int i = 3; i >= 0; i--) {
00227         YLS[i] = 0;
00228         YLS[7-i] = 0;
00229         wait(0.03);
00230     }
00231     for(int i = 7; i >= 0; i--) {
00232         GLS[7-i] = 0;
00233         RLS[i] = 0;
00234         wait(0.03);
00235     }
00236 }
00237 
00238 void PT4(void)//ギャクヒコウジョー
00239 {
00240     for(int i = 7; i >= 0; i--) {
00241         GLS[i] = !GLS[i];
00242         YLS[i] = !YLS[i];
00243         RLS[i] = !RLS[i];
00244         wait(0.03);
00245     }
00246 }
00247 
00248 void PT5(void)//トケイ
00249 {
00250     for(int i = 0; i<8; i++) {
00251         GLS[i] = 1;
00252         wait(0.03);
00253     }
00254     for(int i = 0; i<8; i++) {
00255         GLS[i] = 0;
00256     }
00257 }
00258 
00259 void PT6(void)//ハントケイ
00260 {
00261 
00262 }