/* 
*******************************************************************************
THIS INFORMATION (or THIS SOFTWARE or THIS DOCUMENT) IS FOR GUIDANCE ONLY. 
ST MAKES NO WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, WITH REGARD TO THIS 
SOFTWARE nor for any infringement of patents or other rights of third parties 
which may result from its use.

ST MICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, INDIRECT 
INCIDENTAL OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING 
IN CONNECTION WITH OR ARISING FROM THE FURNISHING, PERFORMANCE, OR USE 
OF THIS SOFTWARE.

Specifications mentioned in this publication are subject to change without 
notice. This publication supersedes and replaces all information previously 
supplied. STMicroelectronics products are not authorized for use as critical 
components in life support devices or systems without the express written 
approval of STMicroelectronics.  
*******************************************************************************
COPYRIGHT 2003 STMicroelectronics
Source File Name : main.c
Group            : IPSW, CMG - IPDF
Author           : MCD Application Team
Date First Issued: 08/03/2002
********************************Documentation**********************************
General Purpose -Top Level file.This file calls the functions, written in 
     different source directories of ST7 Library.
     -This will run an application including SCI to hyperterminal communication 
      and ADC conversion on channel0.Both applications will run simultaneously.
     -SPI- M95040 EEPROM communication, means writing 10 bytes data on EEPROM 
      and reading data from EEPROM is included.
     -TIMER A will generate the rising edge by enabling output compare on PF4 
      pin. This generated signal is applied to PB5 pin , which is the input 
      capture for PWMART.In the Interrupt subroutine, User to PC communication 
      executed.
     -Program will wait for the external interrupt EI0. If this interrupt is 
      generated, it will run the application of I2C and 24C04 EEPROM.
*******************************************************************************
Hardware 
    -MDT2 application board to run all application except I2C-EEPROM.
     MDT1 Training board 2 to run I2C-EEPROM application.
     Hyperterminal on PC.
     Serial cabel from MDT2 serial port to PC communication port.
     PB5 and PF4 pins must be connected together.            
*******************************************************************************     
********************************RevisionHistory********************************
_______________________________________________________________________________
Date : 08/03/2002              	Release:  1.0
Date : 23/09/2003              	Release:  2.0
******************************************************************************/     
#include "ST7lib_config.h"                               /*Configuration File*/ 

/* Buffers Declaration to display messages on the Hyperterminal(PC) with using 
  SCI functions*/

const unsigned char buff1[]= {"\n\rST7 LIBRARY DEMO IS GOING ON\n\r"};
const unsigned char buff2[]= {"ADC CONVERSION GOING ON FOR Ch.0\n\r"};
const unsigned char buff3[]= {"ADC CONVERSION COMPLETED\n\r"};
const unsigned char buff4[]= {"SPI-EEPROM APPLICATION STARTED\n\r"};
const unsigned char buff5[]= {"10 bytes Data is written on EEPROM\n\r"};
const unsigned char buff6[]= {"SPI is reading EEPROM\n\r"};
const unsigned char buff7[]= {"Verifying the Read & Write data\n\r"};
const unsigned char buff8[]= {"SPI WORKING OK\n\r"};
const unsigned char buff9[]= {"TALK TO PC:\n\r"};
const unsigned char buff10[]= {"MESSAGE LENGTH (00-20)\n"};
const unsigned char buff11[]= {"\n\rMESSAGE:"};
const unsigned char buff12[]= {"\n\rPRESS EI0 TO RUN I2C APPLICATION"};
const unsigned char buff13[]= {"\n\rMDT1 BOARD IS NOT AVAILABLE"};
const unsigned char buff14[]= {"\n\rPRESS RESET TO RUN APPLICATION AGAIN"};
const unsigned char buff15[]= {"\n\n\rI2C-EEPROM APPLICATION STARTED"};
const unsigned char buff16[]= {"\n\rI2C writing 8 bytes data on EEPROM"};
const unsigned char buff17[]= {"\n\rI2C reading EEPROM\n\r"};
const unsigned char buff18[]= {"Verifying the Read & Write data\n\r"};
const unsigned char buff19[]= {"I2C WORKING FINE\n\r"}; 
const unsigned char buff20[]= {"DEMO IS OVER\n\r"};
const unsigned char buff21[]= {"GOOD DAY\n\r"};
const unsigned char Buff_Out1[]={11,12,13,15,57,97,37,54};
    
/* function for applying delay during application*/
extern void Delay(void);   
extern void Delay_1(void);
/*counter function starts after completing application*/
extern void counter (void);
/*Display function to display ADC converted data on seven segment LED*/
extern void display(unsigned char,unsigned char);
static unsigned char PC_Buffer[20],length[2];
static unsigned char len_message=0,length1=0,length2=0;
static unsigned char digit1,digit2,wdg=0;
static unsigned char Count, Wait;
static unsigned char Buff_In1[8];
static unsigned char R_DATA=0,TX_Add_Ret=0,TX_Byte_Ret=0,Read_status2=0,
               RX_Byte_Ret=0;
unsigned int ADC_Buffer[10];      /*ADC converted data buffer declaration*/
/******************************** For DEMO Applicaton use only**************/
//#define MDT2
#define MDT2_MDT1               

