STM32F0xx Standard Peripherals Firmware Library
|
STM32F0xx_StdPeriph_Examples/TIM/TIM_ADCTrigger/main.c
Go to the documentation of this file.
00001 /** 00002 ****************************************************************************** 00003 * @file TIM/TIM_ADCTrigger/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 TIM_ADC_Trigger 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 #else 00045 #define MESSAGE2 " STM32072B-EVAL " 00046 #endif /* USE_STM320518_EVAL */ 00047 00048 /* Private macro -------------------------------------------------------------*/ 00049 /* Private variables ---------------------------------------------------------*/ 00050 __IO uint32_t ADCmvoltp = 0 ; 00051 __IO uint16_t ADC1ConvertedValue = 0, ADC1ConvertedVoltage = 0; 00052 00053 /* Private function prototypes -----------------------------------------------*/ 00054 static void TIM_Config(void); 00055 static void ADC_Config(void); 00056 static void Display_Init(void); 00057 static void Display(void); 00058 00059 /* Private functions ---------------------------------------------------------*/ 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 /* TIM1 Configuration */ 00079 TIM_Config(); 00080 00081 /* ADC1 Configuration */ 00082 ADC_Config(); 00083 00084 /* Infinite loop */ 00085 while (1) 00086 { 00087 /* Test EOC flag */ 00088 while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET); 00089 00090 /* Get ADC1 converted data */ 00091 ADC1ConvertedValue =ADC_GetConversionValue(ADC1); 00092 00093 /* Compute the voltage */ 00094 ADC1ConvertedVoltage = (ADC1ConvertedValue *3300)/0xFFF; 00095 00096 /* Display converted data on the LCD */ 00097 Display(); 00098 } 00099 } 00100 00101 /** 00102 * @brief ADC and TIM configuration 00103 * @param None 00104 * @retval None 00105 */ 00106 static void TIM_Config(void) 00107 { 00108 TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; 00109 TIM_OCInitTypeDef TIM_OCInitStructure; 00110 00111 /* ADC1 and TIM1 Periph clock enable */ 00112 RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 , ENABLE); 00113 00114 /* TIM1 DeInit */ 00115 TIM_DeInit(TIM1); 00116 TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); 00117 TIM_OCStructInit(&TIM_OCInitStructure); 00118 00119 /* Time base configuration */ 00120 TIM_TimeBaseStructure.TIM_Period = 0xFF; 00121 TIM_TimeBaseStructure.TIM_Prescaler = 0x0; 00122 TIM_TimeBaseStructure.TIM_ClockDivision = 0x0; 00123 TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; 00124 TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); 00125 00126 /* Output Compare PWM Mode configuration */ 00127 TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; /* low edge by default */ 00128 TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; 00129 TIM_OCInitStructure.TIM_Pulse = 0x01; 00130 TIM_OC4Init(TIM1, &TIM_OCInitStructure); 00131 00132 /* TIM1 enable counter */ 00133 TIM_Cmd(TIM1, ENABLE); 00134 00135 /* Main Output Enable */ 00136 TIM_CtrlPWMOutputs(TIM1, ENABLE); 00137 } 00138 00139 /** 00140 * @brief ADC and TIM configuration 00141 * @param None 00142 * @retval None 00143 */ 00144 static void ADC_Config(void) 00145 { 00146 ADC_InitTypeDef ADC_InitStructure; 00147 GPIO_InitTypeDef GPIO_InitStructure; 00148 00149 /* GPIOC Periph clock enable */ 00150 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE); 00151 00152 /* ADC1 and TIM1 Periph clock enable */ 00153 RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); 00154 00155 /* Configure ADC Channel11 as analog input */ 00156 #ifdef USE_STM320518_EVAL 00157 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 ; /* Configure ADC Channel11 as analog input */ 00158 #else 00159 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 ; /* Configure ADC Channel10 as analog input */ 00160 #endif /* USE_STM320518_EVAL */ 00161 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; 00162 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; 00163 GPIO_Init(GPIOC, &GPIO_InitStructure); 00164 00165 /* ADC1 DeInit */ 00166 ADC_DeInit(ADC1); 00167 00168 /* Initialize ADC structure */ 00169 ADC_StructInit(&ADC_InitStructure); 00170 00171 /* Configure the ADC1 in continous mode withe a resolution equal to 12 bits */ 00172 ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; 00173 ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; 00174 ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_Rising; 00175 ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC4; 00176 ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; 00177 ADC_InitStructure.ADC_ScanDirection = ADC_ScanDirection_Upward; 00178 ADC_Init(ADC1, &ADC_InitStructure); 00179 00180 /* Convert the ADC1 Channel11 and channel10 with 239.5 Cycles as sampling time */ 00181 #ifdef USE_STM320518_EVAL 00182 ADC_ChannelConfig(ADC1, ADC_Channel_11 , ADC_SampleTime_239_5Cycles); 00183 #else 00184 ADC_ChannelConfig(ADC1, ADC_Channel_10 , ADC_SampleTime_239_5Cycles); 00185 #endif /* USE_STM320518_EVAL */ 00186 00187 /* ADC Calibration */ 00188 ADC_GetCalibrationFactor(ADC1); 00189 00190 /* Enable the ADC peripheral */ 00191 ADC_Cmd(ADC1, ENABLE); 00192 00193 /* Wait the ADRDY flag */ 00194 while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADRDY)); 00195 00196 /* ADC1 regular Software Start Conv */ 00197 ADC_StartOfConversion(ADC1); 00198 } 00199 00200 /** 00201 * @brief Display ADC converted value on LCD 00202 * @param None 00203 * @retval None 00204 */ 00205 static void Display(void) 00206 { 00207 uint32_t v=0,mv=0, mvolt = 0; 00208 uint8_t text[50], voltp = 0; 00209 00210 v=(ADC1ConvertedVoltage)/1000; 00211 mv = (ADC1ConvertedVoltage%1000)/100; 00212 mvolt = (v *100) + (mv*10); 00213 00214 /* Set the LCD Text size */ 00215 LCD_SetFont(&Font16x24); 00216 00217 sprintf((char*)text," Potentiometer=%d,%dV",v,mv); 00218 00219 /* Set the LCD Back Color and Text Color*/ 00220 LCD_SetBackColor(White); 00221 LCD_SetTextColor(Blue); 00222 00223 LCD_DisplayStringLine(LINE(3),text); 00224 00225 if(ADCmvoltp != mvolt ) 00226 { 00227 voltp = (uint8_t)((mvolt * 100) / 330); 00228 sprintf((char*)text," %d % ",voltp); 00229 LCD_ClearLine(LINE(6)); 00230 LCD_DisplayStringLine(LINE(6),text); 00231 /* Set the LCD Text Color */ 00232 LCD_SetTextColor(Black); 00233 /* Displays a rectangle on the LCD */ 00234 LCD_DrawRect(169, 243, 22, 167 ); 00235 00236 00237 /* Set the LCD White Color */ 00238 LCD_SetBackColor(White); 00239 LCD_DrawFullRect(170, 242, 165, 20); 00240 00241 /* Set the LCD Green Color */ 00242 LCD_SetBackColor(Green); 00243 00244 if( mvolt < 4) 00245 { 00246 mvolt = 4; 00247 } 00248 00249 LCD_DrawFullRect(170, 242, (uint16_t)(mvolt / 2) , 20); 00250 00251 /* Update the new value */ 00252 ADCmvoltp = mvolt; 00253 } 00254 } 00255 00256 /** 00257 * @brief Display Init (LCD) 00258 * @param None 00259 * @retval None� 00260 */ 00261 static void Display_Init(void) 00262 { 00263 /* Initialize the LCD */ 00264 #ifdef USE_STM320518_EVAL 00265 STM320518_LCD_Init(); 00266 #else 00267 STM32072B_LCD_Init(); 00268 #endif /* USE_STM320518_EVAL */ 00269 00270 /* Clear the LCD */ 00271 LCD_Clear(White); 00272 00273 /* Set the LCD Text size */ 00274 LCD_SetFont(&Font8x12); 00275 00276 /* Set the LCD Back Color and Text Color*/ 00277 LCD_SetBackColor(Blue); 00278 LCD_SetTextColor(White); 00279 00280 /* Display */ 00281 LCD_DisplayStringLine(LINE(0x13), "TIM_ADCTrigger:Potentiometer monitoring"); 00282 00283 /* Set the LCD Text size */ 00284 LCD_SetFont(&Font16x24); 00285 00286 LCD_DisplayStringLine(LINE(0), MESSAGE1); 00287 LCD_DisplayStringLine(LINE(1), MESSAGE2); 00288 00289 /* Set the LCD Back Color and Text Color*/ 00290 LCD_SetBackColor(White); 00291 LCD_SetTextColor(Blue); 00292 } 00293 00294 #ifdef USE_FULL_ASSERT 00295 00296 /** 00297 * @brief Reports the name of the source file and the source line number 00298 * where the assert_param error has occurred. 00299 * @param file: pointer to the source file name 00300 * @param line: assert_param error line source number 00301 * @retval None 00302 */ 00303 void assert_failed(uint8_t* file, uint32_t line) 00304 { 00305 /* User can add his own implementation to report the file name and line number, 00306 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ 00307 00308 /* Infinite loop */ 00309 while (1) 00310 { 00311 } 00312 } 00313 #endif 00314 00315 /** 00316 * @} 00317 */ 00318 00319 /** 00320 * @} 00321 */ 00322 00323 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/