/** ###################################################################
**     THIS COMPONENT MODULE IS GENERATED BY THE TOOL. DO NOT MODIFY IT.
**     Filename  : HSync.C
**     Project   : vga2scart
**     Processor : MC9S08QD4CPC
**     Component : Capture
**     Version   : Component 02.211, Driver 01.27, CPU db: 3.00.002
**     Compiler  : CodeWarrior HCS08 C Compiler
**     Date/Time : 7.7.2010, 0:50
**     Abstract  :
**         This bean "Capture" simply implements the capture function
**         of timer. The counter counts the same way as in free run mode. On
**         the selected edge of the input signal (on the input pin), the current
**         content of the counter register is written into the capture
**         register and the OnCapture event is called.
**     Settings  :
**             Timer capture encapsulation : Capture
**
**         Timer
**             Timer                   : TPM1
**             Counter shared          : Yes
**
**         High speed mode
**             Prescaler               : divide-by-1
**           Maximal time for capture register
**             Xtal ticks              : 256
**             microseconds            : 8192
**             milliseconds            : 8
**             seconds (real)          : 0.008192
**             Hz                      : 122
**           One tick of timer is
**             nanoseconds             : 125
**
**         Initialization:
**              Timer                  : Enabled
**              Events                 : Enabled
**
**         Timer registers
**              Capture                : TPMC1V    [$0049]
**              Counter                : TPMCNT    [$0041]
**              Mode                   : TPMSC     [$0040]
**              Run                    : TPMSC     [$0040]
**              Prescaler              : TPMSC     [$0040]
**
**         Used input pin              : 
**             ----------------------------------------------------
**                Number (on package)  |    Name
**             ----------------------------------------------------
**                       7             |  PTA1_KBI1P1_TPM1CH1_ADC1P1
**             ----------------------------------------------------
**
**         Port name                   : PTA
**         Bit number (in port)        : 1
**         Bit mask of the port        : $0002
**
**         Signal edge/level           : falling
**         Priority                    : 
**         Pull option                 : up
**
**     Contents  :
**         GetCaptureValue - byte HSync_GetCaptureValue(HSync_TCapturedValue *Value);
**         GetPinValue     - bool HSync_GetPinValue(void);
**
**     Copyright : 1997 - 2009 Freescale Semiconductor, Inc. All Rights Reserved.
**     
**     http      : www.freescale.com
**     mail      : support@freescale.com
** ###################################################################*/

/* MODULE HSync. */

#include "Events.h"
#include "HSync.h"




/*
** ===================================================================
**     Method      :  HSync_GetCaptureValue (component Capture)
**
**     Description :
**         This method gets the last value captured by enabled timer.
**         Note: one tick of timer is
**               125 ns in high speed mode
**     Parameters  :
**         NAME            - DESCRIPTION
**       * Value           - A pointer to the content of the
**                           capture register
**     Returns     :
**         ---             - Error code, possible codes:
**                           ERR_OK - OK
**                           ERR_SPEED - This device does not work in
**                           the active speed mode
** ===================================================================
*/
/*
byte HSync_GetCaptureValue(HSync_TCapturedValue *Value)

**  This method is implemented as a macro. See HSync.h file.  **
*/

/*
** ===================================================================
**     Method      :  HSync_GetPinValue (component Capture)
**
**     Description :
**         The method reads the Capture pin value. The method is
**         available only if it is possible to read the pin value
**         (usually not available for internal signals).
**     Parameters  : None
**     Returns     :
**         ---             - Capture pin value.
**                           <true> - high level
**                           <false> - low level.
** ===================================================================
*/
/*
byte HSync_GetPinValue()

**  This method is implemented as a macro. See HSync.h file.  **
*/

/*
** ===================================================================
**     Method      :  HSync_Init (component Capture)
**
**     Description :
**         Initializes the associated peripheral(s) and the bean internal 
**         variables. The method is called automatically as a part of the 
**         application initialization code.
**         This method is internal. It is used by Processor Expert only.
** ===================================================================
*/
void HSync_Init(void)
{
  /* TPMSC: TOF=0,TOIE=0,CPWMS=0,CLKSB=0,CLKSA=0,PS2=0,PS1=0,PS0=0 */
  setReg8(TPMSC, 0x00);                /* Stop HW */ 
  /* TPMMOD: BIT15=0,BIT14=0,BIT13=0,BIT12=0,BIT11=0,BIT10=0,BIT9=0,BIT8=0,BIT7=0,BIT6=0,BIT5=0,BIT4=0,BIT3=0,BIT2=0,BIT1=0,BIT0=0 */
  setReg16(TPMMOD, 0x00U);             /* Disable modulo register */ 
  /* TPMCNTH: BIT15=0,BIT14=0,BIT13=0,BIT12=0,BIT11=0,BIT10=0,BIT9=0,BIT8=0 */
  setReg8(TPMCNTH, 0x00);              /* Reset counter */ 
  /* TPMC1V: BIT15=0,BIT14=0,BIT13=0,BIT12=0,BIT11=0,BIT10=0,BIT9=0,BIT8=0,BIT7=0,BIT6=0,BIT5=0,BIT4=0,BIT3=0,BIT2=0,BIT1=0,BIT0=0 */
  setReg16(TPMC1V, 0x00U);             /* Clear capture register */ 
  /* TPMSC: PS2=0,PS1=0,PS0=0 */
  clrReg8Bits(TPMSC, 0x07);            /* Set prescaler register */ 
  /* TPMC1SC: CH1F=0,CH1IE=1,MS1B=0,MS1A=0,ELS1B=1,ELS1A=0,??=0,??=0 */
  setReg8(TPMC1SC, 0x48);              /* Enable both interrupt and capture function */ 
}


/*
** ===================================================================
**     Method      :  Interrupt (component Capture)
**
**     Description :
**         The method services the interrupt of the selected peripheral(s)
**         and eventually invokes of the bean event(s).
**         This method is internal. It is used by Processor Expert only.
** ===================================================================
*/
ISR(HSync_Interrupt)
{
  (void)TPMC1SC;                       /* Dummy read to reset interrupt request flag */
  /* TPMC1SC: CH1F=0 */
  clrReg8Bits(TPMC1SC, 0x80);          /* Reset interrupt request flag */ 
  HSync_OnCapture();                   /* Invoke user event */
}



/* END HSync. */

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