/******************************************************************************
                          MAIN APPLICATION BEGINS
******************************************************************************/
void main (void);                        // Prototype for main
void main (void)                    
{                                    
    /* Declaration of the variables used in the application*/

    unsigned char SPI_Buffer[10];/*SPI-EEPROM read data stored in this buffer*/               
    float ADC_DATA=0; 
    unsigned char i,sub_add=0x00;
   
    digit1=0;digit2=0;wdg=0;  
    
/******************************************************************************
    IO Library called to initialise the PB3 as output and PA0 as input 
******************************************************************************/
    
    IO_Init(); 
    IO_Output (IO_PUSH_PULL, IO_PORT_B, ((unsigned char)IO_PIN_3 | ((unsigned char)IO_PIN_6 | ((unsigned char)IO_PIN_7))));
    IO_Output (IO_PUSH_PULL, IO_PORT_A, ((unsigned char)IO_PIN_3 | ((unsigned char)IO_PIN_2)));
   // IO_Input (IO_PULL_UP    , IO_PORT_D, IO_PIN_0);
/******************************************************************************
    ITC Library called to set the priority, for SCI level 3 and for EI) and 
    PWMART level 2.
******************************************************************************/    

    ITC_Init ();    
    //ITC_SetPriority (IT_SCI, IT_LEVEL_3);
    ITC_SetPriority (((unsigned char)IT_EI0 | ((unsigned char)IT_PWMART | ((unsigned char)IT_TIMERB | ((unsigned char)IT_TIMERA)))), IT_LEVEL_2);
    ITC_ConfigureInterrupt (IT_PortA, (unsigned char)0, IT_EDGE_F);
    
/******************************************************************************
    MCC Library called to initialise PF0 pin for MCO enable and RTC timer
******************************************************************************/

    MCC_Init(MCC_DEFAULT);
    MCC_Init(MCO_ENABLE);
    MCC_RTC_Timer(MCC_RTC_1);

/******************************************************************************
    SCI Library called to initialise sci peripheral with its parity disable, 
   and baudrate 19200, 8 bit data and 1 bit stop bit.
******************************************************************************/    

    SCI_Init(SCI_DEFAULT_PARAM1, SCI_DEFAULT_PARAM2);
    SCI_Select_Baudrate(SCI_PR_13 | SCI_TR_2 | SCI_RR_2);
    EnableInterrupts                                 /*Enable the interrupts*/
    
    SCI_Mode(SCI_TX_ENABLE);                       /*SCI transmit enable mode*/
   // SCI_IT_Enable(SCI_TRANSMIT_REGISTER_READY);/*SCI transmit interruptenable*/
    SCI_PutBuffer(buff1,(unsigned char)sizeof(buff1));       /*Display the message on the hyperterminal*/

/******************************************************************************
    ADC Library called to initialise channel to be applied with analog data 
    and speed of conversion
******************************************************************************/    

    ADC_Init (ADC_SPEED);	
    ADC_Select_Channel((unsigned char)0);
    Delay();
    while(!(SCI_IsTransmitCompleted()));
 
/******************************************************************************
    SCI and ADC application run simultaneously for one message display on 
    hyperterminal and stored the 10 bytes of conversion in ADC_Buffer[10].
******************************************************************************/    

    SCI_PutBuffer(buff2,(unsigned char)sizeof(buff2));
    ADC_Enable ();                                     /*Start ADC conversion*/
    for (i=0;i<10;i++)
    {
      while (!(ADC_Test_Conversn_Complete ()));           /*check for the EOC*/
      ADC_Buffer[i]= ADC_Conversn_Read ();         /*store the converted data*/
      ADC_DATA = (float)(ADC_DATA + ADC_Buffer[i]);
    }
    ADC_Disable ();                                         /*Disable the ADC*/
    
    while(!(SCI_IsTransmitCompleted()));
    Delay();
    ADC_DATA = (ADC_DATA / 10); 
    ADC_DATA = ((ADC_DATA * 5)/1024);
    i=0;
    while (ADC_DATA > i)
    {
    	i++;
    }
    digit1 = (unsigned char)(i - 1);	
    ADC_DATA = (float)((ADC_DATA - digit1) * 10.0);
    i = 0;
    while (ADC_DATA > i)
    {
    	i++;
    }
    digit2 = (unsigned char)(i - 1);	
    SCI_PutBuffer(buff3,(unsigned char)sizeof(buff3));              
    while(!(SCI_IsTransmitCompleted()));
    Delay();
    SCI_PutBuffer(buff4,(unsigned char)sizeof(buff4));              
    while(!(SCI_IsTransmitCompleted()));
    
/******************************************************************************
                 SPI to EEPROM (95040) APPLICATION STARTS
      Initialise the SPI with CPOL, CPHA, baudrate(fcpu/16) and master mode                 
******************************************************************************/    

    SPI_Init(((unsigned char)SPI_DEFAULT | ((unsigned char)SPI_ENABLE | ((unsigned char)SPI_BAUDRATE_16 | ((unsigned char)SPI_CLK_PP_0)))),
    	     SPI_MSTR_SW);  
    
    IO_Write (IO_PORT_B, IO_PIN_3, IO_DATA_LOW);                /*PB3 pin low*/
    SPI_PutByte((unsigned char)0x01);                           /*access the EEPROM register*/
    while(!(SPI_IsTransmitCompleted()==SPI_TRANSMIT_OK));
    SPI_Clear_Flags();       /*check transmission complete and clear flag set*/
    SPI_PutByte((unsigned char)0xF0);              /*clear all flags in the EEPROM registers*/
    while(!(SPI_IsTransmitCompleted()==SPI_TRANSMIT_OK));
    SPI_Clear_Flags();                                       
    IO_Write (IO_PORT_B, IO_PIN_3, IO_DATA_HIGH);              /*set PB3 High*/
    
/******************************************************************************
                    SPI to EEPROM (95040) WRITE OPERATION
******************************************************************************/    

    for (i=0;i<10;i++)
    { 
      IO_Write (IO_PORT_B, IO_PIN_3, IO_DATA_LOW);              /*set PB3 low*/
      SPI_PutByte((unsigned char)0x06);                      /*access the register of EEPROM*/
      while(!(SPI_IsTransmitCompleted()==SPI_TRANSMIT_OK));
      SPI_Clear_Flags();              /*check for the successful transmission*/                          
      IO_Write (IO_PORT_B, IO_PIN_3, IO_DATA_HIGH);            /*set PB3 high*/
    
      IO_Write (IO_PORT_B, IO_PIN_3, IO_DATA_LOW);              /*set PB3 low*/   	     
      SPI_PutByte((unsigned char)0x02);                 /*write in the data memory of EEPROM*/
      while(!(SPI_IsTransmitCompleted()==SPI_TRANSMIT_OK));
      SPI_Clear_Flags();              /*check for the successful transmission*/
      SPI_PutByte(sub_add);                  /*load the sub address of EEPROM*/
      while(!(SPI_IsTransmitCompleted()==SPI_TRANSMIT_OK));
      SPI_Clear_Flags();              /*check for the successful transmission*/
      SPI_PutByte((unsigned char)ADC_Buffer[i]);       /*write data on EEPROM*/
      while(!(SPI_IsTransmitCompleted()==SPI_TRANSMIT_OK));
      SPI_Clear_Flags();              /*check for the successful transmission*/
      IO_Write (IO_PORT_B, IO_PIN_3, IO_DATA_HIGH);            /*set PB3 high*/
      sub_add++;
    
      do
      {
        IO_Write (IO_PORT_B, IO_PIN_3, IO_DATA_LOW);   	     
        SPI_PutByte((unsigned char)0x05);              /* Read the status register of EEPROM*/
        while(!(SPI_IsTransmitCompleted()==SPI_TRANSMIT_OK));
        SPI_Clear_Flags();            /*check for the successful transmission*/
        SPI_PutByte((unsigned char)0x01);                          /*generate 8 clock pulses*/
        while(!(SPI_IsTransmitCompleted()==SPI_TRANSMIT_OK));
        SPI_Clear_Flags();            /*check for the successful transmission*/
        IO_Write (IO_PORT_B, IO_PIN_3, IO_DATA_HIGH);          /*set PB3 high*/
      } while (SPI_GetByte() & 0x01);        /*check write complete in EEPROM*/
    }
    Delay();
    
    /************DISPLAY MESSAGES FOR SPI WRITE OPERATION OVER****************/
    SCI_PutBuffer(buff5,(unsigned char)sizeof(buff5));              
    while(!(SCI_IsTransmitCompleted()));
    Delay();
    SCI_PutBuffer(buff6,(unsigned char)sizeof(buff6));              
    while(!(SCI_IsTransmitCompleted()));

/******************************************************************************
                    SPI to EEPROM (95040) READ OPERATION
******************************************************************************/    

    sub_add=0x00;               
    for (i=0;i<10;i++)
    {
      IO_Write (IO_PORT_B, IO_PIN_3, IO_DATA_LOW);   	          /*set PB3 low*/
      SPI_PutByte((unsigned char)0x03);                  /*Set the EEPROM for read operation*/
      while(!(SPI_IsTransmitCompleted()==SPI_TRANSMIT_OK));      	 
      SPI_Clear_Flags();              /*check for the successful transmission*/
      SPI_PutByte(sub_add);                           /*load the read address*/
      while(!(SPI_IsTransmitCompleted()==SPI_TRANSMIT_OK));      	 
      SPI_Clear_Flags();              /*check for the successful transmission*/                         
      SPI_PutByte((unsigned char)0x01);                            /*generate 8 clock pulses*/
      while(!(SPI_IsTransmitCompleted()==SPI_TRANSMIT_OK));
      while(!(SPI_IsReceptionCompleted()==SPI_RECEIVE_OK));
      SPI_Buffer[i] = SPI_GetByte();               /*get the data from EEPROM*/
      IO_Write (IO_PORT_B, IO_PIN_3, IO_DATA_HIGH);            /*set PB3 high*/
      sub_add++;
    }                                  
    Delay();
    Delay();

    /****************DISPLAY MESSAGE FOR SPI OPERATION OVER******************/

    SCI_PutBuffer(buff7,(unsigned char)sizeof(buff7));              
    while(!(SCI_IsTransmitCompleted()));                          
    
/******************************************************************************
       VERIFY THE DATA FOR SPI to EEPROM (95040) READ AND WRITE OPERATION
******************************************************************************/       

    for (i=0;i<10;i++)
    {
      while (!((unsigned char)ADC_Buffer[i]==SPI_Buffer[i]));
    }
    Delay();
    
    SCI_PutBuffer(buff8,(unsigned char)sizeof(buff8));              
    while(!(SCI_IsTransmitCompleted()));  

/******************************************************************************
    TIMER A Library called to generate the rising edge at the pin PF4 
******************************************************************************/    

    TIMERA_Init(TIMER_FCPU_8); 
    TIMERA_OCMP_Mode(TIMER_OCMP_1,0xf000,TIMER_OUTPUT_R);

/******************************************************************************
    PWMART Library called to capture the rising edge, generated by TIMER A,
    applied on PB5.
******************************************************************************/ 

    PWMART_Init(PWMART_DEFAULT);
    PWMART_Counter_Enable(PWMART_REG_ARR,(unsigned char)0xB0,PWMART_CLK_1);
    PWMART_ICAP_Mode(PWMART_ICAP_1,((unsigned char)PWMART_SENSITIVITY_R | 
                     ((unsigned char)PWMART_ICAP1_IT_ENABLE)));

    Delay_1();     
    WaitforInterrupt
   while(1)
   { 
    for (i=0;i<6;i++)
    {  
       Delay_1(); 
       SCI_PutBuffer(buff14,(unsigned char)sizeof(buff14));
       counter();	/*counter will start on 7segment display on MDT2 board*/
    }
    	WDG_Refresh(0x50);   
   } 	
}

