/* This is auto-generated code from LDmicro. Do not edit this file! Go
   back to the ladder diagram source for changes in the logic, and make
   any C additions either in ladder.h or in additional .c files linked
   against this one. */

/* You must provide ladder.h; there you must provide:
      * a typedef for SWORD and BOOL, signed 16 bit and boolean types
        (probably typedef signed short SWORD; typedef unsigned char BOOL;)

   You must also provide implementations of all the I/O read/write
   either as inlines in the header file or in another source file. (The
   I/O functions are all declared extern.)

   See the generated source code (below) for function names. */
#include "ladder.h"

/* Define EXTERN_EVERYTHING in ladder.h if you want all symbols extern.
   This could be useful to implement `magic variables,' so that for
   example when you write to the ladder variable duty_cycle, your PLC
   runtime can look at the C variable U_duty_cycle and use that to set
   the PWM duty cycle on the micro. That way you can add support for
   peripherals that LDmicro doesn't know about. */
#ifdef EXTERN_EVERYTHING
#define STATIC 
#else
#define STATIC static
#endif

/* Define NO_PROTOTYPES if you don't want LDmicro to provide prototypes for
   all the I/O functions (Read_U_xxx, Write_U_xxx) that you must provide.
   If you define this then you must provide your own prototypes for these
   functions in ladder.h, or provide definitions (e.g. as inlines or macros)
   for them in ladder.h. */
#ifdef NO_PROTOTYPES
#define PROTO(x)
#else
#define PROTO(x) x
#endif

/* U_xxx symbols correspond to user-defined names. There is such a symbol
   for every internal relay, variable, timer, and so on in the ladder
   program. I_xxx symbols are internally generated. */
STATIC BOOL I_b_mcr = 0;
#define Read_I_b_mcr() I_b_mcr
#define Write_I_b_mcr(x) I_b_mcr = x
STATIC BOOL I_b_rung_top = 0;
#define Read_I_b_rung_top() I_b_rung_top
#define Write_I_b_rung_top(x) I_b_rung_top = x
STATIC BOOL U_b_Rosc = 0;
#define Read_U_b_Rosc() U_b_Rosc
#define Write_U_b_Rosc(x) U_b_Rosc = x
STATIC BOOL I_b_Tof_antiglitch = 0;
#define Read_I_b_Tof_antiglitch() I_b_Tof_antiglitch
#define Write_I_b_Tof_antiglitch(x) I_b_Tof_antiglitch = x
STATIC SWORD U_i_Tof = 0;
STATIC SWORD U_i_Ton = 0;

/* You provide this function. */
PROTO(extern BOOL Read_U_b_Xup(void);)

STATIC BOOL I_b_oneShot_0000 = 0;
#define Read_I_b_oneShot_0000() I_b_oneShot_0000
#define Write_I_b_oneShot_0000(x) I_b_oneShot_0000 = x
STATIC SWORD U_i_Ccnt = 0;
STATIC SWORD U_i_Trto = 0;

/* You provide these functions. */
PROTO(BOOL Read_U_b_Yup(void);)
PROTO(void Write_U_b_Yup(BOOL v);)


/* You provide this function. */
PROTO(extern BOOL Read_U_b_Xdown(void);)

STATIC BOOL I_b_oneShot_0001 = 0;
#define Read_I_b_oneShot_0001() I_b_oneShot_0001
#define Write_I_b_oneShot_0001(x) I_b_oneShot_0001 = x
STATIC SWORD I_i_scratch = 0;

/* You provide these functions. */
PROTO(BOOL Read_U_b_Ydown(void);)
PROTO(void Write_U_b_Ydown(BOOL v);)


/* You provide this function. */
PROTO(extern BOOL Read_U_b_Xres(void);)

