KL24 Input Capture for Duty Cycle

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

KL24 Input Capture for Duty Cycle

Jump to solution
624 Views
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.

Tags (2)
0 Kudos
1 Solution
396 Views
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.

View solution in original post

0 Kudos
2 Replies
397 Views
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 Kudos
396 Views
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 Kudos