/******************** (C) COPYRIGHT 2006 STMicroelectronics ********************
* File Name          : main.c
* Author             : MCD Application Team
* Date First Issued  : 04/06/2006
* Description        : Short Example, Program using the ADC peripheral.
*                      The purpose of this program is to convert an analog input 
*                      using the ADC and display the result on the LEDs
*
*  !!! This example will work for the STR9 devices, NOT for the STR7 devices !!!
*
*             			On the REva board, you will need to plug the enable jumpers 
*                      for the 8 LEDs (D0->D7) and the Potentiometer (POT)
*                      
********************************************************************************
* History:
*  03/017/2006 : Created
*  03/022/2006 : RAISONANCE
		adapted for the REva board, use LEDs instead of timer as output
*******************************************************************************/
#include "91x_lib.h"

TIM_InitTypeDef   TIM_InitStructure;
ADC_InitTypeDef   ADC_InitStructure;
GPIO_InitTypeDef  GPIO_InitStructure;

//choose the ADC channel to use
#define ADC_CHANNEL ADC_Channel_6
#define ADC_CHANNEL_MODE ADC_Channel_6_Mode


int main(void)
{

  u16 Conversion_Value = 0;
  
  #ifdef DEBUG
    debug();
  #endif

  SCU_APBPeriphClockConfig(__ADC, ENABLE);    /* Enable the clock for the ADC */
  ADC_DeInit();                               /* ADC Deinitialization */
  
  SCU_APBPeriphClockConfig(__GPIO6, ENABLE);  /* Enable the clock for the GPIO6 */
  GPIO_DeInit(GPIO6);                         /* GPIO6 Deinitialization */

  /* Configure the GPIO4 pins as analog input */
  GPIO_ANAPinConfig(GPIO_ANAChannelALL, ENABLE);
  
  /* GPIO6 configuration (all GPIO6 as output) */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
  GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput;
  GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull;
  GPIO_InitStructure.GPIO_IPConnected = GPIO_IPConnected_Disable/*GPIO_IPConnected_Enable*/;
  GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt1;
  GPIO_Init(GPIO6,&GPIO_InitStructure);

  
  /* ADC Structure Initialization */
  ADC_StructInit(&ADC_InitStructure);

  /* Configure the ADC in continuous mode conversion */
  ADC_InitStructure.ADC_CHANNEL_MODE = ADC_NoThreshold_Conversion;
  ADC_InitStructure.ADC_Select_Channel = ADC_CHANNEL;
  ADC_InitStructure.ADC_Scan_Mode = DISABLE;
  ADC_InitStructure.ADC_Conversion_Mode = ADC_Continuous_Mode;
  
  /* Enable the ADC */
  ADC_Cmd(ENABLE);

  /* Prescaler config */
  ADC_PrescalerConfig(0xF);

  /* Configure the ADC */
  ADC_Init(&ADC_InitStructure);

  /* Start the conversion */
  ADC_ConversionCmd(ADC_Conversion_Start);

  while(1)
  {
    /* Wait until conversion completion */
    while(ADC_GetFlagStatus(ADC_FLAG_ECV) == RESET);

    /* Get the conversion value */
    Conversion_Value = ADC_GetConversionValue(ADC_CHANNEL);

    /* Clear the end of conversion flag */
    ADC_ClearFlag(ADC_FLAG_ECV);

	 /* write the ADC value on the LEDs as a gauge */
	 GPIO_Write(GPIO6,  0xff00 >> (((Conversion_Value*9)/0x400)));

  }
  
  return 0;
}

/******************* (C) COPYRIGHT 2006 STMicroelectronics *****END OF FILE****/