STATIC BOOL I_b_parOut_0000 = 0;
#define Read_I_b_parOut_0000() I_b_parOut_0000
#define Write_I_b_parOut_0000(x) I_b_parOut_0000 = x
STATIC BOOL I_b_parThis_0000 = 0;
#define Read_I_b_parThis_0000() I_b_parThis_0000
#define Write_I_b_parThis_0000(x) I_b_parThis_0000 = x
STATIC BOOL I_b_scratch = 0;
#define Read_I_b_scratch() I_b_scratch
#define Write_I_b_scratch(x) I_b_scratch = x
STATIC BOOL I_b_oneShot_0002 = 0;
#define Read_I_b_oneShot_0002() I_b_oneShot_0002
#define Write_I_b_oneShot_0002(x) I_b_oneShot_0002 = x
STATIC BOOL I_b_oneShot_0003 = 0;
#define Read_I_b_oneShot_0003() I_b_oneShot_0003
#define Write_I_b_oneShot_0003(x) I_b_oneShot_0003 = x
STATIC BOOL I_b_oneShot_0004 = 0;
#define Read_I_b_oneShot_0004() I_b_oneShot_0004
#define Write_I_b_oneShot_0004(x) I_b_oneShot_0004 = x
STATIC SWORD U_i_Ccirc = 0;
STATIC SWORD I_i_scratch2 = 0;
STATIC BOOL I_b_oneShot_0005 = 0;
#define Read_I_b_oneShot_0005() I_b_oneShot_0005
#define Write_I_b_oneShot_0005(x) I_b_oneShot_0005 = x
STATIC BOOL I_b_Tpulse_antiglitch = 0;
#define Read_I_b_Tpulse_antiglitch() I_b_Tpulse_antiglitch
#define Write_I_b_Tpulse_antiglitch(x) I_b_Tpulse_antiglitch = x
STATIC SWORD U_i_Tpulse = 0;

/* You provide these functions. */
PROTO(BOOL Read_U_b_Ypulse(void);)
PROTO(void Write_U_b_Ypulse(BOOL v);)



/* Call this function once per PLC cycle. You are responsible for calling
   it at the interval that you specified in the MCU configuration when you
   generated this code. */
