Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 #include "mbed.h" 00002 #include "Mycan.h" 00003 00004 Mycan can(p30, p29, 100000); //// CAN (PinName rd, PinName td) 00005 00006 DigitalOut LED[4] = 00007 { 00008 DigitalOut(LED1), 00009 DigitalOut(LED2), 00010 DigitalOut(LED3), 00011 DigitalOut(LED4), 00012 }; 00013 00014 DigitalOut led_R(p14); 00015 DigitalOut led_B(p18); 00016 00017 DigitalIn sw[11] = 00018 { 00019 DigitalIn(p5), 00020 DigitalIn(p6), 00021 DigitalIn(p8), 00022 DigitalIn(p10), 00023 DigitalIn(p12), 00024 DigitalIn(p7), 00025 DigitalIn(p9), 00026 DigitalIn(p11), 00027 DigitalIn(p13), 00028 DigitalIn(p16), 00029 DigitalIn(p15) 00030 }; 00031 00032 Ticker ticker; 00033 Serial pc(USBTX, USBRX); 00034 00035 void canFnc(); 00036 void readLineData(); 00037 bool T_sw[4], B_sw[4], S_sw, Z_sw, M_sw; 00038 short int send_val[2], line_data[2]; 00039 00040 int main() 00041 { 00042 for(int i=0; i<11; i++) 00043 sw[i].mode(PullUp); 00044 ticker.attach(&canFnc, 0.0065); 00045 while(1) 00046 { 00047 S_sw = sw[0]; 00048 pc.printf("%d\t", S_sw); 00049 for(int i=0; i<4; i++) { 00050 B_sw[i] = sw[i+1]; 00051 pc.printf("%d\t", B_sw[i]); 00052 } 00053 for(int i=0; i<4; i++) { 00054 T_sw[i] = sw[i+5]; 00055 pc.printf("%d\t", T_sw[i]); 00056 } 00057 Z_sw = sw[9]; 00058 if(Z_sw) { 00059 led_R = 1; 00060 led_B = 0; 00061 } else { 00062 led_R = 0; 00063 led_B = 1; 00064 } 00065 M_sw = sw[10]; 00066 00067 pc.printf("%d\t", Z_sw); 00068 pc.printf("%d\t", M_sw); 00069 pc.printf("%d\t", send_val[0]); 00070 pc.printf("%d\t", send_val[1]); 00071 pc.printf("%d\t", line_data[0]); 00072 pc.printf("%d\t", line_data[1]); 00073 pc.printf("\n"); 00074 } 00075 } 00076 00077 void canFnc() 00078 { 00079 send_val[0] = send_val[1] = 0; 00080 if(S_sw) 00081 send_val[0] += 64; 00082 if(B_sw[0]) 00083 send_val[0] += 32; 00084 if(B_sw[1]) 00085 send_val[0] += 16; 00086 if(B_sw[2]) 00087 send_val[0] += 8; 00088 if(B_sw[3]) 00089 send_val[0] += 4; 00090 if(!T_sw[0]) 00091 send_val[0] += 2; 00092 if(!T_sw[1]) 00093 send_val[0] += 1; 00094 if(!T_sw[2]) 00095 send_val[1] += 8; 00096 if(!T_sw[3]) 00097 send_val[1] += 4; 00098 if(Z_sw) 00099 send_val[1] += 2; 00100 if(M_sw) 00101 send_val[1] += 1; 00102 00103 readLineData(); 00104 00105 can.setI(2, 0, (short int)(send_val[0])); 00106 can.setI(2, 1, (short int)(send_val[1])); 00107 can.setI(2, 2, (short int)(line_data[0])); 00108 can.setI(2, 3, (short int)(line_data[1])); 00109 LED[0] = can.send(); 00110 } 00111 00112 void readLineData() 00113 { 00114 int read_line_data[4]; 00115 00116 for(int i = 0; i < 4; i++) { 00117 can.readI(); 00118 read_line_data[i] = can.get(i+6, 0); 00119 } 00120 00121 int residue_data, true_num[4], true_all[4]; 00122 00123 for(int i = 0; i < 4; i++) 00124 { 00125 residue_data = read_line_data[i]; 00126 true_all[i] = true_num[i] = 0; 00127 00128 if(residue_data < 0) { 00129 if(residue_data == -200) 00130 residue_data = 0; 00131 else residue_data *= -1; 00132 00133 true_num[i] += 1; 00134 if(i == 1 || i == 3) 00135 true_all[i] += 7 + 8; 00136 else true_all[i] += 7; 00137 } 00138 00139 if(residue_data >= 64) { 00140 residue_data -= 64; 00141 true_num[i] += 1; 00142 if(i == 1 || i == 3) 00143 true_all[i] += 6 + 8; 00144 else true_all[i] += 6; 00145 } 00146 00147 if(residue_data >= 32) { 00148 residue_data -= 32; 00149 true_num[i] += 1; 00150 if(i == 1 || i == 3) 00151 true_all[i] += 5 + 8; 00152 else true_all[i] += 5; 00153 } 00154 00155 if(residue_data >= 16) { 00156 residue_data -= 16; 00157 true_num[i] += 1; 00158 if(i == 1 || i == 3) 00159 true_all[i] += 4 + 8; 00160 else true_all[i] += 4; 00161 } 00162 00163 if(residue_data >= 8) { 00164 residue_data -= 8; 00165 true_num[i] += 1; 00166 if(i == 1 || i == 3) 00167 true_all[i] += 3 + 8; 00168 else true_all[i] += 3; 00169 } 00170 00171 if(residue_data >= 4) { 00172 residue_data -= 4; 00173 true_num[i] += 1; 00174 if(i == 1 || i == 3) 00175 true_all[i] += 2 + 8; 00176 else true_all[i] += 2; 00177 } 00178 00179 if(residue_data >= 2) { 00180 residue_data -= 2; 00181 true_num[i] += 1; 00182 if(i == 1 || i == 3) 00183 true_all[i] += 1 + 8; 00184 else true_all[i] += 1; 00185 } 00186 00187 if(residue_data >= 1) { 00188 residue_data -= 1; 00189 true_num[i] += 1; 00190 if(i == 1 || i == 3) 00191 true_all[i] += 8; 00192 } 00193 } 00194 00195 if(true_all[0] + true_all[1] != 0 || true_num[0] + true_num[1] != 0) 00196 line_data[0] = (true_all[0] + true_all[1]) / (true_num[0] + true_num[1]); 00197 else line_data[0] = 0; 00198 00199 if(true_all[2] + true_all[3] != 0 || true_num[2] + true_num[3] != 0) 00200 line_data[1] = (true_all[2] + true_all[3]) / (true_num[2] + true_num[3]); 00201 else line_data[1] = 0; 00202 }
Generated on Thu Jul 14 2022 03:56:56 by
