#define  GLOBALS_API_RTC
#include "../App_Headers/includes.h"
#include "../Library/TimeCtrl.h"
INT16U  RT_10ms = 0,
        RT_20ms = 0,
        RT_25ms = 0,
        RT_50ms = 0,
        RT_60ms = 0,
        RT_100ms = 0,
        RT_200ms = 0,
        RT_500ms = 0,
        RT_1s = 0,
        RT_2s = 0;


/*-------------------------------------------------------------------------
* Function Name  : Api_SetTimer1
* Description    : ���ö�ʱ��
* Input          : 
* Output         : None
* Return         : None
* onther         : 
--------------------------------------------------------------------------*/
void Api_SetTimer0(INT16U Second,ClockType *T)
{
    T->TEnable0 = Enable;
    T->OverTime0 = Second;
    Clock.Time0 = null;
}
/*-------------------------------------------------------------------------
* Function Name  : API_TimerOver
* Description    : ���ö�ʱ��
* Input          : 
* Output         : None
* Return         : None
* onther         : 
--------------------------------------------------------------------------*/
INT8U Api_TimerOver0(ClockType *T)
{
    if((T->Time0 >= T->OverTime0)&&(T->TEnable0 == Enable))
    {        
        T->TEnable0 = Disable;
        T->Time0 = null;
        return 1;
    }
    else
    {
        return 0;
    }    
}
/*-------------------------------------------------------------------------
* Function Name  : Api_SetTimer1
* Description    : ���ö�ʱ��
* Input          : 
* Output         : None
* Return         : None
* onther         : 
--------------------------------------------------------------------------*/
void Api_SetTimer1(INT16U Second,ClockType *T)
{    
    T->OverTime1 = Second;
    Clock.Time1 = null;
    T->TEnable1 = Enable;
}
/*-------------------------------------------------------------------------
* Function Name  : API_TimerOver
* Description    : ���ö�ʱ��
* Input          : 
* Output         : None
* Return         : None
* onther         : 
--------------------------------------------------------------------------*/
INT8U Api_TimerOver1(ClockType *T)
{
    if((T->Time1 >= T->OverTime1)&&(T->TEnable1 == Enable))
    {        
        T->TEnable1 = Disable;
        T->Time1 = null;
        return 1;
    }
    else
    {
        return 0;
    }    
}
/*-------------------------------------------------------------------------
* Function Name  : Api_SetTimer1
* Description    : ���ö�ʱ��
* Input          : 
* Output         : None
* Return         : None
* onther         : 
--------------------------------------------------------------------------*/
void Api_SetTimer2(INT16U Second,ClockType *T)
{
    T->TEnable2 = Enable;
    T->OverTime2 = Second;
    Clock.Time2 = null;
}
/*-------------------------------------------------------------------------
* Function Name  : API_TimerOver
* Description    : ���ö�ʱ��
* Input          : 
* Output         : None
* Return         : None
* onther         : 
--------------------------------------------------------------------------*/
INT8U Api_TimerOver2(ClockType *T)
{
    if((T->Time2 >= T->OverTime2)&&(T->TEnable2 == Enable))
    {        
        T->TEnable2 = Disable;
        T->Time2 = null;
        return 1;
    }
    else
    {
        return 0;
    }    
}
/*-------------------------------------------------------------------------
* Function Name  : Api_SetTimer3
* Description    : ���ö�ʱ��
* Input          : 
* Output         : None
* Return         : None
* onther         : 
--------------------------------------------------------------------------*/
void Api_SetTimer3(INT16U Second,ClockType *T)
{
    T->TEnable3 = Enable;
    T->OverTime3 = Second;
    Clock.Time3 = null;
}
/*-------------------------------------------------------------------------
* Function Name  : Api_TimerOver3
* Description    : ���ö�ʱ��
* Input          : 
* Output         : None
* Return         : None
* onther         : 
--------------------------------------------------------------------------*/
INT8U Api_TimerOver3(ClockType *T)
{
    if((T->Time3 >= T->OverTime3)&&(T->TEnable3 == Enable))
    {        
        T->TEnable3 = Disable;
        T->Time3 = null;
        return 1;
    }
    else
    {
        return 0;
    }    
}
/*-------------------------------------------------------------------------
* Function Name  : Api_SetTimer4
* Description    : ���ö�ʱ��
* Input          : 
* Output         : None
* Return         : None
* onther         : 
--------------------------------------------------------------------------*/
void Api_SetTimer4(INT16U Second,ClockType *T)
{
    T->TEnable4 = Enable;
    T->OverTime4 = Second;
    Clock.Time4 = null;
}
/*-------------------------------------------------------------------------
* Function Name  : Api_TimerOver4
* Description    : ���ö�ʱ��
* Input          : 
* Output         : None
* Return         : None
* onther         : 
--------------------------------------------------------------------------*/
INT8U Api_TimerOver4(ClockType *T)
{
    if((T->Time4 >= T->OverTime4)&&(T->TEnable4 == Enable))
    {        
        T->TEnable4 = Disable;
        T->Time4 = null;
        return 1;
    }
    else
    {
        return 0;
    }    
}
/*-------------------------------------------------------------------------
* Function Name  : Api_SetTimer5
* Description    : ���ö�ʱ��
* Input          : 
* Output         : None
* Return         : None
* onther         : 
--------------------------------------------------------------------------*/
void Api_SetTimer5(INT16U Second,ClockType *T)
{
    T->TEnable5 = Enable;
    T->OverTime5 = Second;
    Clock.Time5 = null;
}
/*-------------------------------------------------------------------------
* Function Name  : Api_TimerOver5
* Description    : ���ö�ʱ��
* Input          : 
* Output         : None
* Return         : None
* onther         : 
--------------------------------------------------------------------------*/
INT8U Api_TimerOver5(ClockType *T)
{
    if((T->Time5 >= T->OverTime5)&&(T->TEnable5 == Enable))
    {        
        T->TEnable5 = Disable;
        T->Time5 = null;
        return 1;
    }
    else
    {
        return 0;
    }    
}
/*-------------------------------------------------------------------------
* Function Name  : Api_SetTimer6
* Description    : ���ö�ʱ��
* Input          : 
* Output         : None
* Return         : None
* onther         : 
--------------------------------------------------------------------------*/
void Api_SetTimer6(INT16U Second,ClockType *T)
{
    T->TEnable6 = Enable;
    T->OverTime6 = Second;
    Clock.Time6 = null;
}
/*-------------------------------------------------------------------------
* Function Name  : Api_TimerOver5
* Description    : ���ö�ʱ��
* Input          : 
* Output         : None
* Return         : None
* onther         : 
--------------------------------------------------------------------------*/
INT8U Api_TimerOver6(ClockType *T)
{
    if((T->Time6 >= T->OverTime6)&&(T->TEnable6 == Enable))
    {        
        T->TEnable6 = Disable;
        T->Time6 = null;
        return 1;
    }
    else
    {
        return 0;
    }    
}
/*-------------------------------------------------------------------------
* Function Name  : Api_SetTimer7
* Description    : ���ö�ʱ��
* Input          : 
* Output         : None
* Return         : None
* onther         : 
--------------------------------------------------------------------------*/
void Api_SetTimer7(INT16U Second,ClockType *T)
{
    T->TEnable7 = Enable;
    T->OverTime7 = Second;
    Clock.Time7 = null;
}
/*-------------------------------------------------------------------------
* Function Name  : Api_TimerOver7
* Description    : ���ö�ʱ��
* Input          : 
* Output         : None
* Return         : None
* onther         : 
--------------------------------------------------------------------------*/
INT8U Api_TimerOver7(ClockType *T)
{
    if((T->Time7 >= T->OverTime7)&&(T->TEnable7 == Enable))
    {        
        T->TEnable7 = Disable;
        T->Time7 = null;
        return 1;
    }
    else
    {
        return 0;
    }    
}

