news 2026/6/10 2:43:54

stm32f10x tim.h定时器结构体

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
stm32f10x tim.h定时器结构体

定时器结构体

TIM_TimeBaseInitTypeDef;定时的基本应用

TIM_OCInitTypeDef;比较输出,compare->用于pwm,初始化调用该结构体

TIM_ICInitTypeDef;输入捕获,用于测量脉冲宽度

在此路径下可查看timebase的应用实例

D:\STM32开发用资料\STM32官方固件库\STM32F10x_StdPeriph_Lib_V3.5.0\STM32F10x_StdPeriph_Lib_V3.5.0\Project\STM32F10x_StdPeriph_Examples\TIM\TimeBase

/** ****************************************************************************** * @file TIM/TimeBase/main.c * @author MCD Application Team * @version V3.5.0 * @date 08-April-2011 * @brief Main program body ****************************************************************************** * @attention * * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. * * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2> ****************************************************************************** */ /* Includes ------------------------------------------------------------------*/ #include "stm32f10x.h" /** @addtogroup STM32F10x_StdPeriph_Examples * @{ */ /** @addtogroup TIM_TimeBase * @{ */ /* Private typedef -----------------------------------------------------------*/ /* Private define ------------------------------------------------------------*/ /* Private macro -------------------------------------------------------------*/ /* Private variables ---------------------------------------------------------*/ TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure; __IO uint16_t CCR1_Val = 40961; __IO uint16_t CCR2_Val = 27309; __IO uint16_t CCR3_Val = 13654; __IO uint16_t CCR4_Val = 6826; uint16_t PrescalerValue = 0; /* Private function prototypes -----------------------------------------------*/ void RCC_Configuration(void); void GPIO_Configuration(void); void NVIC_Configuration(void); /* Private functions ---------------------------------------------------------*/ /** * @brief Main program * @param None * @retval None */ int main(void) { /*!< At this stage the microcontroller clock setting is already configured, this is done through SystemInit() function which is called from startup file (startup_stm32f10x_xx.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32f10x.c file */ /* System Clocks Configuration */ RCC_Configuration(); /* NVIC Configuration */ NVIC_Configuration(); /* GPIO Configuration */ GPIO_Configuration(); /* --------------------------------------------------------------- TIM2 Configuration: Output Compare Timing Mode: TIM2 counter clock at 6 MHz CC1 update rate = TIM2 counter clock / CCR1_Val = 146.48 Hz CC2 update rate = TIM2 counter clock / CCR2_Val = 219.7 Hz CC3 update rate = TIM2 counter clock / CCR3_Val = 439.4 Hz CC4 update rate = TIM2 counter clock / CCR4_Val = 878.9 Hz --------------------------------------------------------------- */ /* Compute the prescaler value */ PrescalerValue = (uint16_t) (SystemCoreClock / 12000000) - 1; /* Time base configuration */ TIM_TimeBaseStructure.TIM_Period = 65535; TIM_TimeBaseStructure.TIM_Prescaler = 0; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); /* Prescaler configuration */ TIM_PrescalerConfig(TIM2, PrescalerValue, TIM_PSCReloadMode_Immediate); /* Output Compare Timing Mode configuration: Channel1 */ TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Timing; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = CCR1_Val; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; TIM_OC1Init(TIM2, &TIM_OCInitStructure); TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Disable); /* Output Compare Timing Mode configuration: Channel2 */ TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = CCR2_Val; TIM_OC2Init(TIM2, &TIM_OCInitStructure); TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Disable); /* Output Compare Timing Mode configuration: Channel3 */ TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = CCR3_Val; TIM_OC3Init(TIM2, &TIM_OCInitStructure); TIM_OC3PreloadConfig(TIM2, TIM_OCPreload_Disable); /* Output Compare Timing Mode configuration: Channel4 */ TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = CCR4_Val; TIM_OC4Init(TIM2, &TIM_OCInitStructure); TIM_OC4PreloadConfig(TIM2, TIM_OCPreload_Disable); /* TIM IT enable */ TIM_ITConfig(TIM2, TIM_IT_CC1 | TIM_IT_CC2 | TIM_IT_CC3 | TIM_IT_CC4, ENABLE); /* TIM2 enable counter */ TIM_Cmd(TIM2, ENABLE); while (1); } /** * @brief Configures the different system clocks. * @param None * @retval None */ void RCC_Configuration(void) { /* PCLK1 = HCLK/4 */ RCC_PCLK1Config(RCC_HCLK_Div4); /* TIM2 clock enable */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); /* GPIOC clock enable */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); } /** * @brief Configure the GPIO Pins. * @param None * @retval None */ void GPIO_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure; /* GPIOC Configuration:Pin6, 7, 8 and 9 as alternate function push-pull */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOC, &GPIO_InitStructure); } /** * @brief Configure the nested vectored interrupt controller. * @param None * @retval None */ void NVIC_Configuration(void) { NVIC_InitTypeDef NVIC_InitStructure; /* Enable the TIM2 global Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); } #ifdef USE_FULL_ASSERT /** * @brief Reports the name of the source file and the source line number * where the assert_param error has occurred. * @param file: pointer to the source file name * @param line: assert_param error line source number * @retval None */ void assert_failed(uint8_t* file, uint32_t line) { /* User can add his own implementation to report the file name and line number, ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ while (1) {} } #endif /** * @} */ /** * @} */ /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/10 15:19:05

