|
本帖最后由 stm32_beginner 于 2015-1-8 09:20 编辑 基于cube的stm32f407定时器应用,125ms定时,晶振25M 时钟配置: void SystemClock_Config(void) { RCC_ClkInitTypeDef RCC_ClkInitStruct; RCC_OscInitTypeDef RCC_OscInitStruct; /* Enable Power Control clock */ __PWR_CLK_ENABLE(); /* The voltage scaling allows optimizing the power consumption when the device is clocked below the maximum system frequency, to update the voltage scaling value regarding system frequency refer to product datasheet. */ __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1); /* Enable HSE Oscillator and activate PLL with HSE as source */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; RCC_OscInitStruct.HSEState = RCC_HSE_ON; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; RCC_OscInitStruct.PLL.PLLM = 8; RCC_OscInitStruct.PLL.PLLN = 336; RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2; RCC_OscInitStruct.PLL.PLLQ = 7; HAL_RCC_OscConfig(&RCC_OscInitStruct); /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 clocks dividers */ RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2); RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2; HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5); } 定时器初始化: /* TIM6 init function */ void MX_TIM6_Init(void) { /* ----------------------------------------------------------------------- In this example TIM3 input clock (TIM3CLK) is set to 2 * APB1 clock (PCLK1), since APB1 prescaler is different from 1. TIM3CLK = 2 * PCLK1 PCLK1 = HCLK / 4 => TIM3CLK = HCLK / 2 = SystemCoreClock /2 =84 To get TIM3 counter clock at 10 KHz, the Prescaler is computed as following: Prescaler = (TIM3CLK / TIM3 counter clock) - 1 Prescaler = ((SystemCoreClock /2) /10 KHz) - 1 Note: SystemCoreClock variable holds HCLK frequency and is defined in system_stm32f4xx.c file. Each time the core clock (HCLK) changes, user had to update SystemCoreClock variable value. Otherwise, any configuration based on this variable will be incorrect. This variable is updated in three ways: 1) by calling CMSIS function SystemCoreClockUpdate() 2) by calling HAL API function HAL_RCC_GetSysClockFreq() 3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency ----------------------------------------------------------------------- */ /* Compute the prescaler value to have TIM3 counter clock equal to 10 KHz */ uwPrescalerValue = (uint32_t) ((SystemCoreClock /2) / 10000) - 1; //10KHz·ÖƵ 1ÃëÖÓ10000´Î /* Set TIMx instance */ htim6.Instance = TIM6; /* Initialize TIM3 peripheral as follow: + Period = 10000 - 1 + Prescaler = ((SystemCoreClock/2)/10000) - 1 + ClockDivision = 0 + Counter direction = Up */ //htim6.Init.Period = 5000 - 1; htim6.Init.Period = 1250 - 1; htim6.Init.Prescaler = uwPrescalerValue; htim6.Init.ClockDivision = 0; htim6.Init.CounterMode = TIM_COUNTERMODE_UP; if(HAL_TIM_Base_Init(&htim6) != HAL_OK) { /* Initialization Error */ //Error_Handler(); } /*##-2- Start the TIM Base generation in interrupt mode ####################*/ /* Start Channel1 */ if(HAL_TIM_Base_Start_IT(&htim6) != HAL_OK) { /* Starting Error */ //Error_Handler(); } } 中断处理,125msx4=500ms: void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) { //BSP_LED_Toggle(LED4);HAL_GPIO_TogglePin(GPIO_PORT[Led], GPIO_PIN[Led]); if(++flag_125ms == 4) { HAL_GPIO_TogglePin(GPIOD, GPIO_PIN_12); //TestLed flag_125ms = 0; } } 附加源码(里面其他应用内容准备在板子上应用的,才写了初始化和配置,请忽略不计,懒得删除了)
TEST_TIM6_125ms.part01.rar
(9 MB, 下载次数: 149)
|
| 不错 F4到手后一直没有时间弄 回头得弄下 |
| 感谢分享。。。 |
| 感谢分享,我来学习学习 |
| 缺少压缩包03? |
【银杏科技ARM+FPGA双核心应用】STM32H7系列35——USB_VCP_FS
【银杏科技ARM+FPGA双核心应用】STM32H7系列28——USB_HID
粉丝分享 | 图说CRC原理应用及STM32硬件CRC外设
【银杏科技ARM+FPGA双核心应用】STM32H7系列56——CAN
【银杏科技ARM+FPGA双核心应用】STM32H7系列25——IWDG
如何在 Vitis 中使用 UIO 驱动框架创建简单的 Linux 用户应用
STM32 IAP应用中的几个常见问题
TFT LCD 控制接口FSMC/LTDC/DSI 应用文档
介绍FreeRTOS基础及其应用
【银杏科技ARM+FPGA双核心应用】STM32H750福利
微信公众号
手机版