本帖最后由 〽️〽️〽️ 于 2020-12-3 18:03 编辑
背景:
STM32F031F6P6, TSSOP20,采用的是内置晶振8MHz 倍频到48MHz
PA0口捕获脉冲信号对应通道是 TIM2-CH1,
搞了20天了,实在搞不出了,来问问看
问题就是 if(TIM_GetITStatus(TIM2, TIM_IT_CC1) != RESET) 一直进不去
-------------------------------------------------------------------------------------------------------
按31楼 指导方式:
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; 或
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
加了 GPIO_PinAFConfig(GPIOA,GPIO_Pin_0,GPIO_AF_2);
但还是不能进入 if(TIM_GetITStatus(TIM2, TIM_IT_CC1) != RESET)
-------------------------------------------------------------------------------------------------------
void TIM2_IRQHandler(void)
{
if ( TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET )
{
//可以正常1ms进入
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
}
if(TIM_GetITStatus(TIM2, TIM_IT_CC1) != RESET)
{
//解码的关键地方
//可从来没有进入过!!!!!! 备注:这里PA0输入信号高低电平是正常的
TIM_ClearITPendingBit(TIM2, TIM_IT_CC1);
}
}
void TIM2_Cap_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
TIM_DeInit(TIM2);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure);
TIM_TimeBaseStructure.TIM_Period =48-1;
TIM_TimeBaseStructure.TIM_Prescaler =1000-1;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
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(TIM2, &TIM_ICInitStructure);
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_ITConfig(TIM2,TIM_IT_Update|TIM_IT_CC1,ENABLE);
TIM_Cmd(TIM2,ENABLE );
}
int main(void)
{
SetSysClock();
delay_init(48);
//略...
TIM2_Cap_Init();
while(1)
{
while ( GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0)!= RESET); //测试用 无意义,证明PA0有电平变化,不会一直高电平
while ( GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0)!= SET); //测试用 无意义,证明PA0有电平变化,不会一直低电平
//其他 无关联函数 测试时 也是注销掉了,防止其他导致的错误 略...
}
} |