/******************************************************************************
                                  END OF MAIN
******************************************************************************/

/******************************************************************************
Interrupt Subroutine for EI2
******************************************************************************/
#ifdef _HIWARE_                                  /* test for HIWARE Compiler */
#pragma TRAP_PROC SAVE_REGS            /* additional registers will be saved */
#else
#ifdef _COSMIC_                                  /* test for Cosmic Compiler */
@interrupt                                      /* Cosmic interrupt handling */
#else
#error"Unsupported Compiler!"                 /* Compiler Defines not found! */
#endif
#endif

void EI2_IT_Routine (void)
{
    unsigned int i;
                                               
    for (i = 0; i < 5000; i++)
    {
      Nop
    }
} 
 
/******************************************************************************
Interrupt Subroutine for External Interrupt0
******************************************************************************/
#ifdef _HIWARE_                                  /* Test for HIWARE Compiler */
#pragma TRAP_PROC SAVE_REGS            /* Additional registers will be saved */
#else
#ifdef _COSMIC_                                  /* Test for Cosmic Compiler */
@interrupt                                      /* Cosmic interrupt handling */
#else
#error"Unsupported Compiler!"                 /* Compiler Defines not found! */
#endif
#endif

void EI0_IT_Routine(void)
{   unsigned char i;
    I2C_TxErrCode_t Temp;
	  IO_Output(IO_PUSH_PULL,IO_PORT_A,IO_PIN_0);
		
#ifdef MDT2_MDT1
    while (!wdg)
    {
      wdg++;
      IO_Write (IO_PORT_D, ((unsigned char)IO_PIN_1 | ((unsigned char)IO_PIN_2 | ((unsigned char)IO_PIN_7 | ((unsigned char)IO_PIN_3 | ((unsigned char)IO_PIN_4 |
    		 ((unsigned char)IO_PIN_5 | ((unsigned char)IO_PIN_6))))))), IO_DATA_LOW);
      IO_Write (IO_PORT_B, IO_PIN_6, IO_DATA_HIGH);
      TIMERA_Init(TIMER_DEFAULT);
      TIMERA_Init(TIMER_FCPU_8);
      TIMERA_IT_Enable(TIMER_OVF_IT_ENABLE);

      SCI_Mode(SCI_TX_ENABLE);
      SCI_PutBuffer(buff15,(unsigned char)sizeof(buff15));
      while(!(SCI_IsTransmitCompleted()));
      I2C_Init(I2C_ENABLE_ACK);       /* For interrupt driven mode */
      I2C_MultiMaster_Config(); 
      I2C_Select_Speed(I2C_DEFAULT_PARAM2,50);/* Check for standard speed Mode */
      Delay_1();
      Delay_1();
      SCI_PutBuffer(buff16,(unsigned char)sizeof(buff16));
      while(!(SCI_IsTransmitCompleted()));

      I2C_Generate_Start();                       /* Check if START bit is set */
      while(!(I2C_IsTransmitCompleted()== I2C_START_OK));
      I2C_Load_Address(0xA0,I2C_TX_MODE);

      Temp = I2C_IsTransmitCompleted();
      while(!(I2C_IsTransmitCompleted()== I2C_ADD_TX_OK));

      switch (Temp)                           /* Check Add.transmission status */
      {
          case I2C_TX_AF:
               TX_Add_Ret = 0x01; 
               Nop
               Nop
               I2C_Error_Clear ();
               break;
          case I2C_TX_ARLO:
               TX_Add_Ret = 0x02;
               I2C_Error_Clear ();               
               break;
          case I2C_TX_BERR:
               TX_Add_Ret = 0x03;
               I2C_Error_Clear ();               
               break;
          case I2C_ADD_TX_OK: 
               TX_Add_Ret = 0x04;
               break;       
          case I2C_DATA_TX_OK:
               TX_Add_Ret = 0x05;
               break;  
          case I2C_TX_BUFFER_ONGOING:
               TX_Add_Ret = 0x06;
               break;
          default:
               break;
     }
 
      I2C_Load_Address(0x00,I2C_SUB_ADD);               /* Send EEPROM sub Address */

      Temp = I2C_IsTransmitCompleted();
      while(!(  I2C_IsTransmitCompleted()== I2C_DATA_TX_OK));
      switch (Temp)                          /* Check Add.transmission status */
      {
          case I2C_TX_AF:
               TX_Add_Ret = 0x01; 
               Nop
               Nop
               I2C_Error_Clear ();
               break;
          case I2C_TX_ARLO:
               TX_Add_Ret = 0x02;
               I2C_Error_Clear ();               
               break;
          case I2C_TX_BERR:
               TX_Add_Ret = 0x03;
               I2C_Error_Clear ();               
               break;
          case I2C_ADD_TX_OK: 
               TX_Add_Ret = 0x04;
               break;       
          case I2C_DATA_TX_OK:
               TX_Add_Ret = 0x05;
               break;  
          case I2C_TX_BUFFER_ONGOING:
               TX_Add_Ret = 0x06;
               break;
          default:
               break;
      } 

      switch (I2C_PutBuffer (Buff_Out1,sizeof(Buff_Out1))) /* Check data transmission status */
      {
          case I2C_TX_AF:
               TX_Byte_Ret = 0x01;
               I2C_Error_Clear () ;
               break;
          case I2C_TX_ARLO:                          /* Disconnect data line */
               TX_Byte_Ret = 0x02;
               I2C_Error_Clear () ;
               break;
          case I2C_TX_BERR:
               TX_Byte_Ret = 0x03;
               I2C_Error_Clear () ;
          case I2C_DATA_TX_OK:
               Nop
               TX_Byte_Ret = 0x04;
               break;
          default:
               break;
      }

      I2C_Generate_Stop();
     
      Delay_1();
      Delay_1();
      SCI_PutBuffer(buff17,(unsigned char)sizeof(buff17));
      while(!(SCI_IsTransmitCompleted()));
      I2C_Generate_Start();
      while(!(I2C_IsTransmitCompleted()== I2C_START_OK));
      I2C_Load_Address(0xA0,I2C_TX_MODE);             /* Send EEPROM Address */
      Temp = I2C_IsTransmitCompleted();     /* Check Add.transmission status */
      while(!( I2C_IsTransmitCompleted()== I2C_ADD_TX_OK));

      I2C_Load_Address(0x00,I2C_SUB_ADD);         /* Send EEPROM sub Address */
      while(!( I2C_IsTransmitCompleted()== I2C_DATA_TX_OK));
      I2C_Generate_Stop();                                  /* Generate Stop */


      I2C_Generate_Start();                            /* Generate START bit */   
      while(!(I2C_IsTransmitCompleted()== I2C_START_OK));
      I2C_Load_Address(0xA0,I2C_RX_MODE);             /* Send EEPROM Address */
    
      while(!( I2C_IsTransmitCompleted()== I2C_ADD_TX_OK));

      switch (I2C_GetBuffer (Buff_In1,sizeof(Buff_In1)))     /* Check data reception status */
      {
          case I2C_RX_AF:
               RX_Byte_Ret = 0x01;
               I2C_Error_Clear ();
               Nop
               Nop
               break;
          case I2C_RX_ARLO:
               RX_Byte_Ret = 0x02;
               I2C_Error_Clear ();
               break;
          case I2C_RX_BERR:
               RX_Byte_Ret = 0x03;
               I2C_Error_Clear ();
               break;
          case I2C_DATA_RX_OK:
               Nop  
              RX_Byte_Ret = 0x04;
               break; 
          default:
               break;
      }

      I2C_Peripheral_Disable();
      Delay_1();
      Delay_1();
      SCI_PutBuffer(buff18,(unsigned char)sizeof(buff18));
      while(!(SCI_IsTransmitCompleted()));
      for (i=0;i<8;i++)
      {
     	  while (!(Buff_In1[i] == Buff_Out1[i]));
      }     	
      Delay_1();
      SCI_PutBuffer(buff19,(unsigned char)sizeof(buff19));
      while(!(SCI_IsTransmitCompleted()));
      SCI_PutBuffer(buff20,(unsigned char)sizeof(buff20));
      while(!(SCI_IsTransmitCompleted()));
      SCI_PutBuffer(buff21,(unsigned char)sizeof(buff21));
      while(!(SCI_IsTransmitCompleted()));
      Delay_1(); 
//      SCI_IT_Disable((unsigned char)SCI_TRANSMIT_REGISTER_READY); 
                         /*Added to come back to main not to SCI_IT_Routine*/
 }      
#endif

#ifdef MDT2
      SCI_PutBuffer(buff13,(unsigned char)sizeof(buff13));
      while(!(SCI_IsTransmitCompleted()));
//      SCI_IT_Disable((unsigned char)SCI_TRANSMIT_REGISTER_READY); 
                          /*Added to come back to main not to SCI_IT_Routine*/
#endif    
    
}   

