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.
Dependencies: PCAL955x mbed mbed-rtos AT45
main.cpp
00001 00002 /*** PLC F446RE ***/ 00003 // 512 KB Flash / 128 KB SRAM System / 4 KB SRAM Backup 00004 00005 #include "mbed.h" 00006 #include "rtos.h" 00007 #include "AT45.h" 00008 #include "PCAL9555.h" 00009 00010 #include "can.h" 00011 #include "config_io.h" 00012 #include "ext_vars.h" 00013 #include "set_vars.h" 00014 00015 #include "task_ob.h" 00016 00017 #define CAN_BAUD 50000 // скорость 00018 #define UART_BAUD 9600 // скорость 00019 #define MAX_SERL 30 // макс. длинна строки из usb/uart 00020 00021 00022 00023 00024 /******* создание локальных переменных *************************/ 00025 00026 int busin = 0; 00027 int t_sys = 0 ; 00028 00029 char pc_buffer [MAX_SERL+2]; 00030 int pc_i=0; 00031 int pc_im=0; 00032 00033 float st_start=0.0; 00034 float st_end=0.0; 00035 float st_old=0.0; 00036 float st_cycl=0.0; 00037 float st_task=0.0; 00038 00039 int new_outs=0; 00040 00041 /********** прототипы локальных функций **********************/ 00042 void read_inputs(); 00043 void write_outs(); 00044 void save_stat(); 00045 void logic_tasks(); 00046 void can_rw(); 00047 void timers(); 00048 void pc_rd(); 00049 int read_val(); 00050 00051 /************** Threads **************************************/ 00052 Thread read_uart; 00053 Thread work_can; 00054 RtosTimer t_10msec(&logic_tasks); 00055 00056 00057 /*************** старт программы PLC **************************/ 00058 int main() 00059 { 00060 led=1; 00061 st.start(); 00062 00063 // ----- настраиваем CAN ------ 00064 can.frequency(CAN_BAUD); 00065 00066 //----- настраиваем порт USB ------ 00067 pc.baud(UART_BAUD); 00068 pc.printf( "\r\n> START PLC \r\n"); 00069 pc.printf(">CPU SystemCoreClock is %d Hz\r\n", SystemCoreClock); 00070 00071 00072 //------ проверка FLASH ---------- 00073 pc.printf("> flash size: %d bytes \n", spif.device_size()); 00074 00075 00076 //------- запускаем потоки Thread ------ 00077 work_can.start(can_rw); // обмен информ. по CAN 00078 read_uart.start(pc_rd); // чтение UART 00079 00080 00081 //--- запускаем логич.задач по таймеру 10msec ------ 00082 t_10msec.start(10); 00083 00084 00085 /*** обработка команд по USB ***/ 00086 while (true) { 00087 00088 if(t_sys<500){led=1;}else{led=0;} 00089 led_r=led; 00090 00091 // проверяем usb 00092 if (pc_i>1 && pc_i==pc_im){ 00093 pc_i = 0; 00094 //pc.printf("usb:%s",pc_buffer); 00095 //led = !led; 00096 00097 00098 // --- устанавливаем значение переменных ---- 00099 if(strstr(pc_buffer,"=")){ 00100 00101 if(strstr (pc_buffer,"vr000")) { vr000=read_val(); pc.printf( "ok\n"); } 00102 if(strstr (pc_buffer,"vr001")) { vr001=read_val(); pc.printf( "ok\n"); } 00103 if(strstr (pc_buffer,"vr002")) { vr002=read_val(); pc.printf( "ok\n"); } 00104 00105 if(strstr (pc_buffer,"pr001")) { pr001=read_val(); pc.printf( "ok\n"); } 00106 if(strstr (pc_buffer,"pr002")) { pr002=read_val(); pc.printf( "ok\n"); } 00107 00108 } 00109 00110 // --- проверка значения переменных ----- 00111 if(strstr(pc_buffer,"?")){ 00112 00113 if(strstr (pc_buffer,"vr000")) { pc.printf( "vr000:%d\n", vr000); } 00114 if(strstr (pc_buffer,"vr001")) { pc.printf( "vr001:%d\n", vr001); } 00115 if(strstr (pc_buffer,"vr002")) { pc.printf( "vr002:%d\n", vr002); } 00116 00117 if(strstr (pc_buffer,"pr001")) { pc.printf( "pr001:%d\n", pr001); } 00118 if(strstr (pc_buffer,"pr002")) { pc.printf( "pr002:%d\n", pr002); } 00119 00120 00121 // для контроля состояния 00122 00123 if(strstr (pc_buffer,"dc_in")) { pc.printf( "\n--- PLC --- \n> inputs:%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d\n", x01,x02,x03,x04,x05,x06,x07,x08,x09,x10,x11,x12,x13,x14,x15,x16); } 00124 if(strstr (pc_buffer,"dc_out")) { pc.printf( "\n--- PLC --- \n> outputs:%d%d%d%d%d%d%d%d%\n", y01,y02,y03,y04,y05,y06,y07,y08); } 00125 if(strstr (pc_buffer,"an_in")) { pc.printf( "\n--- PLC --- \n> an_in1=%f an_in2=%f an_in3=%f an_in4=%f \n", Vin_1, Vin_2, Vin_3, Vin_4 ); } 00126 if(strstr (pc_buffer,"an_out")) { pc.printf( "\n--- PLC --- \n> an_out1=%f an_out2=%f \n", Vout_1, Vout_2 ); } 00127 if(strstr (pc_buffer,"stime")) { pc.printf( "\n--- PLC --- \n> work cycle %f / using %f sec \n", st_cycl, st_task ); } 00128 00129 if(strstr (pc_buffer,"sm-1")) { pc.printf( "\n--- modul SM01 adr=1 --- \n> car position-%d moving-%d alarm-%d \n> inputs %d-%d-%d-%d outputs %d-%d \n", sm1_pos, sm1_mov, sm1_alr, a101, a102, a103, a104, b101, b102 ); } 00130 if(strstr (pc_buffer,"sm-2")) { pc.printf( "\n--- modul SM01 adr=2 --- \n> car position-%d moving-%d alarm-%d \n> inputs %d-%d-%d-%d outputs %d-%d \n", sm2_pos, sm2_mov, sm2_alr, a201, a202, a203, a204, b201, b202 ); } 00131 if(strstr (pc_buffer,"sm-3")) { pc.printf( "\n--- modul SM01 adr=3 --- \n> car position-%d moving-%d alarm-%d \n> inputs %d-%d-%d-%d outputs %d-%d \n", sm3_pos, sm3_mov, sm3_alr, a301, a302, a303, a304, b301, b302 ); } 00132 if(strstr (pc_buffer,"sm-4")) { pc.printf( "\n--- modul SM01 adr=4 --- \n> car position-%d moving-%d alarm-%d \n> inputs %d-%d-%d-%d outputs %d-%d \n", sm4_pos, sm4_mov, sm4_alr, a401, a402, a403, a404, b401, b402 ); } 00133 00134 if(strstr (pc_buffer,"dm-1")) { pc.printf( "\n--- modul DM0806 adr=1 --- \n> inputs:%d%d%d%d%d%d%d%d% \n> outputs:%d%d%d%d%d%d% \n", x101,x102,x103,x104,x105,x106,x107,x108, y101,y102,y103,y104,y105,y106 ); } 00135 if(strstr (pc_buffer,"dm-2")) { pc.printf( "\n--- modul DM0806 adr=2 --- \n> inputs:%d%d%d%d%d%d%d%d% \n> outputs:%d%d%d%d%d%d% \n", x201,x202,x203,x204,x205,x206,x207,x208, y201,y202,y203,y204,y205,y206 ); } 00136 if(strstr (pc_buffer,"dm-3")) { pc.printf( "\n--- modul DM0806 adr=3 --- \n> inputs:%d%d%d%d%d%d%d%d% \n> outputs:%d%d%d%d%d%d% \n", x301,x302,x303,x304,x305,x306,x307,x308, y301,y302,y303,y304,y305,y306 ); } 00137 if(strstr (pc_buffer,"dm-4")) { pc.printf( "\n--- modul DM0806 adr=4 --- \n> inputs:%d%d%d%d%d%d%d%d% \n> outputs:%d%d%d%d%d%d% \n", x401,x402,x403,x404,x405,x406,x407,x408, y401,y402,y403,y404,y405,y406 ); } 00138 00139 } 00140 00141 if(strstr (pc_buffer,"reset")) { NVIC_SystemReset() ; } 00142 00143 00144 00145 } 00146 00147 pc_im=pc_i; 00148 Thread::wait(100); 00149 } 00150 00151 } 00152 00153 00154 /**************** дополнительные функции **********************/ 00155 00156 // логические задачи. вызываются для выполнения 00157 // каждые 10 мсек 00158 void logic_tasks(){ 00159 // чтение сист.таймера 00160 st_start=st.read(); 00161 00162 00163 // подготовка входн.информации 00164 timers(); 00165 read_inputs(); 00166 00167 // логичкские задачи 00168 task_ob(); 00169 00170 // формирование выходн.информации 00171 write_outs(); 00172 save_stat(); 00173 00174 // определяем реальное время цикла/использ.время 00175 st_end=st.read(); 00176 st_task=st_end-st_start; 00177 st_cycl=st_start-st_old; 00178 st_old=st_start; 00179 00180 } 00181 00182 00183 //---------------------------------------- 00184 // установка выходов контроллера 00185 // в соответствии значения преременным 00186 void write_outs(){ 00187 // устанавливаем локальные выходы ПЛК 00188 y_1=y01; y_5=y05; 00189 y_2=y02; y_6=y06; 00190 y_3=y03; y_7=y07; 00191 y_4=y04; y_8=y08; 00192 00193 anout1=Vout_1; 00194 anout2=Vout_2; 00195 00196 // упаковываем выходы для DM0806 ADR=1 00197 dm1_outs = 0; 00198 if(y106==1){dm1_outs = dm1_outs |1;} // y106 00199 dm1_outs = dm1_outs << 1; 00200 if(y105==1){dm1_outs = dm1_outs |1;} // y105 00201 dm1_outs = dm1_outs << 1; 00202 if(y104==1){dm1_outs = dm1_outs |1;} // y104 00203 dm1_outs = dm1_outs << 1; 00204 if(y103==1){dm1_outs = dm1_outs |1;} // y103 00205 dm1_outs = dm1_outs << 1; 00206 if(y102==1){dm1_outs = dm1_outs |1;} // y102 00207 dm1_outs = dm1_outs << 1; 00208 if(y101==1){dm1_outs = dm1_outs |1;} // y101 00209 00210 // упаковываем выходы для DM0806 ADR=2 00211 dm2_outs = 0; 00212 if(y206==1){dm2_outs = dm2_outs |1;} // y206 00213 dm2_outs = dm2_outs << 1; 00214 if(y205==1){dm2_outs = dm2_outs |1;} // y205 00215 dm2_outs = dm2_outs << 1; 00216 if(y204==1){dm2_outs = dm2_outs |1;} // y204 00217 dm2_outs = dm2_outs << 1; 00218 if(y203==1){dm2_outs = dm2_outs |1;} // y203 00219 dm2_outs = dm2_outs << 1; 00220 if(y202==1){dm2_outs = dm2_outs |1;} // y202 00221 dm2_outs = dm2_outs << 1; 00222 if(y201==1){dm2_outs = dm2_outs |1;} // y201 00223 00224 // упаковываем выходы для DM0806 ADR=3 00225 dm3_outs = 0; 00226 if(y306==1){dm3_outs = dm3_outs |1;} // y306 00227 dm3_outs = dm3_outs << 1; 00228 if(y305==1){dm3_outs = dm3_outs |1;} // y305 00229 dm3_outs = dm3_outs << 1; 00230 if(y304==1){dm3_outs = dm3_outs |1;} // y304 00231 dm3_outs = dm3_outs << 1; 00232 if(y303==1){dm3_outs = dm3_outs |1;} // y303 00233 dm3_outs = dm3_outs << 1; 00234 if(y302==1){dm3_outs = dm3_outs |1;} // y302 00235 dm3_outs = dm3_outs << 1; 00236 if(y301==1){dm3_outs = dm3_outs |1;} // y301 00237 00238 00239 // упаковываем выходы для DM0806 ADR=4 00240 dm4_outs = 0; 00241 if(y406==1){dm4_outs = dm4_outs |1;} // y406 00242 dm4_outs = dm4_outs << 1; 00243 if(y405==1){dm4_outs = dm4_outs |1;} // y405 00244 dm4_outs = dm4_outs << 1; 00245 if(y404==1){dm4_outs = dm4_outs |1;} // y404 00246 dm4_outs = dm4_outs << 1; 00247 if(y403==1){dm4_outs = dm4_outs |1;} // y403 00248 dm4_outs = dm4_outs << 1; 00249 if(y402==1){dm4_outs = dm4_outs |1;} // y402 00250 dm4_outs = dm4_outs << 1; 00251 if(y401==1){dm4_outs = dm4_outs |1;} // y401 00252 00253 00254 // при изменении по DM0806 посылаем в CAN 00255 if(dm1_outs!=mdm1_outs || dm2_outs!=mdm2_outs || dm3_outs!=mdm3_outs || dm4_outs!=mdm4_outs){ 00256 int _id=50; 00257 char _b1= dm1_outs; 00258 char _b2= dm2_outs ; 00259 char _b3= dm3_outs ; 00260 char _b4= dm4_outs ; 00261 can_save_wr(_id,_b1,_b2,_b3,_b4); 00262 } 00263 00264 00265 // упаковываем выходы для SM01 ADR=1-4 00266 sm_outs=0; 00267 if(b402==1){sm_outs = sm_outs |1;} // b402 00268 sm_outs = sm_outs << 1; 00269 if(b401==1){sm_outs = sm_outs |1;} // b401 00270 sm_outs = sm_outs << 1; 00271 if(b302==1){sm_outs = sm_outs |1;} // b302 00272 sm_outs = sm_outs << 1; 00273 if(b301==1){sm_outs = sm_outs |1;} // b301 00274 sm_outs = sm_outs << 1; 00275 if(b202==1){sm_outs = sm_outs |1;} // b202 00276 sm_outs = sm_outs << 1; 00277 if(b201==1){sm_outs = sm_outs |1;} // b201 00278 sm_outs = sm_outs << 1; 00279 if(b102==1){sm_outs = sm_outs |1;} // b102 00280 sm_outs = sm_outs << 1; 00281 if(b101==1){sm_outs = sm_outs |1;} // b101 00282 00283 00284 // при изменении по SM01 посылаем в CAN 00285 if(sm_outs != msm_outs || sm_position != msm_position){ 00286 int _id=60; 00287 char _b1=dm1_outs; 00288 char _b2= sm_position / 100 ; 00289 char _b3= (sm_position - _b2*100) / 10 ; 00290 char _b4= (sm_position - _b2*100 - _b3*10) ; 00291 can_save_wr(_id,_b1,_b2,_b3,_b4); 00292 } 00293 00294 00295 } 00296 00297 00298 //---------------------------------------- 00299 // формирование значения переменных 00300 // в соответствии с сигналами на входах контроллера 00301 // и принятой по CAN информацией 00302 void read_inputs(){ 00303 // считываем локальные выходы ПЛК 00304 busin=inps; 00305 if(busin & 1){ x01=0; }else{ x01=1;} 00306 if(busin & 2){ x02=0; }else{ x02=1;} 00307 if(busin & 4){ x03=0; }else{ x03=1;} 00308 if(busin & 8){ x04=0; }else{ x04=1;} 00309 if(busin & 16){ x05=0; }else{ x05=1;} 00310 if(busin & 32){ x06=0; }else{ x06=1;} 00311 if(busin & 64){ x07=0; }else{ x07=1;} 00312 if(busin & 128){ x08=0; }else{ x08=1;} 00313 if(busin & 256){ x09=0; }else{ x09=1;} 00314 if(busin & 512){ x10=0; }else{ x10=1;} 00315 if(busin & 1024){ x11=0; }else{ x11=1;} 00316 if(busin & 2048){ x12=0; }else{ x12=1;} 00317 if(busin & 4096){ x13=0; }else{ x13=1;} 00318 if(busin & 8192){ x14=0; }else{ x14=1;} 00319 if(busin & 16384){ x15=0; }else{ x15=1;} 00320 if(busin & 32768){ x16=0; }else{ x16=1;} 00321 00322 Vin_1 = aninp1; 00323 Vin_2 = aninp2; 00324 Vin_3 = aninp3; 00325 Vin_4 = aninp4; 00326 00327 // проверяем принятые посылки CAN 00328 int resalt=1; 00329 while(resalt==1){ 00330 resalt=can_select_rd(); 00331 if(resalt==1){ 00332 00333 // inps from DM ADR=1 00334 if(r_id==51){ 00335 if( r_b1&1 ){ x101=1; } else { x101=0; } 00336 if((r_b1>>1)&1){ x102=1; } else { x102=0; } 00337 if((r_b1>>2)&1){ x103=1; } else { x103=0; } 00338 if((r_b1>>3)&1){ x104=1; } else { x104=0; } 00339 if((r_b1>>4)&1){ x105=1; } else { x105=0; } 00340 if((r_b1>>5)&1){ x106=1; } else { x106=0; } 00341 if((r_b1>>6)&1){ x107=1; } else { x107=0; } 00342 if((r_b1>>7)&1){ x108=1; } else { x108=0; } 00343 } 00344 00345 // inps from DM ADR=2 00346 if(r_id==52){ 00347 if( r_b1&1 ){ x201=1; } else { x201=0; } 00348 if((r_b1>>1)&1){ x202=1; } else { x202=0; } 00349 if((r_b1>>2)&1){ x203=1; } else { x203=0; } 00350 if((r_b1>>3)&1){ x204=1; } else { x204=0; } 00351 if((r_b1>>4)&1){ x205=1; } else { x205=0; } 00352 if((r_b1>>5)&1){ x206=1; } else { x206=0; } 00353 if((r_b1>>6)&1){ x207=1; } else { x207=0; } 00354 if((r_b1>>7)&1){ x208=1; } else { x208=0; } 00355 } 00356 00357 // inps from DM ADR=3 00358 if(r_id==53){ 00359 if( r_b1&1 ){ x301=1; } else { x301=0; } 00360 if((r_b1>>1)&1){ x302=1; } else { x302=0; } 00361 if((r_b1>>2)&1){ x303=1; } else { x303=0; } 00362 if((r_b1>>3)&1){ x304=1; } else { x304=0; } 00363 if((r_b1>>4)&1){ x305=1; } else { x305=0; } 00364 if((r_b1>>5)&1){ x306=1; } else { x306=0; } 00365 if((r_b1>>6)&1){ x307=1; } else { x307=0; } 00366 if((r_b1>>7)&1){ x308=1; } else { x308=0; } 00367 } 00368 00369 // inps from DM ADR=4 00370 if(r_id==54){ 00371 if( r_b1&1 ){ x401=1; } else { x401=0; } 00372 if((r_b1>>1)&1){ x402=1; } else { x402=0; } 00373 if((r_b1>>2)&1){ x403=1; } else { x403=0; } 00374 if((r_b1>>3)&1){ x404=1; } else { x404=0; } 00375 if((r_b1>>4)&1){ x405=1; } else { x405=0; } 00376 if((r_b1>>5)&1){ x406=1; } else { x406=0; } 00377 if((r_b1>>6)&1){ x407=1; } else { x407=0; } 00378 if((r_b1>>7)&1){ x408=1; } else { x408=0; } 00379 } 00380 00381 // inps from SM ADR=1 00382 if(r_id==61){ 00383 if(r_b1 & 4) { a101=1; } else{ a101=0; } 00384 if(r_b1 & 8) { a102=1; } else{ a102=0; } 00385 if(r_b1 & 16){ a103=1; } else{ a103=0; } 00386 if(r_b1 & 32){ a104=1; } else{ a104=0; } 00387 if(r_b1 & 64){ sm1_mov=1; } else{ sm1_mov=0; } 00388 if(r_b1 & 128){ sm1_alr=1; } else{ sm1_alr=0; } 00389 sm1_pos = r_b2*100+r_b3*10+r_b4; 00390 } 00391 00392 // inps from SM ADR=2 00393 if(r_id==62){ 00394 if(r_b1 & 4) { a201=1; } else{ a201=0; } 00395 if(r_b1 & 8) { a202=1; } else{ a202=0; } 00396 if(r_b1 & 16){ a203=1; } else{ a203=0; } 00397 if(r_b1 & 32){ a204=1; } else{ a204=0; } 00398 if(r_b1 & 64){ sm2_mov=1; } else{ sm2_mov=0; } 00399 if(r_b1 & 128){ sm2_alr=1; } else{ sm2_alr=0; } 00400 sm2_pos = r_b2*100+r_b3*10+r_b4; 00401 } 00402 00403 // inps from SM ADR=3 00404 if(r_id==63){ 00405 if(r_b1 & 4) { a301=1; } else{ a301=0; } 00406 if(r_b1 & 8) { a302=1; } else{ a302=0; } 00407 if(r_b1 & 16){ a303=1; } else{ a303=0; } 00408 if(r_b1 & 32){ a304=1; } else{ a304=0; } 00409 if(r_b1 & 64){ sm3_mov=1; } else{ sm3_mov=0; } 00410 if(r_b1 & 128){ sm3_alr=1; } else{ sm3_alr=0; } 00411 sm3_pos = r_b2*100+r_b3*10+r_b4; 00412 } 00413 00414 // inps from SM ADR=4 00415 if(r_id==64){ 00416 if(r_b1 & 4) { a401=1; } else{ a401=0; } 00417 if(r_b1 & 8) { a402=1; } else{ a402=0; } 00418 if(r_b1 & 16){ a403=1; } else{ a403=0; } 00419 if(r_b1 & 32){ a404=1; } else{ a404=0; } 00420 if(r_b1 & 64){ sm4_mov=1; } else{ sm4_mov=0; } 00421 if(r_b1 & 128){ sm4_alr=1; } else{ sm4_alr=0; } 00422 sm4_pos = r_b2*100+r_b3*10+r_b4; 00423 } 00424 00425 00426 00427 } 00428 } 00429 00430 } 00431 00432 //---------------------------------------- 00433 // сохранение значения переменных для обработки 00434 // в след.цикле (установка, сброс) 00435 void save_stat(){ 00436 00437 mx01=x01; mx02=x02; mx03=x03; mx04=x04; 00438 mx05=x05; mx06=x06; mx07=x07; mx08=x08; 00439 mx09=x09; mx10=x10; mx11=x11; mx12=x12; 00440 mx13=x13; mx14=x14; mx15=x15; mx16=x16; 00441 my01=y01; my02=y02; my03=y03; my04=y04; 00442 my05=y05; my06=y06; my07=y07; my08=y08; 00443 00444 mx101=x101; mx102=x102; mx103=x103; mx104=x104; 00445 mx105=x105; mx106=x106; mx107=x107; mx108=x108; 00446 mx201=x201; mx202=x202; mx203=x103; mx204=x104; 00447 mx205=x205; mx206=x206; mx207=x107; mx208=x108; 00448 mx301=x301; mx302=x302; mx303=x303; mx304=x304; 00449 mx305=x305; mx306=x306; mx307=x307; mx308=x308; 00450 mx401=x401; mx402=x402; mx403=x403; mx404=x404; 00451 mx405=x405; mx406=x406; mx407=x407; mx408=x408; 00452 my101=y101; my102=y102; my103=y103; 00453 my104=y104; my105=y105; my106=y106; 00454 my201=y201; my202=y202; my203=y203; 00455 my204=y204; my205=y205; my206=y206; 00456 my301=y301; my302=y302; my303=y303; 00457 my304=y304; my305=y305; my306=y306; 00458 my401=y401; my402=y402; my403=y403; 00459 my404=y404; my405=y405; my406=y406; 00460 00461 ma101=a101; ma102=a102; ma103=a103; ma104=a104; 00462 ma201=a201; ma202=a202; ma203=a203; ma204=a204; 00463 ma301=a301; ma302=a302; ma303=a303; ma304=a304; 00464 ma401=a401; ma402=a402; ma403=a403; ma404=a404; 00465 mb101=b101; mb102=b102; mb201=b201; mb202=b202; 00466 mb301=b301; mb302=b302; mb401=b401; mb402=b402; 00467 00468 msm_outs=sm_outs; 00469 msm_position=sm_position; 00470 00471 mdm1_outs=dm1_outs; 00472 mdm2_outs=dm2_outs; 00473 mdm3_outs=dm3_outs; 00474 mdm4_outs=dm4_outs; 00475 00476 00477 00478 } 00479 00480 //---------------------------------------- 00481 // изменение значения секундных и милисекундных таймеров 00482 // (дискретность 10 мсек) 00483 void timers(){ 00484 00485 int i = 10 ; // время цикла в 10мсек 00486 int max1=30000; // макс. значение для мсек таймеров (30мин) 00487 int max2=18000; // макс. значение для сек таймеров (5час) 00488 00489 // timers, 10ms 00490 if(t_sys<1000){ 00491 t_sys=t_sys+i; 00492 }else{ 00493 t_sys = 0; 00494 if(t101<max2){t101=t101+1;} 00495 if(t102<max2){t102=t102+1;} 00496 if(t103<max2){t103=t103+1;} 00497 if(t104<max2){t104=t104+1;} 00498 if(t105<max2){t105=t105+1;} 00499 if(t106<max2){t106=t106+1;} 00500 if(t107<max2){t107=t107+1;} 00501 if(t108<max2){t108=t108+1;} 00502 if(t109<max2){t109=t109+1;} 00503 if(t110<max2){t110=t110+1;} 00504 } 00505 00506 if(t01<max1){t01=t01+i;} 00507 if(t02<max1){t02=t02+i;} 00508 if(t03<max1){t03=t03+i;} 00509 if(t04<max1){t04=t04+i;} 00510 if(t05<max1){t05=t05+i;} 00511 if(t06<max1){t06=t06+i;} 00512 if(t07<max1){t07=t07+i;} 00513 if(t08<max1){t08=t08+i;} 00514 if(t09<max1){t09=t09+i;} 00515 if(t10<max1){t10=t10+i;} 00516 00517 } 00518 00519 //---------------------------------------- 00520 // прием и отправка посылок 00521 // по CAN шине 00522 void can_rw() 00523 { 00524 CANMessage msg_w; 00525 CANMessage msg_r; 00526 00527 for(int i=0; i<=10; i++){ r_mess_id[i]=0; w_mess_id[i]=0; } 00528 for(int i=0; i<=40; i++){ r_mess_data[i]=0; w_mess_data[i]=0; } 00529 00530 00531 while (true){ 00532 00533 // проверяем, если есть новая посылка 00534 // то помещаем в очередь can_save_rd() 00535 if(can.read(msg_r)){ 00536 int id = msg_r.id; 00537 char b1 = msg_r.data[0]; 00538 char b2 = msg_r.data[1]; 00539 char b3 = msg_r.data[2]; 00540 char b4 = msg_r.data[3]; 00541 can_save_rd(id,b1,b2,b3,b4); 00542 led_b = !led_b; 00543 } 00544 00545 00546 // проверяем очередь на отправку can_select_wr() 00547 // и если есть , то отправляем 00548 if(can_select_wr()){ 00549 msg_w.len = 4; 00550 msg_w.id = w_id; 00551 msg_w.data[0] = w_b1; 00552 msg_w.data[1] = w_b2; 00553 msg_w.data[2] = w_b3; 00554 msg_w.data[3] = w_b4; 00555 can.write(msg_w); 00556 00557 if(can.tderror()){ led_b = !led_b; w_alr=1; } else { w_alr=0; } 00558 00559 } 00560 00561 00562 Thread::wait(2); 00563 } 00564 00565 } 00566 00567 //---------------------------------------- 00568 // посимвольный прием из USB и сохранение 00569 // информации в буфере, обрабатывается после того как все принято 00570 void pc_rd() 00571 { 00572 sprintf(pc_buffer, "buff_ok"); 00573 while(1) 00574 { 00575 if( pc.readable() ) 00576 { 00577 pc_buffer[pc_i] = pc.getc(); 00578 if(pc_i<MAX_SERL){pc_i++; } 00579 pc_buffer[pc_i]=0; 00580 } 00581 } 00582 } 00583 00584 //---------------------------------------- 00585 // поиск и чтение значения из строки 00586 // возвращает целое число. 00587 int read_val() { 00588 char inp_c[10]; 00589 int val=0; 00590 int i=0; 00591 int p=0; 00592 00593 while(p<MAX_SERL){ 00594 if(pc_buffer[p]=='='){ break ; } 00595 p++; 00596 } 00597 00598 p++; 00599 i=0; if(pc_buffer[p+i]>=48 && pc_buffer[p+i]<=57){ inp_c[i]=pc_buffer[p+i]; } else {inp_c[i]=0 ; } 00600 i=1; if(pc_buffer[p+i]>=48 && pc_buffer[p+i]<=57){ inp_c[i]=pc_buffer[p+i]; } else {inp_c[i]=0 ; } 00601 i=2; if(pc_buffer[p+i]>=48 && pc_buffer[p+i]<=57){ inp_c[i]=pc_buffer[p+i]; } else {inp_c[i]=0 ; } 00602 i=3; if(pc_buffer[p+i]>=48 && pc_buffer[p+i]<=57){ inp_c[i]=pc_buffer[p+i]; } else {inp_c[i]=0 ; } 00603 i=4; if(pc_buffer[p+i]>=48 && pc_buffer[p+i]<=57){ inp_c[i]=pc_buffer[p+i]; } else {inp_c[i]=0 ; } 00604 i=5; if(pc_buffer[p+i]>=48 && pc_buffer[p+i]<=57){ inp_c[i]=pc_buffer[p+i]; } else {inp_c[i]=0 ; } 00605 inp_c[6]=0 ; 00606 00607 val = atoi(inp_c); 00608 return val; 00609 } 00610 00611 00612 00613 00614 00615 00616 00617 00618 00619 00620 00621 00622 00623
Generated on Sat Jul 16 2022 13:16:41 by
1.7.2