STM32F407-14.3.7-01PWM输入模式

2023-12-01 13:04

本文主要是介绍STM32F407-14.3.7-01PWM输入模式,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

PWM 输入模式 


此模式是输入捕获模式的一个特例。其实现步骤与输入捕获模式基本相同,仅存在以下不同之处:
 
例如,可通过以下步骤对应用于 TI1① 的 PWM 的周期(位于 TIMx_CCR1⑨ 寄存器中)和占空 比(位于 TIMx_CCR2⑮ 寄存器中)进行测量(取决于 CK_INT① 频率和预分频器的值): 
● IC1⑦ 与 IC2⑬ 两个信号被映射至同一个 TI1① 输入。 
● IC1⑦ 与 IC2⑬ 这两个信号在边沿处有效,但极性相反。 
● 选择TI1FP 与 TI2FP 中的 TI1FP④ 信号作为触发输入(也可选择TI2FP),并将从模式控制器配置为复位模式。 
● 向 TIMx_CCMR1 寄存器中的 CC1S② 位写入 01,选择 TIMx_CCR1 的有效输入: TI1①。 
● 向 CC1P 位和 CC1NP③ 位写入 “0” (上升沿有效)。选择 TI1FP1④ 的有效极性 : 上升沿有效(用于 TIMx_CCR1⑨ 中捕获上升沿时的计数值 和 TIMx_CNT⑯ 计数器清零): 
● 向 TIMx_CCMR1 寄存器中的 CC2S⑪ 位写入 10,选择 TIMx_CCR2 的有效输入: TI1①。 
● 向 CC2P 位和 CC2NP⑫ 位写入 “1” (下降沿有效)。选择 TI1FP2 的有效极性 : 下降沿有效(用于 TIMx_CCR2⑮ 中捕获下降沿时的计数值)。 
● 选择有效触发输入:向 TIMx_SMCR 寄存器中的 TS⑤ 位写入 101(选择 TI1FP1)。 
● 向 TIMx_SMCR 寄存器中的 SMS⑥ 位写入 100 , 将从模式控制器配置为复位模式。 
● 向 TIMx_CCER 寄存器中的 CC1E⑧ 位和 CC2E⑭ 位写入“1” , 使能捕获。 
根据F103的库函数TIM_PWMIConfig()微调了数据手册中框图的位置关系.

TIM_PWMIConfig()函数

/*	stm32f10x_tim.h 中宏定义 */
#define  TIM_ICPolarity_Rising             ((uint16_t)0x0000)
#define  TIM_ICPolarity_Falling            ((uint16_t)0x0002)#define TIM_ICSelection_DirectTI           ((uint16_t)0x0001) /*!< TIM Input 1, 2, 3 or 4 is selected to be connected to IC1, IC2, IC3 or IC4, respectively */
#define TIM_ICSelection_IndirectTI         ((uint16_t)0x0002) /*!< TIM Input 1, 2, 3 or 4 is selected to be connected to IC2, IC1, IC4 or IC3, respectively. *//*	stm32f10x_tim.c 中PWMIConfig定义 */
/*** @brief  Configures the TIM peripheral according to the specified*         parameters in the TIM_ICInitStruct to measure an external PWM signal.* @param  TIMx: where x can be  1, 2, 3, 4, 5, 8, 9, 12 or 15 to select the TIM peripheral.* @param  TIM_ICInitStruct: pointer to a TIM_ICInitTypeDef structure*         that contains the configuration information for the specified TIM peripheral.* @retval None*/
void TIM_PWMIConfig(TIM_TypeDef *TIMx, TIM_ICInitTypeDef *TIM_ICInitStruct)
{uint16_t icoppositepolarity = TIM_ICPolarity_Rising;uint16_t icoppositeselection = TIM_ICSelection_DirectTI;/* Check the parameters */assert_param(IS_TIM_LIST6_PERIPH(TIMx));/*互补极性设置*//* Select the Opposite Input Polarity */if (TIM_ICInitStruct->TIM_ICPolarity == TIM_ICPolarity_Rising){icoppositepolarity = TIM_ICPolarity_Falling;}else{icoppositepolarity = TIM_ICPolarity_Rising;}/*交叉通道设置*//* Select the Opposite Input */if (TIM_ICInitStruct->TIM_ICSelection == TIM_ICSelection_DirectTI){icoppositeselection = TIM_ICSelection_IndirectTI;}else{icoppositeselection = TIM_ICSelection_DirectTI;}/*分别配置IC1,IC2通道*/if (TIM_ICInitStruct->TIM_Channel == TIM_Channel_1){/* TI1 Configuration */TI1_Config(TIMx, TIM_ICInitStruct->TIM_ICPolarity, TIM_ICInitStruct->TIM_ICSelection,TIM_ICInitStruct->TIM_ICFilter);/* Set the Input Capture Prescaler value */TIM_SetIC1Prescaler(TIMx, TIM_ICInitStruct->TIM_ICPrescaler);/* TI2 Configuration */TI2_Config(TIMx, icoppositepolarity, icoppositeselection, TIM_ICInitStruct->TIM_ICFilter);/* Set the Input Capture Prescaler value */TIM_SetIC2Prescaler(TIMx, TIM_ICInitStruct->TIM_ICPrescaler);}else{/* TI2 Configuration */TI2_Config(TIMx, TIM_ICInitStruct->TIM_ICPolarity, TIM_ICInitStruct->TIM_ICSelection,TIM_ICInitStruct->TIM_ICFilter);/* Set the Input Capture Prescaler value */TIM_SetIC2Prescaler(TIMx, TIM_ICInitStruct->TIM_ICPrescaler);/* TI1 Configuration */TI1_Config(TIMx, icoppositepolarity, icoppositeselection, TIM_ICInitStruct->TIM_ICFilter);/* Set the Input Capture Prescaler value */TIM_SetIC1Prescaler(TIMx, TIM_ICInitStruct->TIM_ICPrescaler);}
}

