/*
*******************************************************************************
* COPYRIGHT 2002 STMicroelectronics
* Source File Name : main.c                      
* Group            : IPSW,CMG - IPDF
* Author           : MCD Application Team
* Date First Issued: 6/3/2002
********************************Documentation**********************************
* General Purpose - This file shows the application example for   
                                  ST72f521 device. 
********************************RevisionHistory********************************
______________________________________________________________________________
Date :6/3/2002            Release:1.0  
Date :28/04/04         MISRA changes       
*******************************************************************************/
#include "st7lib_config.h"

//prototype declaration
void main(void);
void TIMERA_IT_Routine(void);

void main(void)        
{                                      
unsigned int OCR_VALUE = 0x2050;
IO_Input (IO_FLOATING,IO_PORT_F,((unsigned char)IO_PIN_5 | (unsigned char)
                                                                     IO_PIN_6));
                                         /* ICAP1,ICAP2 pins asloating input  */
TIMERA_Init (TIMER_FCPU_8);        /* Timer Clock to Fcpu/8 and reset counter */
EnableInterrupts                                    /* Clear I bit in CC reg  */
/* Timer compares 0x2050 with free running counter                            */
TIMERA_OCMP_Mode(TIMER_OCMP_2,OCR_VALUE,TIMER_OUTPUT_R );   
while(!(TIMERA_Status_Flag(TIMER_FLAG_OCF2)== TRUE));
TIMERA_Clear_Flag(TIMER_FLAG_OCF2);             /* Clear output compare2 flag */
TIMERA_IT_Enable(TIMER_ICAP_IT_ENABLE);          /*  Enable capture interrupt */
TIMERA_ICAP_Mode(TIMER_ICAP_2,TIMER_EDGE_1); /*Detect rising edge at ICAP2 pin*/

/*  Run TIMERA in PWM mode if _Enable_PWM_ is  defined                        */
#ifdef _Enable_PWM_                                       /*   Generate PWM   */
TIMERA_PWM_Mode(TIMER_OUTPUT1_F,TIMER_OUTPUT2_R,(unsigned int)0x001C,
                                                          (unsigned int)0x005F);
#endif                                                         

/*  Run TIMERA in OPM mode if  _Enable_OPM_ is  defined                       */
#ifdef _Enable_OPM_ 
TIMERA_Clear_Flag(TIMER_FLAG_ICF1);            /* Clear  Input capture1  Flag */
TIMERA_Mode_Disable(TIMER_MODE_5);                             /* Disable PWM */
TIMERA_OPM_Mode(TIMER_EDGE_1,TIMER_OUTPUT1_F,TIMER_OUTPUT2_R,
                                                          (unsigned int)0x1383); 
#endif                                                          /*  5ms pulse */
while(1);                                                /* For testing only  */
}
/* Program  end */

/* ----------------------------------------------------------------------------
 ROUTINE NAME : TIMERA_IT_Routine
 INPUT        : None
 OUTPUT       : None 
 DESCRIPTION  : Interrupt service routine 
 COMMENTS     : This gets automatically executed when any of the timer 
                interrupt is enabled. If  the same functions are called in the
                main Tree and the interrupt Tree, the function Re-entrant error 
                occurs in case COSMIC compiler is used with models other than 
                stack models.
                The timer_hr.h is included as the actual hardware register are
                read to clear the flags.For configuring the port pins,I/O 
                library is used.
 -----------------------------------------------------------------------------*/
#ifdef USE_TIMERA 
#ifdef _HIWARE_                                   /* Test for HIWARE Compiler */ 
#pragma TRAP_PROC SAVE_REGS             /* Additional registers will be saved */
#else
#ifdef _COSMIC_                                   /* Test for Cosmic Compiler */
@interrupt                           
#else
#error "Unsupported Compiler!"                 /* Compiler Defines not found! */ 
#endif
#endif
void TIMERA_IT_Routine(void)
{
 unsigned int CAP2_Value;                         /*  Define local variables  */                                    
 unsigned char i,Temp;    
if(TACSR & 0x10)
/* if(TIMERA_Status_Flag(TIMER_FLAG_ICF1)==TRUE)         Call to Check ICF2   */
{
            Temp = TACSR;                                      /* Clear ICF2  */
            Temp = TAIC2LR;                     
/* TIMERA_Clear_Flag(TIMER_FLAG_ICF2);                    Call to clear ICF2  */                      
/* call to get capture value and also clear ICF2                              */
CAP2_Value = TIMERA_ICAP_Getvalue(TIMER_ICAP_2);     
                                                 /* Port PB0 pushpull output  */
IO_Output (IO_PUSH_PULL,IO_PORT_B,IO_PIN_0);
IO_Write (IO_PORT_B,IO_PIN_0,IO_DATA_HIGH);            /* Turn ON LED at PB0  */                                                                                                                                                              //turn on LED when input capture occurs
for(i=0;i<=250;i++)                                                /* Delay   */
{
 Nop
}  
IO_Write (IO_PORT_B,IO_PIN_0,IO_DATA_LOW);             /* Turn ON LED at PC0  */
}
}
#endif 

/******************** (c) 2002  ST Microelectronics *************END OF FILE***/
