程序是参考网上的一些程序,但是程序程序停留在中断里面了—— if(TIM3CH3_CAPTURE_STA&0X40)//已经捕获到高电平了
void TIM3_Cap_Init(u16 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM3_ICInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE); //开启TIMER3的时钟
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
GPIO_PinAFConfig(GPIOC,GPIO_PinSource2, GPIO_AF_TIM3);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_ResetBits(GPIOB,GPIO_Pin_8);
TIM_TimeBaseStructure.TIM_Period = arr;
TIM_TimeBaseStructure.TIM_Prescaler = psc;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure);
TIM3_ICInitStructure.TIM_Channel = TIM_Channel_3; //选择通道1
TIM3_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //有效捕获极性为上升沿
TIM3_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM3_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM3_ICInitStructure.TIM_ICFilter = 0;
TIM_ICInit(TIM3,&TIM3_ICInitStructure);
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_ITConfig(TIM3,TIM_IT_Update|TIM_IT_CC3,ENABLE);
TIM_Cmd(TIM3,ENABLE);
}
uint8_t TIM3CH3_CAPTURE_STA = 0; // 输入捕获状态
uint16_t TIM3CH3_CAPTURE_VAL; // 输入捕获值
void TIM3_IRQHandler(void)
{
if((TIM3CH3_CAPTURE_STA&0X80) == 0) //还未成功捕获
{
if(TIM_GetITStatus(TIM3,TIM_IT_Update) != RESET)
{
if(TIM3CH3_CAPTURE_STA&0X40)//已经捕获到高电平了
{
if((TIM3CH3_CAPTURE_STA&0X3F)==0X3F)//高电平太长了
{
TIM3CH3_CAPTURE_STA|=0X80;//标记成功捕获了一次
TIM3CH3_CAPTURE_VAL = 0XFFFF;
}
else
TIM3CH3_CAPTURE_STA++;
}
}
if(TIM_GetITStatus(TIM3,TIM_IT_CC3) !=RESET)
{
if(TIM3CH3_CAPTURE_STA & 0x40)
{
TIM3CH3_CAPTURE_STA |= 0X80; //标记成功捕获到一次
TIM3CH3_CAPTURE_VAL = TIM_GetCounter(TIM3);
TIM_OC3PolarityConfig(TIM3,TIM_ICPolarity_Rising); //CC3P = 0sz设置为上升沿捕获
}
else
{
TIM3CH3_CAPTURE_STA = 0; //标记成功捕获到一次
TIM3CH3_CAPTURE_VAL = 0;
TIM_SetCounter(TIM3,0);
TIM3CH3_CAPTURE_STA|=0X40;
TIM_OC3PolarityConfig(TIM3,TIM_ICPolarity_Falling); //CC1P=1 设置为下降沿捕获
}
}
}
TIM_ClearITPendingBit(TIM3, TIM_IT_CC3|TIM_IT_Update);
}
void TIM3_Cap_run(void)
{
uint32_t temp = 0;
// delay_10ms();
if(TIM3CH3_CAPTURE_STA & 0X80)//成功捕获了一次上升沿
{
temp = TIM3CH3_CAPTURE_STA & 0x3F;
temp *= 65536;
temp += TIM3CH3_CAPTURE_VAL; // 得到总的高电平时间
printf("HIGH:%d us\r\n",temp); // 打印总的高电平时间
TIM3CH3_CAPTURE_STA = 0; // 开启下一次捕获
}
}
请问各位大神,这程序是哪儿有问题,芯片是STM32F407ZG. |