TI1_Config (函数)

/*	通道 1 配置*/
/*** @brief  Configure the TI1 as Input.* @param  TIMx: where x can be 1 to 17 except 6 and 7 to select the TIM peripheral.* @param  TIM_ICPolarity : The Input Polarity.*   This parameter can be one of the following values:*     @arg TIM_ICPolarity_Rising*     @arg TIM_ICPolarity_Falling* @param  TIM_ICSelection: specifies the input to be used.*   This parameter can be one of the following values:*     @arg TIM_ICSelection_DirectTI: TIM Input 1 is selected to be connected to IC1.*     @arg TIM_ICSelection_IndirectTI: TIM Input 1 is selected to be connected to IC2.*     @arg TIM_ICSelection_TRC: TIM Input 1 is selected to be connected to TRC.* @param  TIM_ICFilter: Specifies the Input Capture Filter.*   This parameter must be a value between 0x00 and 0x0F.* @retval None*/
static void TI1_Config(TIM_TypeDef *TIMx, uint16_t TIM_ICPolarity, uint16_t TIM_ICSelection,uint16_t TIM_ICFilter)
{uint16_t tmpccmr1 = 0, tmpccer = 0;/* Disable the Channel 1: Reset the CC1E Bit */TIMx->CCER &= (uint16_t) ~((uint16_t)TIM_CCER_CC1E);tmpccmr1 = TIMx->CCMR1;tmpccer = TIMx->CCER;/* Select the Input and set the filter */tmpccmr1 &= (uint16_t)(((uint16_t) ~((uint16_t)TIM_CCMR1_CC1S)) & ((uint16_t) ~((uint16_t)TIM_CCMR1_IC1F)));tmpccmr1 |= (uint16_t)(TIM_ICSelection | (uint16_t)(TIM_ICFilter << (uint16_t)4));if ((TIMx == TIM1) || (TIMx == TIM8) || (TIMx == TIM2) || (TIMx == TIM3) ||(TIMx == TIM4) || (TIMx == TIM5)){/* Select the Polarity and set the CC1E Bit */tmpccer &= (uint16_t) ~((uint16_t)(TIM_CCER_CC1P));tmpccer |= (uint16_t)(TIM_ICPolarity | (uint16_t)TIM_CCER_CC1E);}else{/* Select the Polarity and set the CC1E Bit */tmpccer &= (uint16_t) ~((uint16_t)(TIM_CCER_CC1P | TIM_CCER_CC1NP));tmpccer |= (uint16_t)(TIM_ICPolarity | (uint16_t)TIM_CCER_CC1E);}/* Write to TIMx CCMR1 and CCER registers */TIMx->CCMR1 = tmpccmr1;TIMx->CCER = tmpccer;
}

TI2_Config (函数)

/*	通道2配置*/
/*** @brief  Configure the TI2 as Input.* @param  TIMx: where x can be 1, 2, 3, 4, 5, 8, 9, 12 or 15 to select the TIM peripheral.* @param  TIM_ICPolarity : The Input Polarity.*   This parameter can be one of the following values:*     @arg TIM_ICPolarity_Rising*     @arg TIM_ICPolarity_Falling* @param  TIM_ICSelection: specifies the input to be used.*   This parameter can be one of the following values:*     @arg TIM_ICSelection_DirectTI: TIM Input 2 is selected to be connected to IC2.*     @arg TIM_ICSelection_IndirectTI: TIM Input 2 is selected to be connected to IC1.*     @arg TIM_ICSelection_TRC: TIM Input 2 is selected to be connected to TRC.* @param  TIM_ICFilter: Specifies the Input Capture Filter.*   This parameter must be a value between 0x00 and 0x0F.* @retval None*/
static void TI2_Config(TIM_TypeDef *TIMx, uint16_t TIM_ICPolarity, uint16_t TIM_ICSelection,uint16_t TIM_ICFilter)
{uint16_t tmpccmr1 = 0, tmpccer = 0, tmp = 0;/* Disable the Channel 2: Reset the CC2E Bit */TIMx->CCER &= (uint16_t) ~((uint16_t)TIM_CCER_CC2E);tmpccmr1 = TIMx->CCMR1;tmpccer = TIMx->CCER;tmp = (uint16_t)(TIM_ICPolarity << 4);/* Select the Input and set the filter */tmpccmr1 &= (uint16_t)(((uint16_t) ~((uint16_t)TIM_CCMR1_CC2S)) & ((uint16_t) ~((uint16_t)TIM_CCMR1_IC2F)));tmpccmr1 |= (uint16_t)(TIM_ICFilter << 12);tmpccmr1 |= (uint16_t)(TIM_ICSelection << 8);if ((TIMx == TIM1) || (TIMx == TIM8) || (TIMx == TIM2) || (TIMx == TIM3) ||(TIMx == TIM4) || (TIMx == TIM5)){/* Select the Polarity and set the CC2E Bit */tmpccer &= (uint16_t) ~((uint16_t)(TIM_CCER_CC2P));tmpccer |= (uint16_t)(tmp | (uint16_t)TIM_CCER_CC2E);}else{/* Select the Polarity and set the CC2E Bit */tmpccer &= (uint16_t) ~((uint16_t)(TIM_CCER_CC2P | TIM_CCER_CC2NP));tmpccer |= (uint16_t)(TIM_ICPolarity | (uint16_t)TIM_CCER_CC2E);}/* Write to TIMx CCMR1 and CCER registers */TIMx->CCMR1 = tmpccmr1;TIMx->CCER = tmpccer;
}

