KL24 Input Capture for Duty Cycle

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

KL24 Input Capture for Duty Cycle

跳至解决方案
1,646 次查看
lucianomoretti
Contributor IV


Having issues setting up Input Capture to measure a Duty cycle. The pulse I'm trying to capture is ~2uS in length, approximately 28uS between pulses.  I've got the TimeUnit_LDD set to a 48mhz counter frequency, Channel 0 capture on both edges with Interrupts enabled with a high priority.

My OnChannel0 ISR routine stores 10 samples and then would calculate  the duty cycle from those numbers.

void TU2_OnChannel0(LDD_TUserData *UserDataPtr)

{

  static uint32_t captureValues[10] = {0};

  static int captureIndex = 0;

  TU2_GetCaptureValue(TU2_DeviceData, 0, &captureValues[captureIndex]);

  captureIndex++;

  if(captureIndex == 10){

    // TODO: Calculate duty cycle here

    Duty_Cycle = 0;   

    // Reset the index to start a new measure.

    captureIndex = 0;

    TU2_ResetCounter(TU2_DeviceData);

  }

}

If I breakpoint in the if(captureIndex ==10) what I end up seeing is my captureValues array being populated with pairs of values like the following:

[991, 991, 2358, 2358, 3725, 3725, 5092, 5092, 6460, 6460]

The time between the even slotted numbers seems to be equivalent to the 28uS period between rising edges I expect. The odd slots are supposed to correspond to the falling edge 2uS later, but I end up with the same counter value as the preceding even slot.  The interrupt appears to be happening twice, as my indexes increment.

标记 (2)
0 项奖励
回复
1 解答
1,418 次查看
lucianomoretti
Contributor IV

Figured it out.

I  added a GPIO pin toggle to my ISR and then scoped both signals.  Interrupt latency is too high.  By the time I get to reading the capture register the trailing edge of my pulse has already happened and set it to the time of the END of the pulse.  So I'm reading the end twice in a row.

I'm going to have to disable interrupts and poll the pin manually.

在原帖中查看解决方案

0 项奖励
回复
2 回复数
1,419 次查看
lucianomoretti
Contributor IV

Figured it out.

I  added a GPIO pin toggle to my ISR and then scoped both signals.  Interrupt latency is too high.  By the time I get to reading the capture register the trailing edge of my pulse has already happened and set it to the time of the END of the pulse.  So I'm reading the end twice in a row.

I'm going to have to disable interrupts and poll the pin manually.

0 项奖励
回复
1,418 次查看
egoodii
Senior Contributor III

The 'overhead' of PE-generic-code may be hurting you as well (TU2_GetCaptureValue, plus the entry to YOUR ISR from their handler).  Compiler optimization is also something to keep in mind.

Your other option is to run the signal to two timer channels, configured for opposing edges.  The 'bigger' Kinetis members have timer-modules with the built-in capability to link two consecutive timer-channels to ONE input for just such use.

0 项奖励
回复