Quellcode |
![]() ![]() ![]() |
Erwähnenswerte Dateien im Projekt
1. Datei gpio.c
In dieser Datei wird der GPIO-Pin 3 (Pin D7 auf der Unterseite des STM-Microcontrollers) konfiguriert. Die Konfigurationswerte sind in der Datei blau markiert.
34 /* 35 * 02-32-Kaschie_Wegmessung 36 * gpio.c 37 * Version: 1.1 - Configuration of GPIO pin 3 38 * (pin D7 on backside of STM-Microcontroller) 39 * Made by: Tobias Pontiggia & Anhtuan Tran 40 */ 41 42 /* Includes ------------------------------------------------------------------*/ 43 #include "gpio.h" 44 /* USER CODE BEGIN 0 */ 45 46 /* USER CODE END 0 */ 47 48 /*----------------------------------------------------------------------------*/ 49 /* Configure GPIO */ 50 /*----------------------------------------------------------------------------*/ 51 /* USER CODE BEGIN 1 */ 52 53 /* USER CODE END 1 */ 54 55 /** Configure pins as 56 * Analog 57 * Input 58 * Output 59 * EVENT_OUT 60 * EXTI 61 */ 62 void MX_GPIO_Init(void) 63 { 64 65 GPIO_InitTypeDef GPIO_InitStruct; 66 67 /* GPIO Ports Clock Enable */ 68 __HAL_RCC_GPIOI_CLK_ENABLE(); 69 __HAL_RCC_GPIOH_CLK_ENABLE(); 70 71 /* Configure the GPIO pin : PI3 */ 72 GPIO_InitStruct.Pin = GPIO_PIN_3; 73 /* Check for the rising flank at the pin*/ 74 GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING; 75 /* Set up the GPIO pin to PULLDOWN*/ 76 GPIO_InitStruct.Pull = GPIO_PULLDOWN; 77 HAL_GPIO_Init(GPIOI, &GPIO_InitStruct); 78 } 79 80 /* USER CODE BEGIN 2 */ 81 82 /* USER CODE END 2 */ 83 84 /** 85 * @} 86 */ 87 88 /** 89 * @} 90 */ 91 92 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 93
2. Datei stm32f7xx_it.c
In dieser Datei wird die Interrupt-Service-Routine des externen Interrupts 3 programmiert. Die selbst geschriebenen Code-Zeilen sind in der Datei blau markiert.
38 /* 39 * 02-32-Kaschie_Wegmessung 40 * stm32f7xx_it.c 41 * Version: 1.1 - Programming of the external service interrupt 3 42 * Made by: Tobias Pontiggia & Anhtuan Tran 43 */ 44 45 /* Includes ------------------------------------------------------------------*/ 46 #include "stm32f7xx_hal.h" 47 #include "main.h" 48 #include "stm32f7xx_it.h" 49 #include "GUI.h" 50 #include "stdbool.h" 51 52 /*KASCHIE-WEGMESSUNG VARIABLES - BEGIN*/ 53 /* Declaration of counter for positive flank of sensor signal*/ 54 extern int16_t counter; 55 /* Declaration of flag for direction of sledge 56 * Proposition (in context with direction): 57 * true = clockwise rotation 58 * false = anti-clockwise rotation*/ 59 extern bool direction ; 60 /*KASCHIE-WEGMESSUNG VARIABLES - END*/ 61 62 /* Private typedef -----------------------------------------------------------*/ 63 /* Private define ------------------------------------------------------------*/ 64 /* Private macro -------------------------------------------------------------*/ 65 /* Private variables ---------------------------------------------------------*/ 66 extern volatile GUI_TIMER_TIME OS_TimeMS; 67 extern LTDC_HandleTypeDef hltdc; 68 extern TIM_HandleTypeDef TimHandle; 69 70 /* Private function prototypes -----------------------------------------------*/ 71 /* Private functions ---------------------------------------------------------*/ 72 73 /******************************************************************************/ 74 /* Cortex-M7 Processor Exceptions Handlers */ 75 /******************************************************************************/ 76 77 /** 78 * @brief This function handles NMI exception. 79 * @param None 80 * @retval None 81 */ 82 void NMI_Handler(void) 83 { 84 } 85 86 /** 87 * @brief This function handles Hard Fault exception. 88 * @param None 89 * @retval None 90 */ 91 void HardFault_Handler(void) 92 { 93 /* Go to infinite loop when Hard Fault exception occurs */ 94 while (1) 95 { 96 } 97 } 98 99 /** 100 * @brief This function handles Memory Manage exception. 101 * @param None 102 * @retval None 103 */ 104 void MemManage_Handler(void) 105 { 106 /* Go to infinite loop when Memory Manage exception occurs */ 107 while (1) 108 { 109 } 110 } 111 112 /** 113 * @brief This function handles Bus Fault exception. 114 * @param None 115 * @retval None 116 */ 117 void BusFault_Handler(void) 118 { 119 /* Go to infinite loop when Bus Fault exception occurs */ 120 while (1) 121 { 122 } 123 } 124 125 /** 126 * @brief This function handles Usage Fault exception. 127 * @param None 128 * @retval None 129 */ 130 void UsageFault_Handler(void) 131 { 132 /* Go to infinite loop when Usage Fault exception occurs */ 133 while (1) 134 { 135 } 136 } 137 138 /** 139 * @brief This function handles Debug Monitor exception. 140 * @param None 141 * @retval None 142 */ 143 void DebugMon_Handler(void) 144 { 145 } 146 147 /** 148 * @brief This function handles SysTick Handler. 149 * @param None 150 * @retval None 151 */ 152 void SysTick_Handler(void) 153 { 154 /* Update the LocalTime by adding 1 ms each SysTick interrupt */ 155 HAL_IncTick(); 156 157 OS_TimeMS++; 158 } 159 160 /******************************************************************************/ 161 /* STM32F7xx Peripherals Interrupt Handlers */ 162 /* Add here the Interrupt Handler for the used peripheral(s) (PPP), for the */ 163 /* available peripheral interrupt handler's name please refer to the startup */ 164 /* file (startup_stm32f7xx.s). */ 165 /******************************************************************************/ 166 167 /** 168 * @brief This function handles PPP interrupt request. 169 * @param None 170 * @retval None 171 */ 172 /*void PPP_IRQHandler(void) 173 { 174 }*/ 175 176 /** 177 * @brief This function handles TIM interrupt request. 178 * @param None 179 * @retval None 180 */ 181 void TIM3_IRQHandler(void) 182 { 183 HAL_TIM_IRQHandler(&TimHandle); 184 } 185 186 /* USER CODE: Programming of external service interrupt 3 - BEGIN */ 187 void EXTI3_IRQHandler(void) 188 { 189 /* USER CODE BEGIN EXTI3_IRQn 0 */ 190 /* Detection of the flank of sensor output*/ 191 HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_3); 192 /* Check the direction flag for the counter 193 * For clockwise rotation: increase the counter about 1 194 * For anti-clockwise rotation: decrease the counter about 1 195 * */ 196 if (direction == true) 197 { 198 counter++; 199 } 200 else 201 { 202 counter--; 203 } 204 /* USER CODE END EXTI3_IRQn 0 */ 205 } 206 /* USER CODE: Programming of external service interrupt 3 - END */ 207 208 209 /** 210 * @brief This function handles LTDC global interrupt request. 211 * @param None 212 * @retval None 213 */ 214 void LTDC_IRQHandler(void) 215 { 216 HAL_LTDC_IRQHandler(&hltdc); 217 } 218 219 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 220
3. Datei main.c
In dieser Datei wird der Counter hochgezählt und anschließend in die Distanz umgerechnet. Der Counter- und Distanzwert werden auf dem Display des STM-Microcontrollers aus gegeben. Die dazugehörigen Code-Zeilen sind in der Datei blau markiert.
47 /* 48 * 02-32-Kaschie_Wegmessung 49 * main.c 50 * Version: 1.1 - Hardware configuration for external timer 51 * Version: 1.2 - Hardware configuration for external service interrupt 52 * Version: 1.3 - Programming of the external service interrupt 53 * Version: 1.4 - Programming of the display output 54 * Made by: Tobias Pontiggia & Anhtuan Tran 55 */ 56 57 /* Includes ------------------------------------------------------------------*/ 58 #include "main.h" 59 #include "WM.h" 60 #include"gpio.h" 61 #include "GUI.h" 62 #include "stdbool.h" 63 64 /* Private typedef -----------------------------------------------------------*/ 65 /* Private define ------------------------------------------------------------*/ 66 /* Private macro -------------------------------------------------------------*/ 67 /* Private variables ---------------------------------------------------------*/ 68 uint8_t GUI_Initialized = 0; 69 TIM_HandleTypeDef TimHandle; 70 uint32_t uwPrescalerValue = 0; 71 72 /* USER CODE: KASCHIE-WEGMESSUNG VARIABLES - BEGIN*/ 73 /* Counter for positive flank of sensor signal*/ 74 int16_t counter=0; 75 /* Flag for direction of sledge */ 76 bool direction = true; 77 /* Variable for distance of sledge*/ 78 float distance=0; 79 /* USER CODE: KASCHIE-WEGMESSUNG VARIABLES - END*/ 80 81 82 /* Private function prototypes -----------------------------------------------*/ 83 static void SystemClock_Config(void); 84 void BSP_Background(void); 85 86 static void CPU_CACHE_Enable(void); 87 88 /* Private functions ---------------------------------------------------------*/ 89 static void MX_NVIC_Init(void); 90 /** 91 * @brief Main program 92 * @param None 93 * @retval None 94 */ 95 int main(void) 96 { 97 /* Enable the CPU Cache */ 98 CPU_CACHE_Enable(); 99 100 /* STM32F7xx HAL library initialization: 101 - Configure the Flash ART accelerator on ITCM interface 102 - Configure the Systick to generate an interrupt each 1 msec 103 - Set NVIC Group Priority to 4 104 - Global MSP (MCU Support Package) initialization 105 */ 106 HAL_Init(); 107 108 /* Configure the system clock to 200 MHz */ 109 SystemClock_Config(); 110 111 /*Initialize all configured peripherals*/ 112 MX_GPIO_Init(); 113 /*Initialize interrupts*/ 114 MX_NVIC_Init(); 115 116 117 /* Configure LED1 */ 118 BSP_LED_Init(LED1); 119 120 /***********************************************************/ 121 122 /* Compute the prescaler value to have TIM3 counter clock equal to 10 KHz */ 123 uwPrescalerValue = (uint32_t) ((SystemCoreClock /2) / 10000) - 1; 124 125 /* Set TIMx instance */ 126 TimHandle.Instance = TIM3; 127 128 /* Initialize TIM3 peripheral as follows: 129 + Period = 500 - 1 130 + Prescaler = ((SystemCoreClock/2)/10000) - 1 131 + ClockDivision = 0 132 + Counter direction = Up 133 */ 134 TimHandle.Init.Period = 500 - 1; 135 TimHandle.Init.Prescaler = uwPrescalerValue; 136 TimHandle.Init.ClockDivision = 0; 137 TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP; 138 if(HAL_TIM_Base_Init(&TimHandle) != HAL_OK) 139 { 140 141 while(1) 142 { 143 } 144 } 145 146 /*##-2- Start the TIM Base generation in interrupt mode ####################*/ 147 /* Start Channel1 */ 148 if(HAL_TIM_Base_Start_IT(&TimHandle) != HAL_OK) 149 { 150 while(1) 151 { 152 } 153 } 154 155 /***********************************************************/ 156 157 /* Init the STemWin GUI Library */ 158 BSP_SDRAM_Init(); /* Initializes the SDRAM device */ 159 __HAL_RCC_CRC_CLK_ENABLE(); /* Enable the CRC Module */ 160 GUI_Init(); 161 162 /* USER CODE: DISPLAY OUTPUT - BEGIN 163 * LCD_GetXSize() returns physical X-size of LCD in pixels 164 * */ 165 while(1) 166 { 167 /* conversion of counter into distance */ 168 distance=(float) counter * 1.06; // 169 /* Set the font size of output text */ 170 GUI_SetFont(&GUI_Font32B_1); 171 /* Output of the text*/ 172 GUI_DispStringAt("02-32-Kaschie_Wegmessung",60,0); 173 GUI_DispStringAt("COUNTER:",0,(LCD_GetYSize()-20)/2-25); 174 /* Output of counter on the display */ 175 GUI_DispDecAt( counter, (LCD_GetXSize()-100)/2, (LCD_GetYSize()-20)/2-25,4); 176 /* Output of the text */ 177 GUI_DispStringAt("DISTANCE:", 0, (LCD_GetYSize()-20)/2+25); 178 /* Output of distance on the display */ 179 GUI_DispDecAt(distance, (LCD_GetXSize()-100)/2, (LCD_GetYSize()-20)/2+25,4); 180 /* Output of the text*/ 181 GUI_DispStringAt("mm",(LCD_GetXSize()-100)/2+75, (LCD_GetYSize()-20)/2+25); 182 /* Set the font size of output text */ 183 GUI_SetFont(&GUI_Font20B_1); 184 /* Output of the text*/ 185 GUI_DispStringAt("Made by: Tobias Pontiggia & Anhtuan Tran",60, (LCD_GetYSize()-20)); 186 } 187 /* USER CODE: DISPLAY OUTPUT -END*/ 188 189 GUI_Initialized = 1; 190 191 /* Activate the use of memory device feature */ 192 WM_SetCreateFlags(WM_CF_MEMDEV); 193 194 MainTask(); 195 196 /* Infinite loop */ 197 for(;;); 198 } 199 200 /** 201 * @brief Period elapsed callback in non blocking mode 202 * @param htim: TIM handle 203 * @retval None 204 */ 205 void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) 206 { 207 BSP_Background(); 208 } 209 210 /** 211 * @brief TIM MSP Initialization 212 * This function configures the hardware resources used in this application: 213 * - Peripheral's clock enable 214 * - Peripheral's GPIO Configuration 215 * @param htim: TIM handle pointer 216 * @retval None 217 */ 218 void HAL_TIM_Base_MspInit(TIM_HandleTypeDef *htim) 219 { 220 /*##-1- Enable peripherals and GPIO Clocks #################################*/ 221 /* TIMx Peripheral clock enable */ 222 __HAL_RCC_TIM3_CLK_ENABLE(); 223 224 /*##-2- Configure the NVIC for TIMx ########################################*/ 225 /* Set the TIMx priority */ 226 HAL_NVIC_SetPriority(TIM3_IRQn, 0, 1); 227 228 /* Enable the TIMx global Interrupt */ 229 HAL_NVIC_EnableIRQ(TIM3_IRQn); 230 } 231 232 /** 233 * @brief BSP_Background. 234 * @param None 235 * @retval None 236 */ 237 void BSP_Background(void) 238 { 239 BSP_LED_Toggle(LED1); 240 } 241 242 /** 243 * @brief System Clock Configuration 244 * The system Clock is configured as follow : 245 * System Clock source = PLL (HSE) 246 * SYSCLK(Hz) = 200000000 247 * HCLK(Hz) = 200000000 248 * AHB Prescaler = 1 249 * APB1 Prescaler = 4 250 * APB2 Prescaler = 2 251 * HSE Frequency(Hz) = 25000000 252 * PLL_M = 25 253 * PLL_N = 400 254 * PLL_P = 2 255 * PLL_Q = 8 256 * VDD(V) = 3.3 257 * Main regulator output voltage = Scale1 mode 258 * Flash Latency(WS) = 6 259 * @param None 260 * @retval None 261 */ 262 static void SystemClock_Config(void) 263 { 264 RCC_ClkInitTypeDef RCC_ClkInitStruct; 265 RCC_OscInitTypeDef RCC_OscInitStruct; 266 HAL_StatusTypeDef ret = HAL_OK; 267 268 /* Enable HSE Oscillator and activate PLL with HSE as source */ 269 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; 270 RCC_OscInitStruct.HSEState = RCC_HSE_ON; 271 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; 272 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; 273 RCC_OscInitStruct.PLL.PLLM = 25; 274 RCC_OscInitStruct.PLL.PLLN = 400; 275 RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2; 276 RCC_OscInitStruct.PLL.PLLQ = 8; 277 278 ret = HAL_RCC_OscConfig(&RCC_OscInitStruct); 279 if(ret != HAL_OK) 280 { 281 while(1) { ; } 282 } 283 284 /* Activate the OverDrive to reach the 200 MHz Frequency */ 285 ret = HAL_PWREx_EnableOverDrive(); 286 if(ret != HAL_OK) 287 { 288 while(1) { ; } 289 } 290 291 /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 clocks dividers */ 292 RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2); 293 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; 294 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; 295 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4; 296 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2; 297 298 ret = HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_6); 299 if(ret != HAL_OK) 300 { 301 while(1) { ; } 302 } 303 } 304 305 306 /** NVIC Configuration 307 */ 308 static void MX_NVIC_Init(void) 309 { 310 /* EXTI3_IRQn interrupt configuration */ 311 HAL_NVIC_SetPriority(EXTI3_IRQn, 0, 0); 312 HAL_NVIC_EnableIRQ(EXTI3_IRQn); 313 } 314 315 316 317 318 /** 319 * @brief CPU L1-Cache enable. 320 * @param None 321 * @retval None 322 */ 323 static void CPU_CACHE_Enable(void) 324 { 325 /* Enable I-Cache */ 326 SCB_EnableICache(); 327 328 /* Enable D-Cache */ 329 SCB_EnableDCache(); 330 } 331 332 #ifdef USE_FULL_ASSERT 333 /** 334 * @brief Reports the name of the source file and the source line number 335 * where the assert_param error has occurred. 336 * @param file: pointer to the source file name 337 * @param line: assert_param error line source number 338 * @retval None 339 */ 340 void assert_failed(uint8_t* file, uint32_t line) 341 { 342 /* User can add his own implementation to report the file name and line number, 343 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ 344 345 /* Infinite loop */ 346 while (1) 347 {} 348 } 349 #endif 350 351 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 352
Das ganze Programm kann im Folgenden als ZIP-Datei heruntergeladen werden: STM32Cube_FW_F7_V1.5.0_Kaschie.zip