/*
*******************************************************************************
* COPYRIGHT 2003 STMicroelectronics
* Source File Name : main.c                      
* Group            : IPSW,CMG - IPDF
* Author           : MCD Application Team
* Date First Issued: 22/9/2003
********************************Documentation**********************************
* General Purpose -This file shows the application example for ST72f561 device.
********************************RevisionHistory********************************
_______________________________________________________________________________
Date :22/9/2003 	       Release:2.0  
Date :28/04/04                 MISRA changes                                     
******************************************************************************/
#include "st7lib_config.h"    /* File for user to select the required device */

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

void main(void)         
{  
    unsigned char TIMER8_OCR_VALUE = 0x50;                                    
/* Configuring the Port B pin 0 & 2 as floating for ICAP1 and ICAP2 */
    IO_Input(IO_FLOATING,IO_PORT_B,((unsigned char)IO_PIN_0 | (unsigned char)
                                                                    IO_PIN_2));
    TIMER8_Init (TIMER8_FCPU_8); /* Timer8 Clock to Fcpu/8 and reset counter */
    EnableInterrupts                              /* Clear I bit in CC reg  */
    /*     Timer8 compares 0x50 with free running counter                    */  
    TIMER8_OCMP_Mode(TIMER8_OCMP_2,TIMER8_OCR_VALUE,TIMER8_OUTPUT_R );  
    while(!(TIMER8_Status_Flag(TIMER8_FLAG_OCF2)== TRUE));
    TIMER8_Clear_Flag(TIMER8_FLAG_OCF2);       /* Clear output compare2 flag */
    TIMER8_IT_Enable(TIMER8_ICAP_IT_ENABLE);    /*  Enable capture interrupt */
                                            /*Detect rising edge at ICAP2 pin*/
    TIMER8_ICAP_Mode(TIMER8_ICAP_2,TIMER8_EDGE_1);


    /*        Run TIMER8 in PWM mode if _Enable_PWM_ is  defined             */
#ifdef _Enable_PWM_                 /*   Generate PWM of frequency of 10 KHz */
    TIMER8_PWM_Mode(TIMER8_OUTPUT1_F,TIMER8_OUTPUT2_R,0x1C,0x5F);
#endif                                                         

  /*         Run TIMER8 in OPM mode if  _Enable_OPM_ is  defined             */
#ifdef _Enable_OPM_ 
    TIMER8_Clear_Flag(TIMER8_FLAG_ICF1);      /* Clear  Input capture1  Flag */
    TIMER8_Mode_Disable(TIMER8_MODE_5);                       /* Disable PWM */
                                                              /* 0.1ms pulse */
    TIMER8_OPM_Mode(TIMER8_EDGE_1,TIMER8_OUTPUT1_F,TIMER8_OUTPUT2_R,0x5F); 
#endif                                    
    while(1);                                           /* For testing only  */
}
                                                             /* Program  end */

/* ----------------------------------------------------------------------------
 ROUTINE NAME : TIMER8_IT_Routine
 INPUT        : None
 OUTPUT       : None 
 DESCRIPTION  : Interrupt service routine 
 COMMENTS     : This gets automatically executed when any of the timer8 
                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.For configuring the port pins,I/O library is used.
 ----------------------------------------------------------------------------*/
#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 TIMER8_IT_Routine(void)
{
    unsigned int CAP2_Value;                     /*  Define local variables  */                                    
    unsigned char i,Temp;    
    
    if(T8CSR & 0x10)
/* if(TIMER8_Status_Flag(TIMER8_FLAG_ICF2)==TRUE)       Call to Check ICF2   */
    {
        Temp = T8CSR;                                         /* Clear ICF2  */
        Temp = T8IC2R;                     
/* TIMER8_Clear_Flag(TIMER8_FLAG_ICF2);                  Call to clear ICF2  */                      
/* call to get capture value and also clear ICF2                             */
        CAP2_Value = TIMER8_ICAP_Getvalue(TIMER8_ICAP_2);     
                                                /* Port PA0 pushpull output  */
        IO_Output(IO_PUSH_PULL,IO_PORT_A,IO_PIN_0);

        IO_Write(IO_PORT_A,IO_PIN_0,IO_DATA_HIGH);     /* Port PA0 made high */        
        for(i=0;i<=250;i++)                                       /* Delay   */
        {
             Nop
        }  
        IO_Write(IO_PORT_A,IO_PIN_0,IO_DATA_LOW);        /* Port A0 made low */
    }
}

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