STM32F0xx Standard Peripherals Firmware Library
|
STM32F0xx_StdPeriph_Examples/ADC/ADC_LowPower/main.c
Go to the documentation of this file.
00001 /** 00002 ****************************************************************************** 00003 * @file ADC/ADC_LowPower/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>© 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_LowPower 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 #define MESSAGE4 " Press KEY button " 00046 #else 00047 #define MESSAGE2 " STM32072B-EVAL " 00048 #define MESSAGE3 " Turn RV3(PC.00) " 00049 #define MESSAGE4 "Press TAMPER button " 00050 #endif /* USE_STM320518_EVAL */ 00051 00052 00053 /* Uncomment the line below if you will use the ADC Low Power features */ 00054 /* #define ADC_LOWPOWER */ 00055 00056 /* Private macro -------------------------------------------------------------*/ 00057 /* Private variables ---------------------------------------------------------*/ 00058 __IO uint16_t ADC1ConvertedValue = 0, ADC1ConvertedVoltage = 0; 00059 00060 /* Private function prototypes -----------------------------------------------*/ 00061 static void Display_Init(void); 00062 static void Display(void); 00063 static void ADC_Config(void); 00064 static void TIM_Config(void); 00065 00066 /* Private functions ---------------------------------------------------------*/ 00067 00068 /** 00069 * @brief Main program. 00070 * @param None 00071 * @retval None 00072 */ 00073 int main(void) 00074 { 00075 /*!< At this stage the microcontroller clock setting is already configured, 00076 this is done through SystemInit() function which is called from startup 00077 file (startup_stm32f0xx.s) before to branch to application main. 00078 To reconfigure the default setting of SystemInit() function, refer to 00079 system_stm32f0xx.c file 00080 */ 00081 00082 /* LCD Display init */ 00083 Display_Init(); 00084 00085 /* Key button and Tamper button configuration */ 00086 #ifdef USE_STM320518_EVAL 00087 STM_EVAL_PBInit(BUTTON_KEY, BUTTON_MODE_GPIO); 00088 #else 00089 STM_EVAL_PBInit(BUTTON_TAMPER, BUTTON_MODE_GPIO); 00090 #endif /* USE_STM320518_EVAL */ 00091 00092 /* Configures LED1 GPIO */ 00093 STM_EVAL_LEDInit(LED1); 00094 00095 /* Configure ADC1 */ 00096 ADC_Config(); 00097 00098 /* Configure TIM3 */ 00099 TIM_Config(); 00100 00101 /* Infinite loop */ 00102 while (1) 00103 { 00104 /* Press Key button for STM320518_EVAL and Tamper button for STM32072B_EVAL to get the converted data */ 00105 #ifdef USE_STM320518_EVAL 00106 while(STM_EVAL_PBGetState(BUTTON_KEY) != RESET); 00107 #else 00108 while(STM_EVAL_PBGetState(BUTTON_TAMPER) != RESET); 00109 #endif /* USE_STM320518_EVAL */ 00110 00111 /* Get ADC1 converted data */ 00112 ADC1ConvertedValue =ADC_GetConversionValue(ADC1); 00113 00114 /* Compute the voltage */ 00115 ADC1ConvertedVoltage = (ADC1ConvertedValue *3300)/0xFFF; 00116 00117 /* Display converted data on the LCD */ 00118 Display(); 00119 } 00120 } 00121 00122 /** 00123 * @brief ADC1 configuration 00124 * @param None 00125 * @retval None 00126 */ 00127 static void ADC_Config(void) 00128 { 00129 ADC_InitTypeDef ADC_InitStructure; 00130 GPIO_InitTypeDef GPIO_InitStructure; 00131 NVIC_InitTypeDef NVIC_InitStructure; 00132 00133 /* GPIOC Peripheral clock enable */ 00134 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE); 00135 00136 /* ADC1 Peripheral clock enable */ 00137 RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); 00138 00139 /* Configure ADC Channel11(for STM320518 EVAL) and Chanenl10(for STM32072B EVAL) as analog input */ 00140 #ifdef USE_STM320518_EVAL 00141 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 ; 00142 #else 00143 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 ; 00144 #endif /* USE_STM320518_EVAL */ 00145 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; 00146 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; 00147 GPIO_Init(GPIOC, &GPIO_InitStructure); 00148 00149 /* ADCs DeInit */ 00150 ADC_DeInit(ADC1); 00151 00152 /* Configure the ADC1 in continous mode withe a resolution equal to 12 bits*/ 00153 ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; 00154 ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; 00155 ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_Rising; 00156 ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T3_TRGO; 00157 ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; 00158 ADC_InitStructure.ADC_ScanDirection = ADC_ScanDirection_Upward; 00159 ADC_Init(ADC1, &ADC_InitStructure); 00160 00161 /* Convert the ADC1 Channel 11 and channel10 with 28.5 Cycles as sampling time */ 00162 #ifdef USE_STM320518_EVAL 00163 ADC_ChannelConfig(ADC1, ADC_Channel_11 , ADC_SampleTime_28_5Cycles); 00164 #else 00165 ADC_ChannelConfig(ADC1, ADC_Channel_10 , ADC_SampleTime_28_5Cycles); 00166 #endif /* USE_STM320518_EVAL */ 00167 00168 /* ADC Calibration */ 00169 ADC_GetCalibrationFactor(ADC1); 00170 00171 /* Enable OVR interupt */ 00172 ADC_ITConfig(ADC1, ADC_IT_OVR, ENABLE); 00173 00174 /* Configure and enable ADC1 interrupt */ 00175 NVIC_InitStructure.NVIC_IRQChannel = ADC1_COMP_IRQn; 00176 NVIC_InitStructure.NVIC_IRQChannelPriority = 0; 00177 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; 00178 NVIC_Init(&NVIC_InitStructure); 00179 00180 /* Enable the ADC peripheral */ 00181 ADC_Cmd(ADC1, ENABLE); 00182 00183 /* Wait the ADRDY flag */ 00184 while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADRDY)); 00185 00186 #if defined (ADC_LOWPOWER) 00187 /* Enable the wait conversion mode */ 00188 ADC_WaitModeCmd(ADC1, ENABLE); 00189 00190 /* Enable the Auto power off mode */ 00191 ADC_AutoPowerOffCmd(ADC1, ENABLE); 00192 #endif 00193 00194 /* ADC1 regular Software Start Conv */ 00195 ADC_StartOfConversion(ADC1); 00196 } 00197 00198 /** 00199 * @brief TIM3 configuration 00200 * @param None 00201 * @retval None 00202 */ 00203 static void TIM_Config(void) 00204 { 00205 TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; 00206 TIM_OCInitTypeDef TIM_OCInitStructure; 00207 00208 /* ADC1 Peripheral clock enable */ 00209 RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); 00210 00211 TIM_DeInit(TIM3); 00212 00213 TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); 00214 00215 TIM_OCStructInit(&TIM_OCInitStructure); 00216 00217 /* Time base configuration */ 00218 TIM_TimeBaseStructure.TIM_Period = 0xFF; 00219 TIM_TimeBaseStructure.TIM_Prescaler = 0x0; 00220 TIM_TimeBaseStructure.TIM_ClockDivision = 0x0; 00221 TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; 00222 TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); 00223 00224 /* TIM3 TRGO selection */ 00225 TIM_SelectOutputTrigger(TIM3, TIM_TRGOSource_Update); 00226 00227 /* TIM3 enable counter */ 00228 TIM_Cmd(TIM3, ENABLE); 00229 } 00230 /** 00231 * @brief Display ADC converted value on LCD 00232 * @param None 00233 * @retval None 00234 */ 00235 static void Display(void) 00236 { 00237 uint32_t v=0,mv=0; 00238 uint8_t text[50]; 00239 00240 v=(ADC1ConvertedVoltage)/1000; 00241 mv = (ADC1ConvertedVoltage%1000)/100; 00242 sprintf((char*)text," V(Pot) = %d,%d V ",v,mv); 00243 /* Set the LCD Back Color and Text Color*/ 00244 LCD_SetBackColor(White); 00245 LCD_SetTextColor(Blue); 00246 00247 LCD_DisplayStringLine(LINE(6),text); 00248 } 00249 00250 /** 00251 * @brief Display Init (LCD) 00252 * @param None 00253 * @retval None 00254 */ 00255 static void Display_Init(void) 00256 { 00257 /* Initialize the LCD */ 00258 #ifdef USE_STM320518_EVAL 00259 STM320518_LCD_Init(); 00260 #else 00261 STM32072B_LCD_Init(); 00262 #endif /* USE_STM320518_EVAL */ 00263 00264 /* Clear the LCD */ 00265 LCD_Clear(White); 00266 00267 /* Set the LCD Text size */ 00268 LCD_SetFont(&Font8x12); 00269 00270 /* Set the LCD Back Color and Text Color*/ 00271 LCD_SetBackColor(Blue); 00272 LCD_SetTextColor(White); 00273 00274 /* Display */ 00275 LCD_DisplayStringLine(LINE(0x13), " ADC Low Power Mode example "); 00276 /* Set the LCD Text size */ 00277 LCD_SetFont(&Font16x24); 00278 00279 LCD_DisplayStringLine(LINE(0), MESSAGE1); 00280 LCD_DisplayStringLine(LINE(1), MESSAGE2); 00281 00282 /* Set the LCD Back Color and Text Color*/ 00283 LCD_SetBackColor(White); 00284 LCD_SetTextColor(Blue); 00285 00286 /* Display */ 00287 LCD_DisplayStringLine(LINE(3), MESSAGE3); 00288 LCD_DisplayStringLine(LINE(4), MESSAGE4); 00289 } 00290 00291 #ifdef USE_FULL_ASSERT 00292 00293 /** 00294 * @brief Reports the name of the source file and the source line number 00295 * where the assert_param error has occurred. 00296 * @param file: pointer to the source file name 00297 * @param line: assert_param error line source number 00298 * @retval None 00299 */ 00300 void assert_failed(uint8_t* file, uint32_t line) 00301 { 00302 /* User can add his own implementation to report the file name and line number, 00303 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ 00304 00305 /* Infinite loop */ 00306 while (1) 00307 { 00308 } 00309 } 00310 #endif 00311 00312 /** 00313 * @} 00314 */ 00315 00316 /** 00317 * @} 00318 */ 00319 00320 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/