STM8L15x Standard Peripherals Drivers: stm8l15x_adc.c Source File

STM8L15x/16x Standard Peripherals Drivers

STM8L15x Standard Peripherals Drivers

stm8l15x_adc.c

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm8l15x_adc.c
00004   * @author  MCD Application Team
00005   * @version V1.5.0
00006   * @date    13-May-2011
00007   * @brief   This file provides firmware functions to manage the following 
00008   *          functionalities of the Analog to Digital Convertor (ADC) peripheral:           
00009   *           - Initialization and Configuration
00010   *           - Power saving
00011   *           - Analog Watchdog configuration              
00012   *           - Temperature Sensor & Vrefint (Voltage Reference internal) management 
00013   *           - Regular Channels Configuration
00014   *           - Regular Channels DMA Configuration
00015   *           - Injected channels Configuration      
00016   *           - Interrupts and flags management       
00017   *         
00018   *  @verbatim
00019   *                               
00020   *          ===================================================================      
00021   *                                   How to use this driver
00022   *          ===================================================================      
00023   *          1. Enable The ADC controller clock using CLK_PeripheralClockConfig() 
00024   *            function : CLK_PeripheralClockConfig(CLK_Peripheral_ADC1, ENABLE).
00025   *
00026   *            2. Configure the ADC Prescaler, conversion resolution and data 
00027   *              alignment using the ADC_Init() function.
00028   * 
00029   *            3. Activate the ADC peripheral using ADC_Cmd() function.  
00030   *
00031   *          Regular channels group configuration
00032   *          ====================================    
00033   *            - To configure the ADC regular channels group features, use 
00034   *              ADC_Init() and ADC_RegularChannelConfig() functions.
00035   *            - To activate the continuous mode, use the ADC_continuousModeCmd()
00036   *              function.
00037   *            - To configure and activate the Discontinuous mode, use the 
00038   *              ADC_DiscModeChannelCountConfig() and ADC_DiscModeCmd() functions.        
00039   *            - To read the ADC converted values, use the ADC_GetConversionValue()
00040   *              function.
00041   *
00042   *          DMA for Regular channels group features configuration
00043   *          ====================================================== 
00044   *           - To enable the DMA mode for regular channels group, use the 
00045   *             ADC_DMACmd() function.
00046   *           - To enable the generation of DMA requests continuously at the end
00047   *             of the last DMA transfer, use the ADC_DMARequestAfterLastTransferCmd() 
00048   *             function.    
00049              
00050   *          Injected channels group configuration
00051   *          =====================================    
00052   *            - To configure the ADC Injected channels group features, use 
00053   *              ADC_InjectedChannelConfig() and  ADC_InjectedSequencerLengthConfig()
00054   *              functions.
00055   *            - To activate the continuous mode, use the ADC_continuousModeCmd()
00056   *              function.
00057   *            - To activate the Injected Discontinuous mode, use the 
00058   *              ADC_InjectedDiscModeCmd() function.  
00059   *            - To activate the AutoInjected mode, use the ADC_AutoInjectedConvCmd() 
00060   *              function.        
00061   *            - To read the ADC converted values, use the ADC_GetInjectedConversionValue() 
00062   *              function.
00063   *              
00064   *  @endverbatim
00065   *         
00066   ******************************************************************************
00067   * @attention
00068   *
00069   * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
00070   * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
00071   * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
00072   * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
00073   * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
00074   * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
00075   *
00076   * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
00077   ******************************************************************************  
00078   */ 
00079 
00080 /* Includes ------------------------------------------------------------------*/
00081 #include "stm8l15x_adc.h"
00082 
00083 /** @addtogroup STM8L15x_StdPeriph_Driver
00084   * @{
00085   */
00086 
00087 /** @defgroup ADC 
00088   * @brief ADC driver modules
00089   * @{
00090   */ 
00091   
00092 /* Private typedef -----------------------------------------------------------*/
00093 /* Private define ------------------------------------------------------------*/
00094 /* Private macro -------------------------------------------------------------*/
00095 /* Private variables ---------------------------------------------------------*/
00096 /* Private function prototypes -----------------------------------------------*/
00097 /* Private functions ---------------------------------------------------------*/
00098 
00099 /** @defgroup ADC_Private_Functions
00100   * @{
00101   */ 
00102     
00103     
00104 /** @defgroup ADC_Group1 Initialization and Configuration functions
00105  *  @brief   Initialization and Configuration functions 
00106  *
00107 @verbatim    
00108  ===============================================================================
00109                       Initialization and Configuration functions
00110  ===============================================================================  
00111   This section provides functions allowing to:
00112    - Enable or disable the ADC peripheral,
00113    - Initialize and configure the ADC Prescaler, ADC Conversion Resolution 
00114      (12bit..6bit), ADC Continuous Conversion Mode (Continuous or Single 
00115      conversion),
00116    - Configure External trigger Sensitivity and source,
00117    - Start ADC conversion, by software trigger.
00118 
00119 
00120 @endverbatim
00121   * @{
00122   */
00123 /**
00124   * @brief  Deinitializes the ADC peripheral registers to their default reset 
00125   *         values.
00126   * @param  ADCx where x can be 1 to select the specified ADC peripheral.
00127   * @retval None
00128   */
00129 void ADC_DeInit(ADC_TypeDef* ADCx)
00130 {
00131   /*  Set the Configuration registers to their reset values */
00132   ADCx->CR1 =  ADC_CR1_RESET_VALUE;
00133   ADCx->CR2 =  ADC_CR2_RESET_VALUE;
00134   ADCx->CR3 =  ADC_CR3_RESET_VALUE;
00135 
00136   /*  Set the status registers to their reset values */
00137   ADCx->SR =  (uint8_t)~ADC_SR_RESET_VALUE;
00138 
00139   /*  Set the High threshold registers to their reset values */
00140   ADCx->HTRH =  ADC_HTRH_RESET_VALUE;
00141   ADCx->HTRL =  ADC_HTRL_RESET_VALUE;
00142 
00143   /*  Set the low threshold registers to their reset values */
00144   ADCx->LTRH =  ADC_LTRH_RESET_VALUE;
00145   ADCx->LTRL =  ADC_LTRL_RESET_VALUE;
00146 
00147   /*  Set the channels sequence registers to their reset values */
00148   ADCx->SQR[0] =  ADC_SQR1_RESET_VALUE;
00149   ADCx->SQR[1] =  ADC_SQR2_RESET_VALUE;
00150   ADCx->SQR[2] =  ADC_SQR3_RESET_VALUE;
00151   ADCx->SQR[3] =  ADC_SQR4_RESET_VALUE;
00152 
00153   /*  Set the channels Trigger registers to their reset values */
00154   ADCx->TRIGR[0] =  ADC_TRIGR1_RESET_VALUE;
00155   ADCx->TRIGR[1] =  ADC_TRIGR2_RESET_VALUE;
00156   ADCx->TRIGR[2] =  ADC_TRIGR3_RESET_VALUE;
00157   ADCx->TRIGR[3] =  ADC_TRIGR4_RESET_VALUE;
00158 }
00159 
00160 /**
00161   * @brief  Initializes the specified ADC peripheral according to the specified 
00162   *         parameters.
00163   * @param  ADCx where x can be 1 to select the specified ADC peripheral.
00164   * @param  ADC_ConversionMode : specifies the ADC conversion mode,
00165   *          This parameter can be one of the following values:
00166   *            @arg ADC_ConversionMode_Single: Single Conversion Mode
00167   *            @arg ADC_ConversionMode_Continuous: Continuous Conversion Mode
00168   * @param  ADC_Resolution : specifies the ADC Data resolution,
00169   *          This parameter can be one of the following values:
00170   *            @arg ADC_Resolution_12Bit: 12 bit resolution
00171   *            @arg ADC_Resolution_10Bit: 10 bit resolution
00172   *            @arg ADC_Resolution_8Bit: 8 bit resolution
00173   *            @arg ADC_Resolution_6Bit: 6 bit resolution      
00174   * @param  ADC_Prescaler : specifies the ADC Prescaler,
00175   *          This parameter can be one of the following values:
00176   *            @arg ADC_Prescaler_1: ADC Clock frequency is divided by 1
00177   *            @arg ADC_Prescaler_2: ADC Clock frequency is divided by 2
00178   * @retval None
00179   */
00180 void ADC_Init(ADC_TypeDef* ADCx,
00181               ADC_ConversionMode_TypeDef ADC_ConversionMode,
00182               ADC_Resolution_TypeDef ADC_Resolution,
00183               ADC_Prescaler_TypeDef ADC_Prescaler)
00184 {
00185   /* Check the parameters */
00186   assert_param(IS_ADC_CONVERSION_MODE(ADC_ConversionMode));
00187   assert_param(IS_ADC_RESOLUTION(ADC_Resolution));
00188   assert_param(IS_ADC_PRESCALER(ADC_Prescaler));
00189 
00190   /*clear CR1 register */
00191   ADCx->CR1 &= (uint8_t)~(ADC_CR1_CONT | ADC_CR1_RES);
00192 
00193   /* set the resolution and the conversion mode */
00194   ADCx->CR1 |= (uint8_t)((uint8_t)ADC_ConversionMode | (uint8_t)ADC_Resolution);
00195 
00196   /*clear CR2 register */
00197   ADCx->CR2 &= (uint8_t)~(ADC_CR2_PRESC);
00198 
00199   /* set the Prescaler */
00200   ADCx->CR2 |= (uint8_t) ADC_Prescaler;
00201 }
00202 
00203 /**
00204   * @brief  Enables or disables the selected ADC peripheral.
00205   * @param  ADCx where x can be 1 to select the specified ADC peripheral.
00206   * @param  NewState : new state of the specified ADC peripheral.
00207     *         This parameter can be: ENABLE or DISABLE.
00208   * @retval None
00209   */
00210 void ADC_Cmd(ADC_TypeDef* ADCx,
00211              FunctionalState NewState)
00212 {
00213   /* Check the parameters */
00214   assert_param(IS_FUNCTIONAL_STATE(NewState));
00215 
00216   if (NewState != DISABLE)
00217   {
00218     /* Set the ADON bit to wake up the specified ADC from power down mode */
00219     ADCx->CR1 |= ADC_CR1_ADON;
00220   }
00221   else
00222   {
00223     /* Disable the selected ADC peripheral */
00224     ADCx->CR1 &= (uint8_t)~ADC_CR1_ADON;
00225   }
00226 }
00227 
00228 /**
00229   * @brief  Starts ADC conversion, by software trigger.
00230   * @param  ADCx where x can be 1 to select the specified ADC peripheral.
00231   * @retval None
00232   */
00233 void ADC_SoftwareStartConv(ADC_TypeDef* ADCx)
00234 {
00235   /*  Start the ADC software conversion */
00236   ADCx->CR1 |= ADC_CR1_START;
00237 }
00238 
00239 /**
00240   * @brief  Configures the ADC conversion through external trigger.
00241   * @param  ADCx where x can be 1 to select the specified ADC peripheral.
00242   * @param  ADC_ExtEventSelection : Specifies the external trigger.
00243   *          This parameter can be one of the following values:
00244   *            @arg ADC_ExtEventSelection_None: Conversion starts only by software start
00245   *            @arg ADC_ExtEventSelection_Trigger1: Trigger 1  Enables conversion
00246   *            @arg ADC_ExtEventSelection_Trigger2: Trigger 2  Enables conversion
00247   *            @arg ADC_ExtEventSelection_Trigger3: Trigger 3  Enables conversion      
00248   * @param  ADC_ExtTRGSensitivity : Specifies the external trigger sensitivity.
00249   *          This parameter can be one of the following values:
00250   *            @arg ADC_ExtTRGSensitivity_Rising: External Trigger Sensitivity is Rising Edge
00251   *            @arg ADC_ExtTRGSensitivity_Falling: External Trigger Sensitivity is Falling Edge
00252   *            @arg ADC_ExtTRGSensitivity_All: External Trigger Sensitivity is Falling and Rising Edge
00253   * @retval None
00254   */
00255 void ADC_ExternalTrigConfig(ADC_TypeDef* ADCx,
00256                             ADC_ExtEventSelection_TypeDef ADC_ExtEventSelection,
00257                             ADC_ExtTRGSensitivity_TypeDef ADC_ExtTRGSensitivity)
00258 {
00259   /* Check the parameters */
00260   assert_param(IS_ADC_EXT_EVENT_SELECTION(ADC_ExtEventSelection));
00261   assert_param(IS_ADC_EXT_TRG_SENSITIVITY(ADC_ExtTRGSensitivity));
00262 
00263   /*clear old configuration of CR2 register */
00264   ADCx->CR2 &= (uint8_t)~(ADC_CR2_TRIGEDGE | ADC_CR2_EXTSEL);
00265 
00266   /* set the External Trigger Edge Sensitivity  and the external event 
00267      selection */
00268   ADCx->CR2 |= (uint8_t)( (uint8_t)ADC_ExtTRGSensitivity | \
00269                           (uint8_t)ADC_ExtEventSelection);
00270 }
00271 
00272  /**
00273   * @}
00274   */
00275 
00276 /** @defgroup ADC_Group2 Analog Watchdog configuration functions
00277  *  @brief   Analog Watchdog configuration functions 
00278  *
00279 @verbatim   
00280  ===============================================================================
00281                     Analog Watchdog configuration functions
00282  ===============================================================================  
00283 
00284   This section provides functions allowing to configure the Analog Watchdog
00285   (AWD) feature in the ADC.
00286   
00287   A typical configuration Analog Watchdog is done following these steps :
00288    1. the ADC guarded channel is selected using the 
00289       ADC_AnalogWatchdogChannelSelect() function.
00290    2. The Analog watchdog lower and higher threshold are configured using the  
00291       ADC_AnalogWatchdogThresholdsConfig() function.
00292    
00293    Note : Both AWD selection and thresholds can be configured with one unique 
00294           function ADC_AnalogWatchdogConfig(), which is kept for firmware 
00295           compatibility reason.
00296 
00297 @endverbatim
00298   * @{
00299   */
00300 /**
00301   * @brief  Configures the channel to be checked by the Analog watchdog.
00302   * @param  ADCx where x can be 1 to select the specified ADC peripheral.
00303   * @param  ADC_AnalogWatchdogSelection : Specifies the channel to be checked 
00304   *         by the Analog watchdog.
00305   *          This parameter can be one of the following values:
00306   *            @arg ADC_AnalogWatchdogSelection_Channel0: AWD affected to Channel 0
00307   *            @arg ADC_AnalogWatchdogSelection_Channel1: AWD affected to Channel 1
00308   *            @arg ADC_AnalogWatchdogSelection_Channel2: AWD affected to Channel 2
00309   *            @arg ADC_AnalogWatchdogSelection_Channel3: AWD affected to Channel 3
00310   *            @arg ADC_AnalogWatchdogSelection_Channel4: AWD affected to Channel 4
00311   *            @arg ADC_AnalogWatchdogSelection_Channel5: AWD affected to Channel 5
00312   *            @arg ADC_AnalogWatchdogSelection_Channel6: AWD affected to Channel 6
00313   *            @arg ADC_AnalogWatchdogSelection_Channel7: AWD affected to Channel 7
00314   *            @arg ADC_AnalogWatchdogSelection_Channel8: AWD affected to Channel 8
00315   *            @arg ADC_AnalogWatchdogSelection_Channel9: AWD affected to Channel 9
00316   *            @arg ADC_AnalogWatchdogSelection_Channel10: AWD affected to Channel 10
00317   *            @arg ADC_AnalogWatchdogSelection_Channel11: AWD affected to Channel 11
00318   *            @arg ADC_AnalogWatchdogSelection_Channel12: AWD affected to Channel 12
00319   *            @arg ADC_AnalogWatchdogSelection_Channel13: AWD affected to Channel 13
00320   *            @arg ADC_AnalogWatchdogSelection_Channel14: AWD affected to Channel 14
00321   *            @arg ADC_AnalogWatchdogSelection_Channel15: AWD affected to Channel 15
00322   *            @arg ADC_AnalogWatchdogSelection_Channel16: AWD affected to Channel 16
00323   *            @arg ADC_AnalogWatchdogSelection_Channel17: AWD affected to Channel 17
00324   *            @arg ADC_AnalogWatchdogSelection_Channel18: AWD affected to Channel 18
00325   *            @arg ADC_AnalogWatchdogSelection_Channel19: AWD affected to Channel 19
00326   *            @arg ADC_AnalogWatchdogSelection_Channel20: AWD affected to Channel 20
00327   *            @arg ADC_AnalogWatchdogSelection_Channel21: AWD affected to Channel 21
00328   *            @arg ADC_AnalogWatchdogSelection_Channel22: AWD affected to Channel 22
00329   *            @arg ADC_AnalogWatchdogSelection_Channel23: AWD affected to Channel 23
00330   *         @ref ADC_AnalogWatchdogSelection_TypeDef enumeration.
00331   * @retval None
00332   */
00333 void ADC_AnalogWatchdogChannelSelect(ADC_TypeDef* ADCx,
00334                                      ADC_AnalogWatchdogSelection_TypeDef ADC_AnalogWatchdogSelection)
00335 {
00336   /* Check the parameters */
00337   assert_param(IS_ADC_ANALOGWATCHDOG_SELECTION(ADC_AnalogWatchdogSelection));
00338 
00339   /* Reset the CHSEL bits */
00340   ADCx->CR3 &= ((uint8_t)~ADC_CR3_CHSEL);
00341 
00342   /* Select the channel to be checked by the Analog watchdog */
00343   ADCx->CR3 |= (uint8_t)ADC_AnalogWatchdogSelection;
00344 }
00345 
00346 /**
00347   * @brief  Configures the high and low thresholds of the Analog watchdog.
00348   * @param  ADCx where x can be 1 to select the specified ADC peripheral.
00349   * @param  HighThreshold: Analog watchdog High threshold value.
00350     *       This parameter must be a 12bit value.
00351   * @param  LowThreshold: Analog watchdog Low threshold value.
00352     *       This parameter must be a 12bit value.
00353   * @retval None
00354   */
00355 void ADC_AnalogWatchdogThresholdsConfig(ADC_TypeDef* ADCx, uint16_t HighThreshold, uint16_t LowThreshold)
00356 {
00357   /* Check the parameters */
00358   assert_param(IS_ADC_THRESHOLD(HighThreshold));
00359   assert_param(IS_ADC_THRESHOLD(LowThreshold));
00360 
00361   /* Set the ADC high threshold */
00362   ADCx->HTRH = (uint8_t)(HighThreshold >> 8);
00363   ADCx->HTRL = (uint8_t)(HighThreshold);
00364 
00365   /* Set the ADC low threshold */
00366   ADCx->LTRH = (uint8_t)(LowThreshold >> 8);
00367   ADCx->LTRL = (uint8_t)(LowThreshold);
00368 }
00369 
00370 /**
00371   * @brief  Configures the Analog watchdog.
00372   * @param  ADCx where x can be 1 to select the specified ADC peripheral.
00373   * @param  ADC_AnalogWatchdogSelection : Specifies the channel to be checked 
00374   *         by the Analog watchdog.
00375   *          This parameter can be one of the following values:
00376   *            @arg ADC_AnalogWatchdogSelection_Channel0: AWD affected to Channel 0
00377   *            @arg ADC_AnalogWatchdogSelection_Channel1: AWD affected to Channel 1
00378   *            @arg ADC_AnalogWatchdogSelection_Channel2: AWD affected to Channel 2
00379   *            @arg ADC_AnalogWatchdogSelection_Channel3: AWD affected to Channel 3
00380   *            @arg ADC_AnalogWatchdogSelection_Channel4: AWD affected to Channel 4
00381   *            @arg ADC_AnalogWatchdogSelection_Channel5: AWD affected to Channel 5
00382   *            @arg ADC_AnalogWatchdogSelection_Channel6: AWD affected to Channel 6
00383   *            @arg ADC_AnalogWatchdogSelection_Channel7: AWD affected to Channel 7
00384   *            @arg ADC_AnalogWatchdogSelection_Channel8: AWD affected to Channel 8
00385   *            @arg ADC_AnalogWatchdogSelection_Channel9: AWD affected to Channel 9
00386   *            @arg ADC_AnalogWatchdogSelection_Channel10: AWD affected to Channel 10
00387   *            @arg ADC_AnalogWatchdogSelection_Channel11: AWD affected to Channel 11
00388   *            @arg ADC_AnalogWatchdogSelection_Channel12: AWD affected to Channel 12
00389   *            @arg ADC_AnalogWatchdogSelection_Channel13: AWD affected to Channel 13
00390   *            @arg ADC_AnalogWatchdogSelection_Channel14: AWD affected to Channel 14
00391   *            @arg ADC_AnalogWatchdogSelection_Channel15: AWD affected to Channel 15
00392   *            @arg ADC_AnalogWatchdogSelection_Channel16: AWD affected to Channel 16
00393   *            @arg ADC_AnalogWatchdogSelection_Channel17: AWD affected to Channel 17
00394   *            @arg ADC_AnalogWatchdogSelection_Channel18: AWD affected to Channel 18
00395   *            @arg ADC_AnalogWatchdogSelection_Channel19: AWD affected to Channel 19
00396   *            @arg ADC_AnalogWatchdogSelection_Channel20: AWD affected to Channel 20
00397   *            @arg ADC_AnalogWatchdogSelection_Channel21: AWD affected to Channel 21
00398   *            @arg ADC_AnalogWatchdogSelection_Channel22: AWD affected to Channel 22
00399   *            @arg ADC_AnalogWatchdogSelection_Channel23: AWD affected to Channel 23  
00400   * @param  HighThreshold: Analog watchdog High threshold value.
00401     *       This parameter must be a 12bit value.
00402   * @param  LowThreshold: Analog watchdog Low threshold value.
00403     *       This parameter must be a 12bit value.
00404   * @retval None
00405   */
00406 void ADC_AnalogWatchdogConfig(ADC_TypeDef* ADCx,
00407                               ADC_AnalogWatchdogSelection_TypeDef ADC_AnalogWatchdogSelection,
00408                               uint16_t HighThreshold,
00409                               uint16_t LowThreshold)
00410 {
00411   /* Check the parameters */
00412   assert_param(IS_ADC_ANALOGWATCHDOG_SELECTION(ADC_AnalogWatchdogSelection));
00413   assert_param(IS_ADC_THRESHOLD(HighThreshold));
00414   assert_param(IS_ADC_THRESHOLD(LowThreshold));
00415 
00416   /*Reset the CHSEL bits */
00417   ADCx->CR3 &= ((uint8_t)~ADC_CR3_CHSEL);
00418 
00419   /* Select the channel to be checked by the Analog watchdog.*/
00420   ADCx->CR3 |= (uint8_t)ADC_AnalogWatchdogSelection;
00421 
00422   /* Set the ADC high threshold */
00423   ADCx->HTRH = (uint8_t)(HighThreshold >> 8);
00424   ADCx->HTRL = (uint8_t)(HighThreshold);
00425 
00426   /* Set the ADC low threshold */
00427   ADCx->LTRH = (uint8_t)(LowThreshold >> 8);
00428   ADCx->LTRL = (uint8_t)LowThreshold;
00429 }
00430 
00431  /**
00432   * @}
00433   */
00434 
00435 /** @defgroup ADC_Group3 Temperature Sensor & Vrefint (Voltage Reference 
00436  *            internal) management functions
00437  *  @brief   Temperature Sensor & Vrefint (Voltage Reference internal) 
00438  *            management functions 
00439  *
00440 @verbatim   
00441  ===============================================================================
00442   Temperature Sensor & Vrefint (Voltage Reference internal) management functions
00443  ===============================================================================  
00444 
00445   This section provides functions allowing to enable/ disable the internal 
00446   connections between the ADC and the Temperature Sensor and the Vrefint source.
00447      
00448   A typical configuration to get the Temperature sensor or/and Vrefint channels 
00449   voltages is done following these steps :
00450    1. Enable the internal connection of Temperature sensor or/and Vrefint sources 
00451       with the ADC channels: 
00452       - for the Temperature sensor using ADC_TempSensorCmd() function.
00453       - for the Internal Voltage reference using ADC_VrefintCmd() function. 
00454    2. Enable the ADC_Channel_TempSensor and/or ADC_Channel_Vrefint channels 
00455       using ADC_ChannelCmd()function. 
00456    3. Get the voltage values, using ADC_GetConversionValue().
00457  
00458 @endverbatim
00459   * @{
00460   */
00461   
00462 /**
00463   * @brief  Enables or disables the Temperature sensor internal reference.
00464   * @param  NewState : new state of the Temperature sensor internal reference.
00465     *         This parameter can be: ENABLE or DISABLE.
00466   * @retval None
00467   */
00468 void ADC_TempSensorCmd(FunctionalState NewState)
00469 {
00470   /* Check the parameters */
00471   assert_param(IS_FUNCTIONAL_STATE(NewState));
00472 
00473   if (NewState != DISABLE)
00474   {
00475     /*Enable the Temperature sensor internal reference.*/
00476     ADC1->TRIGR[0] |= (uint8_t)(ADC_TRIGR1_TSON);
00477   }
00478   else
00479   {
00480     /*Disable the Temperature sensor internal reference.*/
00481     ADC1->TRIGR[0] &= (uint8_t)(~ADC_TRIGR1_TSON);
00482   }
00483 }
00484 
00485 /**
00486   * @brief  Enables or disables the Internal Voltage reference.
00487   * @param  NewState : new state of the Internal Voltage reference.
00488     *         This parameter can be: ENABLE or DISABLE.
00489   * @retval None
00490   */
00491 void ADC_VrefintCmd(FunctionalState NewState)
00492 {
00493   /* Check the parameters */
00494   assert_param(IS_FUNCTIONAL_STATE(NewState));
00495 
00496   if (NewState != DISABLE)
00497   {
00498     /* Enable the Internal Voltage reference.*/
00499     ADC1->TRIGR[0] |= (uint8_t)(ADC_TRIGR1_VREFINTON);
00500   }
00501   else
00502   {
00503     /* Disable the Internal Voltage reference.*/
00504     ADC1->TRIGR[0] &= (uint8_t)(~ADC_TRIGR1_VREFINTON);
00505   }
00506 }
00507 
00508  /**
00509   * @}
00510   */
00511 /** @defgroup ADC_Group4 Channels Configuration functions
00512  *  @brief   Channels Configuration functions
00513  *
00514 @verbatim    
00515  ===============================================================================
00516                       Channels Configuration functions
00517  ===============================================================================  
00518   This section provides functions allowing to:
00519    - Enable or disable the ADC channel using ADC_ChannelCmd() function,
00520    - Configure the channels sampling times using ADC_SamplingTimeConfig() 
00521      function.
00522      Note: there are 2 sampling times configuration values : 
00523             - 1st Group value : for channels 0..23  
00524             - 2nd Group value : for channels 24..27 (depending on the MCU 
00525               package density) and Temperature Sensor and Vrefint channels. 
00526    - Configure the channels Schmitt Trigger for each channel using 
00527      ADC_SchmittTriggerConfig() function.
00528    - Get the current ADC conversion value.
00529    
00530  @endverbatim
00531   * @{
00532   */
00533     
00534 /**
00535   * @brief  Enables or disables the selected ADC channel(s).
00536   * @param  ADCx where x can be 1 to select the specified ADC peripheral.
00537   * @param  ADC_Channels: specifies the ADC channels to be initialized
00538   *          This parameter can be one of the following values:
00539   *            @arg ADC_Channel_0: Channel 0
00540   *            @arg ADC_Channel_1: Channel 1
00541   *            @arg ADC_Channel_2: Channel 2
00542   *            @arg ADC_Channel_3: Channel 3
00543   *            @arg ADC_Channel_4: Channel 4
00544   *            @arg ADC_Channel_5: Channel 5
00545   *            @arg ADC_Channel_6: Channel 6
00546   *            @arg ADC_Channel_7: Channel 7
00547   *            @arg ADC_Channel_8: Channel 8
00548   *            @arg ADC_Channel_9: Channel 9
00549   *            @arg ADC_Channel_10: Channel 10
00550   *            @arg ADC_Channel_11: Channel 11
00551   *            @arg ADC_Channel_12: Channel 12
00552   *            @arg ADC_Channel_13: Channel 13
00553   *            @arg ADC_Channel_14: Channel 14
00554   *            @arg ADC_Channel_15: Channel 15
00555   *            @arg ADC_Channel_16: Channel 16
00556   *            @arg ADC_Channel_17: Channel 17
00557   *            @arg ADC_Channel_18: Channel 18
00558   *            @arg ADC_Channel_19: Channel 19
00559   *            @arg ADC_Channel_20: Channel 20
00560   *            @arg ADC_Channel_21: Channel 21
00561   *            @arg ADC_Channel_22: Channel 22
00562   *            @arg ADC_Channel_23: Channel 23
00563   *            @arg ADC_Channel_24: Channel 24
00564   *            @arg ADC_Channel_25: Channel 25
00565   *            @arg ADC_Channel_26: Channel 26
00566   *            @arg ADC_Channel_27: Channel 27
00567   *            @arg ADC_Channel_Vrefint: Vrefint Channel
00568   *            @arg ADC_Channel_TempSensor: Temperature sensor Channel
00569   *            @arg ADC_Channel_00To07: select from channel00 to channel07
00570   *            @arg ADC_Channel_08To15: select from channel08 to channel15
00571   *            @arg ADC_Channel_16To23: select from channel16 to channel23
00572   *            @arg ADC_Channel_24To27: select from channel24 to channel27
00573   * @param  NewState : new state of the specified ADC channel(s).
00574     *         This parameter can be: ENABLE or DISABLE.
00575   * @retval None
00576   */
00577 void ADC_ChannelCmd(ADC_TypeDef* ADCx, ADC_Channel_TypeDef ADC_Channels, FunctionalState NewState)
00578 {
00579   uint8_t regindex = 0;
00580   /* Check the parameters */
00581   assert_param(IS_FUNCTIONAL_STATE(NewState));
00582 
00583   regindex = (uint8_t)((uint16_t)ADC_Channels >> 8);
00584 
00585   if (NewState != DISABLE)
00586   {
00587     /* Enable the selected ADC channel(s). */
00588     ADCx->SQR[regindex] |= (uint8_t)(ADC_Channels);
00589   }
00590   else
00591   {
00592     /* Disable the selected ADC channel(s). */
00593     ADCx->SQR[regindex] &= (uint8_t)(~(uint8_t)(ADC_Channels));
00594   }
00595 }
00596 
00597 /**
00598   * @brief  Configures the sampling time for the selected ADC channel group.
00599   * @param  ADCx where x can be 1 to select the specified ADC peripheral.
00600   * @param  ADC_GroupChannels : ADC channel group to configure.
00601   *          This parameter can be one of the following values:
00602   *            @arg ADC_Group_SlowChannels: Slow Channels group
00603   *            @arg ADC_Group_FastChannels: Fast Channels group  
00604   * @note   The channels of 1st ADC Group can be channels 0..23  
00605   * @note   The channels of 2nd ADC Group can be channels 24..27 (depending on the MCU 
00606   *         package density) and Temperature Sensor and Vrefint channels.  
00607   * @param  ADC_SamplingTime : Specifies the sample time value
00608   *          This parameter can be one of the following values:
00609   *            @arg ADC_SamplingTime_4Cycles: Sampling Time Cycles is 4
00610   *            @arg ADC_SamplingTime_9Cycles: Sampling Time Cycles is 9
00611   *            @arg ADC_SamplingTime_16Cycles: Sampling Time Cycles is 16
00612   *            @arg ADC_SamplingTime_24Cycles: Sampling Time Cycles is 24
00613   *            @arg ADC_SamplingTime_48Cycles: Sampling Time Cycles is 48
00614   *            @arg ADC_SamplingTime_96Cycles: Sampling Time Cycles is 96
00615   *            @arg ADC_SamplingTime_192Cycles: Sampling Time Cycles is 192
00616   *            @arg ADC_SamplingTime_384Cycles: Sampling Time Cycles is 384
00617   * @retval None
00618   */
00619 void ADC_SamplingTimeConfig(ADC_TypeDef* ADCx,
00620                             ADC_Group_TypeDef ADC_GroupChannels,
00621                             ADC_SamplingTime_TypeDef ADC_SamplingTime)
00622 {
00623   /* Check the parameters */
00624   assert_param(IS_ADC_GROUP(ADC_GroupChannels));
00625   assert_param(IS_ADC_SAMPLING_TIME_CYCLES(ADC_SamplingTime));
00626 
00627   if ( ADC_GroupChannels != ADC_Group_SlowChannels)
00628   {
00629     /* Configures the sampling time for the Fast ADC channel group. */
00630     ADCx->CR3 &= (uint8_t)~ADC_CR3_SMPT2;
00631     ADCx->CR3 |= (uint8_t)(ADC_SamplingTime << 5);
00632   }
00633   else
00634   {
00635     /* Configures the sampling time for the Slow ADC channel group. */
00636     ADCx->CR2 &= (uint8_t)~ADC_CR2_SMPT1;
00637     ADCx->CR2 |= (uint8_t)ADC_SamplingTime;
00638   }
00639 }
00640 
00641 /**
00642   * @brief  Configures the status of the Schmitt Trigger for the selected ADC 
00643   *         channel(s).
00644   * @param  ADCx where x can be 1 to select the specified ADC peripheral.
00645   * @param  ADC_Channels: specifies the ADC channels to be initialized,
00646   *          This parameter can be one of the following values:
00647   *            @arg ADC_Channel_0: Channel 0
00648   *            @arg ADC_Channel_1: Channel 1
00649   *            @arg ADC_Channel_2: Channel 2
00650   *            @arg ADC_Channel_3: Channel 3
00651   *            @arg ADC_Channel_4: Channel 4
00652   *            @arg ADC_Channel_5: Channel 5
00653   *            @arg ADC_Channel_6: Channel 6
00654   *            @arg ADC_Channel_7: Channel 7
00655   *            @arg ADC_Channel_8: Channel 8
00656   *            @arg ADC_Channel_9: Channel 9
00657   *            @arg ADC_Channel_10: Channel 10
00658   *            @arg ADC_Channel_11: Channel 11
00659   *            @arg ADC_Channel_12: Channel 12
00660   *            @arg ADC_Channel_13: Channel 13
00661   *            @arg ADC_Channel_14: Channel 14
00662   *            @arg ADC_Channel_15: Channel 15
00663   *            @arg ADC_Channel_16: Channel 16
00664   *            @arg ADC_Channel_17: Channel 17
00665   *            @arg ADC_Channel_18: Channel 18
00666   *            @arg ADC_Channel_19: Channel 19
00667   *            @arg ADC_Channel_20: Channel 20
00668   *            @arg ADC_Channel_21: Channel 21
00669   *            @arg ADC_Channel_22: Channel 22
00670   *            @arg ADC_Channel_23: Channel 23
00671   *            @arg ADC_Channel_24: Channel 24
00672   *            @arg ADC_Channel_25: Channel 25
00673   *            @arg ADC_Channel_26: Channel 26
00674   *            @arg ADC_Channel_27: Channel 27
00675   *            @arg ADC_Channel_Vrefint: Vrefint Channel
00676   *            @arg ADC_Channel_TempSensor: Temperature sensor Channel
00677   *            @arg ADC_Channel_00To07: select from channel00 to channel07
00678   *            @arg ADC_Channel_08To15: select from channel08 to channel15
00679   *            @arg ADC_Channel_16To23: select from channel16 to channel23
00680   *            @arg ADC_Channel_24To27: select from channel24 to channel27
00681   * @param  NewState : new state of the Schmitt Trigger
00682     *         This parameter can be: ENABLE or DISABLE.
00683   * @retval None
00684   */
00685 void ADC_SchmittTriggerConfig(ADC_TypeDef* ADCx, ADC_Channel_TypeDef ADC_Channels,
00686                               FunctionalState NewState)
00687 {
00688   uint8_t regindex = 0;
00689   /* Check the parameters */
00690   assert_param(IS_FUNCTIONAL_STATE(NewState));
00691 
00692   regindex = (uint8_t)((uint16_t)ADC_Channels >> 8);
00693 
00694   if (NewState != DISABLE)
00695   {
00696     /* Enable the Schmitt Trigger for the selected ADC channel(s).*/
00697     ADCx->TRIGR[regindex] &= (uint8_t)(~(uint8_t)ADC_Channels);
00698   }
00699   else
00700   {
00701     /* Disable the Schmitt Trigger for the selected ADC channel(s).*/
00702     ADCx->TRIGR[regindex] |= (uint8_t)(ADC_Channels);
00703   }
00704 }
00705 
00706 /**
00707   * @brief  Returns the last ADC converted data.
00708   * @param  ADCx where x can be 1 to select the specified ADC peripheral.
00709   * @retval The Data conversion value.
00710   */
00711 uint16_t ADC_GetConversionValue(ADC_TypeDef* ADCx)
00712 {
00713   uint16_t tmpreg = 0;
00714 
00715   /* Get last ADC converted data.*/
00716   tmpreg = (uint16_t)(ADCx->DRH);
00717   tmpreg = (uint16_t)((uint16_t)((uint16_t)tmpreg << 8) | ADCx->DRL);
00718 
00719   /* Return the selected ADC conversion value */
00720   return (uint16_t)(tmpreg) ;
00721 }
00722 
00723  /**
00724   * @}
00725   */
00726 
00727 /** @defgroup ADC_Group5 ADC Channels DMA Configuration function
00728  *  @brief   ADC Channels DMA Configuration function 
00729  *
00730 @verbatim   
00731  ===============================================================================
00732                    ADC Channels DMA Configuration function
00733  ===============================================================================  
00734 
00735   This section provides a function allowing to configure the DMA for ADC 
00736   channel.
00737   Since converted channel values are stored into a unique data register, 
00738   it is useful to use DMA for conversion of more than one channel. This 
00739   avoids the loss of the data already stored in the ADC Data register. 
00740   
00741   When the DMA mode is enabled (using the ADC_DMACmd() function), after each
00742   conversion of a channel, a DMA request is generated.
00743 
00744 @endverbatim
00745   * @{
00746   */
00747 /**
00748   * @brief  Enables or disables the specified ADC DMA request.
00749   * @param  ADCx where x can be 1 to select the specified ADC peripheral.
00750   * @param  NewState : new state of the specified ADC DMA transfer.
00751     *         This parameter can be: ENABLE or DISABLE.
00752   * @retval None
00753   */
00754 void ADC_DMACmd(ADC_TypeDef* ADCx, FunctionalState NewState)
00755 {
00756   /* Check the parameters */
00757   assert_param(IS_FUNCTIONAL_STATE(NewState));
00758 
00759   if (NewState != DISABLE)
00760   {
00761     /* Enable the specified ADC DMA request */
00762     ADCx->SQR[0] &= (uint8_t)~ADC_SQR1_DMAOFF;
00763   }
00764   else
00765   {
00766     /* Disable the specified ADC DMA request */
00767     ADCx->SQR[0] |= ADC_SQR1_DMAOFF;
00768   }
00769 }
00770 
00771 /**
00772   * @}
00773   */
00774 
00775 /** @defgroup ADC_Group6 Interrupts and flags management functions
00776  *  @brief   Interrupts and flags management functions
00777  *
00778 @verbatim   
00779  ===============================================================================
00780                    Interrupts and flags management functions
00781  ===============================================================================  
00782 
00783   This section provides functions allowing to configure the ADC Interrupts and 
00784   get the status and clear flags and Interrupts pending bits.
00785   
00786   The ADC provides 3 Interrupt sources and 3 Flags:
00787   
00788   Flags :
00789   ---------- 
00790      1. ADC_FLAG_OVR : Overrun detection when ADC channel converted data is lost
00791 
00792      2. ADC_FLAG_EOC : End of conversion�- to indicate the end of a regular 
00793                   CHANNEL conversion or a GROUP conversions, depending of the 
00794                   ADC Continuous Conversion Mode (Continuous or Single 
00795                   conversion) and of the DMA usage.
00796                   Note : if DMA is used, EOC occurs at the end of the sequence 
00797                          conversion, else it occurs after each conversion
00798      3. ADC_FLAG_AWD: to indicate if the converted voltage crosses the 
00799                 programmed Analog watchdog thresholds values.
00800 
00801      
00802   Interrupts :
00803   ------------
00804      1. ADC_IT_OVR : specifies the interrupt source for the Overrun detection event.
00805      2. ADC_IT_EOC : specifies the interrupt source for the End of conversion event.
00806      3. ADC_IT_AWD : specifies the interrupt source for the Analog watchdog event.
00807 
00808 @endverbatim
00809   * @{
00810   */ 
00811 
00812 /**
00813   * @brief  Enables or disables the specified ADC interrupts.
00814   * @param  ADCx where x can be 1 to select the specified ADC peripheral.
00815   * @param  ADC_IT : specifies the ADC interrupt sources to be enabled or 
00816   *         disabled. 
00817   *          This parameter can be one of the following values:
00818   *            @arg ADC_IT_EOC: End of Conversion Interrupt
00819   *            @arg ADC_IT_AWD: Analog WatchDog Interrupt
00820   *            @arg ADC_IT_OVER: Over Run Interrupt
00821   * @param  NewState : new state of the specified ADC interrupts.
00822     *         This parameter can be: ENABLE or DISABLE.
00823   * @retval None
00824   */
00825 void ADC_ITConfig(ADC_TypeDef* ADCx, ADC_IT_TypeDef ADC_IT, FunctionalState NewState)
00826 {
00827   /* Check the parameters */
00828   assert_param(IS_FUNCTIONAL_STATE(NewState));
00829   assert_param(IS_ADC_IT(ADC_IT));
00830 
00831   if (NewState != DISABLE)
00832   {
00833     /* Enable the selected ADC interrupts */
00834     ADCx->CR1 |= (uint8_t) ADC_IT;
00835   }
00836   else
00837   {
00838     /* Disable the selected ADC interrupts */
00839     ADCx->CR1 &= (uint8_t)(~ADC_IT);
00840   }
00841 }
00842 
00843 /**
00844   * @brief  Checks whether the specified ADC flag is set or not.
00845   * @param  ADCx where x can be 1 to select the specified ADC peripheral.
00846   * @param  ADC_FLAG: specifies the flag to check.
00847   *          This parameter can be one of the following values:
00848   *            @arg ADC_FLAG_EOC: End of Conversion flag
00849   *            @arg ADC_FLAG_AWD: Analog WatchDog flag
00850   *            @arg ADC_FLAG_OVER: Over Run flag    
00851   * @retval The new state of ADC_FLAG (SET or RESET).
00852   */
00853 FlagStatus ADC_GetFlagStatus(ADC_TypeDef* ADCx, ADC_FLAG_TypeDef ADC_FLAG)
00854 {
00855   FlagStatus flagstatus = RESET;
00856 
00857   /* Check the parameters */
00858   assert_param(IS_ADC_GET_FLAG(ADC_FLAG));
00859 
00860   /* Check the status of the specified ADC flag */
00861   if ((ADCx->SR & ADC_FLAG) != (uint8_t)RESET)
00862   {
00863     /* ADC_FLAG is set */
00864     flagstatus = SET;
00865   }
00866   else
00867   {
00868     /* ADC_FLAG is reset */
00869     flagstatus = RESET;
00870   }
00871 
00872   /* Return the ADC_FLAG status */
00873   return  flagstatus;
00874 }
00875 
00876 /**
00877   * @brief  Clears the ADC's pending flags.
00878   * @param  ADCx where x can be 1 to select the specified ADC peripheral.
00879   * @param  ADC_FLAG: specifies the flag to clear.
00880   *          This parameter can be one of the following values:
00881   *            @arg ADC_FLAG_EOC: End of Conversion flag
00882   *            @arg ADC_FLAG_AWD: Analog WatchDog flag
00883   *            @arg ADC_FLAG_OVER: Over Run flag 
00884   * @retval None
00885   */
00886 void ADC_ClearFlag(ADC_TypeDef* ADCx,
00887                    ADC_FLAG_TypeDef ADC_FLAG)
00888 {
00889   /* Check the parameters */
00890   assert_param(IS_ADC_CLEAR_FLAG(ADC_FLAG));
00891 
00892   /* Clear the selected ADC flags */
00893   ADCx->SR = (uint8_t)~ADC_FLAG;
00894 }
00895 
00896 /**
00897   * @brief  Checks whether the specified ADC interrupt has occurred or not.
00898   * @param  ADCx where x can be 1 to select the specified ADC peripheral.
00899   * @param  ADC_IT: specifies the ADC interrupt source to check.
00900   *          This parameter can be one of the following values:
00901   *            @arg ADC_IT_EOC: End of Conversion Interrupt
00902   *            @arg ADC_IT_AWD: Analog WatchDog Interrupt
00903   *            @arg ADC_IT_OVER: Over Run Interrupt
00904   * @retval Status of ADC_IT (SET or RESET).
00905   */
00906 ITStatus ADC_GetITStatus(ADC_TypeDef* ADCx,
00907                          ADC_IT_TypeDef ADC_IT)
00908 {
00909   ITStatus itstatus = RESET;
00910   uint8_t itmask = 0, enablestatus = 0;
00911 
00912   /* Check the parameters */
00913   assert_param(IS_ADC_GET_IT(ADC_IT));
00914 
00915   /* Get the ADC IT index */
00916   itmask = (uint8_t)(ADC_IT >> 3);
00917   itmask =  (uint8_t)((uint8_t)((uint8_t)(itmask & (uint8_t)0x10) >> 2) | \
00918                                 (uint8_t)(itmask & (uint8_t)0x03));
00919 
00920   /* Get the ADC_IT enable bit status */
00921   enablestatus = (uint8_t)(ADCx->CR1 & (uint8_t)ADC_IT) ;
00922 
00923   /* Check the status of the specified ADC interrupt */
00924   if (((ADCx->SR & itmask) != (uint8_t)RESET) && enablestatus)
00925   {
00926     /* ADC_IT is set */
00927     itstatus = SET;
00928   }
00929   else
00930   {
00931     /* ADC_IT is reset */
00932     itstatus = RESET;
00933   }
00934 
00935   /* Return the ADC_IT status */
00936   return  itstatus;
00937 }
00938 
00939 /**
00940   * @brief  Clears the ADC�s interrupt pending bits.
00941   * @param  ADCx where x can be 1 to select the specified ADC peripheral.
00942   * @param  ADC_IT: specifies the ADC interrupt pending bit to clear.
00943   *          This parameter can be one of the following values:
00944   *            @arg ADC_IT_EOC: End of Conversion Interrupt
00945   *            @arg ADC_IT_AWD: Analog WatchDog Interrupt
00946   *            @arg ADC_IT_OVER: Over Run Interrupt
00947   * @retval None
00948   */
00949 void ADC_ClearITPendingBit(ADC_TypeDef* ADCx,
00950                            ADC_IT_TypeDef ADC_IT)
00951 {
00952   uint8_t itmask = 0;
00953 
00954   /* Check the parameters */
00955   assert_param(IS_ADC_IT(ADC_IT));
00956 
00957   /* Get the ADC IT index */
00958   itmask = (uint8_t)(ADC_IT >> 3);
00959   itmask =  (uint8_t)((uint8_t)(((uint8_t)(itmask & (uint8_t)0x10)) >> 2) | \
00960                                  (uint8_t)(itmask & (uint8_t)0x03));
00961 
00962   /* Clear the selected ADC interrupt pending bits */
00963   ADCx->SR = (uint8_t)~itmask;
00964 }
00965 
00966 /**
00967   * @}
00968   */ 
00969 
00970 /**
00971   * @}
00972   */
00973 
00974 /**
00975   * @}
00976   */ 
00977 
00978 /**
00979   * @}
00980   */ 
00981 
00982 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/
STM8S Firmware Library: Overview

 

 

 

For complete documentation on STM8L15x 8-bit microcontrollers platform visit www.st.com