问答

汇集网友智慧,解决技术难题

21ic问答首页 - TIM3捕获配置时,只要一NVIC_Init使能中断就卡死,咨询下大家

ni TIM3 Tru structure ST NVIC

TIM3捕获配置时,只要一NVIC_Init使能中断就卡死,咨询下大家

AProgrammer2023-12-01
STM32F103ZET6板子,想用TIM3_CH2来捕获TIM8 CH3输出的pwm,为此将pwm TIM8_CH3输出的管脚PC8 和 TIM3_CH2管脚PA7 连起来后,发现程序执行不下去了(不连起来就能执行)。卡死了好像。请帮看下可能原因。

做过的尝试:
1、自己把TIM3 CH2的优先级改成最高(抢占优先级为0,响应优先级也为0),也不行。
2、PC8 和 TIM3_CH2管脚PA7 连线去除,发现程序能执行下去
3、中断不使能,就是不执行Nvic_init也可以执行下去,说明肯定是和中断相关。

TIM3_CH2初始化的代码:

void InitFan3InletFG(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    TIM_ICInitTypeDef TIM_ICInitStructure;
    TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
    NVIC_InitTypeDef NVIC_InitStructure;

    //初始化GPIO复用
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    //初始化定时器
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);

    TIM_TimeBaseStructure.TIM_Period = 0xFFFF-1;
    TIM_TimeBaseStructure.TIM_Prescaler = 72-1; //1us,总时间65ms
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);

    /* IC2,上升沿触发 TI2FP2 */
    TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
    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 = 0x0;
    TIM_PWMIConfig(TIM3, &TIM_ICInitStructure);

#if 0
    /* IC2, 下降沿触发,TI2FP1*/
    TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;
    TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling;
    TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_IndirectTI;
    TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
    TIM_ICInitStructure.TIM_ICFilter = 0x0;
    TIM_PWMIConfig(TIM5, &TIM_ICInitStructure);
#endif

    TIM_SelectInputTrigger(TIM3, TIM_TS_TI2FP2);
    TIM_SelectSlaveMode(TIM3, TIM_SlaveMode_Reset);
    TIM_SelectMasterSlaveMode(TIM3,TIM_MasterSlaveMode_Enable);

    TIM_Cmd(TIM3, ENABLE);

    //使能CC2捕获中断,主
////    TIM_ClearITPendingBit(TIM3, TIM_IT_CC2); //20231130 1101: try add this
    TIM_ITConfig(TIM3, TIM_IT_CC2, ENABLE);

    //使能中断
    NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //3; //0; //1; // 3;  //20231130pm5 try modified to 1
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
///////    NVIC_Init(&NVIC_InitStructure); ///////1130pm15: disable try
    NVIC_Init(&NVIC_InitStructure);
}
中断处理代码:

void TIM3_IRQHandler(void)
{
    Timer3ISR();
}

void Timer3ISR(void)
{
    int ret;

    g_tim3Count++;

    if (g_tim3Count % 100 == 0) {

        //printf("timer3 got\r\n");
    }

    TIM_ClearITPendingBit(TIM3, TIM_IT_CC2);
    TIM_ClearITPendingBit(TIM3, TIM_IT_CC1);
    TIM_ClearITPendingBit(TIM3, TIM_IT_CC3);
    TIM_ClearITPendingBit(TIM3, TIM_IT_CC4);

    //TIM3, CH2通道
    ret = TIM_GetFlagStatus(TIM3, TIM_FLAG_CC2);
    if (ret)
    {
        TIM_ClearITPendingBit(TIM3, TIM_IT_CC2);
        Tim3Ch2Isr();
    }

    //TIM3, CH1通道
    ret = TIM_GetFlagStatus(TIM3, TIM_FLAG_CC1);
    if (ret)
    {
        TIM_ClearITPendingBit(TIM3, TIM_IT_CC1);
        Tim3Ch1Isr();
    }

    //TIM3, CH3通道
    ret = TIM_GetFlagStatus(TIM3, TIM_FLAG_CC3);
    if (ret)
    {
        TIM_ClearITPendingBit(TIM3, TIM_IT_CC3);
        Tim3Ch3Isr();
    }

    //TIM3, CH4通道
    ret = TIM_GetFlagStatus(TIM3, TIM_FLAG_CC4);
    if (ret)
    {
        TIM_ClearITPendingBit(TIM3, TIM_IT_CC4);
        Tim3Ch4Isr();
    }
}

回答 +关注 15
1476人浏览 1人回答问题 分享 举报
1 个回答

您需要登录后才可以回复 登录 | 注册