PLC 16 in / 8 rev / an in-out / CAN

Dependencies:   PCAL955x mbed mbed-rtos AT45

Committer:
Oleg_B
Date:
Fri Feb 12 10:55:27 2021 +0000
Revision:
2:439534d6bb89
Parent:
src/main.cpp@1:3768b56b12e7
files move to f

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Oleg_B 0:d05a7158dbad 1
Oleg_B 0:d05a7158dbad 2 /*** PLC F446RE ***/
Oleg_B 0:d05a7158dbad 3 // 512 KB Flash / 128 KB SRAM System / 4 KB SRAM Backup
Oleg_B 0:d05a7158dbad 4
Oleg_B 0:d05a7158dbad 5 #include "mbed.h"
Oleg_B 0:d05a7158dbad 6 #include "rtos.h"
Oleg_B 0:d05a7158dbad 7 #include "AT45.h"
Oleg_B 0:d05a7158dbad 8 #include "PCAL9555.h"
Oleg_B 0:d05a7158dbad 9
Oleg_B 0:d05a7158dbad 10 #include "can.h"
Oleg_B 0:d05a7158dbad 11 #include "config_io.h"
Oleg_B 0:d05a7158dbad 12 #include "ext_vars.h"
Oleg_B 0:d05a7158dbad 13 #include "set_vars.h"
Oleg_B 0:d05a7158dbad 14
Oleg_B 0:d05a7158dbad 15 #include "task_ob.h"
Oleg_B 0:d05a7158dbad 16
Oleg_B 0:d05a7158dbad 17 #define CAN_BAUD 50000 // скорость
Oleg_B 0:d05a7158dbad 18 #define UART_BAUD 9600 // скорость
Oleg_B 0:d05a7158dbad 19 #define MAX_SERL 30 // макс. длинна строки из usb/uart
Oleg_B 0:d05a7158dbad 20
Oleg_B 0:d05a7158dbad 21
Oleg_B 0:d05a7158dbad 22
Oleg_B 0:d05a7158dbad 23
Oleg_B 0:d05a7158dbad 24 /******* создание локальных переменных *************************/
Oleg_B 0:d05a7158dbad 25
Oleg_B 0:d05a7158dbad 26 int busin = 0;
Oleg_B 0:d05a7158dbad 27 int t_sys = 0 ;
Oleg_B 0:d05a7158dbad 28
Oleg_B 0:d05a7158dbad 29 char pc_buffer [MAX_SERL+2];
Oleg_B 0:d05a7158dbad 30 int pc_i=0;
Oleg_B 0:d05a7158dbad 31 int pc_im=0;
Oleg_B 0:d05a7158dbad 32
Oleg_B 0:d05a7158dbad 33 float st_start=0.0;
Oleg_B 0:d05a7158dbad 34 float st_end=0.0;
Oleg_B 0:d05a7158dbad 35 float st_old=0.0;
Oleg_B 0:d05a7158dbad 36 float st_cycl=0.0;
Oleg_B 0:d05a7158dbad 37 float st_task=0.0;
Oleg_B 0:d05a7158dbad 38
Oleg_B 0:d05a7158dbad 39 int new_outs=0;
Oleg_B 0:d05a7158dbad 40
Oleg_B 0:d05a7158dbad 41 /********** прототипы локальных функций **********************/
Oleg_B 0:d05a7158dbad 42 void read_inputs();
Oleg_B 0:d05a7158dbad 43 void write_outs();
Oleg_B 0:d05a7158dbad 44 void save_stat();
Oleg_B 0:d05a7158dbad 45 void logic_tasks();
Oleg_B 0:d05a7158dbad 46 void can_rw();
Oleg_B 0:d05a7158dbad 47 void timers();
Oleg_B 0:d05a7158dbad 48 void pc_rd();
Oleg_B 0:d05a7158dbad 49 int read_val();
Oleg_B 0:d05a7158dbad 50
Oleg_B 0:d05a7158dbad 51 /************** Threads **************************************/
Oleg_B 0:d05a7158dbad 52 Thread read_uart;
Oleg_B 0:d05a7158dbad 53 Thread work_can;
Oleg_B 0:d05a7158dbad 54 RtosTimer t_10msec(&logic_tasks);
Oleg_B 0:d05a7158dbad 55
Oleg_B 0:d05a7158dbad 56
Oleg_B 0:d05a7158dbad 57 /*************** старт программы PLC **************************/
Oleg_B 0:d05a7158dbad 58 int main()
Oleg_B 0:d05a7158dbad 59 {
Oleg_B 0:d05a7158dbad 60 led=1;
Oleg_B 0:d05a7158dbad 61 st.start();
Oleg_B 0:d05a7158dbad 62
Oleg_B 0:d05a7158dbad 63 // ----- настраиваем CAN ------
Oleg_B 0:d05a7158dbad 64 can.frequency(CAN_BAUD);
Oleg_B 0:d05a7158dbad 65
Oleg_B 0:d05a7158dbad 66 //----- настраиваем порт USB ------
Oleg_B 0:d05a7158dbad 67 pc.baud(UART_BAUD);
Oleg_B 0:d05a7158dbad 68 pc.printf( "\r\n> START PLC \r\n");
Oleg_B 0:d05a7158dbad 69 pc.printf(">CPU SystemCoreClock is %d Hz\r\n", SystemCoreClock);
Oleg_B 0:d05a7158dbad 70
Oleg_B 0:d05a7158dbad 71
Oleg_B 0:d05a7158dbad 72 //------ проверка FLASH ----------
Oleg_B 0:d05a7158dbad 73 pc.printf("> flash size: %d bytes \n", spif.device_size());
Oleg_B 0:d05a7158dbad 74
Oleg_B 0:d05a7158dbad 75
Oleg_B 0:d05a7158dbad 76 //------- запускаем потоки Thread ------
Oleg_B 0:d05a7158dbad 77 work_can.start(can_rw); // обмен информ. по CAN
Oleg_B 0:d05a7158dbad 78 read_uart.start(pc_rd); // чтение UART
Oleg_B 0:d05a7158dbad 79
Oleg_B 0:d05a7158dbad 80
Oleg_B 0:d05a7158dbad 81 //--- запускаем логич.задач по таймеру 10msec ------
Oleg_B 0:d05a7158dbad 82 t_10msec.start(10);
Oleg_B 0:d05a7158dbad 83
Oleg_B 0:d05a7158dbad 84
Oleg_B 0:d05a7158dbad 85 /*** обработка команд по USB ***/
Oleg_B 0:d05a7158dbad 86 while (true) {
Oleg_B 0:d05a7158dbad 87
Oleg_B 0:d05a7158dbad 88 if(t_sys<500){led=1;}else{led=0;}
Oleg_B 0:d05a7158dbad 89 led_r=led;
Oleg_B 0:d05a7158dbad 90
Oleg_B 0:d05a7158dbad 91 // проверяем usb
Oleg_B 0:d05a7158dbad 92 if (pc_i>1 && pc_i==pc_im){
Oleg_B 0:d05a7158dbad 93 pc_i = 0;
Oleg_B 0:d05a7158dbad 94 //pc.printf("usb:%s",pc_buffer);
Oleg_B 0:d05a7158dbad 95 //led = !led;
Oleg_B 0:d05a7158dbad 96
Oleg_B 0:d05a7158dbad 97
Oleg_B 0:d05a7158dbad 98 // --- устанавливаем значение переменных ----
Oleg_B 0:d05a7158dbad 99 if(strstr(pc_buffer,"=")){
Oleg_B 0:d05a7158dbad 100
Oleg_B 0:d05a7158dbad 101 if(strstr (pc_buffer,"vr000")) { vr000=read_val(); pc.printf( "ok\n"); }
Oleg_B 0:d05a7158dbad 102 if(strstr (pc_buffer,"vr001")) { vr001=read_val(); pc.printf( "ok\n"); }
Oleg_B 0:d05a7158dbad 103 if(strstr (pc_buffer,"vr002")) { vr002=read_val(); pc.printf( "ok\n"); }
Oleg_B 0:d05a7158dbad 104
Oleg_B 0:d05a7158dbad 105 if(strstr (pc_buffer,"pr001")) { pr001=read_val(); pc.printf( "ok\n"); }
Oleg_B 0:d05a7158dbad 106 if(strstr (pc_buffer,"pr002")) { pr002=read_val(); pc.printf( "ok\n"); }
Oleg_B 0:d05a7158dbad 107
Oleg_B 0:d05a7158dbad 108 }
Oleg_B 0:d05a7158dbad 109
Oleg_B 0:d05a7158dbad 110 // --- проверка значения переменных -----
Oleg_B 0:d05a7158dbad 111 if(strstr(pc_buffer,"?")){
Oleg_B 0:d05a7158dbad 112
Oleg_B 0:d05a7158dbad 113 if(strstr (pc_buffer,"vr000")) { pc.printf( "vr000:%d\n", vr000); }
Oleg_B 0:d05a7158dbad 114 if(strstr (pc_buffer,"vr001")) { pc.printf( "vr001:%d\n", vr001); }
Oleg_B 0:d05a7158dbad 115 if(strstr (pc_buffer,"vr002")) { pc.printf( "vr002:%d\n", vr002); }
Oleg_B 1:3768b56b12e7 116
Oleg_B 0:d05a7158dbad 117 if(strstr (pc_buffer,"pr001")) { pc.printf( "pr001:%d\n", pr001); }
Oleg_B 0:d05a7158dbad 118 if(strstr (pc_buffer,"pr002")) { pc.printf( "pr002:%d\n", pr002); }
Oleg_B 0:d05a7158dbad 119
Oleg_B 0:d05a7158dbad 120
Oleg_B 0:d05a7158dbad 121 // для контроля состояния
Oleg_B 0:d05a7158dbad 122
Oleg_B 0:d05a7158dbad 123 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); }
Oleg_B 0:d05a7158dbad 124 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); }
Oleg_B 0:d05a7158dbad 125 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 ); }
Oleg_B 0:d05a7158dbad 126 if(strstr (pc_buffer,"an_out")) { pc.printf( "\n--- PLC --- \n> an_out1=%f an_out2=%f \n", Vout_1, Vout_2 ); }
Oleg_B 0:d05a7158dbad 127 if(strstr (pc_buffer,"stime")) { pc.printf( "\n--- PLC --- \n> work cycle %f / using %f sec \n", st_cycl, st_task ); }
Oleg_B 0:d05a7158dbad 128
Oleg_B 1:3768b56b12e7 129 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 ); }
Oleg_B 1:3768b56b12e7 130 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 ); }
Oleg_B 1:3768b56b12e7 131 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 ); }
Oleg_B 1:3768b56b12e7 132 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 ); }
Oleg_B 0:d05a7158dbad 133
Oleg_B 1:3768b56b12e7 134 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 ); }
Oleg_B 1:3768b56b12e7 135 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 ); }
Oleg_B 1:3768b56b12e7 136 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 ); }
Oleg_B 1:3768b56b12e7 137 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 ); }
Oleg_B 0:d05a7158dbad 138
Oleg_B 0:d05a7158dbad 139 }
Oleg_B 0:d05a7158dbad 140
Oleg_B 0:d05a7158dbad 141 if(strstr (pc_buffer,"reset")) { NVIC_SystemReset() ; }
Oleg_B 0:d05a7158dbad 142
Oleg_B 0:d05a7158dbad 143
Oleg_B 0:d05a7158dbad 144
Oleg_B 0:d05a7158dbad 145 }
Oleg_B 0:d05a7158dbad 146
Oleg_B 0:d05a7158dbad 147 pc_im=pc_i;
Oleg_B 0:d05a7158dbad 148 Thread::wait(100);
Oleg_B 0:d05a7158dbad 149 }
Oleg_B 0:d05a7158dbad 150
Oleg_B 0:d05a7158dbad 151 }
Oleg_B 0:d05a7158dbad 152
Oleg_B 0:d05a7158dbad 153
Oleg_B 0:d05a7158dbad 154 /**************** дополнительные функции **********************/
Oleg_B 0:d05a7158dbad 155
Oleg_B 0:d05a7158dbad 156 // логические задачи. вызываются для выполнения
Oleg_B 0:d05a7158dbad 157 // каждые 10 мсек
Oleg_B 0:d05a7158dbad 158 void logic_tasks(){
Oleg_B 0:d05a7158dbad 159 // чтение сист.таймера
Oleg_B 0:d05a7158dbad 160 st_start=st.read();
Oleg_B 0:d05a7158dbad 161
Oleg_B 0:d05a7158dbad 162
Oleg_B 0:d05a7158dbad 163 // подготовка входн.информации
Oleg_B 0:d05a7158dbad 164 timers();
Oleg_B 0:d05a7158dbad 165 read_inputs();
Oleg_B 0:d05a7158dbad 166
Oleg_B 0:d05a7158dbad 167 // логичкские задачи
Oleg_B 0:d05a7158dbad 168 task_ob();
Oleg_B 0:d05a7158dbad 169
Oleg_B 0:d05a7158dbad 170 // формирование выходн.информации
Oleg_B 0:d05a7158dbad 171 write_outs();
Oleg_B 0:d05a7158dbad 172 save_stat();
Oleg_B 0:d05a7158dbad 173
Oleg_B 0:d05a7158dbad 174 // определяем реальное время цикла/использ.время
Oleg_B 0:d05a7158dbad 175 st_end=st.read();
Oleg_B 0:d05a7158dbad 176 st_task=st_end-st_start;
Oleg_B 0:d05a7158dbad 177 st_cycl=st_start-st_old;
Oleg_B 0:d05a7158dbad 178 st_old=st_start;
Oleg_B 0:d05a7158dbad 179
Oleg_B 0:d05a7158dbad 180 }
Oleg_B 0:d05a7158dbad 181
Oleg_B 0:d05a7158dbad 182
Oleg_B 0:d05a7158dbad 183 //----------------------------------------
Oleg_B 0:d05a7158dbad 184 // установка выходов контроллера
Oleg_B 0:d05a7158dbad 185 // в соответствии значения преременным
Oleg_B 0:d05a7158dbad 186 void write_outs(){
Oleg_B 0:d05a7158dbad 187 // устанавливаем локальные выходы ПЛК
Oleg_B 0:d05a7158dbad 188 y_1=y01; y_5=y05;
Oleg_B 0:d05a7158dbad 189 y_2=y02; y_6=y06;
Oleg_B 0:d05a7158dbad 190 y_3=y03; y_7=y07;
Oleg_B 0:d05a7158dbad 191 y_4=y04; y_8=y08;
Oleg_B 0:d05a7158dbad 192
Oleg_B 0:d05a7158dbad 193 anout1=Vout_1;
Oleg_B 0:d05a7158dbad 194 anout2=Vout_2;
Oleg_B 0:d05a7158dbad 195
Oleg_B 0:d05a7158dbad 196 // упаковываем выходы для DM0806 ADR=1
Oleg_B 0:d05a7158dbad 197 dm1_outs = 0;
Oleg_B 0:d05a7158dbad 198 if(y106==1){dm1_outs = dm1_outs |1;} // y106
Oleg_B 0:d05a7158dbad 199 dm1_outs = dm1_outs << 1;
Oleg_B 0:d05a7158dbad 200 if(y105==1){dm1_outs = dm1_outs |1;} // y105
Oleg_B 0:d05a7158dbad 201 dm1_outs = dm1_outs << 1;
Oleg_B 0:d05a7158dbad 202 if(y104==1){dm1_outs = dm1_outs |1;} // y104
Oleg_B 0:d05a7158dbad 203 dm1_outs = dm1_outs << 1;
Oleg_B 0:d05a7158dbad 204 if(y103==1){dm1_outs = dm1_outs |1;} // y103
Oleg_B 0:d05a7158dbad 205 dm1_outs = dm1_outs << 1;
Oleg_B 0:d05a7158dbad 206 if(y102==1){dm1_outs = dm1_outs |1;} // y102
Oleg_B 0:d05a7158dbad 207 dm1_outs = dm1_outs << 1;
Oleg_B 0:d05a7158dbad 208 if(y101==1){dm1_outs = dm1_outs |1;} // y101
Oleg_B 0:d05a7158dbad 209
Oleg_B 0:d05a7158dbad 210 // упаковываем выходы для DM0806 ADR=2
Oleg_B 0:d05a7158dbad 211 dm2_outs = 0;
Oleg_B 0:d05a7158dbad 212 if(y206==1){dm2_outs = dm2_outs |1;} // y206
Oleg_B 0:d05a7158dbad 213 dm2_outs = dm2_outs << 1;
Oleg_B 0:d05a7158dbad 214 if(y205==1){dm2_outs = dm2_outs |1;} // y205
Oleg_B 0:d05a7158dbad 215 dm2_outs = dm2_outs << 1;
Oleg_B 0:d05a7158dbad 216 if(y204==1){dm2_outs = dm2_outs |1;} // y204
Oleg_B 0:d05a7158dbad 217 dm2_outs = dm2_outs << 1;
Oleg_B 0:d05a7158dbad 218 if(y203==1){dm2_outs = dm2_outs |1;} // y203
Oleg_B 0:d05a7158dbad 219 dm2_outs = dm2_outs << 1;
Oleg_B 0:d05a7158dbad 220 if(y202==1){dm2_outs = dm2_outs |1;} // y202
Oleg_B 0:d05a7158dbad 221 dm2_outs = dm2_outs << 1;
Oleg_B 0:d05a7158dbad 222 if(y201==1){dm2_outs = dm2_outs |1;} // y201
Oleg_B 0:d05a7158dbad 223
Oleg_B 0:d05a7158dbad 224 // упаковываем выходы для DM0806 ADR=3
Oleg_B 0:d05a7158dbad 225 dm3_outs = 0;
Oleg_B 0:d05a7158dbad 226 if(y306==1){dm3_outs = dm3_outs |1;} // y306
Oleg_B 0:d05a7158dbad 227 dm3_outs = dm3_outs << 1;
Oleg_B 0:d05a7158dbad 228 if(y305==1){dm3_outs = dm3_outs |1;} // y305
Oleg_B 0:d05a7158dbad 229 dm3_outs = dm3_outs << 1;
Oleg_B 0:d05a7158dbad 230 if(y304==1){dm3_outs = dm3_outs |1;} // y304
Oleg_B 0:d05a7158dbad 231 dm3_outs = dm3_outs << 1;
Oleg_B 0:d05a7158dbad 232 if(y303==1){dm3_outs = dm3_outs |1;} // y303
Oleg_B 0:d05a7158dbad 233 dm3_outs = dm3_outs << 1;
Oleg_B 0:d05a7158dbad 234 if(y302==1){dm3_outs = dm3_outs |1;} // y302
Oleg_B 0:d05a7158dbad 235 dm3_outs = dm3_outs << 1;
Oleg_B 0:d05a7158dbad 236 if(y301==1){dm3_outs = dm3_outs |1;} // y301
Oleg_B 0:d05a7158dbad 237
Oleg_B 0:d05a7158dbad 238
Oleg_B 0:d05a7158dbad 239 // упаковываем выходы для DM0806 ADR=4
Oleg_B 0:d05a7158dbad 240 dm4_outs = 0;
Oleg_B 0:d05a7158dbad 241 if(y406==1){dm4_outs = dm4_outs |1;} // y406
Oleg_B 0:d05a7158dbad 242 dm4_outs = dm4_outs << 1;
Oleg_B 0:d05a7158dbad 243 if(y405==1){dm4_outs = dm4_outs |1;} // y405
Oleg_B 0:d05a7158dbad 244 dm4_outs = dm4_outs << 1;
Oleg_B 0:d05a7158dbad 245 if(y404==1){dm4_outs = dm4_outs |1;} // y404
Oleg_B 0:d05a7158dbad 246 dm4_outs = dm4_outs << 1;
Oleg_B 0:d05a7158dbad 247 if(y403==1){dm4_outs = dm4_outs |1;} // y403
Oleg_B 0:d05a7158dbad 248 dm4_outs = dm4_outs << 1;
Oleg_B 0:d05a7158dbad 249 if(y402==1){dm4_outs = dm4_outs |1;} // y402
Oleg_B 0:d05a7158dbad 250 dm4_outs = dm4_outs << 1;
Oleg_B 0:d05a7158dbad 251 if(y401==1){dm4_outs = dm4_outs |1;} // y401
Oleg_B 0:d05a7158dbad 252
Oleg_B 0:d05a7158dbad 253
Oleg_B 0:d05a7158dbad 254 // при изменении по DM0806 посылаем в CAN
Oleg_B 0:d05a7158dbad 255 if(dm1_outs!=mdm1_outs || dm2_outs!=mdm2_outs || dm3_outs!=mdm3_outs || dm4_outs!=mdm4_outs){
Oleg_B 0:d05a7158dbad 256 int _id=50;
Oleg_B 0:d05a7158dbad 257 char _b1= dm1_outs;
Oleg_B 0:d05a7158dbad 258 char _b2= dm2_outs ;
Oleg_B 0:d05a7158dbad 259 char _b3= dm3_outs ;
Oleg_B 0:d05a7158dbad 260 char _b4= dm4_outs ;
Oleg_B 0:d05a7158dbad 261 can_save_wr(_id,_b1,_b2,_b3,_b4);
Oleg_B 0:d05a7158dbad 262 }
Oleg_B 0:d05a7158dbad 263
Oleg_B 0:d05a7158dbad 264
Oleg_B 0:d05a7158dbad 265 // упаковываем выходы для SM01 ADR=1-4
Oleg_B 0:d05a7158dbad 266 sm_outs=0;
Oleg_B 0:d05a7158dbad 267 if(b402==1){sm_outs = sm_outs |1;} // b402
Oleg_B 0:d05a7158dbad 268 sm_outs = sm_outs << 1;
Oleg_B 0:d05a7158dbad 269 if(b401==1){sm_outs = sm_outs |1;} // b401
Oleg_B 0:d05a7158dbad 270 sm_outs = sm_outs << 1;
Oleg_B 0:d05a7158dbad 271 if(b302==1){sm_outs = sm_outs |1;} // b302
Oleg_B 0:d05a7158dbad 272 sm_outs = sm_outs << 1;
Oleg_B 0:d05a7158dbad 273 if(b301==1){sm_outs = sm_outs |1;} // b301
Oleg_B 0:d05a7158dbad 274 sm_outs = sm_outs << 1;
Oleg_B 0:d05a7158dbad 275 if(b202==1){sm_outs = sm_outs |1;} // b202
Oleg_B 0:d05a7158dbad 276 sm_outs = sm_outs << 1;
Oleg_B 0:d05a7158dbad 277 if(b201==1){sm_outs = sm_outs |1;} // b201
Oleg_B 0:d05a7158dbad 278 sm_outs = sm_outs << 1;
Oleg_B 0:d05a7158dbad 279 if(b102==1){sm_outs = sm_outs |1;} // b102
Oleg_B 0:d05a7158dbad 280 sm_outs = sm_outs << 1;
Oleg_B 0:d05a7158dbad 281 if(b101==1){sm_outs = sm_outs |1;} // b101
Oleg_B 0:d05a7158dbad 282
Oleg_B 0:d05a7158dbad 283
Oleg_B 0:d05a7158dbad 284 // при изменении по SM01 посылаем в CAN
Oleg_B 0:d05a7158dbad 285 if(sm_outs != msm_outs || sm_position != msm_position){
Oleg_B 0:d05a7158dbad 286 int _id=60;
Oleg_B 0:d05a7158dbad 287 char _b1=dm1_outs;
Oleg_B 0:d05a7158dbad 288 char _b2= sm_position / 100 ;
Oleg_B 0:d05a7158dbad 289 char _b3= (sm_position - _b2*100) / 10 ;
Oleg_B 0:d05a7158dbad 290 char _b4= (sm_position - _b2*100 - _b3*10) ;
Oleg_B 0:d05a7158dbad 291 can_save_wr(_id,_b1,_b2,_b3,_b4);
Oleg_B 0:d05a7158dbad 292 }
Oleg_B 0:d05a7158dbad 293
Oleg_B 0:d05a7158dbad 294
Oleg_B 0:d05a7158dbad 295 }
Oleg_B 0:d05a7158dbad 296
Oleg_B 0:d05a7158dbad 297
Oleg_B 0:d05a7158dbad 298 //----------------------------------------
Oleg_B 0:d05a7158dbad 299 // формирование значения переменных
Oleg_B 0:d05a7158dbad 300 // в соответствии с сигналами на входах контроллера
Oleg_B 0:d05a7158dbad 301 // и принятой по CAN информацией
Oleg_B 0:d05a7158dbad 302 void read_inputs(){
Oleg_B 0:d05a7158dbad 303 // считываем локальные выходы ПЛК
Oleg_B 0:d05a7158dbad 304 busin=inps;
Oleg_B 0:d05a7158dbad 305 if(busin & 1){ x01=0; }else{ x01=1;}
Oleg_B 0:d05a7158dbad 306 if(busin & 2){ x02=0; }else{ x02=1;}
Oleg_B 0:d05a7158dbad 307 if(busin & 4){ x03=0; }else{ x03=1;}
Oleg_B 0:d05a7158dbad 308 if(busin & 8){ x04=0; }else{ x04=1;}
Oleg_B 0:d05a7158dbad 309 if(busin & 16){ x05=0; }else{ x05=1;}
Oleg_B 0:d05a7158dbad 310 if(busin & 32){ x06=0; }else{ x06=1;}
Oleg_B 0:d05a7158dbad 311 if(busin & 64){ x07=0; }else{ x07=1;}
Oleg_B 0:d05a7158dbad 312 if(busin & 128){ x08=0; }else{ x08=1;}
Oleg_B 0:d05a7158dbad 313 if(busin & 256){ x09=0; }else{ x09=1;}
Oleg_B 0:d05a7158dbad 314 if(busin & 512){ x10=0; }else{ x10=1;}
Oleg_B 0:d05a7158dbad 315 if(busin & 1024){ x11=0; }else{ x11=1;}
Oleg_B 0:d05a7158dbad 316 if(busin & 2048){ x12=0; }else{ x12=1;}
Oleg_B 0:d05a7158dbad 317 if(busin & 4096){ x13=0; }else{ x13=1;}
Oleg_B 0:d05a7158dbad 318 if(busin & 8192){ x14=0; }else{ x14=1;}
Oleg_B 0:d05a7158dbad 319 if(busin & 16384){ x15=0; }else{ x15=1;}
Oleg_B 0:d05a7158dbad 320 if(busin & 32768){ x16=0; }else{ x16=1;}
Oleg_B 0:d05a7158dbad 321
Oleg_B 0:d05a7158dbad 322 Vin_1 = aninp1;
Oleg_B 0:d05a7158dbad 323 Vin_2 = aninp2;
Oleg_B 0:d05a7158dbad 324 Vin_3 = aninp3;
Oleg_B 0:d05a7158dbad 325 Vin_4 = aninp4;
Oleg_B 0:d05a7158dbad 326
Oleg_B 0:d05a7158dbad 327 // проверяем принятые посылки CAN
Oleg_B 0:d05a7158dbad 328 int resalt=1;
Oleg_B 0:d05a7158dbad 329 while(resalt==1){
Oleg_B 0:d05a7158dbad 330 resalt=can_select_rd();
Oleg_B 0:d05a7158dbad 331 if(resalt==1){
Oleg_B 0:d05a7158dbad 332
Oleg_B 0:d05a7158dbad 333 // inps from DM ADR=1
Oleg_B 0:d05a7158dbad 334 if(r_id==51){
Oleg_B 0:d05a7158dbad 335 if( r_b1&1 ){ x101=1; } else { x101=0; }
Oleg_B 0:d05a7158dbad 336 if((r_b1>>1)&1){ x102=1; } else { x102=0; }
Oleg_B 0:d05a7158dbad 337 if((r_b1>>2)&1){ x103=1; } else { x103=0; }
Oleg_B 0:d05a7158dbad 338 if((r_b1>>3)&1){ x104=1; } else { x104=0; }
Oleg_B 0:d05a7158dbad 339 if((r_b1>>4)&1){ x105=1; } else { x105=0; }
Oleg_B 0:d05a7158dbad 340 if((r_b1>>5)&1){ x106=1; } else { x106=0; }
Oleg_B 0:d05a7158dbad 341 if((r_b1>>6)&1){ x107=1; } else { x107=0; }
Oleg_B 0:d05a7158dbad 342 if((r_b1>>7)&1){ x108=1; } else { x108=0; }
Oleg_B 0:d05a7158dbad 343 }
Oleg_B 0:d05a7158dbad 344
Oleg_B 0:d05a7158dbad 345 // inps from DM ADR=2
Oleg_B 0:d05a7158dbad 346 if(r_id==52){
Oleg_B 0:d05a7158dbad 347 if( r_b1&1 ){ x201=1; } else { x201=0; }
Oleg_B 0:d05a7158dbad 348 if((r_b1>>1)&1){ x202=1; } else { x202=0; }
Oleg_B 0:d05a7158dbad 349 if((r_b1>>2)&1){ x203=1; } else { x203=0; }
Oleg_B 0:d05a7158dbad 350 if((r_b1>>3)&1){ x204=1; } else { x204=0; }
Oleg_B 0:d05a7158dbad 351 if((r_b1>>4)&1){ x205=1; } else { x205=0; }
Oleg_B 0:d05a7158dbad 352 if((r_b1>>5)&1){ x206=1; } else { x206=0; }
Oleg_B 0:d05a7158dbad 353 if((r_b1>>6)&1){ x207=1; } else { x207=0; }
Oleg_B 0:d05a7158dbad 354 if((r_b1>>7)&1){ x208=1; } else { x208=0; }
Oleg_B 0:d05a7158dbad 355 }
Oleg_B 0:d05a7158dbad 356
Oleg_B 0:d05a7158dbad 357 // inps from DM ADR=3
Oleg_B 0:d05a7158dbad 358 if(r_id==53){
Oleg_B 0:d05a7158dbad 359 if( r_b1&1 ){ x301=1; } else { x301=0; }
Oleg_B 0:d05a7158dbad 360 if((r_b1>>1)&1){ x302=1; } else { x302=0; }
Oleg_B 0:d05a7158dbad 361 if((r_b1>>2)&1){ x303=1; } else { x303=0; }
Oleg_B 0:d05a7158dbad 362 if((r_b1>>3)&1){ x304=1; } else { x304=0; }
Oleg_B 0:d05a7158dbad 363 if((r_b1>>4)&1){ x305=1; } else { x305=0; }
Oleg_B 0:d05a7158dbad 364 if((r_b1>>5)&1){ x306=1; } else { x306=0; }
Oleg_B 0:d05a7158dbad 365 if((r_b1>>6)&1){ x307=1; } else { x307=0; }
Oleg_B 0:d05a7158dbad 366 if((r_b1>>7)&1){ x308=1; } else { x308=0; }
Oleg_B 0:d05a7158dbad 367 }
Oleg_B 0:d05a7158dbad 368
Oleg_B 0:d05a7158dbad 369 // inps from DM ADR=4
Oleg_B 0:d05a7158dbad 370 if(r_id==54){
Oleg_B 0:d05a7158dbad 371 if( r_b1&1 ){ x401=1; } else { x401=0; }
Oleg_B 0:d05a7158dbad 372 if((r_b1>>1)&1){ x402=1; } else { x402=0; }
Oleg_B 0:d05a7158dbad 373 if((r_b1>>2)&1){ x403=1; } else { x403=0; }
Oleg_B 0:d05a7158dbad 374 if((r_b1>>3)&1){ x404=1; } else { x404=0; }
Oleg_B 0:d05a7158dbad 375 if((r_b1>>4)&1){ x405=1; } else { x405=0; }
Oleg_B 0:d05a7158dbad 376 if((r_b1>>5)&1){ x406=1; } else { x406=0; }
Oleg_B 0:d05a7158dbad 377 if((r_b1>>6)&1){ x407=1; } else { x407=0; }
Oleg_B 0:d05a7158dbad 378 if((r_b1>>7)&1){ x408=1; } else { x408=0; }
Oleg_B 0:d05a7158dbad 379 }
Oleg_B 0:d05a7158dbad 380
Oleg_B 0:d05a7158dbad 381 // inps from SM ADR=1
Oleg_B 0:d05a7158dbad 382 if(r_id==61){
Oleg_B 0:d05a7158dbad 383 if(r_b1 & 4) { a101=1; } else{ a101=0; }
Oleg_B 0:d05a7158dbad 384 if(r_b1 & 8) { a102=1; } else{ a102=0; }
Oleg_B 0:d05a7158dbad 385 if(r_b1 & 16){ a103=1; } else{ a103=0; }
Oleg_B 0:d05a7158dbad 386 if(r_b1 & 32){ a104=1; } else{ a104=0; }
Oleg_B 0:d05a7158dbad 387 if(r_b1 & 64){ sm1_mov=1; } else{ sm1_mov=0; }
Oleg_B 0:d05a7158dbad 388 if(r_b1 & 128){ sm1_alr=1; } else{ sm1_alr=0; }
Oleg_B 0:d05a7158dbad 389 sm1_pos = r_b2*100+r_b3*10+r_b4;
Oleg_B 0:d05a7158dbad 390 }
Oleg_B 0:d05a7158dbad 391
Oleg_B 0:d05a7158dbad 392 // inps from SM ADR=2
Oleg_B 0:d05a7158dbad 393 if(r_id==62){
Oleg_B 0:d05a7158dbad 394 if(r_b1 & 4) { a201=1; } else{ a201=0; }
Oleg_B 0:d05a7158dbad 395 if(r_b1 & 8) { a202=1; } else{ a202=0; }
Oleg_B 0:d05a7158dbad 396 if(r_b1 & 16){ a203=1; } else{ a203=0; }
Oleg_B 0:d05a7158dbad 397 if(r_b1 & 32){ a204=1; } else{ a204=0; }
Oleg_B 0:d05a7158dbad 398 if(r_b1 & 64){ sm2_mov=1; } else{ sm2_mov=0; }
Oleg_B 0:d05a7158dbad 399 if(r_b1 & 128){ sm2_alr=1; } else{ sm2_alr=0; }
Oleg_B 0:d05a7158dbad 400 sm2_pos = r_b2*100+r_b3*10+r_b4;
Oleg_B 0:d05a7158dbad 401 }
Oleg_B 0:d05a7158dbad 402
Oleg_B 0:d05a7158dbad 403 // inps from SM ADR=3
Oleg_B 0:d05a7158dbad 404 if(r_id==63){
Oleg_B 0:d05a7158dbad 405 if(r_b1 & 4) { a301=1; } else{ a301=0; }
Oleg_B 0:d05a7158dbad 406 if(r_b1 & 8) { a302=1; } else{ a302=0; }
Oleg_B 0:d05a7158dbad 407 if(r_b1 & 16){ a303=1; } else{ a303=0; }
Oleg_B 0:d05a7158dbad 408 if(r_b1 & 32){ a304=1; } else{ a304=0; }
Oleg_B 0:d05a7158dbad 409 if(r_b1 & 64){ sm3_mov=1; } else{ sm3_mov=0; }
Oleg_B 0:d05a7158dbad 410 if(r_b1 & 128){ sm3_alr=1; } else{ sm3_alr=0; }
Oleg_B 0:d05a7158dbad 411 sm3_pos = r_b2*100+r_b3*10+r_b4;
Oleg_B 0:d05a7158dbad 412 }
Oleg_B 0:d05a7158dbad 413
Oleg_B 0:d05a7158dbad 414 // inps from SM ADR=4
Oleg_B 0:d05a7158dbad 415 if(r_id==64){
Oleg_B 0:d05a7158dbad 416 if(r_b1 & 4) { a401=1; } else{ a401=0; }
Oleg_B 0:d05a7158dbad 417 if(r_b1 & 8) { a402=1; } else{ a402=0; }
Oleg_B 0:d05a7158dbad 418 if(r_b1 & 16){ a403=1; } else{ a403=0; }
Oleg_B 0:d05a7158dbad 419 if(r_b1 & 32){ a404=1; } else{ a404=0; }
Oleg_B 0:d05a7158dbad 420 if(r_b1 & 64){ sm4_mov=1; } else{ sm4_mov=0; }
Oleg_B 0:d05a7158dbad 421 if(r_b1 & 128){ sm4_alr=1; } else{ sm4_alr=0; }
Oleg_B 0:d05a7158dbad 422 sm4_pos = r_b2*100+r_b3*10+r_b4;
Oleg_B 0:d05a7158dbad 423 }
Oleg_B 0:d05a7158dbad 424
Oleg_B 0:d05a7158dbad 425
Oleg_B 0:d05a7158dbad 426
Oleg_B 0:d05a7158dbad 427 }
Oleg_B 0:d05a7158dbad 428 }
Oleg_B 0:d05a7158dbad 429
Oleg_B 0:d05a7158dbad 430 }
Oleg_B 0:d05a7158dbad 431
Oleg_B 0:d05a7158dbad 432 //----------------------------------------
Oleg_B 0:d05a7158dbad 433 // сохранение значения переменных для обработки
Oleg_B 0:d05a7158dbad 434 // в след.цикле (установка, сброс)
Oleg_B 0:d05a7158dbad 435 void save_stat(){
Oleg_B 0:d05a7158dbad 436
Oleg_B 0:d05a7158dbad 437 mx01=x01; mx02=x02; mx03=x03; mx04=x04;
Oleg_B 0:d05a7158dbad 438 mx05=x05; mx06=x06; mx07=x07; mx08=x08;
Oleg_B 0:d05a7158dbad 439 mx09=x09; mx10=x10; mx11=x11; mx12=x12;
Oleg_B 0:d05a7158dbad 440 mx13=x13; mx14=x14; mx15=x15; mx16=x16;
Oleg_B 0:d05a7158dbad 441 my01=y01; my02=y02; my03=y03; my04=y04;
Oleg_B 0:d05a7158dbad 442 my05=y05; my06=y06; my07=y07; my08=y08;
Oleg_B 0:d05a7158dbad 443
Oleg_B 0:d05a7158dbad 444 mx101=x101; mx102=x102; mx103=x103; mx104=x104;
Oleg_B 0:d05a7158dbad 445 mx105=x105; mx106=x106; mx107=x107; mx108=x108;
Oleg_B 0:d05a7158dbad 446 mx201=x201; mx202=x202; mx203=x103; mx204=x104;
Oleg_B 0:d05a7158dbad 447 mx205=x205; mx206=x206; mx207=x107; mx208=x108;
Oleg_B 0:d05a7158dbad 448 mx301=x301; mx302=x302; mx303=x303; mx304=x304;
Oleg_B 0:d05a7158dbad 449 mx305=x305; mx306=x306; mx307=x307; mx308=x308;
Oleg_B 0:d05a7158dbad 450 mx401=x401; mx402=x402; mx403=x403; mx404=x404;
Oleg_B 0:d05a7158dbad 451 mx405=x405; mx406=x406; mx407=x407; mx408=x408;
Oleg_B 0:d05a7158dbad 452 my101=y101; my102=y102; my103=y103;
Oleg_B 0:d05a7158dbad 453 my104=y104; my105=y105; my106=y106;
Oleg_B 0:d05a7158dbad 454 my201=y201; my202=y202; my203=y203;
Oleg_B 0:d05a7158dbad 455 my204=y204; my205=y205; my206=y206;
Oleg_B 0:d05a7158dbad 456 my301=y301; my302=y302; my303=y303;
Oleg_B 0:d05a7158dbad 457 my304=y304; my305=y305; my306=y306;
Oleg_B 0:d05a7158dbad 458 my401=y401; my402=y402; my403=y403;
Oleg_B 0:d05a7158dbad 459 my404=y404; my405=y405; my406=y406;
Oleg_B 0:d05a7158dbad 460
Oleg_B 0:d05a7158dbad 461 ma101=a101; ma102=a102; ma103=a103; ma104=a104;
Oleg_B 0:d05a7158dbad 462 ma201=a201; ma202=a202; ma203=a203; ma204=a204;
Oleg_B 0:d05a7158dbad 463 ma301=a301; ma302=a302; ma303=a303; ma304=a304;
Oleg_B 0:d05a7158dbad 464 ma401=a401; ma402=a402; ma403=a403; ma404=a404;
Oleg_B 0:d05a7158dbad 465 mb101=b101; mb102=b102; mb201=b201; mb202=b202;
Oleg_B 0:d05a7158dbad 466 mb301=b301; mb302=b302; mb401=b401; mb402=b402;
Oleg_B 0:d05a7158dbad 467
Oleg_B 0:d05a7158dbad 468 msm_outs=sm_outs;
Oleg_B 0:d05a7158dbad 469 msm_position=sm_position;
Oleg_B 0:d05a7158dbad 470
Oleg_B 0:d05a7158dbad 471 mdm1_outs=dm1_outs;
Oleg_B 0:d05a7158dbad 472 mdm2_outs=dm2_outs;
Oleg_B 0:d05a7158dbad 473 mdm3_outs=dm3_outs;
Oleg_B 0:d05a7158dbad 474 mdm4_outs=dm4_outs;
Oleg_B 0:d05a7158dbad 475
Oleg_B 0:d05a7158dbad 476
Oleg_B 0:d05a7158dbad 477
Oleg_B 0:d05a7158dbad 478 }
Oleg_B 0:d05a7158dbad 479
Oleg_B 0:d05a7158dbad 480 //----------------------------------------
Oleg_B 0:d05a7158dbad 481 // изменение значения секундных и милисекундных таймеров
Oleg_B 0:d05a7158dbad 482 // (дискретность 10 мсек)
Oleg_B 0:d05a7158dbad 483 void timers(){
Oleg_B 0:d05a7158dbad 484
Oleg_B 0:d05a7158dbad 485 int i = 10 ; // время цикла в 10мсек
Oleg_B 0:d05a7158dbad 486 int max1=30000; // макс. значение для мсек таймеров (30мин)
Oleg_B 0:d05a7158dbad 487 int max2=18000; // макс. значение для сек таймеров (5час)
Oleg_B 0:d05a7158dbad 488
Oleg_B 0:d05a7158dbad 489 // timers, 10ms
Oleg_B 0:d05a7158dbad 490 if(t_sys<1000){
Oleg_B 0:d05a7158dbad 491 t_sys=t_sys+i;
Oleg_B 0:d05a7158dbad 492 }else{
Oleg_B 0:d05a7158dbad 493 t_sys = 0;
Oleg_B 0:d05a7158dbad 494 if(t101<max2){t101=t101+1;}
Oleg_B 0:d05a7158dbad 495 if(t102<max2){t102=t102+1;}
Oleg_B 0:d05a7158dbad 496 if(t103<max2){t103=t103+1;}
Oleg_B 0:d05a7158dbad 497 if(t104<max2){t104=t104+1;}
Oleg_B 0:d05a7158dbad 498 if(t105<max2){t105=t105+1;}
Oleg_B 0:d05a7158dbad 499 if(t106<max2){t106=t106+1;}
Oleg_B 0:d05a7158dbad 500 if(t107<max2){t107=t107+1;}
Oleg_B 0:d05a7158dbad 501 if(t108<max2){t108=t108+1;}
Oleg_B 0:d05a7158dbad 502 if(t109<max2){t109=t109+1;}
Oleg_B 0:d05a7158dbad 503 if(t110<max2){t110=t110+1;}
Oleg_B 0:d05a7158dbad 504 }
Oleg_B 0:d05a7158dbad 505
Oleg_B 0:d05a7158dbad 506 if(t01<max1){t01=t01+i;}
Oleg_B 0:d05a7158dbad 507 if(t02<max1){t02=t02+i;}
Oleg_B 0:d05a7158dbad 508 if(t03<max1){t03=t03+i;}
Oleg_B 0:d05a7158dbad 509 if(t04<max1){t04=t04+i;}
Oleg_B 0:d05a7158dbad 510 if(t05<max1){t05=t05+i;}
Oleg_B 0:d05a7158dbad 511 if(t06<max1){t06=t06+i;}
Oleg_B 0:d05a7158dbad 512 if(t07<max1){t07=t07+i;}
Oleg_B 0:d05a7158dbad 513 if(t08<max1){t08=t08+i;}
Oleg_B 0:d05a7158dbad 514 if(t09<max1){t09=t09+i;}
Oleg_B 0:d05a7158dbad 515 if(t10<max1){t10=t10+i;}
Oleg_B 0:d05a7158dbad 516
Oleg_B 0:d05a7158dbad 517 }
Oleg_B 0:d05a7158dbad 518
Oleg_B 0:d05a7158dbad 519 //----------------------------------------
Oleg_B 0:d05a7158dbad 520 // прием и отправка посылок
Oleg_B 0:d05a7158dbad 521 // по CAN шине
Oleg_B 0:d05a7158dbad 522 void can_rw()
Oleg_B 0:d05a7158dbad 523 {
Oleg_B 0:d05a7158dbad 524 CANMessage msg_w;
Oleg_B 0:d05a7158dbad 525 CANMessage msg_r;
Oleg_B 0:d05a7158dbad 526
Oleg_B 0:d05a7158dbad 527 for(int i=0; i<=10; i++){ r_mess_id[i]=0; w_mess_id[i]=0; }
Oleg_B 0:d05a7158dbad 528 for(int i=0; i<=40; i++){ r_mess_data[i]=0; w_mess_data[i]=0; }
Oleg_B 0:d05a7158dbad 529
Oleg_B 0:d05a7158dbad 530
Oleg_B 0:d05a7158dbad 531 while (true){
Oleg_B 0:d05a7158dbad 532
Oleg_B 0:d05a7158dbad 533 // проверяем, если есть новая посылка
Oleg_B 0:d05a7158dbad 534 // то помещаем в очередь can_save_rd()
Oleg_B 0:d05a7158dbad 535 if(can.read(msg_r)){
Oleg_B 0:d05a7158dbad 536 int id = msg_r.id;
Oleg_B 0:d05a7158dbad 537 char b1 = msg_r.data[0];
Oleg_B 0:d05a7158dbad 538 char b2 = msg_r.data[1];
Oleg_B 0:d05a7158dbad 539 char b3 = msg_r.data[2];
Oleg_B 0:d05a7158dbad 540 char b4 = msg_r.data[3];
Oleg_B 0:d05a7158dbad 541 can_save_rd(id,b1,b2,b3,b4);
Oleg_B 0:d05a7158dbad 542 led_b = !led_b;
Oleg_B 0:d05a7158dbad 543 }
Oleg_B 0:d05a7158dbad 544
Oleg_B 0:d05a7158dbad 545
Oleg_B 0:d05a7158dbad 546 // проверяем очередь на отправку can_select_wr()
Oleg_B 0:d05a7158dbad 547 // и если есть , то отправляем
Oleg_B 0:d05a7158dbad 548 if(can_select_wr()){
Oleg_B 0:d05a7158dbad 549 msg_w.len = 4;
Oleg_B 0:d05a7158dbad 550 msg_w.id = w_id;
Oleg_B 0:d05a7158dbad 551 msg_w.data[0] = w_b1;
Oleg_B 0:d05a7158dbad 552 msg_w.data[1] = w_b2;
Oleg_B 0:d05a7158dbad 553 msg_w.data[2] = w_b3;
Oleg_B 0:d05a7158dbad 554 msg_w.data[3] = w_b4;
Oleg_B 0:d05a7158dbad 555 can.write(msg_w);
Oleg_B 0:d05a7158dbad 556
Oleg_B 0:d05a7158dbad 557 if(can.tderror()){ led_b = !led_b; w_alr=1; } else { w_alr=0; }
Oleg_B 0:d05a7158dbad 558
Oleg_B 0:d05a7158dbad 559 }
Oleg_B 0:d05a7158dbad 560
Oleg_B 0:d05a7158dbad 561
Oleg_B 0:d05a7158dbad 562 Thread::wait(2);
Oleg_B 0:d05a7158dbad 563 }
Oleg_B 0:d05a7158dbad 564
Oleg_B 0:d05a7158dbad 565 }
Oleg_B 0:d05a7158dbad 566
Oleg_B 0:d05a7158dbad 567 //----------------------------------------
Oleg_B 0:d05a7158dbad 568 // посимвольный прием из USB и сохранение
Oleg_B 0:d05a7158dbad 569 // информации в буфере, обрабатывается после того как все принято
Oleg_B 0:d05a7158dbad 570 void pc_rd()
Oleg_B 0:d05a7158dbad 571 {
Oleg_B 0:d05a7158dbad 572 sprintf(pc_buffer, "buff_ok");
Oleg_B 0:d05a7158dbad 573 while(1)
Oleg_B 0:d05a7158dbad 574 {
Oleg_B 0:d05a7158dbad 575 if( pc.readable() )
Oleg_B 0:d05a7158dbad 576 {
Oleg_B 0:d05a7158dbad 577 pc_buffer[pc_i] = pc.getc();
Oleg_B 0:d05a7158dbad 578 if(pc_i<MAX_SERL){pc_i++; }
Oleg_B 0:d05a7158dbad 579 pc_buffer[pc_i]=0;
Oleg_B 0:d05a7158dbad 580 }
Oleg_B 0:d05a7158dbad 581 }
Oleg_B 0:d05a7158dbad 582 }
Oleg_B 0:d05a7158dbad 583
Oleg_B 0:d05a7158dbad 584 //----------------------------------------
Oleg_B 0:d05a7158dbad 585 // поиск и чтение значения из строки
Oleg_B 0:d05a7158dbad 586 // возвращает целое число.
Oleg_B 0:d05a7158dbad 587 int read_val() {
Oleg_B 0:d05a7158dbad 588 char inp_c[10];
Oleg_B 0:d05a7158dbad 589 int val=0;
Oleg_B 0:d05a7158dbad 590 int i=0;
Oleg_B 0:d05a7158dbad 591 int p=0;
Oleg_B 0:d05a7158dbad 592
Oleg_B 0:d05a7158dbad 593 while(p<MAX_SERL){
Oleg_B 0:d05a7158dbad 594 if(pc_buffer[p]=='='){ break ; }
Oleg_B 0:d05a7158dbad 595 p++;
Oleg_B 0:d05a7158dbad 596 }
Oleg_B 0:d05a7158dbad 597
Oleg_B 0:d05a7158dbad 598 p++;
Oleg_B 0:d05a7158dbad 599 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 ; }
Oleg_B 0:d05a7158dbad 600 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 ; }
Oleg_B 0:d05a7158dbad 601 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 ; }
Oleg_B 0:d05a7158dbad 602 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 ; }
Oleg_B 0:d05a7158dbad 603 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 ; }
Oleg_B 0:d05a7158dbad 604 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 ; }
Oleg_B 0:d05a7158dbad 605 inp_c[6]=0 ;
Oleg_B 0:d05a7158dbad 606
Oleg_B 0:d05a7158dbad 607 val = atoi(inp_c);
Oleg_B 0:d05a7158dbad 608 return val;
Oleg_B 0:d05a7158dbad 609 }
Oleg_B 0:d05a7158dbad 610
Oleg_B 0:d05a7158dbad 611
Oleg_B 0:d05a7158dbad 612
Oleg_B 0:d05a7158dbad 613
Oleg_B 0:d05a7158dbad 614
Oleg_B 0:d05a7158dbad 615
Oleg_B 0:d05a7158dbad 616
Oleg_B 0:d05a7158dbad 617
Oleg_B 0:d05a7158dbad 618
Oleg_B 0:d05a7158dbad 619
Oleg_B 0:d05a7158dbad 620
Oleg_B 0:d05a7158dbad 621
Oleg_B 0:d05a7158dbad 622
Oleg_B 0:d05a7158dbad 623