/** ###################################################################
**     Filename  : vga2scart.C
**     Project   : vga2scart
**     Processor : MC9S08QD4CPC
**     Version   : Driver 01.11
**     Compiler  : CodeWarrior HCS08 C Compiler
**     Date/Time : 5.7.2010, 1:47
**     Abstract  :
**         Main module.
**         This module contains user's application code.
**     Settings  :
**     Contents  :
**         No public methods
**
** ###################################################################*/
/* MODULE vga2scart */


/* Including needed modules to compile this module/procedure */
#include "Cpu.h"
#include "Events.h"
#include "HSync.h"
#include "HSyncTimeout.h"
#include "VSync.h"
#include "RgbStatus.h"
#include "CSyncEn.h"
#include "CSyncInv.h"
/* Include shared modules, which are used for whole project */
#include "PE_Types.h"
#include "PE_Error.h"
#include "PE_Const.h"
#include "IO_Map.h"

/* User includes (#include below this line is not maintained by Processor Expert) */

#define SYNC_CNT_MAX 30000


void main(void)
{
  /* Write your local variable definition here */
  bool HSyncPolarity, VSyncPolarity;
  bool Invert;

  word SyncCnt = 0;
  word HSyncCnt = 0, VSyncCnt = 0;

  /*** Processor Expert internal initialization. DON'T REMOVE THIS CODE!!! ***/
  PE_low_level_init();
  /*** End of Processor Expert internal initialization.                    ***/

  /* Write your code here */
  for( ; ; )
  {
      Cpu_DisableInt();
      if (HSync_GetPinValue()) HSyncCnt++;
      if (VSync_GetPinValue()) VSyncCnt++;
      Cpu_EnableInt();
      
      SyncCnt++;
    
      if (SyncCnt >= SYNC_CNT_MAX)
      {
          HSyncPolarity = (SyncCnt-HSyncCnt) > HSyncCnt;
          VSyncPolarity = (SyncCnt-VSyncCnt) > VSyncCnt;

          Invert = HSyncPolarity == VSyncPolarity;
          
          CSyncInv_PutVal(Invert);
          CSyncEn_PutVal(!Invert);

          SyncCnt = 0;
          HSyncCnt = VSyncCnt = 0;
      }
  }

  /*** Don't write any code pass this line, or it will be deleted during code generation. ***/
  /*** Processor Expert end of main routine. DON'T MODIFY THIS CODE!!! ***/
  for(;;){}
  /*** Processor Expert end of main routine. DON'T WRITE CODE BELOW!!! ***/
} /*** End of main routine. DO NOT MODIFY THIS TEXT!!! ***/

/* END vga2scart */
/*
** ###################################################################
**
**     This file was created by Processor Expert 3.07 [04.34]
**     for the Freescale HCS08 series of microcontrollers.
**
** ###################################################################
*/
