|
本帖最后由 stm32_beginner 于 2015-1-8 09:20 编辑 + A& J+ W8 B$ c. ` 基于cube的stm32f407定时器应用,125ms定时,晶振25M( }0 N- e! e; x7 N8 f- n 时钟配置: void SystemClock_Config(void)% n3 L, V& k6 T! b8 c {% |+ V3 E$ u1 y8 ^9 i 0 X8 m4 b5 q; n% Q. ^ RCC_ClkInitTypeDef RCC_ClkInitStruct;1 ]. ]7 g* o) \5 L RCC_OscInitTypeDef RCC_OscInitStruct;, o: x, I$ U- b4 y" p /* Enable Power Control clock */ __PWR_CLK_ENABLE();2 A8 \' f1 G' K7 d' a8 z 1 r6 f+ [( p. V /* The voltage scaling allows optimizing the power consumption when the device is clocked below the maximum system frequency, to update the voltage scaling value 5 l, C( E: D v/ S& g* y regarding system frequency refer to product datasheet. */& u& H2 j: H. \( y __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1); + w6 Y4 O% U6 K0 m/ | /* Enable HSE Oscillator and activate PLL with HSE as source */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;2 F* q" m5 R3 H0 h( ] RCC_OscInitStruct.HSEState = RCC_HSE_ON;3 m# b8 i- j3 U F RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;* D4 y" T6 a$ z- Q2 k RCC_OscInitStruct.PLL.PLLM = 8; RCC_OscInitStruct.PLL.PLLN = 336; RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;# i6 t: B$ u: a; z, I" L+ Y RCC_OscInitStruct.PLL.PLLQ = 7; HAL_RCC_OscConfig(&RCC_OscInitStruct); l- x; }9 ?- ?) x /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 clocks dividers */5 V) h- }0 w" H- w0 U7 a RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2); RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;2 Q& v% a$ t* Z, a7 G$ z- B RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;7 Z8 E0 V0 B7 S8 }. h RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4; $ x1 N/ ^% b2 t4 z5 j: m2 T RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2; 7 v/ \& _( h9 M1 F6 s3 t# x HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5); }2 |; i' b( u8 k7 X& i / s9 S7 t( J' x6 w! q9 a. D 定时器初始化: /* TIM6 init function */ void MX_TIM6_Init(void)+ l" X) X/ T6 j9 I6 r0 L { ; F4 u% Q& @0 T7 B6 m" r- b6 w { /* -----------------------------------------------------------------------8 D2 r6 \1 P6 I+ P# O" l8 Q# I4 C In this example TIM3 input clock (TIM3CLK) is set to 2 * APB1 clock (PCLK1), 6 }/ X. q4 y/ }- D; @5 Q' ]3 I3 ~1 Z* E since APB1 prescaler is different from 1. 9 f9 U; C, }6 x( \, b& F! n+ h8 E TIM3CLK = 2 * PCLK1 ; F. f- }% `" T C PCLK1 = HCLK / 4 3 x# O: F2 Z6 }1 U+ S' l. E, k => 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 7 n% V3 s& F, K. i d7 m: T5 a 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 % k9 T$ @* A3 N, s- I variable value. Otherwise, any configuration based on this variable will be incorrect., W* M* e7 {8 x# Z8 O5 n- L This variable is updated in three ways: 1) by calling CMSIS function SystemCoreClockUpdate() 2) by calling HAL API function HAL_RCC_GetSysClockFreq()5 X. e8 n- N. F* V 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 */, h/ U& Y: R3 f& t a# b$ \# \8 `5 T2 | uwPrescalerValue = (uint32_t) ((SystemCoreClock /2) / 10000) - 1; //10KHz·ÖƵ 1ÃëÖÓ10000´Î F j: g. w% c1 c0 B /* Set TIMx instance */8 t7 W3 J# V6 Z# E htim6.Instance = TIM6;: U* V( k9 U( q$ x& k( M$ o 6 T5 n) F0 A q5 r8 V5 O1 [ /* Initialize TIM3 peripheral as follow: + Period = 10000 - 1 + Prescaler = ((SystemCoreClock/2)/10000) - 1 + ClockDivision = 0 + Counter direction = Up */ //htim6.Init.Period = 5000 - 1; 0 K% A3 G" n* R2 ^/ Y. w htim6.Init.Period = 1250 - 1; htim6.Init.Prescaler = uwPrescalerValue;- J! H) ^9 R |5 V, Z htim6.Init.ClockDivision = 0; htim6.Init.CounterMode = TIM_COUNTERMODE_UP;1 a1 m4 N. d( p7 }5 @3 n( H if(HAL_TIM_Base_Init(&htim6) != HAL_OK)* W' s+ k! Q: l5 D2 ?& Y {" Q! y% u9 U, u" j& q: k4 s /* Initialization Error */( E W0 q* i- c$ C, e1 b. w* n ] //Error_Handler();/ T$ H2 t) [" q3 d Y9 ~ } ' t4 \4 S+ \4 B* Y/ @ /*##-2- Start the TIM Base generation in interrupt mode ####################*/ /* Start Channel1 */; _) _2 Z! C$ I/ k" s8 R7 r! B if(HAL_TIM_Base_Start_IT(&htim6) != HAL_OK) { /* Starting Error */ //Error_Handler(); } }) q4 ~# y) s( H5 x 中断处理,125msx4=500ms:% U P# N% ^# T p . h7 m' W$ I: q4 @1 X, Z: u( z void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) {" |- Y: M7 j/ S6 u4 z6 N //BSP_LED_Toggle(LED4);HAL_GPIO_TogglePin(GPIO_PORT[Led], GPIO_PIN[Led]);2 ]# Q `% A" i) S+ Q5 F3 R2 n2 N 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? |
无刷直流电机控制应用+基于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的程序,包含固件、驱动和测试用的应用程序
微信公众号
手机版