STM32F0xx Standard Peripherals Firmware Library
|
STM32F0xx_StdPeriph_Examples/COMP/COMP_PulseWidthMeasurement/main.c
Go to the documentation of this file.
00001 /** 00002 ****************************************************************************** 00003 * @file COMP/COMP_PulseWidthMeasurement/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 COMP_PulseWidthMeasurement 00036 * @{ 00037 */ 00038 00039 /* Private typedef -----------------------------------------------------------*/ 00040 /* Private define ------------------------------------------------------------*/ 00041 /* Private macro -------------------------------------------------------------*/ 00042 /* Private variables ---------------------------------------------------------*/ 00043 __IO uint32_t Capture = 0; 00044 __IO uint32_t MeasuredPulse = 0; 00045 __IO uint32_t DisplayActive = 0; 00046 00047 /* Private function prototypes -----------------------------------------------*/ 00048 /* Private functions ---------------------------------------------------------*/ 00049 static void DAC_Config(void); 00050 static void COMP_Config(void); 00051 static void TIM_Config(void); 00052 static void DisplayOnLCD(uint32_t data); 00053 00054 /** 00055 * @brief Main program. 00056 * @param None 00057 * @retval None 00058 */ 00059 int main(void) 00060 { 00061 /*!< At this stage the microcontroller clock setting is already configured, 00062 this is done through SystemInit() function which is called from startup 00063 file (startup_stm32f0xx.s) before to branch to application main. 00064 To reconfigure the default setting of SystemInit() function, refer to 00065 system_stm32f0xx.c file 00066 */ 00067 00068 /* Initialize the TFT-LCD */ 00069 #ifdef USE_STM320518_EVAL 00070 STM320518_LCD_Init(); 00071 #else 00072 STM32072B_LCD_Init(); 00073 #endif /* USE_STM320518_EVAL */ 00074 00075 /* Clear the TFT-LCD */ 00076 LCD_Clear(LCD_COLOR_WHITE); 00077 00078 /* DAC Channel1 configuration */ 00079 DAC_Config(); 00080 00081 /* COMP1 Configuration */ 00082 COMP_Config(); 00083 00084 /* TIM2 Configuration in input capture mode */ 00085 TIM_Config(); 00086 00087 00088 /* Infinite loop */ 00089 while (1) 00090 { 00091 if (DisplayActive != 0) 00092 { 00093 /* Compute the pulse width in us */ 00094 MeasuredPulse = (uint32_t)(((uint64_t) Capture * 1000000) / ((uint32_t)SystemCoreClock)); 00095 00096 /* Display measured pulse width on Glass LCD and color LCD */ 00097 DisplayOnLCD(MeasuredPulse); 00098 DisplayActive = 0; 00099 } 00100 } 00101 } 00102 00103 /** 00104 * @brief Configures the DAC channel 1 with output buffer enabled. 00105 * @param None 00106 * @retval None 00107 */ 00108 static void DAC_Config(void) 00109 { 00110 00111 /* Init Structure definition */ 00112 DAC_InitTypeDef DAC_InitStructure; 00113 00114 /* DAC clock enable */ 00115 RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE); 00116 00117 /* Fill DAC InitStructure */ 00118 DAC_StructInit(&DAC_InitStructure); 00119 DAC_InitStructure.DAC_Trigger = DAC_Trigger_None; 00120 DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable; 00121 00122 /* DAC Channel1 Init */ 00123 DAC_Init(DAC_Channel_1, &DAC_InitStructure); 00124 00125 /* Enable DAC Channel1 */ 00126 DAC_Cmd(DAC_Channel_1, ENABLE); 00127 00128 /* Set DAC Channel1 DHR register: DAC_OUT1 = (3.3 * 2000) / 4095 ~ 1.61 V */ 00129 DAC_SetChannel1Data(DAC_Align_12b_R, 2000); 00130 } 00131 00132 /** 00133 * @brief Configures COMP1: DAC channel 1 to COMP1 inverting input 00134 * and COMP1 output to TIM2 IC4. 00135 * @param None 00136 * @retval None 00137 */ 00138 static void COMP_Config(void) 00139 { 00140 00141 /* Init Structure definition */ 00142 COMP_InitTypeDef COMP_InitStructure; 00143 GPIO_InitTypeDef GPIO_InitStructure; 00144 00145 /* GPIOA Peripheral clock enable */ 00146 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); 00147 00148 /* Configure PA1: PA1 is used as COMP1 non inveting input */ 00149 GPIO_StructInit(&GPIO_InitStructure); 00150 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; 00151 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; 00152 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; 00153 GPIO_Init(GPIOA, &GPIO_InitStructure); 00154 00155 /* COMP Peripheral clock enable */ 00156 RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE); 00157 00158 /* COMP1 Init: DAC1 output is used COMP1 inverting input */ 00159 COMP_StructInit(&COMP_InitStructure); 00160 COMP_InitStructure.COMP_InvertingInput = COMP_InvertingInput_DAC1; 00161 /* Redirect COMP1 output to TIM2 Input capture 4 */ 00162 COMP_InitStructure.COMP_Output = COMP_Output_TIM2IC4; 00163 COMP_InitStructure.COMP_Mode = COMP_Mode_HighSpeed; 00164 COMP_InitStructure.COMP_Hysteresis = COMP_Hysteresis_No; 00165 COMP_Init(COMP_Selection_COMP1, &COMP_InitStructure); 00166 00167 /* Enable COMP1 */ 00168 COMP_Cmd(COMP_Selection_COMP1, ENABLE); 00169 } 00170 00171 /** 00172 * @brief Configures TIM2 channel 4 in input capture mode 00173 * @param None 00174 * @retval None 00175 */ 00176 static void TIM_Config(void) 00177 { 00178 00179 /* Init Structure definition */ 00180 TIM_ICInitTypeDef TIM_ICInitStructure; 00181 TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; 00182 NVIC_InitTypeDef NVIC_InitStructure; 00183 00184 /* TIM2 clock enable */ 00185 RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); 00186 /* TIM2 Time base configuration */ 00187 TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); 00188 TIM_TimeBaseStructure.TIM_Prescaler = 0; 00189 TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; 00190 TIM_TimeBaseStructure.TIM_Period = 65535; 00191 TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; 00192 TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); 00193 TIM_ClearFlag(TIM2, TIM_FLAG_Update); 00194 00195 /* TIM2 Channel4 Input capture Mode configuration */ 00196 TIM_ICStructInit(&TIM_ICInitStructure); 00197 TIM_ICInitStructure.TIM_Channel = TIM_Channel_4; 00198 /* TIM2 counter is captured at each transition detection: rising or falling edges (both edges) */ 00199 TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_BothEdge; 00200 TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; 00201 TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; 00202 TIM_ICInitStructure.TIM_ICFilter = 0; 00203 TIM_ICInit(TIM2, &TIM_ICInitStructure); 00204 00205 /* TIM2 IRQChannel enable */ 00206 NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn; 00207 NVIC_InitStructure.NVIC_IRQChannelPriority = 0; 00208 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; 00209 NVIC_Init(&NVIC_InitStructure); 00210 00211 /* Enable capture interrupt */ 00212 TIM_ITConfig(TIM2, TIM_IT_CC4, ENABLE); 00213 00214 /* Enable the TIM2 counter */ 00215 TIM_Cmd(TIM2, ENABLE); 00216 00217 /* Reset the flags */ 00218 TIM2->SR = 0; 00219 } 00220 00221 /** 00222 * @brief Display measured pulse width on color LCD 00223 * @param None 00224 * @retval None 00225 */ 00226 static void DisplayOnLCD(uint32_t value) 00227 { 00228 uint8_t text[50]; 00229 sprintf((char*)text,"PulseWidth = %d us ",value); 00230 LCD_DisplayStringLine(LINE(5),text); 00231 } 00232 00233 #ifdef USE_FULL_ASSERT 00234 00235 /** 00236 * @brief Reports the name of the source file and the source line number 00237 * where the assert_param error has occurred. 00238 * @param file: pointer to the source file name 00239 * @param line: assert_param error line source number 00240 * @retval None 00241 */ 00242 void assert_failed(uint8_t* file, uint32_t line) 00243 { 00244 /* User can add his own implementation to report the file name and line number, 00245 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ 00246 00247 /* Infinite loop */ 00248 while (1) 00249 { 00250 } 00251 } 00252 #endif 00253 00254 /** 00255 * @} 00256 */ 00257 00258 /** 00259 * @} 00260 */ 00261 00262 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/