/* =========================================================================== */
/* Project:        ADC_LEDs */
/* File:           ADC_LEDsmain.c */
/* Organization:    */
/* Author:           (initial version generated by RBuilder) */
/* Date:           3/21/2006 */
/* =========================================================================== */

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


/* --------------------------------------------------------------------------- */
/* Function:       PeriphInit */
/* Purpose:        Periph configuration at RESET */
/* Date:           3/21/2006 */
/* --------------------------------------------------------------------------- */
void PeriphInit ( void ) {
   
   /* 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);
   
   
}/* end of  PeriphInit */

/* --------------------------------------------------------------------------- */
/* Function:       main() */
/* Purpose:        main routine */
/* Date:           3/21/2006 */
/* --------------------------------------------------------------------------- */
void main ( void ) {
   
   /* Configure the internal peripherals */
   PeriphInit();

   while ( 1 ){
      
      {
         /* * * * Sample code for the ADC * * *
         This code works with the REva evaluation board. It performs the following:
            a. Measurement of the voltage set by the potentiometer
            b. Translation to a value that fits into the range [0,7] 
            c. Copy of the resulting value to the Port A (LEDs as a gauge). */
         int Conv_Data1;
         const char array_Val_Gauge [8] = {0xFE, 0xFC, 0xF8, 0xF0, 0xE0, 0xC0, 0x80, 0x00};
         
         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 ) {
            IO_ByteWrite(IO_PORT_A, 0xff);         /* Write Port A: all LEDs switched off */
         }
         else {
            Conv_Data1 >>= 7;                    /* Keep the 3 Most Significant Bits */
            IO_ByteWrite(IO_PORT_A, array_Val_Gauge[Conv_Data1]);   /* Write Port A */
         }
         ADC_Disable();
      }
      
      /* Insert your code here... */

   }
}/* end of main */

