首页>>论坛 >>技术社区 >>STM32社区ST MCU技术论坛 >>STM32
|
我要发帖  | 我要投票  | 我要回复  | 收藏
1

stm32 PWM捕捉中断问题

stm32 PWM捕捉中断问题
 

我用Timer3做pwm输出,然后Timer2捕捉这个pulse输出,当pulse输出占空比大于50%时可以正常进入中断,但小于时进不能进入中断,也就是红色 CCR3_Val =18;改为 CCR3_Val =36时,就不能进入中断(注输出是经过三极管反向了)

void IRQ_Tim2(void)
{
 volatile unsigned short IC2Value = 0;
 volatile unsigned short IC1Value = 0;
 if(TIM_GetITStatus(TIM2, TIM_IT_CC2) == SET)
   {
   TIM_ClearITPendingBit(TIM2, TIM_IT_CC2);
 
   IC2Value = TIM_GetCapture2(TIM2);
   IC1Value = TIM_GetCapture1(TIM2);   
   if(start==1)
   {
   if(count<SUM_2)
   {
     raw_data0[group][count]=IC1Value;
     raw_data1[group][count++]=IC2Value;     
     
    } 
   } 
 }
}

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

 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
 /* TIM2 channel 2 pin (PA.01) configuration */
 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
 GPIO_Init(GPIOA, &GPIO_InitStructure);  
 
 RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
 
  NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);  
  
 TIM_DeInit(TIM2);
 TIM_TimeBaseStructure.TIM_Period = 65535;         
 TIM_TimeBaseStructure.TIM_Prescaler = 0;  
 TIM_TimeBaseStructure.TIM_ClockDivision = 0;   
 TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
 TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); 
 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(TIM2, &TIM_ICInitStructure); 
 /* Select the TIM2 Input Trigger: TI2FP2 */
 TIM_SelectInputTrigger(TIM2, TIM_TS_TI2FP2); 
 /* Select the slave Mode: Reset Mode */
 TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_Reset);
 
 /* Enable the Master/Slave Mode */
 TIM_SelectMasterSlaveMode(TIM2, TIM_MasterSlaveMode_Enable);
  TIM_Cmd(TIM2, ENABLE);

 TIM_ITConfig(TIM2, TIM_IT_CC2, ENABLE);
}

void Time3r_Configuration(void)
{

   TIM_OCInitTypeDef  TIM_OCInitStructure;
   TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
   GPIO_InitTypeDef GPIO_InitStructure;
    u16 CCR3_Val =18;  

   RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE); 
   RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO|RCC_APB2Periph_GPIOB,ENABLE);  
    
   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
   GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_0; //| GPIO_Pin_1;
   GPIO_Init(GPIOB, &GPIO_InitStructure);

   TIM_TimeBaseStructure.TIM_Period = 71;
   TIM_TimeBaseStructure.TIM_Prescaler =0;
   TIM_TimeBaseStructure.TIM_ClockDivision = 0;
   TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

   TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);

   /* PWM1 Mode configuration: Channel1 */
   TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
   TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
   TIM_OCInitStructure.TIM_Pulse = CCR3_Val;
   TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;

   TIM_OC3Init(TIM3, &TIM_OCInitStructure);
  
   TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable);
   TIM_ARRPreloadConfig(TIM3, ENABLE);

   /* TIM3 enable counter */
   TIM_Cmd(TIM3, ENABLE);
}

 
相关主题
回复 链接 收藏
 
我要发帖  | 我要投票  | 我要回复  | 收藏
1