/*-------------------------------------------------------------------------
* Function Name  : Api_SetAutoTimer
* Description    : �����Զ��л���ʱ��
* Input          : 
* Output         : None
* Return         : None
* onther         : 
--------------------------------------------------------------------------*/
void Api_SetAutoTimer(INT16U Second,ClockType *T)
{
    T->AutoEnable = Enable;
    T->OverAutoTime = Second;
    Clock.AutoTime = null;
}
/*-------------------------------------------------------------------------
* Function Name  : Api_AutoTimerOver
* Description    : �����Զ��л���ʱ��
* Input          : 
* Output         : None
* Return         : None
* onther         : 
--------------------------------------------------------------------------*/
INT8U Api_AutoTimerOver(ClockType *T)
{
    if((T->AutoTime >= T->OverAutoTime)&&(T->AutoEnable == Enable))
    {        
        T->AutoEnable = Disable;
        T->AutoTime = null;
        return 1;
    }
    else
    {
        return 0;
    }    
}
/*-------------------------------------------------------------------------
* Function Name  : Api_RTC_isr
* Description    : 
* Input          : 
* Output         : None
* Return         : None
* onther         : 
--------------------------------------------------------------------------*/
INT16U a = 0;
INT16U c = 0;
INT16U i = 0;
INT16U j = 2;
INT16U k = 1;
INT16U direction_water  = 1;
INT16U direction_speed = 1;
INT16U  direction_espeed = 1;
INT16U kseenSeven  = 0;
INT16U speed_count = 0; 
INT16U water_temp[]= {55,65,75,85,95,119,125};
interrupt void Api_RTC_isr(void)
{        

    RT_10ms++; 
    RT_20ms++; 
    RT_25ms++; 
    RT_50ms++; 
    RT_60ms++; 
    RT_100ms++;
    RT_200ms++;
    RT_500ms++;
    RT_1s++;
    RT_2s++;
    
    if(pSB1 == 0){
      Key.PushTime_1++;
    }
    
    if(pSB2 == 0){
      Key.PushTime_2++;
    }
    if(count_flag_a==1)
    {
        a++;
        if(a>=300)
        {

            if(direction_speed == 1)
            {
                speed_count++;
                if(speed_count>199)
                {
                    direction_speed = 0;
                }
            }
            else
            {
                speed_count--;
                if(speed_count < 1)
                {
                    direction_speed = 1;
                }

            }

            c++;
            if(c>4)
            {
                c = 0;
                i++;
                if(i>=11)
                {
                    i = 0;
                    if(direction_water==1)
                    {
                        j++;
                        if(j>6)
                        {
                            direction_water = 0;
                        }
                    }
                    else
                    {
                        j--;
                        if(j==1)
                        {
                            direction_water = 1;
                        }
                    }
                }
                if(direction_espeed==1)
                {
                    k++;
                    if(k>10)
                    {
                        direction_espeed = 0;
                    }
                }
                else
                {
                    k--;
                    if(k==2)
                    {
                        direction_espeed = 1;
                    }
                }
            }
            

            water_value = water_temp[j-1];


            speed_value = speed_count;
            Key.CAN_Step = k-1;         
            a = 0;
        }
    }
    if(RT_10ms >= 10){
        RT_10ms_En = 1;
        RT_10ms = null;
    }
    
    if(RT_20ms >= 20){
        RT_20ms_En = 1;
        RT_20ms = null;
        
        
    }
    
    if(RT_25ms >= 25){
        RT_25ms_En = 1;
        RT_25ms = null;
    }
    
    if(RT_50ms >= 50){
        RT_50ms_En = 1;
        RT_50ms = null;
    }
    
    if(RT_60ms >= 60){
        RT_60ms_En = 1;
        RT_60ms = null;
    }
    
    if(RT_100ms >= T_100ms){
        RT_100ms_En = 1;
        RT_100ms = null;
    }
    
    if(RT_200ms >= T_200ms){
        RT_200ms_En = 1;
        RT_200ms = null;
    }
    
    if(RT_500ms >= T_500ms){
        RT_500ms_En = 1;
        RT_500ms = null;
    }
    
    if(RT_1s >= T_1s){
        RT_1s_En = 1;
        RT_1s = null;
    }
    
    if(Key.StepAuto == 1){
        if(RT_2s >= T_2s){
            RT_2s = null;
            
            //Key.CAN_Step++;
        }
    }
    else
    {
        RT_2s = null;
    }
    

    Timer_1ms_Base();
        
    if(Clock.TEnable0)
    {
        if(Clock.Time0 < Clock.OverTime0)
        {
            Clock.Time0++;
        }
    }
    if(Clock.TEnable1)
    {
        if(Clock.Time1 < Clock.OverTime1)
        {
            Clock.Time1++;
        }
    }
    if(Clock.TEnable2)
    {
        if(Clock.Time2 < Clock.OverTime2)
        {
            Clock.Time2++;
        }
    }
    if(Clock.TEnable3)
    {
        if(Clock.Time3 < Clock.OverTime3)
        {
            Clock.Time3++;
        }
    }
    if(Clock.TEnable4)
    {
        if(Clock.Time4 < Clock.OverTime4)
        {
            Clock.Time4++;
        }
    }
    if(Clock.TEnable5)
    {
        if(Clock.Time5 < Clock.OverTime5)
        {
            Clock.Time5++;
        }
    }
    if(Clock.TEnable6)
    {
        if(Clock.Time6 < Clock.OverTime6)
        {
            Clock.Time6++;
        }
    }
    if(Clock.TEnable7)
    {
        if(Clock.Time7 < Clock.OverTime7)
        {
            Clock.Time7++;
        }
    }
    
    
    CPMUFLG_RTIF = ClearFlag;
}