这篇关于STM32F407-14.3.7-01PWM输入模式的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/441250

相关文章

Java设计模式---迭代器模式(Iterator)解读

《Java设计模式---迭代器模式(Iterator)解读》:本文主要介绍Java设计模式---迭代器模式(Iterator),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,... 目录1、迭代器(Iterator)1.1、结构1.2、常用方法1.3、本质1、解耦集合与遍历逻辑2、统一

Java 线程安全与 volatile与单例模式问题及解决方案

《Java线程安全与volatile与单例模式问题及解决方案》文章主要讲解线程安全问题的五个成因(调度随机、变量修改、非原子操作、内存可见性、指令重排序)及解决方案,强调使用volatile关键字... 目录什么是线程安全线程安全问题的产生与解决方案线程的调度是随机的多个线程对同一个变量进行修改线程的修改操

Redis Cluster模式配置

《RedisCluster模式配置》:本文主要介绍RedisCluster模式配置,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录分片 一、分片的本质与核心价值二、分片实现方案对比 ‌三、分片算法详解1. ‌范围分片(顺序分片)‌2. ‌哈希分片3. ‌虚

RabbitMQ工作模式中的RPC通信模式详解

《RabbitMQ工作模式中的RPC通信模式详解》在RabbitMQ中,RPC模式通过消息队列实现远程调用功能,这篇文章给大家介绍RabbitMQ工作模式之RPC通信模式,感兴趣的朋友一起看看吧... 目录RPC通信模式概述工作流程代码案例引入依赖常量类编写客户端代码编写服务端代码RPC通信模式概述在R

Python使用pynput模拟实现键盘自动输入工具

《Python使用pynput模拟实现键盘自动输入工具》在日常办公和软件开发中,我们经常需要处理大量重复的文本输入工作,所以本文就来和大家介绍一款使用Python的PyQt5库结合pynput键盘控制... 目录概述:当自动化遇上可视化功能全景图核心功能矩阵技术栈深度效果展示使用教程四步操作指南核心代码解析

SQL Server身份验证模式步骤和示例代码

《SQLServer身份验证模式步骤和示例代码》SQLServer是一个广泛使用的关系数据库管理系统,通常使用两种身份验证模式:Windows身份验证和SQLServer身份验证,本文将详细介绍身份... 目录身份验证方式的概念更改身份验证方式的步骤方法一:使用SQL Server Management S

Redis高可用-主从复制、哨兵模式与集群模式详解

《Redis高可用-主从复制、哨兵模式与集群模式详解》:本文主要介绍Redis高可用-主从复制、哨兵模式与集群模式的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝... 目录Redis高可用-主从复制、哨兵模式与集群模式概要一、主从复制(Master-Slave Repli

一文带你搞懂Redis Stream的6种消息处理模式

《一文带你搞懂RedisStream的6种消息处理模式》Redis5.0版本引入的Stream数据类型,为Redis生态带来了强大而灵活的消息队列功能,本文将为大家详细介绍RedisStream的6... 目录1. 简单消费模式(Simple Consumption)基本概念核心命令实现示例使用场景优缺点2

Nginx location匹配模式与规则详解

《Nginxlocation匹配模式与规则详解》:本文主要介绍Nginxlocation匹配模式与规则,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、环境二、匹配模式1. 精准模式2. 前缀模式(不继续匹配正则)3. 前缀模式(继续匹配正则)4. 正则模式(大

使用Python实现一键隐藏屏幕并锁定输入

《使用Python实现一键隐藏屏幕并锁定输入》本文主要介绍了使用Python编写一个一键隐藏屏幕并锁定输入的黑科技程序,能够在指定热键触发后立即遮挡屏幕,并禁止一切键盘鼠标输入,这样就再也不用担心自己... 目录1. 概述2. 功能亮点3.代码实现4.使用方法5. 展示效果6. 代码优化与拓展7. 总结1.