STM32F0xx Standard Peripherals Firmware Library: main.c Source File

STM32F0xx Standard Peripherals Library

STM32F0xx_StdPeriph_Examples/ADC/ADC_AnalogWatchdog/main.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    ADC/ADC_AnalogWatchdog/main.c 
00004   * @author  MCD Application Team
00005   * @version V1.4.0
00006   * @date    24-July-2014
00007   * @brief   Main program body
00008   ******************************************************************************
00009   * @attention
00010   *
00011   * <h2><center>&copy; 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 "main.h"
00030 
00031 /** @addtogroup STM32F0xx_StdPeriph_Examples
00032   * @{
00033   */
00034 
00035 /** @addtogroup ADC_AnalogWatchdog
00036   * @{
00037   */
00038 
00039 /* Private typedef -----------------------------------------------------------*/
00040 /* Private define ------------------------------------------------------------*/
00041 #define MESSAGE1   "STM32F0xx CortexM0  " 
00042 #ifdef USE_STM320518_EVAL
00043   #define MESSAGE2   "   STM320518-EVAL   "
00044   #define MESSAGE3   "  Turn RV3(PC.01)    "
00045 #elif defined (USE_STM32072B_EVAL) 
00046   #define MESSAGE2   "   STM32072B-EVAL   " 
00047   #define MESSAGE3   "  Turn RV3(PC.00)    "
00048 #endif /* USE_STM320518_EVAL */
00049 #define MESSAGE4   "   Potentiometer     "
00050 
00051 /* Private macro -------------------------------------------------------------*/
00052 /* Private variables ---------------------------------------------------------*/
00053 __IO uint32_t  ADC1ConvertedValue = 0, ADC1ConvertedVoltage = 0;
00054 
00055 /* Private function prototypes -----------------------------------------------*/
00056 /* Private functions ---------------------------------------------------------*/
00057 static void Display_Init(void);
00058 static void Display(void);
00059 static void ADC_Config(void);
00060 
00061 /**
00062   * @brief  Main program.
00063   * @param  None
00064   * @retval None
00065   */
00066 int main(void)
00067 {
00068   /*!< At this stage the microcontroller clock setting is already configured, 
00069        this is done through SystemInit() function which is called from startup
00070        file (startup_stm32f0xx.s) before to branch to application main.
00071        To reconfigure the default setting of SystemInit() function, refer to
00072        system_stm32f0xx.c file
00073      */ 
00074   
00075   /* LCD Display init  */
00076   Display_Init();
00077   
00078   /* Initialize LED4 */
00079   STM_EVAL_LEDInit(LED4);
00080   
00081   /* Configure ADC channel 11 */
00082   ADC_Config();
00083   
00084   /* Infinite loop */
00085   while (1)
00086   {   
00087     /* Get ADC1 converted data */
00088     ADC1ConvertedValue =ADC_GetConversionValue(ADC1);
00089     
00090     /* Compute the voltage */
00091     ADC1ConvertedVoltage = (ADC1ConvertedValue *3300)/0xFFF;
00092     
00093     
00094     /* Display converted data on the LCD */
00095     Display();
00096   }
00097 }
00098 
00099 /**
00100   * @brief  ADC1 channel configuration
00101   * @param  None
00102   * @retval None
00103   */
00104 static void ADC_Config(void)
00105 {
00106   ADC_InitTypeDef     ADC_InitStructure;
00107   GPIO_InitTypeDef    GPIO_InitStructure;
00108   NVIC_InitTypeDef    NVIC_InitStructure;
00109     
00110   /* GPIOC Periph clock enable */
00111   RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);
00112   
00113   /* ADC1 Periph clock enable */
00114   RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
00115   
00116   /* Configure ADC Channel11 as analog input */
00117 #ifdef USE_STM320518_EVAL
00118   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 ;
00119 #elif defined (USE_STM32072B_EVAL)
00120   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 ;
00121 #endif /* USE_STM320518_EVAL */
00122   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
00123   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
00124   GPIO_Init(GPIOC, &GPIO_InitStructure);
00125   
00126   /* ADC1 DeInit */  
00127   ADC_DeInit(ADC1);
00128   
00129   /* Initialize ADC structure */
00130   ADC_StructInit(&ADC_InitStructure);
00131   
00132   /* Configure the ADC1 in continuous mode withe a resolution equal to 12 bits  */
00133   ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
00134   ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; 
00135   ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
00136   ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
00137   ADC_InitStructure.ADC_ScanDirection = ADC_ScanDirection_Upward;
00138   ADC_Init(ADC1, &ADC_InitStructure);
00139   
00140   /* Convert the ADC1 Channel11 and channel10 with 239.5 Cycles as sampling time */ 
00141 #ifdef USE_STM320518_EVAL
00142   ADC_ChannelConfig(ADC1, ADC_Channel_11 , ADC_SampleTime_239_5Cycles);
00143 #elif defined (USE_STM32072B_EVAL)
00144   ADC_ChannelConfig(ADC1, ADC_Channel_10 , ADC_SampleTime_239_5Cycles);
00145 #endif /* USE_STM320518_EVAL */
00146   
00147   /* Analog watchdog config ******************************************/
00148   /* Configure the ADC Thresholds between 1.5V and 2.5V (1861, 3102) */
00149   ADC_AnalogWatchdogThresholdsConfig(ADC1, 3102, 1861);
00150 
00151   /* Enable the ADC1 single channel  */
00152   ADC_AnalogWatchdogSingleChannelCmd(ADC1, ENABLE);
00153   
00154   ADC_OverrunModeCmd(ADC1, ENABLE);
00155   /* Enable the ADC1 analog watchdog */
00156   ADC_AnalogWatchdogCmd(ADC1,ENABLE);
00157   
00158       /* Select a single ADC1 channel 11 */
00159 #ifdef USE_STM320518_EVAL
00160   ADC_AnalogWatchdogSingleChannelConfig(ADC1, ADC_AnalogWatchdog_Channel_11);
00161 #elif defined (USE_STM32072B_EVAL)
00162   ADC_AnalogWatchdogSingleChannelConfig(ADC1, ADC_AnalogWatchdog_Channel_10);
00163 #endif /* USE_STM320518_EVAL */
00164    
00165   /* Enable AWD interrupt */
00166   ADC_ITConfig(ADC1, ADC_IT_AWD, ENABLE);
00167   
00168   /* Configure and enable ADC1 interrupt */
00169   NVIC_InitStructure.NVIC_IRQChannel = ADC1_COMP_IRQn;
00170   NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
00171   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
00172   NVIC_Init(&NVIC_InitStructure);
00173   
00174   /* Enable the ADC1 Calibration */
00175   ADC_GetCalibrationFactor(ADC1);
00176   
00177   /* Enable the ADC peripheral */
00178   ADC_Cmd(ADC1, ENABLE);     
00179   
00180   /* Wait the ADRDY flag */
00181   while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADRDY)); 
00182   
00183   /* ADC1 regular Software Start Conv */ 
00184   ADC_StartOfConversion(ADC1);
00185 }
00186 
00187 /**
00188   * @brief  Display ADC converted value on LCD
00189   * @param  None
00190   * @retval None
00191   */
00192 static void Display(void)
00193 {
00194   uint32_t v=0,mv=0;
00195   uint8_t text[50];
00196 
00197   /* Set the LCD Back Color and Text Color*/
00198   LCD_SetBackColor(White);
00199   if ((ADC1ConvertedVoltage > 2500) || (ADC1ConvertedVoltage < 1500))
00200   {
00201      LCD_SetTextColor(Red);
00202   }
00203   else
00204   {
00205      LCD_SetTextColor(Blue);
00206   }
00207  
00208   v=(ADC1ConvertedVoltage)/1000;
00209   
00210   mv = (ADC1ConvertedVoltage%1000)/100;
00211   
00212   sprintf((char*)text,"  V(RV3) = %d,%d V    ",v,mv);
00213   LCD_DisplayStringLine(LINE(8),text);
00214 }
00215 
00216 /**
00217   * @brief  Display Init (LCD)
00218   * @param  None
00219   * @retval None
00220   */
00221 static void Display_Init(void)
00222 {
00223   /* Initialize the LCD */
00224 #ifdef USE_STM320518_EVAL
00225   STM320518_LCD_Init();
00226 #elif defined (USE_STM32072B_EVAL)
00227   STM32072B_LCD_Init();
00228 #endif /* USE_STM320518_EVAL */
00229 
00230   /* Clear the LCD */ 
00231   LCD_Clear(White);
00232 
00233   /* Set the LCD Text size */
00234   LCD_SetFont(&Font8x12);
00235 
00236   /* Set the LCD Back Color and Text Color*/
00237   LCD_SetBackColor(Blue);
00238   LCD_SetTextColor(White);
00239 
00240   /* Display */
00241   LCD_DisplayStringLine(LINE(0x13), "       ADC Analog Watchdog example      ");
00242 
00243   /* Set the LCD Text size */
00244   LCD_SetFont(&Font16x24);
00245 
00246   LCD_DisplayStringLine(LINE(0), MESSAGE1);
00247   LCD_DisplayStringLine(LINE(1), MESSAGE2);
00248   
00249   /* Set the LCD Back Color and Text Color*/
00250   LCD_SetBackColor(White);
00251   LCD_SetTextColor(Blue);
00252 
00253   /* Display */
00254   LCD_DisplayStringLine(LINE(3), MESSAGE3);
00255   LCD_DisplayStringLine(LINE(4), MESSAGE4);
00256   
00257     /* Set the LCD Text size */
00258   LCD_SetFont(&Font12x12);
00259   LCD_SetTextColor(Green);
00260   
00261   LCD_DisplayStringLine(LINE(12),"AWD High threshold = 2.5 V");
00262   LCD_DisplayStringLine(LINE(13),"AWD Low threshold  = 1.5 V");
00263   
00264   /* Set the LCD Text size */
00265   LCD_SetFont(&Font16x24); 
00266   LCD_SetTextColor(Blue);
00267 }
00268 
00269 #ifdef  USE_FULL_ASSERT
00270 
00271 /**
00272   * @brief  Reports the name of the source file and the source line number
00273   *         where the assert_param error has occurred.
00274   * @param  file: pointer to the source file name
00275   * @param  line: assert_param error line source number
00276   * @retval None
00277   */
00278 void assert_failed(uint8_t* file, uint32_t line)
00279 { 
00280   /* User can add his own implementation to report the file name and line number,
00281      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
00282 
00283   /* Infinite loop */
00284   while (1)
00285   {
00286   }
00287 }
00288 #endif
00289 
00290 /**
00291   * @}
00292   */
00293 
00294 /**
00295   * @}
00296   */
00297 
00298 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
STM32L1xx Standard Peripherals Library: Footer

 

 

 For complete documentation on STM32 Microcontrollers visit www.st.com/STM32