/******************************************************************************
Interrupt Subroutine for TLI
******************************************************************************/
#ifdef _HIWARE_                                  /* test for HIWARE Compiler */
#pragma TRAP_PROC SAVE_REGS            /* additional registers will be saved */
#else
#ifdef _COSMIC_                                  /* test for Cosmic Compiler */
@interrupt                                      /* Cosmic interrupt handling */
#else
#error"Unsupported Compiler!"                 /* Compiler Defines not found! */
#endif
#endif

void TLI_IT_Routine (void)
{
    volatile int i;
                                                /*Blink the LED*/
    for (i = 0; i < 5000; i++)
    {
      Nop
    }
    
}  

/******************************************************************************
Interrupt Subroutine for MCC
******************************************************************************/
#ifdef _HIWARE_                                  /* test for HIWARE Compiler */
#pragma TRAP_PROC SAVE_REGS            /* additional registers will be saved */
#else
#ifdef _COSMIC_                                  /* test for Cosmic Compiler */
@interrupt                                      /* Cosmic interrupt handling */
#else
#error"Unsupported Compiler!"                 /* Compiler Defines not found! */
#endif
#endif

void MCC_IT_Routine (void)
{    
}
                      
void EI1_IT_Routine (void)
{
}

void EI3_IT_Routine (void)
{
}                          

