本帖最后由 glmyc 于 2017-6-13 10:45 编辑
==F767 新手学习中==
用TIM4和TIM5定时器共8个通道捕获8路超声波成功,每测量5次取中间值三次平均
===========主函数==========
#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "led.h"
#include "sr04t.h"
#include "timer.h"
int main(void)
{
Cache_Enable(); //打开L1-Cache
HAL_Init(); //初始化HAL库
Stm32_Clock_Init(432,25,2,9); //设置时钟,216Mhz
delay_init(216); //延时初始化
uart_init(115200); //串口初始化
LED_Init(); //初始化LED
SR04T_Init(); //初始化超声波发射脚
SR04T_TIM4_Init(0xFFFF-1,108-1);//超声波定时器 4初始化
SR04T_TIM5_Init(0xFFFF-1,108-1);//超声波定时器 5初始化
while(1)
{
sr04tdis(1);//前上 PB4_T, PB6_E超声波TIM4CH1_CAPTURE_VAL
sr04tdis(2);//前下PB5_T, PB7_E超声波TIM4CH2_CAPTURE_VAL
sr04tdis(3);//左前PF9_T, PB8_E超声波TIM4CH3_CAPTURE_VAL
sr04tdis(4);//右前PA15_T, PB9_E超声波TIM4CH4_CAPTURE_VAL
sr04tdis(5);//后上 PG6_T,PH10_E超声波TIM5CH1_CAPTURE_VAL
sr04tdis(6);//后下 PE1_T,PH11_E超声波TIM5CH2_CAPTURE_VAL
sr04tdis(7);//左后PD12_T,PH12_E超声波TIM5CH1_CAPTURE_VAL
sr04tdis(8);//右后 PI4_T, PI0_E超声波TIM5CH1_CAPTURE_VAL
}
}
============timer.h================
#ifndef _TIMER_H
#define _TIMER_H
#include "sys.h"
extern TIM_HandleTypeDef TIM4_Handler; //定时器3PWM句柄
extern TIM_HandleTypeDef TIM5_Handler; //定时器3PWM句柄
extern u8 TIM4CH1_CAPTURE_STA; //输入捕获状态
extern u32 TIM4CH1_CAPTURE_VAL; //输入捕获值
extern u8 TIM4CH2_CAPTURE_STA; //输入捕获状态
extern u32 TIM4CH2_CAPTURE_VAL; //输入捕获值
extern u8 TIM4CH3_CAPTURE_STA; //输入捕获状态
extern u32 TIM4CH3_CAPTURE_VAL; //输入捕获值
extern u8 TIM4CH4_CAPTURE_STA; //输入捕获状态
extern u32 TIM4CH4_CAPTURE_VAL; //输入捕获值
extern u8 TIM5CH1_CAPTURE_STA; //输入捕获状态
extern u32 TIM5CH1_CAPTURE_VAL; //输入捕获值
extern u8 TIM5CH2_CAPTURE_STA; //输入捕获状态
extern u32 TIM5CH2_CAPTURE_VAL; //输入捕获值
extern u8 TIM5CH3_CAPTURE_STA; //输入捕获状态
extern u32 TIM5CH3_CAPTURE_VAL; //输入捕获值
extern u8 TIM5CH4_CAPTURE_STA; //输入捕获状态
extern u32 TIM5CH4_CAPTURE_VAL; //输入捕获值
void SR04T_TIM4_Init(u32 arr,u16 psc);//超声波定时器 4初始化
void SR04T_TIM5_Init(u32 arr,u16 psc);//超声波定时器 5初始化
void HAL_TIM_IC_MspInit(TIM_HandleTypeDef *htim);
void TIM5_IRQHandler(void);
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim);
void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim);
#endif
=============timer.c=========================
#include "timer.h"
#include "sr04t.h"
TIM_HandleTypeDef TIM4_Handler; //定时器4句柄
TIM_HandleTypeDef TIM5_Handler; //定时器5句柄
//捕获状态
//[7]:0,没有成功的捕获;1,成功捕获到一次.
//[6]:0,还没捕获到低电平;1,已经捕获到低电平了.
//[5:0]:捕获低电平后溢出的次数(对于32位定时器来说,1us计数器加1,溢出时间:4294秒)
u8 TIM4CH1_CAPTURE_STA=0; //输入捕获状态
u32 TIM4CH1_CAPTURE_VAL; //输入捕获值(TIM4是32位)
u8 TIM4CH2_CAPTURE_STA=0; //输入捕获状态
u32 TIM4CH2_CAPTURE_VAL; //输入捕获值(TIM4是32位)
u8 TIM4CH3_CAPTURE_STA=0; //输入捕获状态
u32 TIM4CH3_CAPTURE_VAL; //输入捕获值(TIM4是32位)
u8 TIM4CH4_CAPTURE_STA=0; //输入捕获状态
u32 TIM4CH4_CAPTURE_VAL; //输入捕获值(TIM4是32位)
u8 TIM5CH1_CAPTURE_STA=0; //输入捕获状态
u32 TIM5CH1_CAPTURE_VAL; //输入捕获值(TIM5是32位)
u8 TIM5CH2_CAPTURE_STA=0; //输入捕获状态
u32 TIM5CH2_CAPTURE_VAL; //输入捕获值(TIM5是32位)
u8 TIM5CH3_CAPTURE_STA=0; //输入捕获状态
u32 TIM5CH3_CAPTURE_VAL; //输入捕获值(TIM5是32位)
u8 TIM5CH4_CAPTURE_STA=0; //输入捕获状态
u32 TIM5CH4_CAPTURE_VAL; //输入捕获值(TIM5是32位)
//*****************************************************************
//初始化超声波定时器
//*****************************************************************
void SR04T_TIM4_Init(u32 arr,u16 psc){ //初始化定时器4的1/2/3/4通道
TIM_IC_InitTypeDef TIM4_CH1Config;
TIM_IC_InitTypeDef TIM4_CH2Config;
TIM_IC_InitTypeDef TIM4_CH3Config;
TIM_IC_InitTypeDef TIM4_CH4Config;
TIM4_Handler.Instance = TIM4; //通用定时器4
TIM4_Handler.Init.Prescaler = psc; //分频系数
TIM4_Handler.Init.CounterMode = TIM_COUNTERMODE_UP; //向上计时
TIM4_Handler.Init.Period = arr; //定时器自动重载值
TIM4_Handler.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; //时钟分频因子
HAL_TIM_IC_Init(&TIM4_Handler); //初始化输入捕获时基参数
TIM4_CH1Config.ICPolarity = TIM_ICPOLARITY_RISING; //定时器4通道1以上升延捕获
TIM4_CH1Config.ICSelection = TIM_ICSELECTION_DIRECTTI; //映射是直连
TIM4_CH1Config.ICPrescaler = TIM_ICPSC_DIV1; //输入不分频,即每个上升延均捕获
TIM4_CH1Config.ICFilter = 0; //输入不滤波,即有就捕获
HAL_TIM_IC_ConfigChannel(&TIM4_Handler,&TIM4_CH1Config,TIM_CHANNEL_1); //配置TIM4通道1
TIM4_CH2Config.ICPolarity = TIM_ICPOLARITY_RISING; //定时器4通道2以上升延捕获
TIM4_CH2Config.ICSelection = TIM_ICSELECTION_DIRECTTI; //映射是直连
TIM4_CH2Config.ICPrescaler = TIM_ICPSC_DIV1; //输入不分频,即每个上升延均捕获
TIM4_CH2Config.ICFilter = 0; //输入不滤波,即有就捕获
HAL_TIM_IC_ConfigChannel(&TIM4_Handler,&TIM4_CH2Config,TIM_CHANNEL_2); //配置TIM4通道1
TIM4_CH3Config.ICPolarity = TIM_ICPOLARITY_RISING; //定时器4通道3以上升延捕获
TIM4_CH3Config.ICSelection = TIM_ICSELECTION_DIRECTTI; //映射是直连
TIM4_CH3Config.ICPrescaler = TIM_ICPSC_DIV1; //输入不分频,即每个上升延均捕获
TIM4_CH3Config.ICFilter = 0; //输入不滤波,即有就捕获
HAL_TIM_IC_ConfigChannel(&TIM4_Handler,&TIM4_CH3Config,TIM_CHANNEL_3); //配置TIM4通道1
TIM4_CH4Config.ICPolarity = TIM_ICPOLARITY_RISING; //定时器4通道4以上升延捕获
TIM4_CH4Config.ICSelection = TIM_ICSELECTION_DIRECTTI; //映射是直连
TIM4_CH4Config.ICPrescaler = TIM_ICPSC_DIV1; //输入不分频,即每个上升延均捕获
TIM4_CH4Config.ICFilter = 0; //输入不滤波,即有就捕获
HAL_TIM_IC_ConfigChannel(&TIM4_Handler,&TIM4_CH4Config,TIM_CHANNEL_4); //配置TIM4通道1
HAL_TIM_IC_Start_IT(&TIM4_Handler,TIM_CHANNEL_1); //开启TIM4的捕获通道1,并开启捕获中断
HAL_TIM_IC_Start_IT(&TIM4_Handler,TIM_CHANNEL_2); //开启TIM4的捕获通道2,并开启捕获中断
HAL_TIM_IC_Start_IT(&TIM4_Handler,TIM_CHANNEL_3); //开启TIM4的捕获通道3,并开启捕获中断
HAL_TIM_IC_Start_IT(&TIM4_Handler,TIM_CHANNEL_4); //开启TIM4的捕获通道4,并开启捕获中断
__HAL_TIM_ENABLE_IT(&TIM4_Handler,TIM_IT_UPDATE); //使能更新中断
}
void SR04T_TIM5_Init(u32 arr,u16 psc){ //初始化定时器5的1通道
TIM_IC_InitTypeDef TIM5_CH1Config;
TIM_IC_InitTypeDef TIM5_CH2Config;
TIM_IC_InitTypeDef TIM5_CH3Config;
TIM_IC_InitTypeDef TIM5_CH4Config;
TIM5_Handler.Instance = TIM5; //通用定时器5
TIM5_Handler.Init.Prescaler = psc; //分频系数
TIM5_Handler.Init.CounterMode = TIM_COUNTERMODE_UP; //向上计时
TIM5_Handler.Init.Period = arr; //定时器自动重载值
TIM5_Handler.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; //时钟分频因子
HAL_TIM_IC_Init(&TIM5_Handler); //初始化输入捕获时基参数
TIM5_CH1Config.ICPolarity = TIM_ICPOLARITY_RISING; //定时器5通道1以上升延捕获
TIM5_CH1Config.ICSelection = TIM_ICSELECTION_DIRECTTI; //映射是直连
TIM5_CH1Config.ICPrescaler = TIM_ICPSC_DIV1; //输入不分频,即每个上升延均捕获
TIM5_CH1Config.ICFilter = 0; //输入不滤波,即有就捕获
HAL_TIM_IC_ConfigChannel(&TIM5_Handler,&TIM5_CH1Config,TIM_CHANNEL_1); //配置TIM5通道1
TIM5_CH2Config.ICPolarity = TIM_ICPOLARITY_RISING; //定时器5通道1以上升延捕获
TIM5_CH2Config.ICSelection = TIM_ICSELECTION_DIRECTTI; //映射是直连
TIM5_CH2Config.ICPrescaler = TIM_ICPSC_DIV1; //输入不分频,即每个上升延均捕获
TIM5_CH2Config.ICFilter = 0; //输入不滤波,即有就捕获
HAL_TIM_IC_ConfigChannel(&TIM5_Handler,&TIM5_CH2Config,TIM_CHANNEL_2); //配置TIM5通道1
TIM5_CH3Config.ICPolarity = TIM_ICPOLARITY_RISING; //定时器5通道1以上升延捕获
TIM5_CH3Config.ICSelection = TIM_ICSELECTION_DIRECTTI; //映射是直连
TIM5_CH3Config.ICPrescaler = TIM_ICPSC_DIV1; //输入不分频,即每个上升延均捕获
TIM5_CH3Config.ICFilter = 0; //输入不滤波,即有就捕获
HAL_TIM_IC_ConfigChannel(&TIM5_Handler,&TIM5_CH3Config,TIM_CHANNEL_3); //配置TIM5通道1
TIM5_CH4Config.ICPolarity = TIM_ICPOLARITY_RISING; //定时器5通道1以上升延捕获
TIM5_CH4Config.ICSelection = TIM_ICSELECTION_DIRECTTI; //映射是直连
TIM5_CH4Config.ICPrescaler = TIM_ICPSC_DIV1; //输入不分频,即每个上升延均捕获
TIM5_CH4Config.ICFilter = 0; //输入不滤波,即有就捕获
HAL_TIM_IC_ConfigChannel(&TIM5_Handler,&TIM5_CH4Config,TIM_CHANNEL_4); //配置TIM5通道1
HAL_TIM_IC_Start_IT(&TIM5_Handler,TIM_CHANNEL_1); //开启TIM5的捕获通道1,并开启捕获中断
HAL_TIM_IC_Start_IT(&TIM5_Handler,TIM_CHANNEL_2); //开启TIM5的捕获通道2,并开启捕获中断
HAL_TIM_IC_Start_IT(&TIM5_Handler,TIM_CHANNEL_3); //开启TIM5的捕获通道3,并开启捕获中断
HAL_TIM_IC_Start_IT(&TIM5_Handler,TIM_CHANNEL_4); //开启TIM5的捕获通道4,并开启捕获中断
__HAL_TIM_ENABLE_IT(&TIM5_Handler,TIM_IT_UPDATE); //使能更新中断
}
//*****************************************************************
//初始化定时器的回调函数
//定时器5底层驱动,时钟使能,引脚配置
//此函数会被HAL_TIM_IC_Init()调用
//htim:定时器5句柄
//*****************************************************************
void HAL_TIM_IC_MspInit(TIM_HandleTypeDef *htim)
{
if(htim->Instance == TIM4){
GPIO_InitTypeDef GPIO_Initure;
__HAL_RCC_TIM4_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
GPIO_Initure.Pin = GPIO_PIN_6;
GPIO_Initure.Mode = GPIO_MODE_AF_PP;
GPIO_Initure.Pull = GPIO_PULLDOWN;
GPIO_Initure.Speed = GPIO_SPEED_HIGH;
GPIO_Initure.Alternate = GPIO_AF2_TIM4;
HAL_GPIO_Init(GPIOB,&GPIO_Initure);
GPIO_Initure.Pin = GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9;
HAL_GPIO_Init(GPIOB,&GPIO_Initure);
HAL_NVIC_SetPriority(TIM4_IRQn,3,2);
HAL_NVIC_EnableIRQ(TIM4_IRQn);
}
if(htim->Instance == TIM5){
GPIO_InitTypeDef GPIO_Initure;
__HAL_RCC_TIM5_CLK_ENABLE();
__HAL_RCC_GPIOH_CLK_ENABLE();
__HAL_RCC_GPIOI_CLK_ENABLE();
GPIO_Initure.Pin = GPIO_PIN_10|GPIO_PIN_12|GPIO_PIN_11;
GPIO_Initure.Mode = GPIO_MODE_AF_PP;
GPIO_Initure.Pull = GPIO_PULLDOWN;
GPIO_Initure.Speed = GPIO_SPEED_HIGH;
GPIO_Initure.Alternate = GPIO_AF2_TIM5;
HAL_GPIO_Init(GPIOH,&GPIO_Initure);
GPIO_Initure.Pin = GPIO_PIN_0;
GPIO_Initure.Mode = GPIO_MODE_AF_PP;
GPIO_Initure.Pull = GPIO_PULLDOWN;
GPIO_Initure.Speed = GPIO_SPEED_HIGH;
GPIO_Initure.Alternate = GPIO_AF2_TIM5;
HAL_GPIO_Init(GPIOI,&GPIO_Initure);
HAL_NVIC_SetPriority(TIM5_IRQn,3,2);
HAL_NVIC_EnableIRQ(TIM5_IRQn);
}
}
//****************************************
//定时器4、5的中断服务函数
//****************************************
void TIM4_IRQHandler(void){
HAL_TIM_IRQHandler(&TIM4_Handler); //定时器共用处理函数
}
void TIM5_IRQHandler(void){
HAL_TIM_IRQHandler(&TIM5_Handler); //定时器共用处理函数
}
//定时器更新中断(计数溢出)中断处理回调函数, 该函数在HAL_TIM_IRQHandler中会被调用
//更新中断(溢出)发生时执行
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if(htim->Instance == TIM5){
if((TIM5CH1_CAPTURE_STA&0X80)==0)//还未成功捕获
{
if(TIM5CH1_CAPTURE_STA&0X40)//已经捕获到高电平了
{
if((TIM5CH1_CAPTURE_STA&0X3F)==0X3F)//高电平太长了
{
TIM5CH1_CAPTURE_STA|=0X80; //标记成功捕获了一次
TIM5CH1_CAPTURE_VAL=0XFFFE;
}else TIM5CH1_CAPTURE_STA++;
}
}
if((TIM5CH2_CAPTURE_STA&0X80)==0)//还未成功捕获
{
if(TIM5CH2_CAPTURE_STA&0X40)//已经捕获到高电平了
{
if((TIM5CH2_CAPTURE_STA&0X3F)==0X3F)//高电平太长了
{
TIM5CH2_CAPTURE_STA|=0X80; //标记成功捕获了一次
TIM5CH2_CAPTURE_VAL=0XFFFE;
}else TIM5CH2_CAPTURE_STA++;
}
}
if((TIM5CH3_CAPTURE_STA&0X80)==0)//还未成功捕获
{
if(TIM5CH3_CAPTURE_STA&0X40)//已经捕获到高电平了
{
if((TIM5CH3_CAPTURE_STA&0X3F)==0X3F)//高电平太长了
{
TIM5CH3_CAPTURE_STA|=0X80; //标记成功捕获了一次
TIM5CH3_CAPTURE_VAL=0XFFFE;
}else TIM5CH3_CAPTURE_STA++;
}
}
if((TIM5CH4_CAPTURE_STA&0X80)==0)//还未成功捕获
{
if(TIM5CH4_CAPTURE_STA&0X40)//已经捕获到高电平了
{
if((TIM5CH4_CAPTURE_STA&0X3F)==0X3F)//高电平太长了
{
TIM5CH4_CAPTURE_STA|=0X80; //标记成功捕获了一次
TIM5CH4_CAPTURE_VAL=0XFFFE;
}else TIM5CH4_CAPTURE_STA++;
}
}
}
if(htim->Instance == TIM4){
if((TIM4CH1_CAPTURE_STA&0X80)==0)//还未成功捕获
{
if(TIM4CH1_CAPTURE_STA&0X40)//已经捕获到高电平了
{
if((TIM4CH1_CAPTURE_STA&0X3F)==0X3F)//高电平太长了
{
TIM4CH1_CAPTURE_STA|=0X80; //标记成功捕获了一次
TIM4CH1_CAPTURE_VAL=0XFFFE;
}else TIM4CH1_CAPTURE_STA++;
}
}
if((TIM4CH2_CAPTURE_STA&0X80)==0)//还未成功捕获
{
if(TIM4CH2_CAPTURE_STA&0X40)//已经捕获到高电平了
{
if((TIM4CH2_CAPTURE_STA&0X3F)==0X3F)//高电平太长了
{
TIM4CH2_CAPTURE_STA|=0X80; //标记成功捕获了一次
TIM4CH2_CAPTURE_VAL=0XFFFE;
}else TIM4CH2_CAPTURE_STA++;
}
}
if((TIM4CH3_CAPTURE_STA&0X80)==0)//还未成功捕获
{
if(TIM4CH3_CAPTURE_STA&0X40)//已经捕获到高电平了
{
if((TIM4CH3_CAPTURE_STA&0X3F)==0X3F)//高电平太长了
{
TIM4CH3_CAPTURE_STA|=0X80; //标记成功捕获了一次
TIM4CH3_CAPTURE_VAL=0XFFFE;
}else TIM4CH3_CAPTURE_STA++;
}
}
if((TIM4CH4_CAPTURE_STA&0X80)==0)//还未成功捕获
{
if(TIM4CH4_CAPTURE_STA&0X40)//已经捕获到高电平了
{
if((TIM4CH4_CAPTURE_STA&0X3F)==0X3F)//高电平太长了
{
TIM4CH4_CAPTURE_STA|=0X80; //标记成功捕获了一次
TIM4CH4_CAPTURE_VAL=0XFFFE;
}else TIM4CH4_CAPTURE_STA++;
}
}
}
}
//定时器输入捕获中断处理回调函数,该函数在HAL_TIM_IRQHandler中会被调用
//捕获中断发生时执行
void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
{
if(htim->Instance == TIM5){
if((TIM5CH1_CAPTURE_STA&0X80)==0)//还未成功捕获
{
if(TIM5CH1_CAPTURE_STA&0X40) //捕获到一个下降沿
{
TIM5CH1_CAPTURE_STA|=0X80; //标记成功捕获到一次高电平脉宽
TIM5CH1_CAPTURE_VAL=HAL_TIM_ReadCapturedValue(&TIM5_Handler,TIM_CHANNEL_1);//获取当前的捕获值.
TIM_RESET_CAPTUREPOLARITY(&TIM5_Handler,TIM_CHANNEL_1); //一定要先清除原来的设置!!
TIM_SET_CAPTUREPOLARITY(&TIM5_Handler,TIM_CHANNEL_1,TIM_ICPOLARITY_RISING);//配置TIM5通道1上升沿捕获
}else{ //还未开始,第一次捕获上升沿
TIM5CH1_CAPTURE_STA=0; //清空
TIM5CH1_CAPTURE_VAL=0;
TIM5CH1_CAPTURE_STA|=0X40; //标记捕获到了上升沿
__HAL_TIM_DISABLE(&TIM5_Handler); //关闭定时器5
__HAL_TIM_SET_COUNTER(&TIM5_Handler,0);
TIM_RESET_CAPTUREPOLARITY(&TIM5_Handler,TIM_CHANNEL_1); //一定要先清除原来的设置!!
TIM_SET_CAPTUREPOLARITY(&TIM5_Handler,TIM_CHANNEL_1,TIM_ICPOLARITY_FALLING);//定时器5通道1设置为下降沿捕获
__HAL_TIM_ENABLE(&TIM5_Handler);//使能定时器5
}
}
if((TIM5CH2_CAPTURE_STA&0X80)==0)//还未成功捕获
{
if(TIM5CH2_CAPTURE_STA&0X40) //捕获到一个下降沿
{
TIM5CH2_CAPTURE_STA|=0X80; //标记成功捕获到一次高电平脉宽
TIM5CH2_CAPTURE_VAL=HAL_TIM_ReadCapturedValue(&TIM5_Handler,TIM_CHANNEL_2);//获取当前的捕获值.
TIM_RESET_CAPTUREPOLARITY(&TIM5_Handler,TIM_CHANNEL_2); //一定要先清除原来的设置!!
TIM_SET_CAPTUREPOLARITY(&TIM5_Handler,TIM_CHANNEL_2,TIM_ICPOLARITY_RISING);//配置TIM5通道1上升沿捕获
}else{ //还未开始,第一次捕获上升沿
TIM5CH2_CAPTURE_STA=0; //清空
TIM5CH2_CAPTURE_VAL=0;
TIM5CH2_CAPTURE_STA|=0X40; //标记捕获到了上升沿
__HAL_TIM_DISABLE(&TIM5_Handler); //关闭定时器5
__HAL_TIM_SET_COUNTER(&TIM5_Handler,0);
TIM_RESET_CAPTUREPOLARITY(&TIM5_Handler,TIM_CHANNEL_2); //一定要先清除原来的设置!!
TIM_SET_CAPTUREPOLARITY(&TIM5_Handler,TIM_CHANNEL_2,TIM_ICPOLARITY_FALLING);//定时器5通道1设置为下降沿捕获
__HAL_TIM_ENABLE(&TIM5_Handler);//使能定时器5
}
}
if((TIM5CH3_CAPTURE_STA&0X80)==0)//还未成功捕获
{
if(TIM5CH3_CAPTURE_STA&0X40) //捕获到一个下降沿
{
TIM5CH3_CAPTURE_STA|=0X80; //标记成功捕获到一次高电平脉宽
TIM5CH3_CAPTURE_VAL=HAL_TIM_ReadCapturedValue(&TIM5_Handler,TIM_CHANNEL_3);//获取当前的捕获值.
TIM_RESET_CAPTUREPOLARITY(&TIM5_Handler,TIM_CHANNEL_3); //一定要先清除原来的设置!!
TIM_SET_CAPTUREPOLARITY(&TIM5_Handler,TIM_CHANNEL_3,TIM_ICPOLARITY_RISING);//配置TIM5通道1上升沿捕获
}else{ //还未开始,第一次捕获上升沿
TIM5CH3_CAPTURE_STA=0; //清空
TIM5CH3_CAPTURE_VAL=0;
TIM5CH3_CAPTURE_STA|=0X40; //标记捕获到了上升沿
__HAL_TIM_DISABLE(&TIM5_Handler); //关闭定时器5
__HAL_TIM_SET_COUNTER(&TIM5_Handler,0);
TIM_RESET_CAPTUREPOLARITY(&TIM5_Handler,TIM_CHANNEL_3); //一定要先清除原来的设置!!
TIM_SET_CAPTUREPOLARITY(&TIM5_Handler,TIM_CHANNEL_3,TIM_ICPOLARITY_FALLING);//定时器5通道1设置为下降沿捕获
__HAL_TIM_ENABLE(&TIM5_Handler);//使能定时器5
}
}
if((TIM5CH4_CAPTURE_STA&0X80)==0)//还未成功捕获
{
if(TIM5CH4_CAPTURE_STA&0X40) //捕获到一个下降沿
{
TIM5CH4_CAPTURE_STA|=0X80; //标记成功捕获到一次高电平脉宽
TIM5CH4_CAPTURE_VAL=HAL_TIM_ReadCapturedValue(&TIM5_Handler,TIM_CHANNEL_4);//获取当前的捕获值.
TIM_RESET_CAPTUREPOLARITY(&TIM5_Handler,TIM_CHANNEL_4); //一定要先清除原来的设置!!
TIM_SET_CAPTUREPOLARITY(&TIM5_Handler,TIM_CHANNEL_4,TIM_ICPOLARITY_RISING);//配置TIM5通道1上升沿捕获
}else{ //还未开始,第一次捕获上升沿
TIM5CH4_CAPTURE_STA=0; //清空
TIM5CH4_CAPTURE_VAL=0;
TIM5CH4_CAPTURE_STA|=0X40; //标记捕获到了上升沿
__HAL_TIM_DISABLE(&TIM5_Handler); //关闭定时器5
__HAL_TIM_SET_COUNTER(&TIM5_Handler,0);
TIM_RESET_CAPTUREPOLARITY(&TIM5_Handler,TIM_CHANNEL_4); //一定要先清除原来的设置!!
TIM_SET_CAPTUREPOLARITY(&TIM5_Handler,TIM_CHANNEL_4,TIM_ICPOLARITY_FALLING);//定时器5通道1设置为下降沿捕获
__HAL_TIM_ENABLE(&TIM5_Handler);//使能定时器5
}
}
}
if(htim->Instance == TIM4){
if((TIM4CH1_CAPTURE_STA&0X80)==0)//还未成功捕获
{
if(TIM4CH1_CAPTURE_STA&0X40) //捕获到一个下降沿
{&n
一周热门 更多>