/*******************************************************************************
--------------------------------------------------------------------------------
*** adc_pwm example
  
 This example uses the adc and IO of the ST7Lite49M and the REva board.
 You can duplicate and modify it as a starting point for your projects.
 
--------------------------------------------------------------------------------
*** FILES

  
--------------------------------------------------------------------------------
*** JUMPERS

 On the REva board, you must set most jumpers:
 OUTPUT: D0, D1, D3->D7
 ANALOG: POT
 
 On the daughter board, select the RLink clock by linkinl
 the "DNG" pin to OSC1.
 
--------------------------------------------------------------------------------
*** POWER
 If you are not using the USB 5V as power, then you must change the value of the
  ALIMVOLTAGEMV macro to the real value of the power, in mV.
 
--------------------------------------------------------------------------------
********************************************************************************
--------------------------------------------------------------------------------

			Copyright (c), RAISONANCE

--------------------------------------------------------------------------------
 The present source code which is for guidance only aims at providing
 customers with information regarding their products in order for them to
 save time. As a result, RAISONANCE shall not be held liable for any
 direct indirect or consequential damages with respect to any claims arising
 from the content of such a source code and/or the use made by customers of 
 the information contained herein in connection with their products.
--------------------------------------------------------------------------------
*******************************************************************************/
 

/* =========================================================================== */
/* Project:        adc */
/* File:           main.c */
/* Organization:    */
/* Author:          */
/* Date:           27/04/2007 */
/* =========================================================================== */

#include "ST7lib_config.h"

//that's the maximum value returned by the ADC.
#define MAX_ADC_VAL (0x03FF)

/* --------------------------------------------------------------------------- */
/* Function:       PeriphInit */
/* Purpose:        Periph configuration at RESET */
/* Date:            */
/* --------------------------------------------------------------------------- */
void PeriphInit ( void ) 
{
   /* I/O Port initialization */
   IO_Init();
   IO_Output(IO_PUSH_PULL, IO_PORT_A, 0xff);
   /* A/D Converter initialization */
   ADC_Init(ADC_DEFAULT);
   ADC_Select_Channel(3);          
}/* end of  PeriphInit */

/* --------------------------------------------------------------------------- */
/* Function:       main() */
/* Purpose:        main routine */
/* Date:           27/4/2007 */
/* --------------------------------------------------------------------------- */
void main ( void )
{
   
	unsigned long AdcRead;
  /* Configure the internal peripherals */
   PeriphInit();

	//main loop
   while ( 1 )
   {
	//launch conversion
	ADC_Enable();
 	//wait for the conversion to be complete
	while(!ADC_Test_Conversn_Complete ());
	//read the value	
	AdcRead = ADC_Conversn_Read ();
	//end conversion
	ADC_Disable();
	
	//convert in percentage
  	AdcRead *= 8;
	AdcRead /= MAX_ADC_VAL; //max value is 0x3FF
   
   //Put it on portA       
   IO_ByteWrite(IO_PORT_A, 0xFF<<AdcRead);
   }
}/* end of main */


