/*
*******************************************************************************
* COPYRIGHT 2002 STMicroelectronics
* Source File Name : main.c                      
* Group            : IPSW,CMG-IPDF
* Author           : MCD Application Team
* Date First Issued: 5/03/2002
********************************Documentation**********************************
* General Purpose - This file shows the application example for   
                    ST72f521 device and shows the use of PWMART functions. 
********************************RevisionHistory********************************
_______________________________________________________________________________
Date : 5/03/2002    Release : 1.0
Date : 23/9/2003	Release : 2.0
                    1.Updated for Library 2.0 release		      
                    2.Removed pwmart_hr.h reference
*******************************************************************************/
#include "ST7lib_config.h"                               /*  Select ST72F521  */

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

void main(void)
{    
    unsigned char Counter_Data = 0xB0;
    unsigned char Compare_Data = 0xCA;
    unsigned char DutyCycle_Data = 0xCA;

                                  /* ARTIC1,ARTIC2,ARTCLK as floating input  */ 
    IO_Input (IO_FLOATING,IO_PORT_B,((unsigned char)IO_PIN_4 | 
                        ((unsigned char)IO_PIN_5 | (unsigned char)IO_PIN_6))); 
    /* Initialise the timer with external clock frequency 
                                              and overflow interrupt enabled  */
    PWMART_Init( ((unsigned char)PWMART_EXCLK | 
                                         (unsigned char)PWMART_OVF_IT_ENABLE)); 
                                                /* clear I bit in CC register */
    EnableInterrupts                     /* Macro defined in st7lib_config.h  */           
                                             /* Autoload the counter with ARR */
    PWMART_Counter_Enable(PWMART_REG_ARR,Counter_Data,PWMART_CLK_1);
                                         /* Output compare signal on PWM2 pin */
    PWMART_OCMP_Mode(PWMART_OCMP_2,PWMART_POLARITY_0,Compare_Data);  
              /* To detect  rising edge at capture1,capture1 interrupt enable */
    PWMART_ICAP_Mode(PWMART_ICAP_1,((unsigned char)PWMART_SENSITIVITY_R | 
                                        (unsigned char)PWMART_ICAP1_IT_ENABLE));
                                                    /* PWM signal on PWM1 pin */
    PWMART_PWM_Mode(PWMART_1,PWMART_POLARITY_0,DutyCycle_Data); 

    while(1);                                             /* For testing only */
} 



/* ----------------------------------------------------------------------------
 ROUTINE NAME : PWMART_IT_Routine
 INPUT        : None
 OUTPUT       : None 
 DESCRIPTION  : Interrupt service routine 
 COMMENTS     : This gets automatically executed when any of the PWMART 
                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.                
 -----------------------------------------------------------------------------*/
#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 PWMART_IT_Routine(void)
{
    unsigned char CAP1_Value; 
    unsigned char i,Temp; 

    if(PWMART_Status_Flag(PWMART_FLAG_OVF) == TRUE)   
	{                        
        PWMART_Clear_Flag(PWMART_FLAG_OVF);       /*  call only to clear OVF  */
	}

    if (PWMART_Status_Flag (PWMART_FLAG_ICF1)== TRUE)                          
    {

        PWMART_Clear_Flag(PWMART_FLAG_ICF1); /* call only to clear ICF1  */
                            /*  call to get capture value and also clear ICF1 */
        CAP1_Value =  PWMART_ICAP_Getvalue(PWMART_ICAP_1);                                  
                                                     /* Routine for user code */
        IO_Output (IO_PUSH_PULL,IO_PORT_C,IO_PIN_0);
        IO_Write (IO_PORT_C,IO_PIN_0,IO_DATA_HIGH);    /* Turn ON LED at PC0  */
        
        for ( i=0;i<=250;i++)                                      /*  delay  */ 
        {
            Nop
        }
        IO_Write (IO_PORT_C,IO_PIN_0,IO_DATA_LOW);     /* Turn ON LED at PC0  */
    }
}

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