我写了个程序,但未能实现
STM32F407ZET6高级定时器
tiM1的输入捕获,编译没问题,但就是捕获不到沿跳变,求大神指点!
以下是我写的程序:
/***************main.c*****************************/
#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "led.h"
#include "timer.h"
int main(void)
{
u16t=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;
}
}
}
/****************************timer.c**********************/
#include"timer.h"
#include"sys.h"
#include"led.h"
u8 sta;
// [7], 0,没有完成一次捕获;1,已经完成一次捕获
//[6], 0,没有捕获到上升沿;1,;已经捕获到上升沿
//[5:0], 捕获到高电平后定时器的溢出次数
u16 timer1_val=0;
void timer1_cap1_init(u16 arr,u16 psc)
{
GPIO_InitTypeDefGPIO_InitStructure;
TIM_TimeBaseInitTypeDefTIM_TimeBseInitStructure;
TIM_ICInitTypeDefTIM_ICInitStructure;
NVIC_InitTypeDefNVIC_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);
}
那你要了解下每个寄存器,和那些关键配置参数的寄存器
一周热门 更多>