void PlcCycle(void)
{
    Write_I_b_mcr(1);
    
    /* start rung 2 */
    Write_I_b_rung_top(Read_I_b_mcr());
    
    /* start series [ */
    if(!Read_U_b_Rosc()) {
        Write_I_b_rung_top(0);
    }
    
    if(!Read_I_b_Tof_antiglitch()) {
        U_i_Tof = 1;
    }
    Write_I_b_Tof_antiglitch(1);
    if(!Read_I_b_rung_top()) {
        if(U_i_Tof < 1) {
            U_i_Tof++;
            Write_I_b_rung_top(1);
        }
    } else {
        U_i_Tof = 0;
    }
    
    if(Read_I_b_rung_top()) {
        if(U_i_Ton < 1) {
            U_i_Ton++;
            Write_I_b_rung_top(0);
        }
    } else {
        U_i_Ton = 0;
    }
    
    if(Read_I_b_rung_top()) {
        Write_U_b_Rosc(0);
    } else {
        Write_U_b_Rosc(1);
    }
    
    /* ] finish series */
    
    /* start rung 3 */
    Write_I_b_rung_top(Read_I_b_mcr());
    
    /* start series [ */
    if(!Read_U_b_Rosc()) {
        Write_I_b_rung_top(0);
    }
    
    if(!Read_U_b_Xup()) {
        Write_I_b_rung_top(0);
    }
    
    if(Read_I_b_rung_top()) {
        if(!Read_I_b_oneShot_0000()) {
            U_i_Ccnt++;
        }
    }
    Write_I_b_oneShot_0000(Read_I_b_rung_top());
    if(U_i_Ccnt < 20) {
        Write_I_b_rung_top(0);
    } else {
        Write_I_b_rung_top(1);
    }
    
    if(U_i_Trto < 199) {
        if(Read_I_b_rung_top()) {
            U_i_Trto++;
        }
        Write_I_b_rung_top(0);
    } else {
        Write_I_b_rung_top(1);
    }
    
    Write_U_b_Yup(Read_I_b_rung_top());
    
    /* ] finish series */
    
    /* start rung 4 */
    Write_I_b_rung_top(Read_I_b_mcr());
    
    /* start series [ */
    if(!Read_U_b_Rosc()) {
        Write_I_b_rung_top(0);
    }
    
    if(!Read_U_b_Xdown()) {
        Write_I_b_rung_top(0);
    }
    
    if(Read_I_b_rung_top()) {
        if(!Read_I_b_oneShot_0001()) {
            I_i_scratch = 1;
            U_i_Ccnt = U_i_Ccnt - I_i_scratch;
        }
    }
    Write_I_b_oneShot_0001(Read_I_b_rung_top());
    if(U_i_Ccnt < 10) {
        Write_I_b_rung_top(0);
    } else {
        Write_I_b_rung_top(1);
    }
    
    Write_U_b_Ydown(Read_I_b_rung_top());
    
    /* ] finish series */
    
    /* start rung 5 */
    Write_I_b_rung_top(Read_I_b_mcr());
    
    /* start series [ */
    if(!Read_U_b_Xres()) {
        Write_I_b_rung_top(0);
    }
    
    /* start parallel [ */
    Write_I_b_parOut_0000(0);
    Write_I_b_parThis_0000(Read_I_b_rung_top());
    Write_I_b_scratch(Read_I_b_parThis_0000());
    if(Read_I_b_oneShot_0002()) {
        Write_I_b_parThis_0000(0);
    }
    Write_I_b_oneShot_0002(Read_I_b_scratch());
    
    if(Read_I_b_parThis_0000()) {
        Write_I_b_parOut_0000(1);
    }
    Write_I_b_parThis_0000(Read_I_b_rung_top());
    Write_I_b_scratch(Read_I_b_parThis_0000());
    if(!Read_I_b_parThis_0000()) {
        if(Read_I_b_oneShot_0003()) {
            Write_I_b_parThis_0000(1);
        }
    } else {
        Write_I_b_parThis_0000(0);
    }
    Write_I_b_oneShot_0003(Read_I_b_scratch());
    
    if(Read_I_b_parThis_0000()) {
        Write_I_b_parOut_0000(1);
    }
    Write_I_b_rung_top(Read_I_b_parOut_0000());
    /* ] finish parallel */
    if(Read_I_b_rung_top()) {
        U_i_Trto = 0;
    }
    
    /* ] finish series */
    
    /* start rung 6 */
    Write_I_b_rung_top(Read_I_b_mcr());
    
    /* start series [ */
    if(!Read_U_b_Rosc()) {
        Write_I_b_rung_top(0);
    }
    
    if(Read_I_b_rung_top()) {
        if(!Read_I_b_oneShot_0004()) {
            U_i_Ccirc++;
            if(U_i_Ccirc < 8) {
            } else {
                U_i_Ccirc = 0;
            }
        }
    }
    Write_I_b_oneShot_0004(Read_I_b_rung_top());
    
    /* ] finish series */
    
    /* start rung 7 */
    Write_I_b_rung_top(Read_I_b_mcr());
    
    /* start series [ */
    I_i_scratch2 = 3;
    if(U_i_Ccirc == I_i_scratch2) {
    } else {
        Write_I_b_rung_top(0);
    }
    
    Write_I_b_scratch(Read_I_b_rung_top());
    if(!Read_I_b_rung_top()) {
        if(Read_I_b_oneShot_0005()) {
            Write_I_b_rung_top(1);
        }
    } else {
        Write_I_b_rung_top(0);
    }
    Write_I_b_oneShot_0005(Read_I_b_scratch());
    
    if(!Read_I_b_Tpulse_antiglitch()) {
        U_i_Tpulse = 3;
    }
    Write_I_b_Tpulse_antiglitch(1);
    if(!Read_I_b_rung_top()) {
        if(U_i_Tpulse < 3) {
            U_i_Tpulse++;
            Write_I_b_rung_top(1);
        }
    } else {
        U_i_Tpulse = 0;
    }
    
    Write_U_b_Ypulse(Read_I_b_rung_top());
    
    /* ] finish series */
}
