STM32F0xx Standard Peripherals Firmware Library: main.c Source File

STM32F0xx Standard Peripherals Library

STM32F0xx_StdPeriph_Examples/ADC/ADC_BasicExample/main.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    ADC/ADC_BasicExample/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_BasicExample
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 #else 
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 uint16_t  ADC1ConvertedValue = 0, ADC1ConvertedVoltage = 0;
00054 
00055 /* Private function prototypes -----------------------------------------------*/
00056 static void ADC_Config(void);
00057 static void Display_Init(void);
00058 static void Display(void);
00059 
00060 /* Private functions ---------------------------------------------------------*/
00061 
00062 /**
00063   * @brief  Main program.
00064   * @param  None
00065   * @retval None
00066   */
00067 int main(void)
00068 {
00069   /*!< At this stage the microcontroller clock setting is already configured, 
00070        this is done through SystemInit() function which is called from startup
00071        file (startup_stm32f0xx.s) before to branch to application main.
00072        To reconfigure the default setting of SystemInit() function, refer to
00073        system_stm32f0xx.c file
00074      */ 
00075 
00076   /* ADC Configuration */
00077   ADC_Config();
00078   /* LCD Display init  */
00079   Display_Init();
00080     
00081   /* Infinite loop */
00082   while (1)
00083   {
00084     /* Test EOC flag */
00085     while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);
00086     
00087     /* Get ADC1 converted data */
00088     ADC1ConvertedValue =ADC_GetConversionValue(ADC1);
00089     
00090     /* Compute the voltage */
00091     ADC1ConvertedVoltage = (ADC1ConvertedValue *3300)/0xFFF;
00092     
00093     /* Display converted data on the LCD */
00094     Display();
00095   }
00096 }
00097 
00098 /**
00099   * @brief  ADC Configuration
00100   * @param  None
00101   * @retval None
00102   */
00103 static void ADC_Config(void)
00104 {
00105   ADC_InitTypeDef     ADC_InitStructure;
00106   GPIO_InitTypeDef    GPIO_InitStructure;
00107   
00108   /* GPIOC Periph clock enable */
00109   RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);
00110   
00111   /* ADC1 Periph clock enable */
00112   RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
00113   
00114   /* Configure ADC Channel11 as analog input */
00115 #ifdef USE_STM320518_EVAL
00116   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 ;
00117 #else
00118   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 ;
00119 #endif /* USE_STM320518_EVAL */
00120   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
00121   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
00122   GPIO_Init(GPIOC, &GPIO_InitStructure);
00123   
00124   /* ADCs DeInit */  
00125   ADC_DeInit(ADC1);
00126   
00127   /* Initialize ADC structure */
00128   ADC_StructInit(&ADC_InitStructure);
00129   
00130   /* Configure the ADC1 in continuous mode with a resolution equal to 12 bits  */
00131   ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
00132   ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; 
00133   ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
00134   ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
00135   ADC_InitStructure.ADC_ScanDirection = ADC_ScanDirection_Upward;
00136   ADC_Init(ADC1, &ADC_InitStructure); 
00137   
00138   /* Convert the ADC1 Channel 11 with 239.5 Cycles as sampling time */ 
00139 #ifdef USE_STM320518_EVAL
00140   ADC_ChannelConfig(ADC1, ADC_Channel_11 , ADC_SampleTime_239_5Cycles);
00141 #else
00142   ADC_ChannelConfig(ADC1, ADC_Channel_10 , ADC_SampleTime_239_5Cycles);
00143 #endif /* USE_STM320518_EVAL */
00144 
00145   /* ADC Calibration */
00146   ADC_GetCalibrationFactor(ADC1);
00147   
00148   /* Enable the ADC peripheral */
00149   ADC_Cmd(ADC1, ENABLE);     
00150   
00151   /* Wait the ADRDY flag */
00152   while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADRDY)); 
00153   
00154   /* ADC1 regular Software Start Conv */ 
00155   ADC_StartOfConversion(ADC1);
00156   
00157 }
00158 
00159 /**
00160   * @brief  Display ADC converted value on LCD
00161   * @param  None
00162   * @retval None
00163   */
00164 void Display(void)
00165 {
00166   uint32_t v=0,mv=0;
00167   uint8_t text[50];
00168 
00169   v=(ADC1ConvertedVoltage)/1000;
00170   mv = (ADC1ConvertedVoltage%1000)/100;
00171   sprintf((char*)text,"   ADC = %d,%d V   ",v,mv);
00172   /* Set the LCD Back Color and Text Color*/
00173   LCD_SetBackColor(White);
00174   LCD_SetTextColor(Blue);
00175   /* Display */
00176   LCD_DisplayStringLine(LINE(3), MESSAGE3);
00177   LCD_DisplayStringLine(LINE(4), MESSAGE4);
00178   /* Display voltage value */
00179   LCD_DisplayStringLine(LINE(6),text);
00180 }
00181 
00182 /**
00183   * @brief  Display Init (LCD)
00184   * @param  None
00185   * @retval None
00186   */
00187 void Display_Init(void)
00188 {
00189   /* Initialize the LCD */
00190 #ifdef USE_STM320518_EVAL
00191     STM320518_LCD_Init();
00192 #else
00193     STM32072B_LCD_Init();
00194 #endif /* USE_STM320518_EVAL */
00195 
00196   /* Clear the LCD */ 
00197   LCD_Clear(White);
00198 
00199   /* Set the LCD Text size */
00200   LCD_SetFont(&Font8x12);
00201 
00202   /* Set the LCD Back Color and Text Color*/
00203   LCD_SetBackColor(Blue);
00204   LCD_SetTextColor(White);
00205 
00206   /* Display */
00207   LCD_DisplayStringLine(LINE(0x13), "  ADC conversion example (Basic example)");
00208 
00209   /* Set the LCD Text size */
00210   LCD_SetFont(&Font16x24);
00211 
00212   LCD_DisplayStringLine(LINE(0), MESSAGE1);
00213   LCD_DisplayStringLine(LINE(1), MESSAGE2);
00214   
00215   /* Set the LCD Back Color and Text Color*/
00216   LCD_SetBackColor(White);
00217   LCD_SetTextColor(Blue);
00218      
00219 }
00220 
00221 #ifdef  USE_FULL_ASSERT
00222 
00223 /**
00224   * @brief  Reports the name of the source file and the source line number
00225   *         where the assert_param error has occurred.
00226   * @param  file: pointer to the source file name
00227   * @param  line: assert_param error line source number
00228   * @retval None
00229   */
00230 void assert_failed(uint8_t* file, uint32_t line)
00231 { 
00232   /* User can add his own implementation to report the file name and line number,
00233      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
00234 
00235   /* Infinite loop */
00236   while (1)
00237   {
00238   }
00239 }
00240 #endif
00241 
00242 /**
00243   * @}
00244   */
00245 
00246 /**
00247   * @}
00248   */
00249 
00250 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
STM32L1xx Standard Peripherals Library: Footer

 

 

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