|
本帖最后由 stm32_beginner 于 2015-1-8 09:20 编辑 基于cube的stm32f407定时器应用,125ms定时,晶振25M 1 _1 w, g& T3 p' v 时钟配置:9 ^. p2 H- c! x8 V8 P void SystemClock_Config(void) { RCC_ClkInitTypeDef RCC_ClkInitStruct; RCC_OscInitTypeDef RCC_OscInitStruct;) w" a" S9 R7 l7 Z5 c$ H$ E, K /* Enable Power Control clock */ __PWR_CLK_ENABLE(); 7 O7 Z! s3 b( v) S /* The voltage scaling allows optimizing the power consumption when the device is clocked below the maximum system frequency, to update the voltage scaling value 2 ]2 g3 c' g) \3 n/ v6 a* k regarding system frequency refer to product datasheet. */ __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);7 V" v: l" [7 [( r/ s /* Enable HSE Oscillator and activate PLL with HSE as source */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; RCC_OscInitStruct.HSEState = RCC_HSE_ON;% A4 L8 H: ?8 |& q4 t: Y RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;; N4 {2 ?) ^( I8 |+ D5 \ RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; RCC_OscInitStruct.PLL.PLLM = 8; RCC_OscInitStruct.PLL.PLLN = 336;" N7 w7 d& N% i* u, W% y7 y# |6 P RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;% Q" A( D6 O9 Z9 y7 ~ RCC_OscInitStruct.PLL.PLLQ = 7;( x$ Y, k5 e' H4 z HAL_RCC_OscConfig(&RCC_OscInitStruct); /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 clocks dividers */8 O& C" _8 c; Q {0 I' K( H RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);: {0 Z" O2 a3 I+ _/ F$ G RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;' w& g: G( A) B RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;" _& [6 l5 [$ X* e RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4; " E' R+ p8 Y, c0 g4 W; e RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2; / I# `2 j' [& E v# u HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5); % E* i+ d- @4 u' ]0 n& x }6 ^7 |3 _. c6 T 定时器初始化:# k! H6 B6 N% `, C! x /* TIM6 init function */( V# \1 y1 h+ ?6 G8 M2 A4 @5 R void MX_TIM6_Init(void)- a. d: s7 C. Z m( _ {5 l/ l* y/ s* ^. b6 i) r /* -----------------------------------------------------------------------# j; U: Q2 G# N In this example TIM3 input clock (TIM3CLK) is set to 2 * APB1 clock (PCLK1), + u3 `7 y7 k- \/ e+ m5 \$ h since APB1 prescaler is different from 1. TIM3CLK = 2 * PCLK1 PCLK1 = HCLK / 4 => TIM3CLK = HCLK / 2 = SystemCoreClock /2 =84* ?1 T% x, ?0 a) Q0 ?& @0 _( u To get TIM3 counter clock at 10 KHz, the Prescaler is computed as following:0 F: I! D ?2 N. p; h! L Prescaler = (TIM3CLK / TIM3 counter clock) - 1 Prescaler = ((SystemCoreClock /2) /10 KHz) - 1% p) |% l0 s2 N* ]) R$ W3 T: { 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.7 P6 B/ S' [1 \) N5 Z: G This variable is updated in three ways:# g. o+ p8 Y! k; y8 ~( c 1) by calling CMSIS function SystemCoreClockUpdate() 2) by calling HAL API function HAL_RCC_GetSysClockFreq()) y3 K- @; H; K5 _' H; ? 3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency ----------------------------------------------------------------------- */ # \: [* x+ }, D0 Z5 k6 @ /* Compute the prescaler value to have TIM3 counter clock equal to 10 KHz */ uwPrescalerValue = (uint32_t) ((SystemCoreClock /2) / 10000) - 1; //10KHz·ÖƵ 1ÃëÖÓ10000´Î ' R) Y, f- q5 {# H; m$ T* y/ v /* Set TIMx instance */+ W* k. ?! i$ C" a% U. w htim6.Instance = TIM6; $ q" ^5 c2 v, o6 j5 ^2 m /* Initialize TIM3 peripheral as follow: + Period = 10000 - 1 + Prescaler = ((SystemCoreClock/2)/10000) - 1 + ClockDivision = 0 + Counter direction = Up */ //htim6.Init.Period = 5000 - 1; " Y$ i J# S; K; s htim6.Init.Period = 1250 - 1; * d: A2 z+ Y6 T/ R9 ^ htim6.Init.Prescaler = uwPrescalerValue;& T- V A0 p0 y3 Y4 e htim6.Init.ClockDivision = 0; htim6.Init.CounterMode = TIM_COUNTERMODE_UP;# ^3 X0 N) b @! b5 _6 i: M if(HAL_TIM_Base_Init(&htim6) != HAL_OK)& g ^+ R' E4 N- P! L8 r { /* Initialization Error */; S% `" c! G: p0 R. ~ I //Error_Handler();6 {: {2 p7 @; e9 M } - ?& J: {8 K3 t! o0 y /*##-2- Start the TIM Base generation in interrupt mode ####################*/7 S1 v' M( B6 O6 b- E /* Start Channel1 */ if(HAL_TIM_Base_Start_IT(&htim6) != HAL_OK) {9 j# _0 r3 t3 x /* Starting Error */ //Error_Handler();/ o% x: I. t% E } ]( M8 Y) H) x1 B: j 7 T) d( R. C& B$ O3 j" ~ }) a2 K; ~( f' {$ h6 m) }/ c: B 中断处理,125msx4=500ms:& U) x7 r3 O; a+ x & u; O4 ]. W- G1 Q* a void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) {7 ]7 T/ n3 L0 @5 x& j8 g, t* s. z //BSP_LED_Toggle(LED4);HAL_GPIO_TogglePin(GPIO_PORT[Led], GPIO_PIN[Led]);2 C" E4 v/ S2 W# Y! Y& u" K if(++flag_125ms == 4)9 L' `, r- R8 q) a* F0 C) R/ | { HAL_GPIO_TogglePin(GPIOD, GPIO_PIN_12); //TestLed7 [' m* ~ V7 r# |* P- y* N flag_125ms = 0; }; M2 H+ j3 y0 `3 r( r8 X } 附加源码(里面其他应用内容准备在板子上应用的,才写了初始化和配置,请忽略不计,懒得删除了); E5 n O1 P4 m+ ~
TEST_TIM6_125ms.part01.rar
(9 MB, 下载次数: 149)
|
| 不错 F4到手后一直没有时间弄 回头得弄下 |
| 感谢分享。。。 |
| 感谢分享,我来学习学习 |
| 缺少压缩包03? |
无刷直流电机控制应用+基于STM8S系列单片机---电子书
MCSDK FOC应用详解
STM32F10xxx 正交编码器接口应用笔记 及源代码
基于STM32定时器ETR信号的应用示例
《无刷直流电机控制应用 基于STM8S系列单片机》
STM32定时器触发SPI逐字收发之应用示例
【银杏科技ARM+FPGA双核心应用】STM32H7系列10——ADC
【银杏科技ARM+FPGA双核心应用】STM32H7系列57——MDK_FLM
【STM32图书分享之九】—《STM32F 32位ARM微控制器应用设计与实践》
STM32 USB的程序,包含固件、驱动和测试用的应用程序
微信公众号
手机版