程序如下,用
tiM3控制PB5作为输出引脚连接到TIM5的通道1(PA0)上进行捕获。
TIM3和输出引脚的初始化函数:
void tim3_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStruct;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_Init(GPIOB,&GPIO_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_ClearITPendingBit(TIM3,TIM_IT_Update);
TIM_TimeBaseStruct.TIM_Period=20
TIM_TimeBaseStruct.TIM_Prescaler=29
TIM_TimeBaseStruct.TIM_ClockDivision=0;
TIM_TimeBaseStruct.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStruct);
TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);
TIM_Cmd(TIM3,ENABLE);
}
TIM3 的中断函数:
void TIM3_IRQHandler(void)
{
static u8 i;
TIM_ClearITPendingBit(TIM3,TIM_IT_Update);
if(i==0)
{
GPIO_SetBits(GPIOB,GPIO_Pin_5);
i=1;
}
else if(i==1)
{
GPIO_ResetBits(GPIOB,GPIO_Pin_5);
i=0;
}
}
TIM5及输入引脚的初始化函数:
void init_input(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStruct;
TIM_ICInitTypeDef TIM_ICInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5,ENABLE);
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0;
GPIO_Init(GPIOA,&GPIO_InitStructure)
NVIC_InitStructure.NVIC_IRQChannel=TIM5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_ClearITPendingBit(TIM5,TIM_IT_Update);
TIM_TimeBaseStruct.TIM_Period=0xFFFF;
TIM_TimeBaseStruct.TIM_Prescaler=29;
TIM_TimeBaseStruct.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseStruct.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM5,&TIM_TimeBaseStruct);
TIM_ICInitStructure.TIM_Channel=TIM_Channel_1
TIM_ICInitStructure.TIM_ICFilter=0
TIM_ICInitStructure.TIM_ICPolarity=TIM_ICPolarity_Rising
TIM_ICInitStructure.TIM_ICPrescaler=TIM_ICPSC_DIV1
TIM_ICInitStructure.TIM_ICSelection=TIM_ICSelection_DirectTI;
TIM_ICInit(TIM5,&TIM_ICInitStructure);
TIM_ITConfig(TIM5,TIM_IT_Update|TIM_IT_CC1,ENABLE);
TIM_Cmd(TIM5,ENABLE);
printf("input ");
}
TIM5的中断函数
void TIM5_IRQHandler(void)
{
static u8 flag;
static u16 channel1_up,channel1_fall;
if(TIM_GetITStatus(TIM5,TIM_IT_Update)==SET)
{
TIM_SetCounter(TIM5,0);
printf("update ");
TIM_ClearITPendingBit(TIM5,TIM_IT_Update);
}
else if(TIM_GetITStatus(TIM5,TIM_IT_CC1)==SET)
{
if(flag==0)
{
channel1_up=TIM_GetCapture1(TIM5);
TIM_OC1PolarityConfig(TIM5,TIM_ICPolarity_Falling);
flag++;
printf("channel1_up %d ",channel1_up);
}
else if(flag==1)
{
channel1_fall=TIM_GetCapture1(TIM5);
TIM_OC1PolarityConfig(TIM5,TIM_ICPolarity_Rising);
input_tim_count[capture_i]=channel1_fall-channel1_up;
capture_i++;
flag--;
printf("channel1_fall %d ",channel1_fall);
}
TIM_ClearITPendingBit(TIM5,TIM_IT_CC1);
}
}
通过串口输出的时候一直就是update,我想不明白为什么会溢出
一周热门 更多>