提交 | 用户 | age
|
483170
|
1 |
/** |
Q |
2 |
****************************************************************************** |
|
3 |
* @file : PLCfunctions.c |
|
4 |
* @brief : PLC funcstions program body |
|
5 |
****************************************************************************** |
|
6 |
*/ |
|
7 |
//#include "globaldef.h" |
|
8 |
#include "PLCfunctions.h" |
|
9 |
#include "string.h" |
|
10 |
#include "stm32f0xx_hal.h" |
|
11 |
#include <core_cmInstr.h> |
|
12 |
|
842bb6
|
13 |
#if (ENABLE_PLC) |
483170
|
14 |
extern __IO uint32_t uwTick; |
Q |
15 |
|
|
16 |
//unsigned short WDFs[TOTAL_WDFS]; |
|
17 |
|
|
18 |
inline unsigned int GetTick(void) |
|
19 |
{ |
|
20 |
// unsigned short Clk1=SysTick->VAL; |
|
21 |
return uwTick; |
|
22 |
} |
|
23 |
|
|
24 |
//unsigned char CurVAL; |
|
25 |
//unsigned char CurVALs[TOTAL_CurVAL]; |
|
26 |
//stTimer Timers[TOTALTIMERS]; |
|
27 |
|
|
28 |
//unsigned short WX[13]; |
|
29 |
//unsigned short WY[13]; |
|
30 |
//unsigned short WR[64]; |
|
31 |
//unsigned short DT[256]; |
|
32 |
|
|
33 |
//unsigned short SDT[256]; |
|
34 |
|
|
35 |
stPLCMem PLCMem; |
|
36 |
|
|
37 |
int InitTimer(int nIndex, int nType) |
|
38 |
{ |
|
39 |
if (nIndex >= TOTALTIMERS) return -1; |
842bb6
|
40 |
PLCMem.Timers[nIndex].StatByte = 0x0010 | nType; |
483170
|
41 |
// Timers[nIndex].nType = 0; |
842bb6
|
42 |
PLCMem.SV[nIndex] = 0; |
Q |
43 |
PLCMem.EV[nIndex] = 0; |
|
44 |
PLCMem.Timers[nIndex].LastActTime = GetTick(); |
483170
|
45 |
return 0; |
Q |
46 |
} |
|
47 |
|
|
48 |
int RunTimer(int nIndex , int SV) |
|
49 |
{ |
|
50 |
if (nIndex >= TOTALTIMERS) return -1; |
842bb6
|
51 |
if (!PLCMem.Timers[nIndex].bSet) |
483170
|
52 |
{ |
842bb6
|
53 |
PLCMem.SV[nIndex] = SV; |
Q |
54 |
PLCMem.EV[nIndex]= 0; |
|
55 |
PLCMem.Timers[nIndex].LastActTime = GetTick(); |
|
56 |
PLCMem.Timers[nIndex].bSet = 1; |
483170
|
57 |
} |
Q |
58 |
return 0; |
|
59 |
} |
|
60 |
|
|
61 |
int StopTimer(int nIndex) |
|
62 |
{ |
|
63 |
if (nIndex >= TOTALTIMERS) return -1; |
842bb6
|
64 |
if (PLCMem.Timers[nIndex].bSet) |
483170
|
65 |
{ |
842bb6
|
66 |
PLCMem.EV[nIndex] = 0; |
Q |
67 |
PLCMem.Timers[nIndex].LastActTime = GetTick(); |
|
68 |
PLCMem.Timers[nIndex].bSet = 0; |
483170
|
69 |
} |
Q |
70 |
return 0; |
|
71 |
} |
|
72 |
int ResetTimer(int nIndex) |
|
73 |
{ |
|
74 |
if (nIndex >= TOTALTIMERS) return -1; |
842bb6
|
75 |
PLCMem.EV[nIndex] = 0; |
Q |
76 |
PLCMem.Timers[nIndex].bTon = 0; |
|
77 |
PLCMem.Timers[nIndex].LastActTime=GetTick(); |
483170
|
78 |
return 0; |
Q |
79 |
} |
|
80 |
|
|
81 |
int SetTimerValue(int nIndex, int bSet, int SV) |
|
82 |
{ |
|
83 |
if (nIndex >= TOTALTIMERS) return -1; |
|
84 |
if (bSet) {RunTimer(nIndex, SV);} |
|
85 |
else {StopTimer(nIndex);} |
842bb6
|
86 |
return PLCMem.Timers[nIndex].bTon; |
483170
|
87 |
} |
Q |
88 |
|
|
89 |
int ProcessTimer(int nIndex) |
|
90 |
{ |
|
91 |
if (nIndex >= TOTALTIMERS) return -1; |
842bb6
|
92 |
if (!PLCMem.Timers[nIndex].nInited) return 0; |
Q |
93 |
if (PLCMem.Timers[nIndex].bSet) // bSet =1; |
483170
|
94 |
{ |
842bb6
|
95 |
if (!PLCMem.Timers[nIndex].bTon) |
483170
|
96 |
{ |
Q |
97 |
int nScale = TICK_OF_MS; |
842bb6
|
98 |
if (PLCMem.Timers[nIndex].nScale == 0) |
483170
|
99 |
{nScale = TICK_OF_MS; |
842bb6
|
100 |
}else if (PLCMem.Timers[nIndex].nScale == 1) |
483170
|
101 |
{nScale = TICK_OF_RS; |
842bb6
|
102 |
}else if (PLCMem.Timers[nIndex].nScale == 2) |
483170
|
103 |
{nScale = TICK_OF_XS; |
842bb6
|
104 |
}else if (PLCMem.Timers[nIndex].nScale == 3) |
483170
|
105 |
{nScale = TICK_OF_YS; |
Q |
106 |
}else {} |
|
107 |
|
842bb6
|
108 |
int TimeDiff = GetTick() - PLCMem.Timers[nIndex].LastActTime; |
Q |
109 |
if (TimeDiff < 0) { TimeDiff = nScale;} |
483170
|
110 |
if (TimeDiff >= nScale) |
Q |
111 |
{ |
|
112 |
int TimeDiffmS = TimeDiff / nScale; |
842bb6
|
113 |
unsigned short NextEV = PLCMem.EV[nIndex] + TimeDiffmS; |
Q |
114 |
PLCMem.Timers[nIndex].LastActTime += TimeDiffmS*nScale; |
483170
|
115 |
|
842bb6
|
116 |
if (NextEV >= PLCMem.SV[nIndex]) |
483170
|
117 |
{ |
842bb6
|
118 |
NextEV = PLCMem.SV[nIndex]; |
Q |
119 |
PLCMem.Timers[nIndex].bTon =1; |
483170
|
120 |
} |
842bb6
|
121 |
PLCMem.EV[nIndex] = NextEV; |
483170
|
122 |
} |
Q |
123 |
} |
|
124 |
}else //bSet=0; |
|
125 |
{ |
842bb6
|
126 |
if(PLCMem.Timers[nIndex].bTon) |
483170
|
127 |
{ |
842bb6
|
128 |
PLCMem.Timers[nIndex].bTon = 0; |
483170
|
129 |
} |
Q |
130 |
} |
842bb6
|
131 |
SetCoilValue(KLCoilTypeT, nIndex, PLCMem.Timers[nIndex].bTon); |
Q |
132 |
return PLCMem.Timers[nIndex].bTon; |
483170
|
133 |
} |
Q |
134 |
|
|
135 |
int IsTimerOn(int nIndex) |
|
136 |
{ |
|
137 |
if (nIndex >= TOTALTIMERS) return 0; |
|
138 |
ProcessTimer(nIndex); |
842bb6
|
139 |
return PLCMem.Timers[nIndex].bTon; |
483170
|
140 |
|
Q |
141 |
} |
|
142 |
|
|
143 |
int GetTimerSV(int nIndex) |
|
144 |
{ |
|
145 |
if (nIndex >= TOTALTIMERS) return 0; |
|
146 |
// ProcessTimer(nIndex); |
842bb6
|
147 |
return PLCMem.SV[nIndex]; |
483170
|
148 |
// return 0; |
Q |
149 |
} |
|
150 |
int GetTimerEV(int nIndex) |
|
151 |
{ |
|
152 |
if (nIndex >= TOTALTIMERS) return 0; |
|
153 |
// ProcessTimer(nIndex); |
842bb6
|
154 |
return PLCMem.EV[nIndex]; |
483170
|
155 |
// return 0; |
Q |
156 |
} |
|
157 |
|
|
158 |
int PushInVal(void) |
|
159 |
{ |
|
160 |
for (int i=TOTAL_CurVAL -1 ;i>0;i--) |
|
161 |
{ |
842bb6
|
162 |
PLCMem.CurVALs[i]=PLCMem.CurVALs[i-1]; |
483170
|
163 |
} |
842bb6
|
164 |
PLCMem.CurVALs[0]=PLCMem.CurVAL; |
Q |
165 |
return PLCMem.CurVAL; |
483170
|
166 |
} |
Q |
167 |
|
|
168 |
int PopOutVal(void) |
|
169 |
{ |
842bb6
|
170 |
unsigned char theVAL=PLCMem.CurVALs[0]; |
483170
|
171 |
for (int i=0;i<TOTAL_CurVAL-1;i++) |
Q |
172 |
{ |
842bb6
|
173 |
PLCMem.CurVALs[i]=PLCMem.CurVALs[i+1]; |
483170
|
174 |
} |
Q |
175 |
return theVAL; |
|
176 |
} |
|
177 |
|
|
178 |
|
|
179 |
int InitPLC() |
|
180 |
{ |
|
181 |
PLCMem.nScanCount=0; |
|
182 |
for (int i=0;i<256;i++){PLCMem.ProgTrace[i]=0;} |
|
183 |
for (int i=0;i<16;i++) { |
|
184 |
KMem.WR[i]=0; |
|
185 |
} |
|
186 |
for (int i=0;i<256;i++) { |
|
187 |
KMem.DT[i]=0; |
|
188 |
} |
|
189 |
return 0; |
|
190 |
} |
|
191 |
|
|
192 |
int StartPLC() |
|
193 |
{ |
|
194 |
PLCMem.nScanCount = 0; |
|
195 |
for (int i=0;i<256;i++){PLCMem.ProgTrace[i]=0;} |
|
196 |
for (int i=0;i<16;i++) { |
|
197 |
KMem.WR[i]=0; |
|
198 |
} |
|
199 |
for (int i=0;i<256;i++) { |
|
200 |
KMem.DT[i]=0; |
|
201 |
} |
|
202 |
for (int i=0;i<TOTALTIMERS;i++){ |
842bb6
|
203 |
PLCMem.Timers[i].nInited=0; |
483170
|
204 |
} |
Q |
205 |
|
|
206 |
PLCMem.bPLCRunning=1; |
|
207 |
KMRunStat.WorkMode2=PLCMem.bPLCRunning; |
|
208 |
return 0; |
|
209 |
} |
|
210 |
|
|
211 |
int StopPLC() |
|
212 |
{ |
|
213 |
PLCMem.bPLCRunning=0; |
|
214 |
for (int i=0;i<KLDataWXCount;i++) KMem.WY[i]=0; |
|
215 |
for (int i=0;i<KLDataWLCount;i++) KMem.WLY[i]=0; |
842bb6
|
216 |
KMRunStat.WorkMode2=PLCMem.bPLCRunning; |
483170
|
217 |
return 0; |
Q |
218 |
} |
|
219 |
|
|
220 |
inline void SetAddrBit(unsigned short * pW, unsigned char bitAddr) |
|
221 |
{ |
|
222 |
(*pW)|=1<<(bitAddr&0xf); |
|
223 |
} |
|
224 |
|
|
225 |
inline void ResetBit(unsigned short * pW, unsigned char bitAddr) |
|
226 |
{ |
|
227 |
(*pW)&=~(1<<(bitAddr&0xf)); |
|
228 |
} |
|
229 |
static inline void SetBitValue(unsigned short * pW, unsigned char bitAddr, unsigned char Value) |
|
230 |
{ |
|
231 |
if (Value) { SetAddrBit(pW, bitAddr);} |
|
232 |
else {ResetBit(pW, bitAddr);} |
|
233 |
} |
|
234 |
|
|
235 |
static inline unsigned char GetBitValue(unsigned short W, unsigned char bitAddr) |
|
236 |
{ |
|
237 |
if (W&(1<<(bitAddr&0xf))) return 1; |
|
238 |
else return 0; |
|
239 |
} |
|
240 |
|
|
241 |
|
842bb6
|
242 |
int ProcessPLCBinProg(const stBinInstrcn1 * pBinProg, int nProgSteps) |
483170
|
243 |
{ |
Q |
244 |
if (!PLCMem.bPLCRunning) return 0; |
|
245 |
|
|
246 |
if (PLCMem.nScanCount == 0) { |
|
247 |
SetCoilValue(KLCoilTypeSR, 0, 0); |
|
248 |
SetCoilValue(KLCoilTypeSR, 1, 1); |
842bb6
|
249 |
SetCoilValue(KLCoilTypeSR, 10, 1); |
Q |
250 |
SetCoilValue(KLCoilTypeSR, 11, 0); |
|
251 |
SetCoilValue(KLCoilTypeSR, 13, 1); |
483170
|
252 |
} |
Q |
253 |
else |
|
254 |
{ |
|
255 |
SetCoilValue(KLCoilTypeSR, 0, 0); |
|
256 |
SetCoilValue(KLCoilTypeSR, 1, 1); |
842bb6
|
257 |
SetCoilValue(KLCoilTypeSR, 10, 1); |
Q |
258 |
SetCoilValue(KLCoilTypeSR, 11, 0); |
|
259 |
SetCoilValue(KLCoilTypeSR, 13, 0); |
483170
|
260 |
} |
Q |
261 |
for (int i = 0; i < TOTAL_CurVAL; i++) { |
842bb6
|
262 |
PLCMem.CurVALs[i] = 0; |
483170
|
263 |
} |
Q |
264 |
int CurPos = 0; |
842bb6
|
265 |
// stBinInstrcn1 * pBinInstrcn1; |
Q |
266 |
stBinInstrcn15 * pBinInstrcn15; |
|
267 |
stBinInstrcn2 * pBinInstrcn2; |
|
268 |
stBinInstrcn3 * pBinInstrcn3; |
483170
|
269 |
|
Q |
270 |
int lastScanInputVal = 1;//??????,????????,? ?? ??? |
|
271 |
|
842bb6
|
272 |
const stBinInstrcn1 * pBinInstrcn = pBinProg; |
Q |
273 |
|
|
274 |
while (CurPos < nProgSteps) |
483170
|
275 |
{ |
Q |
276 |
unsigned int nNextPos = 1; |
842bb6
|
277 |
unsigned int thisOP = pBinInstrcn[CurPos].nOp; |
483170
|
278 |
// unsigned int nParamCount = 0 |
842bb6
|
279 |
unsigned char thisAddrType = pBinInstrcn[CurPos].nParamType; |
Q |
280 |
unsigned short thisAddr = pBinInstrcn[CurPos].nParamAddr; |
483170
|
281 |
|
Q |
282 |
|
|
283 |
switch (thisOP) |
|
284 |
{ |
|
285 |
// case OP_NONE: |
|
286 |
// break; |
|
287 |
case OP_NOP: |
|
288 |
break; |
|
289 |
//??? ?? |
842bb6
|
290 |
case OP_END: |
Q |
291 |
nNextPos = nProgSteps; |
|
292 |
break; |
483170
|
293 |
case OP_NOT: |
Q |
294 |
case OP_ANS: |
|
295 |
case OP_ORS: |
|
296 |
case OP_PSHS: |
|
297 |
case OP_RDS: |
|
298 |
case OP_POPS: |
|
299 |
case OP_DF: |
|
300 |
case OP_DF_: |
|
301 |
switch (thisOP) |
|
302 |
{ |
|
303 |
case OP_NOT: |
842bb6
|
304 |
PLCMem.CurVAL = !PLCMem.CurVAL; |
483170
|
305 |
break; |
Q |
306 |
case OP_ANS: |
842bb6
|
307 |
PLCMem.CurVAL = PopOutVal() && PLCMem.CurVAL; |
483170
|
308 |
break; |
Q |
309 |
case OP_ORS: |
842bb6
|
310 |
PLCMem.CurVAL = PopOutVal() || PLCMem.CurVAL; |
483170
|
311 |
break; |
Q |
312 |
case OP_PSHS: |
|
313 |
PushInVal(); |
|
314 |
break; |
|
315 |
case OP_RDS: |
842bb6
|
316 |
PLCMem.CurVAL = PLCMem.CurVALs[0] != 0; |
483170
|
317 |
break; |
Q |
318 |
case OP_POPS: |
842bb6
|
319 |
PLCMem.CurVAL = PopOutVal(); |
483170
|
320 |
break; |
Q |
321 |
case OP_DF: |
842bb6
|
322 |
PLCMem.CurVAL = PLCMem.CurVAL && !lastScanInputVal; |
483170
|
323 |
break; |
Q |
324 |
case OP_DF_: |
842bb6
|
325 |
PLCMem.CurVAL = !PLCMem.CurVAL && lastScanInputVal; |
483170
|
326 |
break; |
Q |
327 |
|
|
328 |
default: |
|
329 |
break; |
|
330 |
} |
|
331 |
break; |
|
332 |
// 1???? |
|
333 |
case OP_ST: |
|
334 |
case OP_ST_: |
|
335 |
case OP_AN: |
|
336 |
case OP_AN_: |
|
337 |
case OP_OR: |
|
338 |
case OP_OR_: |
|
339 |
switch (thisOP) |
|
340 |
{ |
|
341 |
case OP_ST: |
|
342 |
PushInVal(); |
842bb6
|
343 |
PLCMem.CurVAL = GetCoilValue(thisAddrType, thisAddr); |
483170
|
344 |
break; |
Q |
345 |
case OP_ST_: |
|
346 |
PushInVal(); |
842bb6
|
347 |
PLCMem.CurVAL = !GetCoilValue(thisAddrType, thisAddr); |
483170
|
348 |
break; |
Q |
349 |
case OP_AN: |
842bb6
|
350 |
PLCMem.CurVAL = PLCMem.CurVAL&&GetCoilValue(thisAddrType, thisAddr); |
483170
|
351 |
break; |
Q |
352 |
case OP_AN_: |
842bb6
|
353 |
PLCMem.CurVAL = PLCMem.CurVAL && (!GetCoilValue(thisAddrType, thisAddr)); |
483170
|
354 |
break; |
Q |
355 |
case OP_OR: |
842bb6
|
356 |
PLCMem.CurVAL = PLCMem.CurVAL || GetCoilValue(thisAddrType, thisAddr); |
483170
|
357 |
break; |
Q |
358 |
case OP_OR_: |
842bb6
|
359 |
PLCMem.CurVAL = PLCMem.CurVAL || (!GetCoilValue(thisAddrType, thisAddr)); |
483170
|
360 |
break; |
Q |
361 |
default: |
|
362 |
break; |
|
363 |
} |
|
364 |
break; |
|
365 |
// 1 ?? ?? |
|
366 |
case OP_OUT: |
|
367 |
case OP_SET: |
|
368 |
case OP_RESET: |
|
369 |
switch (thisOP) |
|
370 |
{ |
|
371 |
case OP_OUT: |
842bb6
|
372 |
SetCoilValue(thisAddrType, thisAddr, PLCMem.CurVAL); |
483170
|
373 |
break; |
Q |
374 |
case OP_SET: |
842bb6
|
375 |
if (PLCMem.CurVAL) SetCoilValue(thisAddrType, thisAddr, 1); |
483170
|
376 |
break; |
Q |
377 |
case OP_RESET: |
842bb6
|
378 |
if (PLCMem.CurVAL) SetCoilValue(thisAddrType, thisAddr, 0); |
483170
|
379 |
break; |
Q |
380 |
default: |
|
381 |
break; |
|
382 |
} |
|
383 |
break; |
|
384 |
// ???? |
|
385 |
case OP_ST_EQ: |
|
386 |
case OP_ST_NE: |
|
387 |
case OP_ST_LT: |
|
388 |
case OP_ST_GT: |
|
389 |
case OP_ST_LE: |
|
390 |
case OP_ST_GE: |
|
391 |
case OP_AN_EQ: |
|
392 |
case OP_AN_NE: |
|
393 |
case OP_AN_LT: |
|
394 |
case OP_AN_GT: |
|
395 |
case OP_AN_LE: |
|
396 |
case OP_AN_GE: |
|
397 |
case OP_OR_EQ: |
|
398 |
case OP_OR_NE: |
|
399 |
case OP_OR_LT: |
|
400 |
case OP_OR_GT: |
|
401 |
case OP_OR_LE: |
|
402 |
case OP_OR_GE: |
842bb6
|
403 |
pBinInstrcn2 = (stBinInstrcn2 *)&pBinInstrcn[CurPos]; |
Q |
404 |
thisAddrType = pBinInstrcn2->nParamType1; |
483170
|
405 |
|
Q |
406 |
switch (thisOP) |
|
407 |
{ |
|
408 |
case OP_ST_EQ: |
|
409 |
PushInVal(); |
842bb6
|
410 |
PLCMem.CurVAL = (GetVarData(thisAddrType, thisAddr) == GetVarData(pBinInstrcn2->nParamType2, pBinInstrcn2->nParamAddr2)); |
483170
|
411 |
break; |
Q |
412 |
case OP_ST_NE: |
|
413 |
PushInVal(); |
842bb6
|
414 |
PLCMem.CurVAL = (GetVarData(thisAddrType, thisAddr) != GetVarData(pBinInstrcn2->nParamType2, pBinInstrcn2->nParamAddr2)); |
483170
|
415 |
break; |
Q |
416 |
case OP_ST_LT: |
|
417 |
PushInVal(); |
842bb6
|
418 |
PLCMem.CurVAL = (GetVarData(thisAddrType, thisAddr) < GetVarData(pBinInstrcn2->nParamType2, pBinInstrcn2->nParamAddr2)); |
483170
|
419 |
break; |
Q |
420 |
case OP_ST_GT: |
|
421 |
PushInVal(); |
842bb6
|
422 |
PLCMem.CurVAL = (GetVarData(thisAddrType, thisAddr) > GetVarData(pBinInstrcn2->nParamType2, pBinInstrcn2->nParamAddr2)); |
483170
|
423 |
break; |
Q |
424 |
case OP_ST_LE: |
|
425 |
PushInVal(); |
842bb6
|
426 |
PLCMem.CurVAL = (GetVarData(thisAddrType, thisAddr) <= GetVarData(pBinInstrcn2->nParamType2, pBinInstrcn2->nParamAddr2)); |
483170
|
427 |
break; |
Q |
428 |
case OP_ST_GE: |
|
429 |
PushInVal(); |
842bb6
|
430 |
PLCMem.CurVAL = (GetVarData(thisAddrType, thisAddr) >= GetVarData(pBinInstrcn2->nParamType2, pBinInstrcn2->nParamAddr2)); |
483170
|
431 |
break; |
Q |
432 |
case OP_AN_EQ: |
842bb6
|
433 |
PLCMem.CurVAL = PLCMem.CurVAL && (GetVarData(thisAddrType, thisAddr) == GetVarData(pBinInstrcn2->nParamType2, pBinInstrcn2->nParamAddr2)); |
483170
|
434 |
break; |
Q |
435 |
case OP_AN_NE: |
842bb6
|
436 |
PLCMem.CurVAL = PLCMem.CurVAL && (GetVarData(thisAddrType, thisAddr) != GetVarData(pBinInstrcn2->nParamType2, pBinInstrcn2->nParamAddr2)); |
483170
|
437 |
break; |
Q |
438 |
case OP_AN_LT: |
842bb6
|
439 |
PLCMem.CurVAL = PLCMem.CurVAL && (GetVarData(thisAddrType, thisAddr) < GetVarData(pBinInstrcn2->nParamType2, pBinInstrcn2->nParamAddr2)); |
483170
|
440 |
break; |
Q |
441 |
case OP_AN_GT: |
842bb6
|
442 |
PLCMem.CurVAL = PLCMem.CurVAL && (GetVarData(thisAddrType, thisAddr) > GetVarData(pBinInstrcn2->nParamType2, pBinInstrcn2->nParamAddr2)); |
483170
|
443 |
break; |
Q |
444 |
case OP_AN_LE: |
842bb6
|
445 |
PLCMem.CurVAL = PLCMem.CurVAL && (GetVarData(thisAddrType, thisAddr) <= GetVarData(pBinInstrcn2->nParamType2, pBinInstrcn2->nParamAddr2)); |
483170
|
446 |
break; |
Q |
447 |
case OP_AN_GE: |
842bb6
|
448 |
PLCMem.CurVAL = PLCMem.CurVAL && (GetVarData(thisAddrType, thisAddr) >= GetVarData(pBinInstrcn2->nParamType2, pBinInstrcn2->nParamAddr2)); |
483170
|
449 |
break; |
Q |
450 |
|
|
451 |
case OP_OR_EQ: |
842bb6
|
452 |
PLCMem.CurVAL = PLCMem.CurVAL || (GetVarData(thisAddrType, thisAddr) == GetVarData(pBinInstrcn2->nParamType2, pBinInstrcn2->nParamAddr2)); |
483170
|
453 |
break; |
Q |
454 |
case OP_OR_NE: |
842bb6
|
455 |
PLCMem.CurVAL = PLCMem.CurVAL || (GetVarData(thisAddrType, thisAddr) != GetVarData(pBinInstrcn2->nParamType2, pBinInstrcn2->nParamAddr2)); |
483170
|
456 |
break; |
Q |
457 |
case OP_OR_LT: |
842bb6
|
458 |
PLCMem.CurVAL = PLCMem.CurVAL || (GetVarData(thisAddrType, thisAddr) < GetVarData(pBinInstrcn2->nParamType2, pBinInstrcn2->nParamAddr2)); |
483170
|
459 |
break; |
Q |
460 |
case OP_OR_GT: |
842bb6
|
461 |
PLCMem.CurVAL = PLCMem.CurVAL || (GetVarData(thisAddrType, thisAddr) > GetVarData(pBinInstrcn2->nParamType2, pBinInstrcn2->nParamAddr2)); |
483170
|
462 |
break; |
Q |
463 |
case OP_OR_LE: |
842bb6
|
464 |
PLCMem.CurVAL = PLCMem.CurVAL || (GetVarData(thisAddrType, thisAddr) <= GetVarData(pBinInstrcn2->nParamType2, pBinInstrcn2->nParamAddr2)); |
483170
|
465 |
break; |
Q |
466 |
case OP_OR_GE: |
842bb6
|
467 |
PLCMem.CurVAL = PLCMem.CurVAL || (GetVarData(thisAddrType, thisAddr) >= GetVarData(pBinInstrcn2->nParamType2, pBinInstrcn2->nParamAddr2)); |
483170
|
468 |
break; |
Q |
469 |
|
|
470 |
default: |
|
471 |
break; |
|
472 |
} |
|
473 |
nNextPos = 2; |
|
474 |
break; |
|
475 |
// ??? |
|
476 |
case OP_TML: |
|
477 |
case OP_TMR: |
|
478 |
case OP_TMX: |
|
479 |
case OP_TMY: |
842bb6
|
480 |
pBinInstrcn15 = (stBinInstrcn15 *)(&pBinInstrcn[CurPos]); |
483170
|
481 |
{ |
842bb6
|
482 |
unsigned char thisNum= pBinInstrcn15->nOpNum; |
Q |
483 |
thisAddrType = pBinInstrcn15->nParamType1; |
|
484 |
thisAddr = pBinInstrcn15->nParamAddr1; |
483170
|
485 |
switch (thisOP) |
Q |
486 |
{ |
|
487 |
case OP_TML: |
842bb6
|
488 |
if (!PLCMem.Timers[thisNum].nInited) InitTimer(thisNum, 0); |
Q |
489 |
if (PLCMem.CurVAL) RunTimer(thisNum, GetVarData(thisAddrType, thisAddr)); |
483170
|
490 |
else StopTimer(thisNum); |
842bb6
|
491 |
PLCMem.CurVAL = ProcessTimer(thisNum); |
483170
|
492 |
|
Q |
493 |
break; |
|
494 |
case OP_TMR: |
842bb6
|
495 |
if (!PLCMem.Timers[thisNum].nInited) InitTimer(thisNum, 1); |
Q |
496 |
if (PLCMem.CurVAL) RunTimer(thisNum, GetVarData(thisAddrType, thisAddr)); |
483170
|
497 |
else StopTimer(thisNum); |
842bb6
|
498 |
PLCMem.CurVAL = ProcessTimer(thisNum); |
483170
|
499 |
break; |
Q |
500 |
case OP_TMX: |
842bb6
|
501 |
if (!PLCMem.Timers[thisNum].nInited) InitTimer(thisNum, 2); |
Q |
502 |
if (PLCMem.CurVAL) RunTimer(thisNum, GetVarData(thisAddrType, thisAddr)); |
483170
|
503 |
else StopTimer(thisNum); |
842bb6
|
504 |
PLCMem.CurVAL = ProcessTimer(thisNum); |
483170
|
505 |
|
Q |
506 |
break; |
|
507 |
case OP_TMY: |
842bb6
|
508 |
if (!PLCMem.Timers[thisNum].nInited) InitTimer(thisNum, 3); |
Q |
509 |
if (PLCMem.CurVAL) RunTimer(thisNum, GetVarData(thisAddrType, thisAddr)); |
483170
|
510 |
else StopTimer(thisNum); |
842bb6
|
511 |
PLCMem.CurVAL = ProcessTimer(thisNum); |
483170
|
512 |
break; |
Q |
513 |
default: |
|
514 |
break; |
|
515 |
} |
|
516 |
|
|
517 |
} |
|
518 |
nNextPos = 2; |
|
519 |
break; |
|
520 |
// 1 ?????? |
|
521 |
case OP_INC: |
|
522 |
case OP_DEC: |
842bb6
|
523 |
pBinInstrcn15 = (stBinInstrcn15 *)(&pBinInstrcn[CurPos]); |
Q |
524 |
thisAddrType = pBinInstrcn15->nParamType1; |
|
525 |
thisAddr = pBinInstrcn15->nParamAddr1; |
483170
|
526 |
nNextPos = 2; |
Q |
527 |
switch (thisOP) |
|
528 |
{ |
|
529 |
case OP_INC: |
842bb6
|
530 |
if (PLCMem.CurVAL) SetVarData(thisAddrType, thisAddr, GetVarData(thisAddrType, thisAddr) + 1); |
483170
|
531 |
break; |
Q |
532 |
case OP_DEC: |
842bb6
|
533 |
if (PLCMem.CurVAL) SetVarData(thisAddrType, thisAddr, GetVarData(thisAddrType, thisAddr) - 1); |
483170
|
534 |
break; |
Q |
535 |
|
|
536 |
default: |
|
537 |
break; |
|
538 |
} |
|
539 |
break; |
|
540 |
// 2?????? |
|
541 |
case OP_MV: |
|
542 |
case OP_ADD2: |
|
543 |
case OP_SUB2: |
842bb6
|
544 |
pBinInstrcn2 = (stBinInstrcn2 *)(&pBinInstrcn[CurPos]); |
483170
|
545 |
{ |
Q |
546 |
int nParamType2, nParamAddr2; |
842bb6
|
547 |
thisAddrType = pBinInstrcn2->nParamType1; |
Q |
548 |
thisAddr = pBinInstrcn2->nParamAddr1; |
|
549 |
nParamType2 = pBinInstrcn2->nParamType2; |
|
550 |
nParamAddr2 = pBinInstrcn2->nParamAddr2; |
483170
|
551 |
|
Q |
552 |
switch (thisOP) |
|
553 |
{ |
|
554 |
case OP_MV: |
842bb6
|
555 |
if (PLCMem.CurVAL) SetVarData(nParamType2, nParamAddr2, GetVarData(thisAddrType, thisAddr)); |
483170
|
556 |
break; |
Q |
557 |
case OP_ADD2: |
842bb6
|
558 |
if (PLCMem.CurVAL) SetVarData(nParamType2, nParamAddr2, GetVarData(thisAddrType, thisAddr) + GetVarData(nParamType2, nParamAddr2)); |
483170
|
559 |
break; |
Q |
560 |
case OP_SUB2: |
842bb6
|
561 |
if (PLCMem.CurVAL) SetVarData(nParamType2, nParamAddr2, GetVarData(nParamType2, nParamAddr2) - GetVarData(thisAddrType, thisAddr)); |
483170
|
562 |
break; |
Q |
563 |
|
|
564 |
default: |
|
565 |
break; |
|
566 |
} |
|
567 |
|
|
568 |
} |
|
569 |
nNextPos = 2; |
|
570 |
break; |
|
571 |
// 3 ?????? |
|
572 |
case OP_ADD3: |
|
573 |
case OP_SUB3: |
|
574 |
case OP_MUL: |
|
575 |
case OP_DIV: |
842bb6
|
576 |
pBinInstrcn3 = (stBinInstrcn3 *)(&pBinInstrcn[CurPos]); |
483170
|
577 |
int nParamType2, nParamAddr2; |
Q |
578 |
int nParamType3, nParamAddr3; |
842bb6
|
579 |
thisAddrType = pBinInstrcn3->nParamType1; |
Q |
580 |
thisAddr = pBinInstrcn3->nParamAddr1; |
|
581 |
nParamType2 = pBinInstrcn3->nParamType2; |
|
582 |
nParamAddr2 = pBinInstrcn3->nParamAddr2; |
|
583 |
nParamType3 = pBinInstrcn3->nParamType3; |
|
584 |
nParamAddr3 = pBinInstrcn3->nParamAddr3; |
483170
|
585 |
switch (thisOP) |
Q |
586 |
{ |
|
587 |
case OP_ADD3: |
842bb6
|
588 |
if (PLCMem.CurVAL) SetVarData(nParamType3, nParamAddr3, GetVarData(thisAddrType, thisAddr) + GetVarData(nParamType2, nParamAddr2)); |
483170
|
589 |
break; |
Q |
590 |
case OP_SUB3: |
842bb6
|
591 |
if (PLCMem.CurVAL) SetVarData(nParamType3, nParamAddr3, GetVarData(thisAddrType, thisAddr) - GetVarData(nParamType2, nParamAddr2)); |
483170
|
592 |
break; |
Q |
593 |
case OP_MUL: |
842bb6
|
594 |
if (PLCMem.CurVAL) { |
483170
|
595 |
short multiplicand = GetVarData(thisAddrType, thisAddr); |
Q |
596 |
short multiplier = GetVarData(nParamType2, nParamAddr2); |
|
597 |
int product = multiplicand * multiplier; |
|
598 |
SetVarData(nParamType3, nParamAddr3, product); |
|
599 |
SetVarData(nParamType3, nParamAddr3 + 1, product >> 16); |
|
600 |
} |
|
601 |
break; |
|
602 |
case OP_DIV: |
842bb6
|
603 |
if (PLCMem.CurVAL) { |
483170
|
604 |
short dividend = GetVarData(thisAddrType, thisAddr); |
Q |
605 |
short divisor = GetVarData(nParamType2, nParamAddr2); |
|
606 |
short quotient = dividend / divisor; |
|
607 |
short remainder = dividend % divisor; |
|
608 |
SetVarData(nParamType3, nParamAddr3, quotient); |
|
609 |
SetVarData(nParamType3, nParamAddr3 + 1, remainder); |
|
610 |
} |
|
611 |
break; |
|
612 |
|
|
613 |
default: |
|
614 |
break; |
|
615 |
} |
|
616 |
nNextPos = 3; |
|
617 |
break; |
|
618 |
|
|
619 |
default: |
|
620 |
break; |
|
621 |
} |
842bb6
|
622 |
lastScanInputVal = GetBitValue( PLCMem.ProgTrace[CurPos>>4],CurPos&0xf); //GetBitValue(PLCMem.WDFs); |
Q |
623 |
SetBitValue( &PLCMem.ProgTrace[CurPos>>4],CurPos&0xf, PLCMem.CurVAL); |
483170
|
624 |
|
842bb6
|
625 |
// lastScanInputVal = PLCMem.ProgTrace[CurPos]; //GetBitValue(PLCMem.WDFs); |
Q |
626 |
// PLCMem.ProgTrace[CurPos] = PLCMem.CurVAL; |
483170
|
627 |
CurPos += nNextPos; |
Q |
628 |
} |
|
629 |
PLCMem.nScanCount++; |
842bb6
|
630 |
return CurPos; |
483170
|
631 |
} |
842bb6
|
632 |
#endif // ENABLE_PLC |