/******************************************************************************
Interrupt Subroutine for PWMART
******************************************************************************/
#ifdef _HIWARE_                                  /* test for HIWARE Compiler */
#pragma TRAP_PROC SAVE_REGS            /* additional registers will be saved */
#else
#ifdef _COSMIC_                                  /* test for Cosmic Compiler */
@interrupt                                      /* Cosmic interrupt handling */
#else
#error"Unsupported Compiler!"                 /* Compiler Defines not found! */
#endif
#endif

void PWMART_IT_Routine (void)
{   
/******************************************************************************
                SCI-PC(Hyperterminal) COMMUNICATION STARTS
******************************************************************************/    
    
    /********Declaration of the variables used for the TALK TO PC application*/
    
    SCI_RxError_t Err;
    PWMART_Clear_Flag(PWMART_FLAG_ICF1);
    PWMART_Mode_Disable(PWMART_ICAP1_IT_DISABLE);  /*Disable PWMART Interrupt*/
    Delay();
    IO_Write (IO_PORT_A, ((unsigned char)IO_PIN_3 | ((unsigned char)IO_PIN_2))    , IO_DATA_LOW);
    SCI_PutBuffer(buff9,(unsigned char)sizeof(buff9));         
    while(!(SCI_IsTransmitCompleted()));
    SCI_PutBuffer(buff10,(unsigned char)sizeof(buff10));/*asked user to pass message length */
    while(!(SCI_IsTransmitCompleted()));
   
/******************************************************************************
             Receive length of message to be written by user
******************************************************************************/    
    
    SCI_Mode(SCI_RX_ENABLE);
    Err =  SCI_GetBuffer(length,(unsigned char)2);
                                                        
    if(Err == SCI_RECEIVE_OK)                       /*Checks the error status*/
    {
      Nop                                                  /* Reception OK*/
    }                                                         
    else                                     /*Error Ocurred during reception*/
    {
      if(Err && SCI_NOISE_ERR)
      {
        SCI_Forced_Clear_Flag();
      }
      if(Err && SCI_OVERRUN_ERR)
      {
        SCI_Forced_Clear_Flag();
      }
      if(Err && SCI_FRAMING_ERR)
      {
        SCI_Forced_Clear_Flag();
      }
      if(Err && SCI_PARITY_ERR)
      {
        SCI_Forced_Clear_Flag();
      }
    } 
      
    IO_Input(IO_PULL_UP_IT,IO_PORT_A,IO_PIN_0);

    SCI_Mode(SCI_TX_ENABLE);
    SCI_PutBuffer(length,(unsigned char)2);
    while(!(SCI_IsTransmitCompleted()));
   
    SCI_Mode(SCI_TX_ENABLE);
    SCI_PutBuffer(buff11,(unsigned char)sizeof(buff11));
    while(!(SCI_IsTransmitCompleted()));

/******************************************************************************
          Conversion of received message length into decimal value
******************************************************************************/    
    
    length1 =(unsigned char)(length[0] - 48);
    length2 =(unsigned char)(length[1] - 48);
    len_message = (unsigned char)(((length1) * (10)) + length2);

/******************************************************************************
                    Reception of message written by user
******************************************************************************/        
    
    SCI_Mode(SCI_RX_ENABLE);
    Err = SCI_GetBuffer(PC_Buffer,len_message);    
    if(Err == SCI_RECEIVE_OK)                       /*Checks the error status*/
    {
      Nop                                                  /* Reception OK*/
    }                                                         
    else                                     /*Error Ocurred during reception*/
    {
      if(Err && SCI_NOISE_ERR)
      {
        SCI_Forced_Clear_Flag();
      }
      if(Err && SCI_OVERRUN_ERR)
      {
        SCI_Forced_Clear_Flag();
      }
      if(Err && SCI_FRAMING_ERR)
      {
        SCI_Forced_Clear_Flag();
      }
      if(Err && SCI_PARITY_ERR)
      {
        SCI_Forced_Clear_Flag();
      }
    }   

/******************************************************************************
              Transmit the same message received from user
******************************************************************************/

    SCI_Init(SCI_DEFAULT_PARAM1, SCI_DEFAULT_PARAM2);
    SCI_Select_Baudrate(SCI_PR_13 | SCI_TR_2 | SCI_RR_2);
    SCI_Mode(SCI_TX_ENABLE);
//    SCI_IT_Enable((unsigned char)SCI_TRANSMIT_REGISTER_READY);
    SCI_PutBuffer(PC_Buffer,(unsigned char)len_message);
    while(!(SCI_IsTransmitCompleted()));
   
    SCI_PutBuffer(buff12,(unsigned char)sizeof(buff12));
    while(!(SCI_IsTransmitCompleted())); 
}

