/******************** (C) COPYRIGHT 2003 STMicroelectronics ********************
* File Name          : main.c
* Author             : MCD Application Team
* Date First Issued  : 16/05/2003
* Description        : This example shows how to use the ADC12 in single channel 
*                      mode and calibrate the conversion result to get a linear
*                      conversion.
*********************************************************************************
* History:
* 24/05/05 : V3.0
* 30/11/04 : V2.0
* 16/05/03 : Created
*******************************************************************************
 THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS WITH
 CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
 AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, INDIRECT
 OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE CONTENT
 OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING INFORMATION
 CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*******************************************************************************/
#include "71x_lib.h"


u16 value,Calib_result;
u16 Conv_Res;

u16 ADC12_Calib(u16 Conv_Res);

int main (void)
{

 #ifdef DEBUG
  debug();
 #endif /* DEBUG */
  /* GPIO peripheral configuration ------------------------------------------*/

  /*  Configure the used analog input channel to HI_AIN_TRI */
  GPIO_Config (GPIO1, 0x0004, GPIO_HI_AIN_TRI);

  /*  Configure GPIO1 pins to output push-pull */
  GPIO_Config (GPIO1, 0x0020, GPIO_OUT_PP);

  /* ADC12 peripheral configuration  ----------------------------------------*/

  /*  Initialize the converter register. */
  ADC12_Init();

  /*  Configure the prescaler register using the configured PCLK with
   a sampling frequency=80Hz */
  ADC12_PrescalerConfig(80);


  /*  Select the conversion mode=single channel */
  ADC12_ModeConfig (ADC12_SINGLE);

  /*  Select the channel to be converted */
  ADC12_ChannelSelect(ADC12_CHANNEL2);

  /*  Start the Converter */
  ADC12_ConversionStart();

  while(1)
{
  GPIO_BitWrite(GPIO1,5,~GPIO_BitRead(GPIO1,5));

  /*  Wait until the availabiliy of data in the specific flags */
  while (ADC12_FlagStatus (ADC12_DA2)==RESET);

  /*  Get the conversion result from the correponding Data register */
  Conv_Res = ADC12_ConversionValue(ADC12_CHANNEL2);
  
  /*  Get the conversion result calibrated */
  value=ADC12_Calib(Conv_Res);

  /* this variable is used to display the conversion result already
   calibrated on the watch windows */
  Calib_result=value;

  }
}
/*******************************************************************************
* Function Name  : ADC12_Calib
* Description    : This is used for ADC12 result calibration
* Input          : Conv_Res : Conversion result  12 bit two complement
* Output         : None
* Return         : Conversion result calibrated 12 bit unsigned value.
*******************************************************************************/
u16 ADC12_Calib(u16 Conv_Res)
{
 if ((Conv_Res & 0x800) == 0x800)
     return (0x800 - (((~ Conv_Res)&0xFFF) +1));
 else
  {
   if ((Conv_Res & 0x800) == 0)
     return (Conv_Res + 0x800);
   else return 0;
  }
}
/******************* (C) COPYRIGHT 2003 STMicroelectronics *****END OF FILE****/

