#use "default.h"
#use cplc.lib

#if (BOARD_TYPE!=LittleG)
#fatal "This program only runs on the BL16xx/LittleG."
#endif

/*
	GENLDR.C

   Ladder like programming.
   Use virtual timers to sequence the bits of the output port OUTBYTE.
   Loop for five times. 
   All output will be off if the variable not_stop is 0.

   The state of the output can be read back on inport(INENLO) or could be
   monitored individually via DIGIN1 to DIGIN8. Loop back the output 
   to the input:
    
            Header H1           Header H2 
            1, IN00, DIGIN1     13, OUTB1
            4, IN01, DIGIN2     14, OUTB2
            3, IN02, DIGIN3     15, OUTB3
            6, IN03, DIGIN4     16, OUTB4
            5, IN04, DIGIN5     17, OUTB5
            8, IN05, DIGIN6     18, OUTB6
            7, IN06, DIGIN7     19, OUTB7
           10, IN07, DIGIN8     20, OUTB8
*/

#define OR  ||
#define     AND         &&
#define     NOT         !
#define     START_T1    T1I
#define     START_T2    T2I
#define     START_T3    T3I
#define     START_T4    T4I
#define     START_T5    T5I
#define     START_T6    T6I
#define     START_T7    T7I
#define     START_T8    T8I

#define     T1_DONE     T1O
#define     T2_DONE     T2O
#define     T3_DONE     T3O
#define     T4_DONE     T4O
#define     T5_DONE     T5O
#define     T6_DONE     T6O
#define     T7_DONE     T7O
#define     T8_DONE     T8O


main()
{
    int not_stop;
    int  start;
    char inp_previous;
    int  detect_rise;
    int  loop_count;

    uplc_init();             // call the virtual drivers
    
    T1RLD = 20;              // 1 count is 25 ms >> 20 x 25ms = 0.5 sec
    T2RLD = 20;
    T3RLD = 20;
    T4RLD = 20;
    T5RLD = 20;
    T6RLD = 20;
    T7RLD = 20;
    T8RLD = 20;
    loop_count = 5;           // loop for 5 times
    not_stop   = 1;           // stop condition for all 

    OUTB1 = OUTB2 = OUTB3 = OUTB4 = OUTB5 = OUTB6 = OUTB7 = OUTB8 = 0; 
               // all outputs are off 
    inp_previous = inport(INENLO);
    detect_rise = 0;
    while(1)
    {
        runwatch();
        
        start   =   not_stop AND 
                    !(OUTB1 + OUTB2 + OUTB3 + OUTB4 + OUTB5 + OUTB6 + 
                        OUTB7 + OUTB8);    
                                            // start condition
        START_T1 =  (start OR T8_DONE OR START_T1) AND NOT T1_DONE;
        OUTB1     =  START_T1 AND not_stop;      
        
        START_T2 =  (T1_DONE OR START_T2) AND NOT T2_DONE;
        OUTB2     =  START_T2 AND not_stop;      
        
        START_T3 =  (T2_DONE OR START_T3) AND NOT T3_DONE;
        OUTB3     =  START_T3 AND not_stop;      
        
        START_T4 =  (T3_DONE OR START_T4) AND NOT T4_DONE;
        OUTB4     =  START_T4 AND not_stop;      

        START_T5 =  (T4_DONE OR START_T5) AND NOT T5_DONE;
        OUTB5     =  START_T5 AND not_stop;      
        
        START_T6 =  (T5_DONE OR START_T6) AND NOT T6_DONE;
        OUTB6     =  START_T6 AND not_stop;      
        
        START_T7 =  (T6_DONE OR START_T7) AND NOT T7_DONE;
        OUTB7     =  START_T7 AND not_stop;      
        
        START_T8 =  (T7_DONE OR START_T8) AND NOT T8_DONE;
        OUTB8     =  START_T8 AND not_stop;      

        if( START_T1 != detect_rise)  // detect positive pulse on START_T1
        {
            detect_rise = START_T1;
            if(START_T1) 
            {
               if(!(loop_count--)) break;  // if 5 times, end program
               printf("\n%d Input STATES >> ", 5 - loop_count);
               if(inp_previous==0xff)
                  printf(" NO IO loop back !!!");
            }
         }

        if( inport(INENLO) != inp_previous )
        {
            inp_previous = inport(INENLO);
            printf("%x ", inp_previous);
        }
    }
    printf("\nThe End.");
}
        

