我写的程序实现不了STM32F407ZGT6高级定时器TIM1的输入捕获,编译没有问题,10ms定时可以实现,但就是捕获不了高电平和低电平,求高手指点!////****以下是主程序main.c//*****
#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "led.h"
#include "timer.h"
int main(void)
{
u16 t=0;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
delay_init(168);
uart_init(115200);
LED_Init();
timer1_cap1_init (9999,167);//10msÒç³öÒ»´Î
while(1)
{
delay_ms(10);
t++;
if(t==50)
{
t=0;
LED0 =!LED0;
}
}
}
///*****以下是定时器初始化和中断服务函数
#include"timer.h"
#include"sys.h"
#include"led.h"
u8 sta=0;//[7]: 0,没有成功的捕获;1,成功捕获到一次。
//[6]: 0,没有捕获到上升沿; 1,已经捕获到上升沿。
//[5:0];捕获到高电平后溢出的次数。
u16 timer1_val=0;
void timer1_cap1_init(u16 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBseInitStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
NVIC_InitTypeDef NVIC_InitSructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_DOWN;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource8,GPIO_AF_TIM1);
TIM_TimeBseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBseInitStructure.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBseInitStructure.TIM_Period=arr;
TIM_TimeBseInitStructure.TIM_Prescaler=psc;
//TIM_TimeBseInitStructure.TIM_RepetitionCounter=
TIM_TimeBaseInit(TIM1,&TIM_TimeBseInitStructure);
TIM_ICInitStructure.TIM_Channel=TIM_Channel_1;
TIM_ICInitStructure.TIM_ICPolarity=TIM_ICPolarity_Rising;
TIM_ICInitStructure.TIM_ICSelection=TIM_ICSelection_DirectTI;
TIM_ICInitStructure.TIM_ICPrescaler=TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICFilter=0x00;
TIM_ICInit(TIM1,&TIM_ICInitStructure);
TIM_ITConfig(TIM1,TIM_IT_Update|TIM_IT_CC1,ENABLE);
TIM_Cmd(TIM1,ENABLE);
NVIC_InitSructure.NVIC_IRQChannel=TIM1_UP_TIM10_IRQn;
NVIC_InitSructure.NVIC_IRQChannelPreemptionPriority=1;
NVIC_InitSructure.NVIC_IRQChannelSubPriority=2;
NVIC_InitSructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitSructure);
NVIC_InitSructure.NVIC_IRQChannel=TIM1_CC_IRQn;
NVIC_InitSructure.NVIC_IRQChannelPreemptionPriority=1;
NVIC_InitSructure.NVIC_IRQChannelSubPriority=3;
NVIC_InitSructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitSructure);
}
void TIM1_UP_TIM10_IRQHandler(void)
{
if(TIM_GetITStatus(TIM1,TIM_IT_Update)!=RESET)
{
if(sta&0x40)//判断是否已经捕获到上升沿
{
sta++;
if(sta&0x3f==0x3f)
{
sta|=0x80;
timer1_val=0xffff;
}
}
}
TIM_ClearITPendingBit(TIM1,TIM_IT_Update);
}
void TIM1_CC_IRQHandler(void)
{
if(TIM_GetITStatus(TIM1,TIM_IT_CC1)!=RESET)
{
if((sta&0x80)==0)//判断是否已经完成一次捕获
{
if(sta&0x40) //判断是否已经捕获到高电平
{
timer1_val=TIM_GetCapture1(TIM1);
TIM_OC1PolarityConfig(TIM1,TIM_ICPolarity_Rising);
sta|=0x80;
LED1=!LED1;
}
else //之前没有捕获到高电平,现在是第一次捕获到高电平
{
sta=0;
timer1_val=0;
sta=0x40;//标记已经捕获到高电平
TIM_Cmd(TIM1,DISABLE);
TIM_SetCounter(TIM1,0);
TIM_OC1PolarityConfig(TIM1,TIM_ICPolarity_Falling);
TIM_Cmd(TIM1,ENABLE);
}
}
}
TIM_ClearITPendingBit(TIM1,TIM_IT_CC1);
}
|