FlexTimer - measuring pulse width

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 
已解决

FlexTimer - measuring pulse width

跳至解决方案
1,348 次查看
bluetiger9
Contributor II

I am trying to measure the pulse width of a PWM signal on a FRDM-K82F board. I am using the FTM0 FlexTimer in dual edge capture mode on channels 6-7. I'm getting the interrupts both for the rising and falling edge of the pulses. My understanding was that pulse width can be calculated  by subtracting the counter values of the two channels. The problem is that as I see these values are always 0.

#include <stdio.h>

#include "main.h"

void FTM0_IRQHandler(void) {

  int status = FTM_GetStatusFlags(FTM0);

  if (status & kFTM_Chnl6Flag) {

  PRINTF("channel6\r\n");

  PRINTF(" cnt %d \r\n", FTM0->CNT);

  PRINTF(" sc %d \r\n", FTM0->CONTROLS[6].CnSC);

  PRINTF(" value 0 %d \r\n", FTM0->CONTROLS[0].CnV);

  PRINTF(" value 1 %d \r\n", FTM0->CONTROLS[1].CnV);

  PRINTF(" value 2 %d \r\n", FTM0->CONTROLS[2].CnV);

  PRINTF(" value 3 %d \r\n", FTM0->CONTROLS[3].CnV);

  PRINTF(" value 4 %d \r\n", FTM0->CONTROLS[4].CnV);

  PRINTF(" value 5 %d \r\n", FTM0->CONTROLS[5].CnV);

  PRINTF(" value 6 %d \r\n", FTM0->CONTROLS[6].CnV);

  PRINTF(" value 7 %d \r\n\r\n\r\n", FTM0->CONTROLS[7].CnV);

  LED_GREEN_ON();

  }

  if (status & kFTM_Chnl7Flag) {

  PRINTF("channel7\r\n");

  PRINTF(" cnt %d \r\n", FTM0->CNT);

  PRINTF(" sc %d \r\n", FTM0->CONTROLS[7].CnSC);

  PRINTF(" value 0 %d \r\n", FTM0->CONTROLS[0].CnV);

  PRINTF(" value 1 %d \r\n", FTM0->CONTROLS[1].CnV);

  PRINTF(" value 2 %d \r\n", FTM0->CONTROLS[2].CnV);

  PRINTF(" value 3 %d \r\n", FTM0->CONTROLS[3].CnV);

  PRINTF(" value 4 %d \r\n", FTM0->CONTROLS[4].CnV);

  PRINTF(" value 5 %d \r\n", FTM0->CONTROLS[5].CnV);

  PRINTF(" value 6 %d \r\n", FTM0->CONTROLS[6].CnV);

  PRINTF(" value 7 %d \r\n\r\n\r\n", FTM0->CONTROLS[7].CnV);

  PRINTF(" pulse width = %d \r\n\r\n\r\n", FTM0->CONTROLS[7].CnV - FTM0->CONTROLS[6].CnV);

  LED_BLUE_ON();

  }

  if (status & kFTM_TimeOverflowFlag) {

  PRINTF("overflow\r\n");

  LED_RED_ON();

  }

  FTM_ClearStatusFlags(FTM0, status);

}

int main(void) {

  /* Init board hardware. */

  BOARD_InitPins();

  BOARD_BootClockRUN();

  BOARD_InitDebugConsole();

  //EnableInterrupts;

  NVIC_EnableIRQ(FTM0_IRQn);

  PRINTF("Start...");

  /* Add your code here */

  CLOCK_EnableClock(kCLOCK_PortD);

  PORT_SetPinMux(BOARD_LED_GREEN_GPIO_PORT, BOARD_LED_GREEN_GPIO_PIN, kPORT_MuxAsGpio);

  PORT_SetPinMux(BOARD_LED_BLUE_GPIO_PORT, BOARD_LED_BLUE_GPIO_PIN, kPORT_MuxAsGpio);

  PORT_SetPinMux(BOARD_LED_RED_GPIO_PORT, BOARD_LED_RED_GPIO_PIN, kPORT_MuxAsGpio);

  // setup PTA1, PTA2 (J2-18,20) as FTM0_CH6, FTM0_CH7

  CLOCK_EnableClock(kCLOCK_PortA);

  PORT_SetPinMux(PORTA, 1, kPORT_MuxAlt3);

  PORT_SetPinMux(PORTA, 2, kPORT_MuxAlt3);

  ftm_config_t ftmInfo;

  FTM_GetDefaultConfig(&ftmInfo);

  ftmInfo.prescale = kFTM_Prescale_Divide_128;

  FTM_Init(FTM0, &ftmInfo);

  //FTM_SetupPwm(FTM0, ftmParam, 2U, kFTM_EdgeAlignedPwm, 24000U, CLOCK_GetFreq(BUS_CLK));

  ftm_dual_edge_capture_param_t param = { kFTM_Continuous, kFTM_RisingEdge, kFTM_FallingEdge };

  FTM_SetupDualEdgeCapture(FTM0, kFTM_Chnl_3, &param, 0);

  //FTM_SetupInputCapture(FTM0, kFTM_Chnl_6, kFTM_RiseAndFallEdge, 0);

  FTM_EnableInterrupts(FTM0,

  kFTM_Chnl6InterruptEnable | kFTM_Chnl7InterruptEnable | kFTM_Chnl3InterruptEnable | kFTM_Chnl4InterruptEnable |

  kFTM_Chnl0InterruptEnable | kFTM_Chnl1InterruptEnable | kFTM_Chnl2InterruptEnable | kFTM_Chnl5InterruptEnable

  | kFTM_TimeOverflowInterruptEnable

  | kFTM_FaultInterruptEnable | kFTM_ReloadInterruptEnable);

  FTM_StartTimer(FTM0, kFTM_SystemClock);

  //FTM_StartTimer(FTM0, kFTM_ExternalClock);

  LED_GREEN_INIT(LOGIC_LED_OFF);

  LED_RED_INIT(LOGIC_LED_OFF);

  LED_BLUE_INIT(LOGIC_LED_OFF);

  while (true) {

  __asm("nop");bbb

  }

}

I tried without interrupts too, pulling the values in while loop, but I'm getting the same values. The result are the same with single edge capture mode too.

Probably I missed something in the setup part or maybe I'm reading the wrong values from wrong place.

Can somebody take look on the above code to see what goes wrong?

Thanks Smiley Happy

0 项奖励
回复
1 解答
756 次查看
bluetiger9
Contributor II

I think I found the solution. The FTM0->MOD was zero, so the counter counted from 0 to 0. Setting the value to 0xFFFF solves the problem:

FTM0->MOD = 0xFFFF

Thanks!

在原帖中查看解决方案

0 项奖励
回复
2 回复数
756 次查看
jeremyzhou
NXP Employee
NXP Employee

Hi Attila,

Thank you for your interest in NXP Semiconductor products and the opportunity to serve you.

I was wondering if you can upload the demo, then I can replicate the kind of issue on the FRDM-K82F board.

I'm looking forward to your reply.

Hope it helps.
Have a great day,
Ping

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

0 项奖励
回复
757 次查看
bluetiger9
Contributor II

I think I found the solution. The FTM0->MOD was zero, so the counter counted from 0 to 0. Setting the value to 0xFFFF solves the problem:

FTM0->MOD = 0xFFFF

Thanks!

0 项奖励
回复