AnsweredAssumed Answered

calcul frequency of PWM signal

Question asked by karmous.neder on Oct 15, 2015
Latest reply on Oct 16, 2015 by karmous.neder
hi  all  membres  i try  to  do  a  code  which  calculate  the frequency of PWM signal  i  try  with  this  code  but i cant get  the right frequency
note : i  use stm32f429ZIT dicovery
/* stm32f429ZIT discovery */
volatile uint16_t Reading[2]; // Readings from PB4 TIM3_CH1 (1us units)
  
void TIM3_IRQHandler(void)
{ char fh[100];
char dh[100];
   LCD_Clear(LCD_COLOR_BLACK);
  if (TIM_GetITStatus(TIM3, TIM_IT_CC1) != RESET)
  {
    TIM_ClearITPendingBit(TIM3, TIM_IT_CC1);
  
    Reading[0] = TIM_GetCapture1(TIM3); // Period (us) ~20000
  Reading[0] = 1/  Reading[0] ;  // get  the  frequency  (1/period)
     
    Reading[1] = TIM_GetCapture2(TIM3); // Duty/Width (us) ~1500 or whatever (600-2400 us)
     
                            sprintf(fh,"F=%lf ", (double)Reading[0]);
                            LCD_SetTextColor(LCD_COLOR_GREEN); // affiche the result  of freauency on the lcd of stm32f429 discovery //
                            LCD_SetBackColor(LCD_COLOR_BLUE);
                            LCD_DisplayStringLine(LINE(5),fh);
                             sprintf(dh,"d=%lf ", (double)Reading[1] ); // affiche the result  of duty cycle  on the lcd of stm32f429 discovery //
                            LCD_SetTextColor(LCD_COLOR_GREEN);
                            LCD_SetBackColor(LCD_COLOR_BLUE);
                            LCD_DisplayStringLine(LINE(6),dh);
  }
}
  
/**************************************************************************************/
  
void TIM3_Configuration(void)
{
  TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  TIM_ICInitTypeDef TIM_ICInitStructure;
  GPIO_InitTypeDef GPIO_InitStructure;
  NVIC_InitTypeDef NVIC_InitStructure;
  
    /* TIM3 clock enable */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
  
  /* GPIO clock enable */
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
  
    /* Configure GPIO input for timer */
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; // PB4 TIM3_CH1
  GPIO_Init(GPIOA, &GPIO_InitStructure);
  
  /* Connect TIM3 pins to AF */
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_TIM3);
  
  /* Enable the TIM3 global Interrupt */
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
  NVIC_Init(&NVIC_InitStructure);
  
  /* Time base configuration - SystemCoreClock = 168000000 for 168 MHz board ABP1 @ 42 MHz (DIV4) */
  TIM_TimeBaseStructure.TIM_Prescaler = (uint16_t) (((SystemCoreClock / 1000000) / 2) - 1); // Shooting for 1 MHz, (1us)
  TIM_TimeBaseStructure.TIM_Period = 0xFFFF; // Maximal
  TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
  
  /* Configure PWM Input Capture */
  TIM_ICInitStructure.TIM_Channel = TIM_Channel_1; // PB4 TIM3_CH1
  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);
  
  /* Select the TIM3 Input Trigger: TI1FP1 */
  TIM_SelectInputTrigger(TIM3, TIM_TS_TI1FP1);
  
  /* Select the slave Mode: Reset Mode */
  TIM_SelectSlaveMode(TIM3, TIM_SlaveMode_Reset);
  TIM_SelectMasterSlaveMode(TIM3, TIM_MasterSlaveMode_Enable);
  
  /* TIM Interrupts enable */
  TIM_ITConfig(TIM3, TIM_IT_CC1, ENABLE);
  
  /* TIM3 enable counter */
  TIM_Cmd(TIM3, ENABLE);
}

Outcomes