2.7 Use Time Tick of System to Create a Timer
The INCON-M series PLC stores the system time tick in the sts.sysTimeTick, unit is 1/256 seconds, PLC can use this time tick to design timers.
A typical timer has a flag for state of timer, a variable for end of time. Writing method: Define a new data type (timerPlc) for the timer first, then make generalTimer the timerPlc data type (create a timer for the PLC program). In the below example, M17 (case 17) is air blow for a period, and time period is set in parameter.
#define oAirBlow (plc.gdo[1].bit.bit00) //air blow output
#define paAirBlowTime (para[10]) //air blow period parameter
struct timerPlc{ //timer structure
int flag; //timer flag
long int time; //count up time tick
}
timerPlc generalTimer; //create general timer
enum {IDLE = 0, START, STEP_1, STEP_2, STEP_3, STEP_4, STEP_5, STEP_6, STEP_7, STEP_8, STEP_9, STEP_10, FINISH}; //flag value
void PLCAPI plcOpen(struct Status &sts, struct PlcBlock &plc, unsigned short *PlcData, long *LongPlcData){
generalTimer.flag = IDLE; //power initial
oAirBlow = 0; //output initial
}
void PLCAPI plcRun(struct Status &sts, struct PlcBlock &plc){
if(plc.mCode.flag){
switch(plc.mCode.data){
case 17: //air blow for a period
if(generalTimer.flag == IDLE){
oAirBlow = 1;
generalTimer.flag = START; //statrt timer to count time
generalTimer.time = sts.sysTimeTick + paAirBlowTime; //count up time tick
}
if(generalTimer.flag == FINISH){
generalTimer.flag = IDLE;
oAirBlow = 0;
plc.mCode.finish = 1;
}
break;
}
}
//general timer stage machine
if(generalTimer.flag != IDLE){
switch(generalTimer.flag){
case START:
generalTimer.flag = STEP_1;
break;
case STEP_1:
if(sts.sysTimeTick >= generalTimer.time)
generalTimer.flag = FINISH;
break;
}
}
}