void SPI_IT_Routine (void)
{
}

/******************************************************************************
Interrupt Subroutine for SCI
******************************************************************************/
#ifdef _HIWARE_                                  /* test for HIWARE Compiler */
#pragma TRAP_PROC SAVE_REGS            /* additional registers will be saved */
#else
#ifdef _COSMIC_                                  /* test for Cosmic Compiler */
@interrupt                                      /* Cosmic interrupt handling */
#else
#error"Unsupported Compiler!"                 /* Compiler Defines not found! */
#endif
#endif

void SCI_IT_Routine(void)
{  
    SCI_IT_Function();
}

/******************************************************************************
Interrupt Subroutine for TIMER
******************************************************************************/
#ifdef _HIWARE_                                  /* test for HIWARE Compiler */
#pragma TRAP_PROC SAVE_REGS            /* additional registers will be saved */
#else
#ifdef _COSMIC_                                  /* test for Cosmic Compiler */
@interrupt                                      /* Cosmic interrupt handling */
#else
#error"Unsupported Compiler!"                 /* Compiler Defines not found! */
#endif
#endif

void TIMERA_IT_Routine (void)
{  
	TIMERA_Clear_Flag(TIMER_FLAG_OVF);
}
            
/******************************************************************************
Interrupt Subroutine for TIMERB
******************************************************************************/
#ifdef _HIWARE_                                  /* test for HIWARE Compiler */
#pragma TRAP_PROC SAVE_REGS            /* additional registers will be saved */
#else
#ifdef _COSMIC_                                  /* test for Cosmic Compiler */
@interrupt                                      /* Cosmic interrupt handling */
#else
#error"Unsupported Compiler!"                 /* Compiler Defines not found! */
#endif
#endif

void TIMERB_IT_Routine (void)
{   
    if ( Count < 15 )
    {
      Count ++ ; 
    }
    else
    {
      Wait = 0 ;
      Count = 0 ;
    }
    	
    TIMERB_Clear_Flag(TIMER_FLAG_OVF);
}

void I2C_IT_Routine (void)
{
}

