本帖最后由 rainic 于 2017-9-9 10:35 编辑
输入脉冲波宽度是变化的,TIM5是输入捕获(一次捕获一个上升沿或者下降沿)计算时间t0,在下降沿到来时立即开启TIM3计时时间为t0/2,时间到则产生中断并开启TIM4输出单脉冲。程序在上面,测试结果是串口有正确的时间输出,示波器并没有单脉冲输出。请大家帮我看看出问题在哪里???
TIM5 PA0 输入捕获,TIM3定时器中断,TIM4 PB6 配置成单脉冲输出。
#include "led.h"
#include "key.h"
#include "sys.h"
#include "usart.h"
#include "timer.h"
extern u32 temp;
int main(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置中断优先级分组
uart_init(115200);
TIM4_OPMGATE_Init(); //单脉冲初始化
TIM3_Int_Init(temp/2,72-1); // TIM3初始化
TIM5_Cap_Init(0XFFFF,72-1); //输入捕获初始化
while(1)
{
}
}
#include "timer.h"
#include "led.h"
#include "usart.h"
//arr自动重装值
//psc时钟预分频数
u16 zhouqi=30;//*0.1ms
u16 gaodianping=10;//*0.1ms
void TIM4_OPMGATE_Init()
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
TIM_OCInitTypeDef TIM_OCInitStruct;
GPIO_InitTypeDef GPIO_InitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);//使能PB口
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE);//使能定时器4
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);//使能复用IO时钟
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStruct);
// GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6;
// GPIO_Init(GPIOA,&GPIO_InitStruct);
//TIM4工作在单脉冲下
TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;//向上计数模式
TIM_TimeBaseInitStruct.TIM_Prescaler = 7199;//预分频值每100u计数一次
TIM_TimeBaseInitStruct.TIM_Period = zhouqi-1; //重装载决定单脉冲周期
TIM_TimeBaseInit(TIM4,&TIM_TimeBaseInitStruct);
TIM_SelectOnePulseMode(TIM4,TIM_OPMode_Single);//设置TIM4在单脉冲模式,且是单一的脉冲,再下一次更新事件停止
TIM_SelectOutputTrigger(TIM4,TIM_TRGOSource_OC1Ref);
TIM_OC1PreloadConfig(TIM4,TIM_OCPreload_Enable);//使能TIM4的通道1预装载寄存器
TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_PWM2;//向上计数模式,一旦TIMx_CNT>TIMx_CCR1时通道1为有效电平
TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Enable;//OC1输出使能
TIM_OCInitStruct.TIM_OCPolarity = TIM_OCPolarity_High;//有效电平为高
TIM_OCInitStruct.TIM_Pulse = zhouqi-gaodianping;//比较捕获1的预装载值,决定单脉冲占空比,得到低电平延续时间
TIM_OC1Init(TIM4,&TIM_OCInitStruct);
TIM_Cmd(TIM4,DISABLE); //Enable the TIM Counter
}
void TIM4_Open()
{
TIM_Cmd(TIM4,ENABLE);//使能TIM2
}
/*************************************************************/
void TIM3_Int_Init(u16 arr3,u16 psc3)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //使能时钟
//TIM3初始化
TIM_TimeBaseStructure.TIM_Period = arr3; //设置在下一个更新时间装入活动的自动重装载值周期的值
TIM_TimeBaseStructure.TIM_Prescaler =psc3; //预分频值
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //根据指定的参数初始化TIMx的时间基数单位
TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE ); //使能指定的TIM3中断,允许更新中断
//中断优先级设置
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; //TIM3中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //先占优先级0级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //从优先级3级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //初始化NVIC寄存器
TIM_Cmd(TIM3, DISABLE); //默认关闭TIM3
}
void TIM3_Open()
{
TIM_Cmd(TIM3,ENABLE);//开启定时器
}
//TIM3中断服务程序
void TIM3_IRQHandler(void) //TIM3ÖD¶Ï
{
if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET) //检查是否产生中断
{
TIM4_Open();//中断里开启TIM4产生单脉冲
}
TIM_ClearITPendingBit(TIM3, TIM_IT_Update ); //清除中断标志
}
/***********************************************************/
//TIM5通道输入捕获配置
TIM_ICInitTypeDef TIM5_ICInitStructure;
void TIM5_Cap_Init(u16 arr5,u16 psc5)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE); //使能TIM5
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //使能GPIO时钟
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; //PA1清除之前设置
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //PA1 输入
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_ResetBits(GPIOA,GPIO_Pin_0); //PA1 下拉
//初始化TIM5 TIM5
TIM_TimeBaseStructure.TIM_Period = arr5; //设定时计数器自动重装值
TIM_TimeBaseStructure.TIM_Prescaler =psc5; //预分频器
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIMÏ向上计数
TIM_TimeBaseInit(TIM5, &TIM_TimeBaseStructure); //根TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数
//初始化TIM5输入捕获参数
TIM5_ICInitStructure.TIM_Channel = TIM_Channel_1; //CC1S=01 选择输入端 IC1映射到TI1上
TIM5_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //上升沿捕获
TIM5_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //映射到TI1上
TIM5_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //配置输入分频,不分频,每个沿都检测
TIM5_ICInitStructure.TIM_ICFilter = 0x00;//IC1F=0000配置输入滤波器 不滤波
TIM_ICInit(TIM5, &TIM5_ICInitStructure);
//中断分组初始化
NVIC_InitStructure.NVIC_IRQChannel = TIM5_IRQn; //TIM3中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; //先占优先级2级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //从优先级0级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
NVIC_Init(&NVIC_InitStructure); //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器
TIM_ITConfig(TIM5,TIM_IT_Update|TIM_IT_CC1,ENABLE);//允许更新中断,允许CC1IE捕获中断
TIM_Cmd(TIM5,ENABLE ); //使能TIM5
}
u8 TIM5CH1_CAPTURE_STA=0; //输入捕获状态
u16 TIM5CH1_CAPTURE_VAL; //输入捕获值
u32 temp=0;
//TIM5中断服务函数
void TIM5_IRQHandler(void)
{
/******************更新中断***************/
if((TIM5CH1_CAPTURE_STA&0X80)==0)//还未成功捕获
{
if (TIM_GetITStatus(TIM5, TIM_IT_Update) != RESET)
{
if(TIM5CH1_CAPTURE_STA&0X40)//已经捕获到高电平
{
if((TIM5CH1_CAPTURE_STA&0X3F)==0X3F)//高电平太长
{
TIM5CH1_CAPTURE_STA|=0X80;//标记成功捕获一次
TIM5CH1_CAPTURE_VAL=0XFFFF;
}
else TIM5CH1_CAPTURE_STA++;
}
}
/****************捕获中断,上升沿下降沿都会触发中断***********/
if (TIM_GetITStatus(TIM5, TIM_IT_CC1) != RESET)//发生捕获事件
{
if(TIM5CH1_CAPTURE_STA&0X40) //捕获到一个下降沿
{
TIM5CH1_CAPTURE_STA|=0X80; //标记成功捕获到一次上升沿
TIM3_Open();
TIM5CH1_CAPTURE_VAL=TIM_GetCapture1(TIM5);
TIM_OC1PolarityConfig(TIM5,TIM_ICPolarity_Rising); //CC1P=0 设置为上升沿捕获
}else //捕获不成功,第一次捕获的是下降沿
{
TIM5CH1_CAPTURE_STA=0; //清0
TIM5CH1_CAPTURE_VAL=0;
TIM_SetCounter(TIM5,0);
TIM5CH1_CAPTURE_STA|=0X40; //标记捕获到了上升沿
TIM_OC1PolarityConfig(TIM5,TIM_ICPolarity_Falling); //CC1P=1设置为下降沿捕获
}
}
}
|