/* =========================================================================== */
/* Project:        BlinkLed */
/* File:           BlinkLedmain.c */
/* Organization:   STMICRO */
/* Author:         STMICRO  (initial version generated by RBuilder) */
/* Date:           3/30/2005 */
/* =========================================================================== */

#include "ST7lib_config.h"
#include <stdio.h>
#include <intrist7.h>

unsigned char blinkingLed = 0x01;
/* --------------------------------------------------------------------------- */
/* Function:       PeriphInit */
/* Purpose:        Periph configuration at RESET */
/* Date:           3/30/2005 */
/* --------------------------------------------------------------------------- */
void PeriphInit ( void ) {
   
   DisableInterrupts ;
   /* A/D Converter initialization */
   ADC_Init(ADC_DEFAULT);
   ADC_Select_Channel(1);
   
   /* I/O Port initialization */
   IO_Init();
   IO_Output(IO_PUSH_PULL, IO_PORT_A, 0x9f);
   IO_Input(IO_PULL_UP, IO_PORT_B, 0x20);
   
   /* Interrupt controller initialization */
   EICR = 0x20; //Ei2 falling edge interrupt
   EISR = 0x20; //Interrupt for PB5 = BT6
   //ITC_Init();
   //ITC_ConfigureInterrupt (IT_PortB,(unsigned char)5,IT_EDGE_F);
   EnableInterrupts;

}/* end of  PeriphInit */

/* --------------------------------------------------------------------------- */
/* Function:       main() */
/* Purpose:        main routine */
/* Date:           3/30/2005 */
/* --------------------------------------------------------------------------- */
void main ( void ) {
   int i,tempo,Conv_Data1;
   const int array_Val_Gauge [8] = {0x2000, 0x1000, 0x800, 0x400, 0x200, 0x100, 0x80, 0x00};
   
   /* Configure the internal peripherals */
   PeriphInit();

   while ( 1 ){
      
      {
         
         ADC_Select_Channel(1);                    /* Select channel 1  */
         ADC_Enable();                             /* Start conversion ADON bit is set */
         while ( !ADC_Test_Conversn_Complete() ) ; /* Wait till conversion completes */
         Conv_Data1 = ADC_Conversn_Read();         /* Read converted value */
         if ( !Conv_Data1 ) {
            tempo = 0x4000;                      /* Tempo set at max value */
         }
         else {
            Conv_Data1 >>= 7;                    /* Keep the 3 Most Significant Bits */
            tempo = array_Val_Gauge[Conv_Data1]; /* Get the corresponding tempo value */
         }
         
         for(i=0;i<tempo;i++);                   /* Tempo = elapse time between high and low value for Led */
         switch(blinkingLed){
          case 1:
           IO_Write (IO_PORT_A,IO_PIN_1,IO_DATA_HIGH);   /* D1 light off */
           IO_Write (IO_PORT_A,IO_PIN_0,IO_DATA_TOGGLE); /* D0 blinks */
           break;
          case 2:
           IO_Write (IO_PORT_A,IO_PIN_0,IO_DATA_HIGH);   /* D0 light off */
           IO_Write (IO_PORT_A,IO_PIN_1,IO_DATA_TOGGLE); /* D1 blinks */
           break;
          default:
           IO_ByteWrite(IO_PORT_A,0x9F);
           break;
         }
                          
         ADC_Disable();
      }
    
   }
}/* end of main */