/******************************************************************************
                      USER DEFINED FUNCTIONS
******************************************************************************/
void counter(void)
{   
    IO_Output (IO_PUSH_PULL, IO_PORT_D, ((unsigned char)IO_PIN_1 | ((unsigned char)IO_PIN_2 |((unsigned char)IO_PIN_3 |((unsigned char)IO_PIN_4 
    		|((unsigned char)IO_PIN_5 | ((unsigned char)IO_PIN_6 | ((unsigned char)IO_PIN_7))))))));
    IO_Output (IO_PUSH_PULL, IO_PORT_A, ((unsigned char)IO_PIN_3 | ((unsigned char)IO_PIN_2)));
    IO_Write (IO_PORT_A, ((unsigned char)IO_PIN_3 | ((unsigned char)IO_PIN_2)), IO_DATA_HIGH);
		   
    IO_Write (IO_PORT_D, ((unsigned char)IO_PIN_1 | ((unsigned char)IO_PIN_2 | ((unsigned char)IO_PIN_3 | ((unsigned char)IO_PIN_4 | ((unsigned char)IO_PIN_5 |
    		 ((unsigned char)IO_PIN_6)))))), IO_DATA_HIGH);
    IO_Write (IO_PORT_D, IO_PIN_7, IO_DATA_LOW);
    
    Wait = 1 ;
    Count = 0;
    TIMERB_Init(TIMER_DEFAULT);
    TIMERB_Init(TIMER_FCPU_8);
    
    TIMERB_IT_Enable(TIMER_OVF_IT_ENABLE);
    while (Wait) ;   
    Wait = 1 ;
    
    IO_Write (IO_PORT_D, ((unsigned char)IO_PIN_4 | ((unsigned char)IO_PIN_5)), IO_DATA_HIGH);
    IO_Write (IO_PORT_D, ((unsigned char)IO_PIN_7 | ((unsigned char)IO_PIN_3 | ((unsigned char)IO_PIN_1 | ((unsigned char)IO_PIN_2 | ((unsigned char)IO_PIN_6))))), 
          		IO_DATA_LOW);
    while (Wait) ;   
    Wait = 1 ;
       
    IO_Write (IO_PORT_D, ((unsigned char)IO_PIN_1 | ((unsigned char)IO_PIN_3 | ((unsigned char)IO_PIN_4 | ((unsigned char)IO_PIN_7 | ((unsigned char)IO_PIN_6))))), 
    		IO_DATA_HIGH);
    IO_Write (IO_PORT_D, ((unsigned char)IO_PIN_2 | ((unsigned char)IO_PIN_5)), IO_DATA_LOW);
    while (Wait) ;   
    Wait = 1 ;

        
    IO_Write (IO_PORT_D, ((unsigned char)IO_PIN_4 | ((unsigned char)IO_PIN_3 | ((unsigned char)IO_PIN_5 | ((unsigned char)IO_PIN_7 | ((unsigned char)IO_PIN_6))))), 
    		IO_DATA_HIGH);
    IO_Write (IO_PORT_D, ((unsigned char)IO_PIN_1 | ((unsigned char)IO_PIN_2)), IO_DATA_LOW);
    while (Wait) ;   
    Wait = 1 ;

       
    IO_Write (IO_PORT_D, ((unsigned char)IO_PIN_4 | ((unsigned char)IO_PIN_2 | ((unsigned char)IO_PIN_5 | ((unsigned char)IO_PIN_7)))) , 
            		IO_DATA_HIGH);
    IO_Write (IO_PORT_D, ((unsigned char)IO_PIN_3 | ((unsigned char)IO_PIN_1 | ((unsigned char)IO_PIN_6))), IO_DATA_LOW);
    while (Wait) ;   
    Wait = 1 ;

        
    IO_Write (IO_PORT_D, ((unsigned char)IO_PIN_5 | ((unsigned char)IO_PIN_3 | ((unsigned char)IO_PIN_2 | ((unsigned char)IO_PIN_7 | ((unsigned char)IO_PIN_6))))), 
    		        IO_DATA_HIGH);
    IO_Write (IO_PORT_D, ((unsigned char)IO_PIN_4 | ((unsigned char)IO_PIN_1)), IO_DATA_LOW);
    while (Wait) ;   
    Wait = 1 ;

        
    IO_Write (IO_PORT_D, ((unsigned char)IO_PIN_7 | ((unsigned char)IO_PIN_2 | ((unsigned char)IO_PIN_3 | ((unsigned char)IO_PIN_1 | ((unsigned char)IO_PIN_5 |
    		        ((unsigned char)IO_PIN_6)))))), IO_DATA_HIGH);
    IO_Write (IO_PORT_D, IO_PIN_4, IO_DATA_LOW);
    while (Wait) ;   
    Wait = 1 ;

        
    IO_Write (IO_PORT_D, ((unsigned char)IO_PIN_3 | ((unsigned char)IO_PIN_4 | ((unsigned char)IO_PIN_5))) , IO_DATA_HIGH);
    IO_Write (IO_PORT_D, ((unsigned char)IO_PIN_1 | ((unsigned char)IO_PIN_2 | ((unsigned char)IO_PIN_6 | ((unsigned char)IO_PIN_7)))) , 
    		        IO_DATA_LOW);
    while (Wait) ;   
    Wait = 1 ;

        
    IO_Write (IO_PORT_D, ((unsigned char)IO_PIN_1 | ((unsigned char)IO_PIN_2 | ((unsigned char)IO_PIN_3 | ((unsigned char)IO_PIN_4 | ((unsigned char)IO_PIN_5 |
    		        ((unsigned char)IO_PIN_6 | ((unsigned char)IO_PIN_7))))))), IO_DATA_HIGH);
    while (Wait) ;   
    Wait = 1 ;

        
    IO_Write (IO_PORT_D, ((unsigned char)IO_PIN_7 | ((unsigned char)IO_PIN_2 | ((unsigned char)IO_PIN_3 | ((unsigned char)IO_PIN_4 | ((unsigned char)IO_PIN_5 |
    		        ((unsigned char)IO_PIN_6)))))), IO_DATA_HIGH);
    IO_Write (IO_PORT_D, IO_PIN_1, IO_DATA_LOW);
    while (Wait) ;   
    Wait = 1 ;

}


/******************************************************************************
Delay function to apply time delay in the application
******************************************************************************/ 
 void Delay(void)
{   volatile unsigned char i; volatile int j;
    for (i=0;i<8;i++)
    {
      for (j=0;j<1000;j++)
      {
       display (digit1,(unsigned char)1);
       display (digit2,(unsigned char)2);
      }
    }
} 

void Delay_1(void)
{   
    volatile unsigned char i; volatile int j;
    for (i=0;i<10;i++)
    {
      for (j=0;j<30000;j++); 
    } 
}