【2025实战版】4步精通YOLOv8 AI自瞄:从零部署到性能优化全解析

【2025实战版】4步精通YOLOv8 AI自瞄&#xff1a;从零部署到性能优化全解析 【免费下载链接】RookieAI_yolov8 基于yolov8实现的AI自瞄项目 项目地址: https://gitcode.com/gh_mirrors/ro/RookieAI_yolov8 你是否在游戏中频繁遭遇瞄准精度不足的困扰&#xff1f;RookieA…

作者头像 李华
网站建设 2026/6/10 15:17:18

Live Room Watcher:全面掌握直播间数据的开源利器

Live Room Watcher&#xff1a;全面掌握直播间数据的开源利器 【免费下载链接】live-room-watcher &#x1f4fa; 可抓取直播间 弹幕, 礼物, 点赞, 原始流地址等 项目地址: https://gitcode.com/gh_mirrors/li/live-room-watcher 想要实时追踪直播间的弹幕互动、礼物动态…

作者头像 李华
网站建设 2026/6/10 14:27:35

DeepChem分子特征工程:从基础到实战的完整指南

你是否曾困惑于如何将复杂的化学结构转化为机器学习模型能够理解的数字语言&#xff1f;&#x1f914; 在药物发现和材料设计的道路上&#xff0c;分子特征工程正是这座连接化学世界与人工智能的桥梁&#xff01; 【免费下载链接】deepchem Democratizing Deep-Learning for Dr…

作者头像 李华
网站建设 2026/6/10 15:18:37

Obsidian美化资源高效获取与专业配置指南

Obsidian美化资源高效获取与专业配置指南 【免费下载链接】awesome-obsidian &#x1f576;️ Awesome stuff for Obsidian 项目地址: https://gitcode.com/gh_mirrors/aw/awesome-obsidian 在Obsidian使用过程中&#xff0c;许多用户面临主题下载缓慢、CSS美化资源应用…

作者头像 李华
网站建设 2026/6/10 15:18:02

3步搞定LLM微调数据集:Easy Dataset完整指南

3步搞定LLM微调数据集&#xff1a;Easy Dataset完整指南 【免费下载链接】easy-dataset A powerful tool for creating fine-tuning datasets for LLM 项目地址: https://gitcode.com/gh_mirrors/ea/easy-dataset 还在为LLM微调的数据集构建而烦恼吗&#xff1f;面对海量…

作者头像 李华