求助STM32F207 TIM4输入捕获进不了中断

2019-07-21 04:43发布

用STM32F207 P8口作为输入捕获,一直进不了中断。把TIM_INT()换成TIM_PWMICONFIG()能进中断,但是读的数全是0.

#include "pwm.h"
#include "stm32f2xx_tim.h"
#include "stm32f2xx_rcc.h"

//PB8, TIM4 CH3,TIM10 CH1
//PB9, TIM4 CH4, TIM11 CH1
//PB10,TIM2 CH3
u32 up = 0;
u32 down = 0;
int status = 0;

void PWM_Int(void)
{
   GPIO_InitTypeDef GPIO_InitStruct;
   TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
   TIM_ICInitTypeDef TIM_ICInitStruct;
   NVIC_InitTypeDef NVIC_InitStruct;

// 1.打开时钟
   RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
   RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);//开不开这个好像没有用。
   RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);

// 2.GPIO初始化

   GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
   GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
   GPIO_InitStruct.GPIO_Pin = GPIO_Pin_8;
   GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
   GPIO_InitStruct.GPIO_Speed = GPIO_Speed_100MHz;
   GPIO_Init(GPIOB, &GPIO_InitStruct);

// 3.引脚复用
   GPIO_PinAFConfig(GPIOB, GPIO_PinSource8, GPIO_AF_TIM4);

   
// 4.采样周期配置
   TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;
   TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;
   TIM_TimeBaseInitStruct.TIM_Period = 20;
   TIM_TimeBaseInitStruct.TIM_Prescaler = 0x3fff;

   TIM_TimeBaseInit(TIM4, &TIM_TimeBaseInitStruct);


   
// 5.TIM配置
   TIM_ICInitStruct.TIM_Channel = TIM_Channel_3;
   TIM_ICInitStruct.TIM_ICFilter = 4;
   TIM_ICInitStruct.TIM_ICPolarity = TIM_ICPolarity_Rising;
   TIM_ICInitStruct.TIM_ICPrescaler = TIM_ICPSC_DIV1;
   TIM_ICInitStruct.TIM_ICSelection = TIM_ICSelection_DirectTI;
   TIM_ICInit(TIM4,&TIM_ICInitStruct);//这里换成TIM_PWMICONFIG能进中断
   

// 6.中断使能

   TIM_ITConfig(TIM4, TIM_IT_CC3, ENABLE);
  
// 7.中断优先级设置
   NVIC_InitStruct.NVIC_IRQChannel = TIM4_IRQn;
   NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
   NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 1;
   NVIC_InitStruct.NVIC_IRQChannelSubPriority = 2;

   NVIC_Init(&NVIC_InitStruct);

// 8.使能TIM4
   TIM_Cmd(TIM4, ENABLE);

}


void TIM4_IRQHandler(void)
{

if (TIM_GetITStatus(TIM4, TIM_IT_CC3) != RESET)
  {
   if(status == 0)
    {
     down = TIM_GetCapture3(TIM4);  //使用TIM_PWMICONFIG能进中断,但DEBUG,这个数一直是0.
     TIM_SetCounter(TIM4,0);
     status = 1;
     TIM_OC3PolarityConfig(TIM4, TIM_OCPolarity_Low);
    }
   if(status == 1)
    {
     up = TIM_GetCapture3(TIM4);  
     TIM_SetCounter(TIM4,0);
     status = 0;
     TIM_OC3PolarityConfig(TIM4, TIM_OCPolarity_High);
    }
  }
  

  TIM_ClearITPendingBit(TIM4, TIM_IT_CC3); //???????
}


友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
5条回答
隋树
2019-07-21 12:14
上面的问题被我解决了(虽然现在还很模糊怎么解决的),又出现新的问题了,用串口把读到的数打印出来。全是0,我用串口打印ox55,是没问题的。


#include "stm32f2xx_gpio.h"
#include "stm32f2xx_usart.h"
#include "usart.h"
#include "stm32f2xx.h"
#include "stm32f2xx_it.h"
#include "pwm.h"

extern u32 up;
extern u32 down;

int main()
{


                u16 uph, upl, downh, downl;
                NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
                USART207_Int();
                PWM_Int();
                while(1)
                {
                        static u16 i,j;
                                uph =(up>>8) & 0x00ff;
                                upl =up & 0x00ff;
                                downh = (down >>8) & 0x00ff;
                                downl = down & 0x00ff;
                                USART_SendData(UART5, uph);
                                while(USART_GetFlagStatus(UART5,USART_FLAG_TC )==RESET);       
                                USART_SendData(UART5, upl);
                                while(USART_GetFlagStatus(UART5,USART_FLAG_TC )==RESET);       
                                USART_SendData(UART5, downh);
                                while(USART_GetFlagStatus(UART5,USART_FLAG_TC )==RESET);       
                                USART_SendData(UART5, downl);               


   for(i=0;i<5000;i++)  
                          for(j=0;j<240;j++);
        for(i=0;i<5000;i++)  
                          for(j=0;j<240;j++);
                }
       
}       

一周热门 更多>