void display (unsigned char digit, unsigned char no)
{ 
    IO_Output (IO_PUSH_PULL, IO_PORT_D, ((unsigned char)IO_PIN_1 | ((unsigned char)IO_PIN_2 |((unsigned char)IO_PIN_3 |((unsigned char)IO_PIN_4 
              		|((unsigned char)IO_PIN_5 | ((unsigned char)IO_PIN_6 | ((unsigned char)IO_PIN_7))))))));  
    IO_Write (IO_PORT_D, ((unsigned char)IO_PIN_1 | ((unsigned char)IO_PIN_2 | ((unsigned char)IO_PIN_3 | ((unsigned char)IO_PIN_4 | ((unsigned char)IO_PIN_5 |
    		 ((unsigned char)IO_PIN_6 | ((unsigned char)IO_PIN_7))))))), IO_DATA_LOW);    		
    IO_Output (IO_PUSH_PULL, IO_PORT_A, ((unsigned char)IO_PIN_3 | ((unsigned char)IO_PIN_2)));

    switch (no)
    {
      case 1:
      IO_Write (IO_PORT_A, IO_PIN_2, IO_DATA_LOW);	
      IO_Write (IO_PORT_A, IO_PIN_3, IO_DATA_HIGH);	
      break;
    
      case 2:
      IO_Write (IO_PORT_A, IO_PIN_3, IO_DATA_LOW);	
      IO_Write (IO_PORT_A, IO_PIN_2, IO_DATA_HIGH);		
      break;
 
      default:
      break;

    }
	
    switch(digit)
    {
      case 0:
        IO_Write (IO_PORT_D, ((unsigned char)IO_PIN_1 | ((unsigned char)IO_PIN_2 | ((unsigned char)IO_PIN_3 | ((unsigned char)IO_PIN_4 | ((unsigned char)IO_PIN_5 |
    	          	 ((unsigned char)IO_PIN_6)))))), IO_DATA_HIGH);
      IO_Write (IO_PORT_D, IO_PIN_7, IO_DATA_LOW);
      break;
    
      case 1:
      IO_Write (IO_PORT_D, ((unsigned char)IO_PIN_4 | ((unsigned char)IO_PIN_5)), IO_DATA_HIGH);
      IO_Write (IO_PORT_D, ((unsigned char)IO_PIN_7 | ((unsigned char)IO_PIN_3 | ((unsigned char)IO_PIN_1 | ((unsigned char)IO_PIN_2 | ((unsigned char)IO_PIN_6))))), 
    	            	IO_DATA_LOW);
      break;
	
      case 2:	
      IO_Write (IO_PORT_D, ((unsigned char)IO_PIN_1 | ((unsigned char)IO_PIN_3 | ((unsigned char)IO_PIN_4 | ((unsigned char)IO_PIN_7 | ((unsigned char)IO_PIN_6))))), 
    	            	IO_DATA_HIGH);
      IO_Write (IO_PORT_D, ((unsigned char)IO_PIN_2 | ((unsigned char)IO_PIN_5)), IO_DATA_LOW);
      break;
        
      case 3:
      IO_Write (IO_PORT_D, ((unsigned char)IO_PIN_4 | ((unsigned char)IO_PIN_3 | ((unsigned char)IO_PIN_5 | ((unsigned char)IO_PIN_7 | ((unsigned char)IO_PIN_6))))), 
              		IO_DATA_HIGH);
      IO_Write (IO_PORT_D, ((unsigned char)IO_PIN_1 | ((unsigned char)IO_PIN_2)), IO_DATA_LOW);
      break;
        
      case 4:
      IO_Write (IO_PORT_D, ((unsigned char)IO_PIN_4 | ((unsigned char)IO_PIN_2 | ((unsigned char)IO_PIN_5 | ((unsigned char)IO_PIN_7)))) , 
    	          	IO_DATA_HIGH);
      IO_Write (IO_PORT_D, ((unsigned char)IO_PIN_3 | ((unsigned char)IO_PIN_1 | ((unsigned char)IO_PIN_6))), IO_DATA_LOW);
      break;
        
      case 5:
      IO_Write (IO_PORT_D, ((unsigned char)IO_PIN_5 | ((unsigned char)IO_PIN_3 | ((unsigned char)IO_PIN_2 | ((unsigned char)IO_PIN_7 | ((unsigned char)IO_PIN_6))))), 
    	          	IO_DATA_HIGH);
      IO_Write (IO_PORT_D, ((unsigned char)IO_PIN_4 | ((unsigned char)IO_PIN_1)), IO_DATA_LOW);
      break;                                  
    
      case 6:
      IO_Write (IO_PORT_D, ((unsigned char)IO_PIN_7 | ((unsigned char)IO_PIN_2 | ((unsigned char)IO_PIN_3 | ((unsigned char)IO_PIN_1 | ((unsigned char)IO_PIN_5 |
    	           ((unsigned char)IO_PIN_6)))))), IO_DATA_HIGH);
      IO_Write (IO_PORT_D, IO_PIN_4, IO_DATA_LOW);
      break;
        
      case 7:
      IO_Write (IO_PORT_D, ((unsigned char)IO_PIN_3 | ((unsigned char)IO_PIN_4 | ((unsigned char)IO_PIN_5))) , IO_DATA_HIGH);
      IO_Write (IO_PORT_D, ((unsigned char)IO_PIN_1 | ((unsigned char)IO_PIN_2 | ((unsigned char)IO_PIN_6 | ((unsigned char)IO_PIN_7)))) , 
    	        	IO_DATA_LOW);
      break;
        
      case 8:
      IO_Write (IO_PORT_D, ((unsigned char)IO_PIN_1 | ((unsigned char)IO_PIN_2 | ((unsigned char)IO_PIN_3 | ((unsigned char)IO_PIN_4 | ((unsigned char)IO_PIN_5 |
    	      	 ((unsigned char)IO_PIN_6 | ((unsigned char)IO_PIN_7))))))), IO_DATA_HIGH);
      break;
        
      case 9:
      IO_Write (IO_PORT_D, ((unsigned char)IO_PIN_7 | ((unsigned char)IO_PIN_2 | ((unsigned char)IO_PIN_3 | ((unsigned char)IO_PIN_4 | ((unsigned char)IO_PIN_5 |
    	      	 ((unsigned char)IO_PIN_6)))))), IO_DATA_HIGH);
      IO_Write (IO_PORT_D, IO_PIN_1, IO_DATA_LOW);
      break;    
      
      default:
      break;
    }
}
	

/**** (c) 2003   STMicroelectronics *************************** END OF FILE **/











