cancel
Showing results for 
Search instead for 
Did you mean: 

calcul frequency of PWM signal

nederkarmous
Associate II
Posted on October 15, 2015 at 17:56

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);
}

#stm32
4 REPLIES 4
Posted on October 15, 2015 at 18:39

Ok, you need to get the LCD stuff out of the interrupt. It's going to be far too slow, and mess with the latency. Have it take the measurements, and flag them to a foreground loop.

Don't do the reciprocal as an integer, how is dividing into 1 going to get any useful result other than 1 or 0 ?? Think about the math for a second.

It's also not measuring seconds, it's measuring in microseconds. So 1000000 of those in a second, 20000 of them in 20 ms (50 Hz)

1000000 / 20000 = 50

double freq = (1000000.0 / (double)Reading[0]);

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
nederkarmous
Associate II
Posted on October 15, 2015 at 19:03

tks  clive  i get  50 hz  frequency  but  i need  to  get the  value of  any  frequency of pwm signal which  comes  from oscilloscope. that  means  i want  to  put  a  pwm signal  on  the  pin PA6  and  it  calculate  the  frequency  of  the  signal  and  affiche  it  on  the  lcd  of  stm32f429zit discovery  CAN  U  HELP  ME  PLZ 

Posted on October 15, 2015 at 21:27

You're not texting, you can use whole words.

Assuming your LCD code works

volatile uint16_t Reading[2]; // Readings from PB4 TIM3_CH1 (1us units)
void TIM3_IRQHandler(void)
{
if (TIM_GetITStatus(TIM3, TIM_IT_CC1) != RESET)
{
TIM_ClearITPendingBit(TIM3, TIM_IT_CC1);
Reading[0] = TIM_GetCapture1(TIM3); // Period (us) ~20000
Reading[1] = TIM_GetCapture2(TIM3); // Duty/Width (us) ~1500 or whatever (600-2400 us)
}
}
{
char fh[100];
char dh[100];
LCD_Clear(LCD_COLOR_BLACK);
while(1)
{
if (Reading[0] != 0)
{
double freq = (1000000.0 / (double)Reading[0]);
double duty = ((0 * (double)Reading[1]) / (double)Reading[0]);
Reading[0] = 0;
sprintf(fh,''F=%lf '', freq);
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=%.1lf %%'', duty ); // 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);
}
}
}

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
nederkarmous
Associate II
Posted on October 16, 2015 at 00:02

thank  you  clive tomorow i will  test  the  result