This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

TIM1 CH1 PWMInput

Dear sir or madam,

I have an STM32VLDiscovery which chip is the STM32F100RB, 24MHz.

I want to use TIM1CH1(PA8) to do PWMInput and calculate period & duty cycle. However, the problem is I can't catch the data through TIM1CH1 but TIM3CH2(PA7) is worked. I checked all of the code of settings between TIM1 and TIM3, and found nothing wrong.

The fault result from TIM1CH1 is the data is randomly. I mean different data numbers return when I DAQ in 100ms for example. My pulse to be measured is about 160ms max of period. My TIM_Prescaler is set to 2400-1 and TIM_Period is set to 1600 for both the TIM1 and TIM3.

Please help me. I'll appreciate if you can give me an example of TIM1CH1 PWMInput. Thank you.

Or anyone kindly could help me directly by my email: blueeyesblue78@gmail.com.

BS

Chuntai Shen

PS: my code of TIM1CH1 for PWMInput mode

uint16_t TIM_Prescaler = 2400 - 1;
uint16_t TIM_Period = 1600;

/* TIM1 clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
/* GPIOA clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
/* TIM1 channel 1 pin (PA.08) configuration */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Enable the TIM1 global Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = TIM1_CC_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* TIM1 configuration: PWM Input mode ------------------------
The external signal is connected to TIM1 CH1 pin (PA.08),
The Rising edge is used as active edge,
The TIM1 CCR2 is used to compute the frequency value
The TIM1 CCR1 is used to compute the duty cycle value
------------------------------------------------------------ */
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 = 0x0;

TIM_TimeBaseStructure.TIM_Period = TIM_Period;
TIM_TimeBaseStructure.TIM_Prescaler = TIM_Prescaler;
TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);

TIM_PWMIConfig(TIM1, &TIM_ICInitStructure);
/* Select the TIM1 Input Trigger: TI2FP2 */
TIM_SelectInputTrigger(TIM1, TIM_TS_TI2FP2);
/* Select the slave Mode: Reset Mode */
TIM_SelectSlaveMode(TIM1, TIM_SlaveMode_Reset);
/* Enable the Master/Slave Mode */
TIM_SelectMasterSlaveMode(TIM1, TIM_MasterSlaveMode_Enable);
/* TIM1 enable counter */
TIM_Cmd(TIM1, ENABLE);
/* Enable the CC2 Interrupt Request */
TIM_ITConfig(TIM1, TIM_IT_CC2, ENABLE);

The interrupt code:
The IC1Value and IC2Value are randomly returned comparatively to the ones from TIM3CH2 are stable. Because my PWM input is stable.

void TIM1_CC_IRQHandler(void)
{
  /* Clear TIM1 Capture compare interrupt pending bit */
  TIM_ClearITPendingBit(TIM1, TIM_IT_CC2);

  /* Get the Input Capture value */
  IC2Value = TIM_GetCapture2(TIM1);

  if (IC2Value != 0)
  {
    IC1Value = TIM_GetCapture1(TIM1);
    /* Duty cycle computation */
    DutyCycle = (TIM_GetCapture1(TIM1) * 100) / IC2Value;

    /* Frequency computation */
    Frequency = SystemCoreClock / IC2Value;
  }
  else
  {
    DutyCycle = 0;
    Frequency = 0;
  }
}

0