STM8S/A Standard Peripherals Drivers
|
stm8s_adc2.c
Go to the documentation of this file.
00001 /** 00002 ****************************************************************************** 00003 * @file stm8s_adc2.c 00004 * @author MCD Application Team 00005 * @version V2.3.0 00006 * @date 16-June-2017 00007 * @brief This file contains all the functions/macros for the ADC2 peripheral. 00008 ****************************************************************************** 00009 * @attention 00010 * 00011 * <h2><center>© COPYRIGHT 2014 STMicroelectronics</center></h2> 00012 * 00013 * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); 00014 * You may not use this file except in compliance with the License. 00015 * You may obtain a copy of the License at: 00016 * 00017 * http://www.st.com/software_license_agreement_liberty_v2 00018 * 00019 * Unless required by applicable law or agreed to in writing, software 00020 * distributed under the License is distributed on an "AS IS" BASIS, 00021 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00022 * See the License for the specific language governing permissions and 00023 * limitations under the License. 00024 * 00025 ****************************************************************************** 00026 */ 00027 00028 /* Includes ------------------------------------------------------------------*/ 00029 #include "stm8s_adc2.h" 00030 00031 /** @addtogroup STM8S_StdPeriph_Driver 00032 * @{ 00033 */ 00034 /* Private typedef -----------------------------------------------------------*/ 00035 /* Private define ------------------------------------------------------------*/ 00036 /* Private macro -------------------------------------------------------------*/ 00037 /* Private variables ---------------------------------------------------------*/ 00038 /* Private function prototypes -----------------------------------------------*/ 00039 /* Private functions ---------------------------------------------------------*/ 00040 00041 /* Public functions ----------------------------------------------------------*/ 00042 00043 /** 00044 * @addtogroup ADC2_Public_Functions 00045 * @{ 00046 */ 00047 00048 /** 00049 * @brief Deinitializes the ADC2 peripheral registers to their default reset 00050 * values. 00051 * @param None 00052 * @retval None 00053 */ 00054 void ADC2_DeInit(void) 00055 { 00056 ADC2->CSR = ADC2_CSR_RESET_VALUE; 00057 ADC2->CR1 = ADC2_CR1_RESET_VALUE; 00058 ADC2->CR2 = ADC2_CR2_RESET_VALUE; 00059 ADC2->TDRH = ADC2_TDRH_RESET_VALUE; 00060 ADC2->TDRL = ADC2_TDRL_RESET_VALUE; 00061 } 00062 00063 /** 00064 * @brief Initializes the ADC2 peripheral according to the specified parameters 00065 * @param ADC2_ConversionMode: specifies the conversion mode 00066 * can be one of the values of @ref ADC2_ConvMode_TypeDef. 00067 * @param ADC2_Channel: specifies the channel to convert 00068 * can be one of the values of @ref ADC2_Channel_TypeDef. 00069 * @param ADC2_PrescalerSelection: specifies the ADC2 prescaler 00070 * can be one of the values of @ref ADC2_PresSel_TypeDef. 00071 * @param ADC2_ExtTrigger: specifies the external trigger 00072 * can be one of the values of @ref ADC2_ExtTrig_TypeDef. 00073 * @param ADC2_ExtTriggerState: specifies the external trigger new state 00074 * can be one of the values of @ref FunctionalState. 00075 * @param ADC2_Align: specifies the converted data alignment 00076 * can be one of the values of @ref ADC2_Align_TypeDef. 00077 * @param ADC2_SchmittTriggerChannel: specifies the schmitt trigger channel 00078 * can be one of the values of @ref ADC2_SchmittTrigg_TypeDef. 00079 * @param ADC2_SchmittTriggerState: specifies the schmitt trigger state 00080 * can be one of the values of @ref FunctionalState. 00081 * @retval None 00082 */ 00083 void ADC2_Init(ADC2_ConvMode_TypeDef ADC2_ConversionMode, ADC2_Channel_TypeDef ADC2_Channel, ADC2_PresSel_TypeDef ADC2_PrescalerSelection, ADC2_ExtTrig_TypeDef ADC2_ExtTrigger, FunctionalState ADC2_ExtTriggerState, ADC2_Align_TypeDef ADC2_Align, ADC2_SchmittTrigg_TypeDef ADC2_SchmittTriggerChannel, FunctionalState ADC2_SchmittTriggerState) 00084 { 00085 /* Check the parameters */ 00086 assert_param(IS_ADC2_CONVERSIONMODE_OK(ADC2_ConversionMode)); 00087 assert_param(IS_ADC2_CHANNEL_OK(ADC2_Channel)); 00088 assert_param(IS_ADC2_PRESSEL_OK(ADC2_PrescalerSelection)); 00089 assert_param(IS_ADC2_EXTTRIG_OK(ADC2_ExtTrigger)); 00090 assert_param(IS_FUNCTIONALSTATE_OK(((ADC2_ExtTriggerState)))); 00091 assert_param(IS_ADC2_ALIGN_OK(ADC2_Align)); 00092 assert_param(IS_ADC2_SCHMITTTRIG_OK(ADC2_SchmittTriggerChannel)); 00093 assert_param(IS_FUNCTIONALSTATE_OK(ADC2_SchmittTriggerState)); 00094 00095 /*-----------------CR1 & CSR configuration --------------------*/ 00096 /* Configure the conversion mode and the channel to convert 00097 respectively according to ADC2_ConversionMode & ADC2_Channel values & ADC2_Align values */ 00098 ADC2_ConversionConfig(ADC2_ConversionMode, ADC2_Channel, ADC2_Align); 00099 /* Select the prescaler division factor according to ADC2_PrescalerSelection values */ 00100 ADC2_PrescalerConfig(ADC2_PrescalerSelection); 00101 00102 /*-----------------CR2 configuration --------------------*/ 00103 /* Configure the external trigger state and event respectively 00104 according to ADC2_ExtTrigStatus, ADC2_ExtTrigger */ 00105 ADC2_ExternalTriggerConfig(ADC2_ExtTrigger, ADC2_ExtTriggerState); 00106 00107 /*------------------TDR configuration ---------------------------*/ 00108 /* Configure the schmitt trigger channel and state respectively 00109 according to ADC2_SchmittTriggerChannel & ADC2_SchmittTriggerNewState values */ 00110 ADC2_SchmittTriggerConfig(ADC2_SchmittTriggerChannel, ADC2_SchmittTriggerState); 00111 00112 /* Enable the ADC2 peripheral */ 00113 ADC2->CR1 |= ADC2_CR1_ADON; 00114 } 00115 00116 /** 00117 * @brief Enables or Disables the ADC2 peripheral. 00118 * @param NewState: specifies the peripheral enabled or disabled state. 00119 * @retval None 00120 */ 00121 void ADC2_Cmd(FunctionalState NewState) 00122 { 00123 /* Check the parameters */ 00124 assert_param(IS_FUNCTIONALSTATE_OK(NewState)); 00125 00126 if (NewState != DISABLE) 00127 { 00128 ADC2->CR1 |= ADC2_CR1_ADON; 00129 } 00130 else /* NewState == DISABLE */ 00131 { 00132 ADC2->CR1 &= (uint8_t)(~ADC2_CR1_ADON); 00133 } 00134 } 00135 00136 /** 00137 * @brief Enables or disables the ADC2 interrupt. 00138 * @param NewState specifies the state of ADC2 interrupt. 00139 * @retval None 00140 */ 00141 void ADC2_ITConfig(FunctionalState NewState) 00142 { 00143 /* Check the parameters */ 00144 assert_param(IS_FUNCTIONALSTATE_OK(NewState)); 00145 00146 if (NewState != DISABLE) 00147 { 00148 /* Enable the ADC2 interrupts */ 00149 ADC2->CSR |= (uint8_t)ADC2_CSR_EOCIE; 00150 } 00151 else /*NewState == DISABLE */ 00152 { 00153 /* Disable the ADC2 interrupts */ 00154 ADC2->CSR &= (uint8_t)(~ADC2_CSR_EOCIE); 00155 } 00156 } 00157 00158 /** 00159 * @brief Configure the ADC2 prescaler division factor. 00160 * @param ADC2_Prescaler: the selected prescaler. 00161 * It can be one of the values of @ref ADC2_PresSel_TypeDef. 00162 * @retval None 00163 */ 00164 void ADC2_PrescalerConfig(ADC2_PresSel_TypeDef ADC2_Prescaler) 00165 { 00166 /* Check the parameter */ 00167 assert_param(IS_ADC2_PRESSEL_OK(ADC2_Prescaler)); 00168 00169 /* Clear the SPSEL bits */ 00170 ADC2->CR1 &= (uint8_t)(~ADC2_CR1_SPSEL); 00171 /* Select the prescaler division factor according to ADC2_PrescalerSelection values */ 00172 ADC2->CR1 |= (uint8_t)(ADC2_Prescaler); 00173 } 00174 00175 /** 00176 * @brief Enables or disables the ADC2 Schmitt Trigger on a selected channel. 00177 * @param ADC2_SchmittTriggerChannel specifies the desired Channel. 00178 * It can be set of the values of @ref ADC2_SchmittTrigg_TypeDef. 00179 * @param NewState specifies the Channel ADC2 Schmitt Trigger new status. 00180 * can have one of the values of @ref FunctionalState. 00181 * @retval None 00182 */ 00183 void ADC2_SchmittTriggerConfig(ADC2_SchmittTrigg_TypeDef ADC2_SchmittTriggerChannel, FunctionalState NewState) 00184 { 00185 /* Check the parameters */ 00186 assert_param(IS_ADC2_SCHMITTTRIG_OK(ADC2_SchmittTriggerChannel)); 00187 assert_param(IS_FUNCTIONALSTATE_OK(NewState)); 00188 00189 if (ADC2_SchmittTriggerChannel == ADC2_SCHMITTTRIG_ALL) 00190 { 00191 if (NewState != DISABLE) 00192 { 00193 ADC2->TDRL &= (uint8_t)0x0; 00194 ADC2->TDRH &= (uint8_t)0x0; 00195 } 00196 else /* NewState == DISABLE */ 00197 { 00198 ADC2->TDRL |= (uint8_t)0xFF; 00199 ADC2->TDRH |= (uint8_t)0xFF; 00200 } 00201 } 00202 else if (ADC2_SchmittTriggerChannel < ADC2_SCHMITTTRIG_CHANNEL8) 00203 { 00204 if (NewState != DISABLE) 00205 { 00206 ADC2->TDRL &= (uint8_t)(~(uint8_t)((uint8_t)0x01 << (uint8_t)ADC2_SchmittTriggerChannel)); 00207 } 00208 else /* NewState == DISABLE */ 00209 { 00210 ADC2->TDRL |= (uint8_t)((uint8_t)0x01 << (uint8_t)ADC2_SchmittTriggerChannel); 00211 } 00212 } 00213 else /* ADC2_SchmittTriggerChannel >= ADC2_SCHMITTTRIG_CHANNEL8 */ 00214 { 00215 if (NewState != DISABLE) 00216 { 00217 ADC2->TDRH &= (uint8_t)(~(uint8_t)((uint8_t)0x01 << ((uint8_t)ADC2_SchmittTriggerChannel - (uint8_t)8))); 00218 } 00219 else /* NewState == DISABLE */ 00220 { 00221 ADC2->TDRH |= (uint8_t)((uint8_t)0x01 << ((uint8_t)ADC2_SchmittTriggerChannel - (uint8_t)8)); 00222 } 00223 } 00224 } 00225 00226 /** 00227 * @brief Configure the ADC2 conversion on selected channel. 00228 * @param ADC2_ConversionMode Specifies the conversion type. 00229 * It can be set of the values of @ref ADC2_ConvMode_TypeDef 00230 * @param ADC2_Channel specifies the ADC2 Channel. 00231 * It can be set of the values of @ref ADC2_Channel_TypeDef 00232 * @param ADC2_Align specifies the converted data alignment. 00233 * It can be set of the values of @ref ADC2_Align_TypeDef 00234 * @retval None 00235 */ 00236 void ADC2_ConversionConfig(ADC2_ConvMode_TypeDef ADC2_ConversionMode, ADC2_Channel_TypeDef ADC2_Channel, ADC2_Align_TypeDef ADC2_Align) 00237 { 00238 /* Check the parameters */ 00239 assert_param(IS_ADC2_CONVERSIONMODE_OK(ADC2_ConversionMode)); 00240 assert_param(IS_ADC2_CHANNEL_OK(ADC2_Channel)); 00241 assert_param(IS_ADC2_ALIGN_OK(ADC2_Align)); 00242 00243 /* Clear the align bit */ 00244 ADC2->CR2 &= (uint8_t)(~ADC2_CR2_ALIGN); 00245 /* Configure the data alignment */ 00246 ADC2->CR2 |= (uint8_t)(ADC2_Align); 00247 00248 if (ADC2_ConversionMode == ADC2_CONVERSIONMODE_CONTINUOUS) 00249 { 00250 /* Set the continuous conversion mode */ 00251 ADC2->CR1 |= ADC2_CR1_CONT; 00252 } 00253 else /* ADC2_ConversionMode == ADC2_CONVERSIONMODE_SINGLE */ 00254 { 00255 /* Set the single conversion mode */ 00256 ADC2->CR1 &= (uint8_t)(~ADC2_CR1_CONT); 00257 } 00258 00259 /* Clear the ADC2 channels */ 00260 ADC2->CSR &= (uint8_t)(~ADC2_CSR_CH); 00261 /* Select the ADC2 channel */ 00262 ADC2->CSR |= (uint8_t)(ADC2_Channel); 00263 } 00264 00265 /** 00266 * @brief Configure the ADC2 conversion on external trigger event. 00267 * @par Full description: 00268 * The selected external trigger event can be enabled or disabled. 00269 * @param ADC2_ExtTrigger to select the External trigger event. 00270 * can have one of the values of @ref ADC2_ExtTrig_TypeDef. 00271 * @param NewState to enable/disable the selected external trigger 00272 * can have one of the values of @ref FunctionalState. 00273 * @retval None 00274 */ 00275 void ADC2_ExternalTriggerConfig(ADC2_ExtTrig_TypeDef ADC2_ExtTrigger, FunctionalState NewState) 00276 { 00277 /* Check the parameters */ 00278 assert_param(IS_ADC2_EXTTRIG_OK(ADC2_ExtTrigger)); 00279 assert_param(IS_FUNCTIONALSTATE_OK(NewState)); 00280 00281 /* Clear the external trigger selection bits */ 00282 ADC2->CR2 &= (uint8_t)(~ADC2_CR2_EXTSEL); 00283 00284 if (NewState != DISABLE) 00285 { 00286 /* Enable the selected external Trigger */ 00287 ADC2->CR2 |= (uint8_t)(ADC2_CR2_EXTTRIG); 00288 } 00289 else /* NewState == DISABLE */ 00290 { 00291 /* Disable the selected external trigger */ 00292 ADC2->CR2 &= (uint8_t)(~ADC2_CR2_EXTTRIG); 00293 } 00294 00295 /* Set the selected external trigger */ 00296 ADC2->CR2 |= (uint8_t)(ADC2_ExtTrigger); 00297 } 00298 00299 /** 00300 * @brief Start ADC2 conversion 00301 * @par Full description: 00302 * This function triggers the start of conversion, after ADC2 configuration. 00303 * @param None 00304 * @retval None 00305 * @par Required preconditions: 00306 * Enable the ADC2 peripheral before calling this function 00307 */ 00308 void ADC2_StartConversion(void) 00309 { 00310 ADC2->CR1 |= ADC2_CR1_ADON; 00311 } 00312 00313 /** 00314 * @brief Get one sample of measured signal. 00315 * @param None 00316 * @retval ConversionValue: value of the measured signal. 00317 * @par Required preconditions: 00318 * ADC2 conversion finished. 00319 */ 00320 uint16_t ADC2_GetConversionValue(void) 00321 { 00322 uint16_t temph = 0; 00323 uint8_t templ = 0; 00324 00325 if ((ADC2->CR2 & ADC2_CR2_ALIGN) != 0) /* Right alignment */ 00326 { 00327 /* Read LSB first */ 00328 templ = ADC2->DRL; 00329 /* Then read MSB */ 00330 temph = ADC2->DRH; 00331 00332 temph = (uint16_t)(templ | (uint16_t)(temph << (uint8_t)8)); 00333 } 00334 else /* Left alignment */ 00335 { 00336 /* Read MSB first*/ 00337 temph = ADC2->DRH; 00338 /* Then read LSB */ 00339 templ = ADC2->DRL; 00340 00341 temph = (uint16_t)((uint16_t)((uint16_t)templ << 6) | (uint16_t)((uint16_t)temph << 8)); 00342 } 00343 00344 return ((uint16_t)temph); 00345 } 00346 00347 /** 00348 * @brief Checks the ADC2 EOC flag status. 00349 * @param None 00350 * @retval FlagStatus Status of the ADC2 EOC flag. 00351 */ 00352 FlagStatus ADC2_GetFlagStatus(void) 00353 { 00354 /* Get EOC flag status */ 00355 return (FlagStatus)(ADC2->CSR & ADC2_CSR_EOC); 00356 } 00357 00358 /** 00359 * @brief Clear the ADC2 EOC Flag. 00360 * @param None 00361 * @retval None 00362 */ 00363 void ADC2_ClearFlag(void) 00364 { 00365 ADC2->CSR &= (uint8_t)(~ADC2_CSR_EOC); 00366 } 00367 00368 /** 00369 * @brief Returns the EOC pending bit status 00370 * @par Parameters: 00371 * None 00372 * @retval FlagStatus: status of the EOC pending bit. 00373 */ 00374 ITStatus ADC2_GetITStatus(void) 00375 { 00376 return (ITStatus)(ADC2->CSR & ADC2_CSR_EOC); 00377 } 00378 00379 /** 00380 * @brief Clear the ADC2 End of Conversion pending bit. 00381 * @param None 00382 * @retval None 00383 */ 00384 void ADC2_ClearITPendingBit(void) 00385 { 00386 ADC2->CSR &= (uint8_t)(~ADC2_CSR_EOC); 00387 } 00388 00389 /** 00390 * @} 00391 */ 00392 00393 /** 00394 * @} 00395 */ 00396 00397 00398 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/