#define  GLOBALS_API_RTC
#include "../App_Headers/includes.h"


/*-------------------------------------------------------------------------
* Function Name  : API_SetTimer0
* 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->TEnable1 = Enable;
    T->OverTime1 = Second;
    Clock.Time1 = null;
}
/*-------------------------------------------------------------------------
* 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_SetTimer2
* 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_TimerOver
* 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_TimerOver
* 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_TimerOver
* 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_SetFuelTimer
* Description    : ���ö�ʱ��
* Input          : 
* Output         : None
* Return         : None
* onther         : 
--------------------------------------------------------------------------*/
void Api_SetFuelTimer(INT16U Second,ClockType *T)
{
    T->TFuelEnable = Enable;
    T->OverFuelTime = Second;
    T->FuelTime = null;
}
/*-------------------------------------------------------------------------
* Function Name  : API_TimerOver
* Description    : ���ö�ʱ��
* Input          : 
* Output         : None
* Return         : None
* onther         : 
--------------------------------------------------------------------------*/
INT8U Api_FuelTimerOver(ClockType *T)
{
    if((T->FuelTime >= T->OverFuelTime)&&(T->TFuelEnable == Enable))
    {        
        T->TFuelEnable = Disable;
        T->FuelTime = null;
        return 1;
    }
    else
    {
        return 0;
    }    
}
/*-------------------------------------------------------------------------
* Function Name  : Api_SetFuel_1Timer
* Description    : ���ö�ʱ��
* Input          : 
* Output         : None
* Return         : None
* onther         : 
--------------------------------------------------------------------------*/
void Api_SetFuel_1Timer(INT16U Second,ClockType *T)
{
    T->TFuel_1Enable = Enable;
    T->OverFuel_1Time = Second;
    T->Fuel_1Time = null;
}
/*-------------------------------------------------------------------------
* Function Name  : Api_Fuel_1TimerOver
* Description    : ���ö�ʱ��
* Input          : 
* Output         : None
* Return         : None
* onther         : 
--------------------------------------------------------------------------*/
INT8U Api_Fuel_1TimerOver(ClockType *T)
{
    if((T->Fuel_1Time >= T->OverFuel_1Time)&&(T->TFuel_1Enable == Enable))
    {        
        T->TFuel_1Enable = Disable;
        T->Fuel_1Time = 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;
    T->Time7 = null;
}
/*-------------------------------------------------------------------------
* Function Name  : API_TimerOver
* 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_SetTimer7
* Description    : ���ö�ʱ��
* Input          : 
* Output         : None
* Return         : None
* onther         : 
--------------------------------------------------------------------------*/
void API_SetTimer8(INT16U Second,ClockType *T)
{
    T->TEnable8 = Enable;
    T->OverTime8 = Second;
    Clock.Time8 = null;
}
/*-------------------------------------------------------------------------
* Function Name  : API_TimerOver
* Description    : ���ö�ʱ��
* Input          : 
* Output         : None
* Return         : None
* onther         : 
--------------------------------------------------------------------------*/
INT8U API_TimerOver8(ClockType *T)
{
    if((T->Time8 >= T->OverTime8)&&(T->TEnable8 == Enable))
    {        
        T->TEnable8 = Disable;
        T->Time8 = null;
        return 1;
    }
    else
    {
        return 0;
    }    
}
/*-------------------------------------------------------------------------
* Function Name  : API_SetTimer10
* Description    : ���ö�ʱ��
* Input          : 
* Output         : None
* Return         : None
* onther         : 
--------------------------------------------------------------------------*/
void API_SetTimer10(INT16U Second,ClockType *T)
{
    T->TEnable10 = Enable;
    T->OverTime10 = Second;
    Clock.Time10 = null;
}
/*-------------------------------------------------------------------------
* Function Name  : API_TimerOver
* Description    : ���ö�ʱ��
* Input          : 
* Output         : None
* Return         : None
* onther         : 
--------------------------------------------------------------------------*/
INT8U API_TimerOver10(ClockType *T)
{
    if((T->Time10 >= T->OverTime10)&&(T->TEnable10 == Enable))
    {        
        T->TEnable10 = Disable;
        T->Time10 = null;
        return 1;
    }
    else
    {
        return 0;
    }    
}
/*-------------------------------------------------------------------------
* Function Name  : API_SetTimer9
* Description    : ���ö�ʱ��
* Input          : 
* Output         : None
* Return         : None
* onther         : 
--------------------------------------------------------------------------*/
void API_SetTimer9(INT16U Second,ClockType *T)
{
    T->TEnable9 = Enable;
    T->OverTime9 = Second;
    Clock.Time9 = null;
}
/*-------------------------------------------------------------------------
* Function Name  : API_TimerOver
* Description    : ���ö�ʱ��
* Input          : 
* Output         : None
* Return         : None
* onther         : 
--------------------------------------------------------------------------*/
INT8U API_TimerOver9(ClockType *T)
{
    if((T->Time9 >= T->OverTime9)&&(T->TEnable9 == Enable))
    {        
        T->TEnable9 = Disable;
        T->Time9 = null;
        return 1;
    }
    else
    {
        return 0;
    }    
}
/*-------------------------------------------------------------------------
* Function Name  : API_ClockGenerate
* Description    : ʱ�������� ʱ��100ms
* Input          : 
* Output         : None
* Return         : None
* onther         : 
--------------------------------------------------------------------------*/
INT8U Charge_Timer = 0;
INT8U Charge_CheckTimer = 0;
void API_ClockGenerate(void)
{  
  if(! CPMUCLKS_RTIOSCSEL)
  {
      CPMUPROT = InitCPMUPROT; 
      CPMUCLKS_RTIOSCSEL = Enable;
      CPMUPROT = Enable;       
  }   
  if((CPMURTI_RTDEC != True)||(CPMURTI_RTR != 0x5f))
  {
      CPMURTI_RTDEC  = True;                 //ʮ����
      CPMURTI_RTR    = 0x5f;                 //400*10^3  4M:0x57;        
      CPMUINT_RTIE   = Enable;               //RTI    
  }    
  if(API_TimerOver0((ClockType*)&Clock))
  {
    if (Curkeyinput == KeyON)
    {
      if (Charge_Timer < 2)
      {
        Charge_Timer++;
      }
      if (Charge_CheckTimer < 30)
      {
        Charge_CheckTimer++;
      }
    }

    if(CanClk.IDF004Time < T_100ms)
    {
        CanClk.IDF004Time ++;
    }          
    if(CanClk.IDFEEETime < T_100ms)
    {
        CanClk.IDFEEETime ++;
    } 
    if(CanClk.IDFECA00Time < T_100ms)
    {
        CanClk.IDFECA00Time ++;
    }
    if(CanClk.IDFECA3DTime < T_100ms)
    {
        CanClk.IDFECA3DTime ++;
    }        
    if(CanClk.IDFF08Time < T_100ms)
    {
        CanClk.IDFF08Time ++;
    }
    if(CanClk.IDFEFFTime < T_100ms)
    {
        CanClk.IDFEFFTime ++;
    } 
    if(CanClk.IDFEE4Time < T_100ms)
    {
        CanClk.IDFEE4Time ++;
    }        
    if(CanClk.IDFE5600Time < T_100ms)
    {
        CanClk.IDFE5600Time ++;
    } 
    /*if(CanClk.ID18ECFF00Time < T_100ms)
    {
        CanClk.ID18ECFF00Time ++;
    } 
    if(CanClk.ID18EBFF00Time < T_100ms)
    {
        CanClk.ID18EBFF00Time ++;
    }*/         
    if(CanClk.IDFE563DTime < T_100ms)
    {
        CanClk.IDFE563DTime ++;
    }                
    if(CanClk.IDFE40Time < T_100ms)
    {
        CanClk.IDFE40Time ++;
    }                  
  /*  if(CanClk.IDFF0ATime < T_100ms)   lzs--shan
    {
        CanClk.IDFF0ATime ++;
    }  */
    if(CanClk.ID0CF00300Time < T_100ms)
    {
        CanClk.ID0CF00300Time ++;
    }
    if(CanClk.IDFEF200Time < T_100ms)
    {
        CanClk.IDFEF200Time ++;
    } 
    if(CanClk.IDFEEFTime < T_100ms)
    {
        CanClk.IDFEEFTime ++;
    }
    if(CanClk.ID5F2Time < T_100ms)
    {
        CanClk.ID5F2Time ++;
    }                    
     if(CanClk.IDDropTime < T_100ms)
    {
       CanClk.IDDropTime ++;
    }
    //
    if(CanClk.Can18FF4AEFTime > null)
    {
       CanClk.Can18FF4AEFTime --;
    }   
    if(CanClk.Can18FEAE17Time > null)
    {
       CanClk.Can18FEAE17Time --;
    } 
    if(CanClk.Can18FE6C17Time > null)
    {
       CanClk.Can18FE6C17Time --;
    }                 
    //
    if(CanClk.IDF001Time < T_100ms)
    {
       CanClk.IDF001Time ++;
    }
    if(CanClk.IDFEBF0BTime < T_100ms)
    {
        CanClk.IDFEBF0BTime ++;
    } 
    if(CanClk.IDFD0700Time < T_100ms)
    {
        CanClk.IDFD0700Time ++;
    }     
    if(VehMeter.CurBoardVal)
    {
        if(g_accTime < T_100ms)
        {
            g_accTime ++;
        } 
    } 
    //-----��ʻԱ�����--Ϋ����----
    if(CanClk.ID18FEFF00Time < T_100ms)
    {
      CanClk.ID18FEFF00Time ++;
    }
    
    if(CanClk.ID1CFFBE18Time < T_100ms)
    {
      CanClk.ID1CFFBE18Time ++;
    }
    //-----��ʻԱ�����--Ϋ����----
 /*   if(CanClk.ID18FECA10Time < T_100ms)
    {
      CanClk.ID18FECA10Time ++;
    }  */
           
    //on
    if(Curkeyinput==KeyON)
    {
      if(OBD_CheckTime <T_5s)
      {
         OBD_CheckTime ++;
      }  
      if( Clock.isFlash>T_500ms)
      {
          Clock.isFlash =0;
          BcmModel.bits.Blinking = !BcmModel.bits.Blinking;
      }
      else
      {
        Clock.isFlash++;
      }
          
    }       
    ///     
    API_SetTimer0(T_100ms,(ClockType*)&Clock);
  }
}
/*-------------------------------------------------------------------------
* Function Name  : Api_RTC_isr                               
* Description    : 
* Input          : 
* Output         : None
* Return         : None
* onther         : 
--------------------------------------------------------------------------*/
interrupt void Api_RTC_isr(void)
{             
  GUI_LedRandomDisplay();
    
  ///////////////  busoff  //////////////////
  if((CANRFLG_TSTAT == 0x03)||(CANRFLG_RSTAT == 0x03))
  {                
      if(CanClk.BusOffTime == null) 
      { 
          if(CanClk.BusOffCn < 255)
          {                  
              CanClk.BusOffCn ++;
          }			
          if(CanClk.BusOffCn >= 1)
          {
              CanClk.BusOffTime = T_200ms;
          }		
	     	  bsp_CAN_BusOff();
      }
  		else
  		{
    			CANTARQ_ABTRQ = 0x07; 			
  		}
      if(CanClk.BusOffTime > null)
      {
          CanClk.BusOffTime --;                 
      }        
  }
  ///////////////////////
    if(DropTime < T_5s)
    {
        DropTime++;
    }  
 
    if(FuelMeter.Time < T_5s)
    {
        FuelMeter.Time ++;        
    } 
                 
    if(Clock.TEnable9)
    {
        if(Clock.Time9 < Clock.OverTime9)
        {
            Clock.Time9++;
        }
    }      
    if(Clock.TEnable8)
    {
        if(Clock.Time8 < Clock.OverTime8)
        {
            Clock.Time8++;
        }
    }     
    if(Clock.TEnable7)
    {
        if(Clock.Time7 < Clock.OverTime7)
        {
            Clock.Time7++;
        }
    }         
    if(Clock.TFuel_1Enable)
    {
        if(Clock.Fuel_1Time < Clock.OverFuel_1Time)
        {
            Clock.Fuel_1Time++;
        }
    } 
    if(Clock.TFuelEnable)
    {
        if(Clock.FuelTime < Clock.OverFuelTime)
        {
            Clock.FuelTime++;
        }
    }            
    if(Clock.TEnable5)
    {
        if(Clock.Time5 < Clock.OverTime5)
        {
            Clock.Time5++;
        }
    }
    if(Clock.TEnable4)
    {
        if(Clock.Time4 < Clock.OverTime4)
        {
            Clock.Time4++;
        }
    }
    if(Clock.TEnable3)
    {
        if(Clock.Time3 < Clock.OverTime3)
        {
            Clock.Time3++;
        }
    }      
    if(Clock.TEnable2)
    {
        if(Clock.Time2 < Clock.OverTime2)
        {
            Clock.Time2++;
        }
    }             
    if(Clock.TEnable1)
    {
        if(Clock.Time1 < Clock.OverTime1)
        {
            Clock.Time1++;
        }
    }
    if(Clock.TEnable0)
    {
        if(Clock.Time0 < Clock.OverTime0)
        {
            Clock.Time0++;
        }
    }
   
    Clock.HalfSeconds++; 
    //
    if(Clock.HalfSeconds > 9)
    {
        Clock.Seconds++;
        Clock.HalfSeconds = null;
    }

    if (Clock.Seconds > 59)
    {
        Clock.Minutes++;
        Clock.Seconds = 0;
    }

    if (Clock.Minutes > 59)
    {
        Clock.Hours++;
        Clock.Minutes = 0;
    }

    if (Clock.Hours > 23)
    {
        Clock.Hours = 0;
    } 
    //
    if (SendTimes < 5)
    {
        SendTimes++;//CAN����ʱ��
    }
    //
    if(KEY1_IN == 0)
    {
		    if(G_Button.PushTime <= T_40s)
		    {
		        G_Button.PushTime ++;
		    }
    } 
    //   
    if((KEY1_IN == UpSpring)&&(Curkeyinput == KeyOFF))
    {
      if(CancelRadioTime<T_5s)
      {
        CancelRadioTime++;
      }
    } 
    else
    {
      CancelRadioTime = 0;
    }
    if(G_Button.MainMenu==RadioMenu) 
    {
      if(QuitRadioTime <T_8s)
      {
        QuitRadioTime++;
      }  
    
    } 
    //�����ٱ�
    if(G_Button.MainMenu == RadioMenu) 
    {
      SetRadioTime++;
    }     
    //ON 
    if(Curkeyinput==KeyON)
    {
      if (TempMeter_DelayTime < T_2s)
      {
         TempMeter_DelayTime++;
      }
      
      if(FuelTime < T_5s)
      {
          FuelTime++;
      } 
      
      //glz--20170518
      if(InitAdblueTime < T_8s)
      {
          InitAdblueTime++;
      }
      if(AdbuleMeter.Time < T_1s)
      {
          AdbuleMeter.Time ++;        
      }
      //glz--201707048
      if(InitQy1Time < T_5s)
      {
          InitQy1Time++;
      } 
      if(QY1Meter.Time < T_1s)
      {
          QY1Meter.Time ++;        
      } 
      if(InitQy2Time < T_5s)
      {
          InitQy2Time++;
      } 
      if(QY2Meter.Time < T_1s)
      {
          QY2Meter.Time ++;        
      }           
      //glz--20170507   
      if(Park_In == LOW)
      {
          if(LED.StopBreakTime < T_300ms)
          {
              LED.StopBreakTime ++;
          }
      }
      else
      {
          LED.StopBreakTime = null;
      } 

      if (T_BOX_time < T_40s)
        {
            T_BOX_time++;
        }
    }
    //off
    if(Curkeyinput==KeyOFF)
    {
      if(bCanDropTimes>0)
      {
        bCanDropTimes--;
      }
      else
      {
        bsp_CAN_Sleep();
      }  
    }
 
    
    
    ///
    CPMUFLG_RTIF = ClearFlag;
}
