AnsweredAssumed Answered

Error in frequency measurement using capture inputs of LPC11U68

Question asked by Akhilesh Sreedharan on Jun 5, 2018
Latest reply on Jun 10, 2018 by jeremyzhou



I am using 16-bit Timer of LPC11U68 and its associated capture inputs to measuring frequency of a source (function generator generating a square wave of 50Hz and 50% duty cycle. The source frequency is 50Hz, however LPC11U68 randomly detects a wrong frequency after some random no. of cycles. 


The hardware setup consists of a OM13058: LPCXpresso Board for LPC11U68 connected to a function generator configured to generate a square wave of 50Hz going from 0V to +3.3V. The pins PIO1_18, PIO1_7, PIO0_2  are shorted together and connected to the output of function generator. The IDE used is LPCXpresso v8.2.2_650


My idea for frequency measurement is to measure time difference between rising and falling edges every half cycle and to use the previous half cycle time to compute frequency using the formula:


Frequency = 1/(current_half_cycle_time + previous_half_cycle_time)


To compute this following steps are taken in the algorithm:


  1. A match is configured for the timer to detect the 16-bit timer overflow, which then increments an overflow count stored in the variable ui32_overflow in the Timer's ISR. 
  2. Capture inputs on pin PIO1_18, PIO1_7, PIO0_2  are configured on both rising and falling edges.
    1. When the capture event occurs, the value loaded in CRx register is stored in the variable ui32_current_capture_count. 
    2. Also, the number of overflows stored in the ui32_overflow variable is loaded in the ui32_save_overflow. 
    3. A flag  ui32_new_cap_event is then set to indicate the code executing in the main that data from the new cycle is ready for computation. 
  3. Once the flag is ui32_new_cap_event is detected by the main, Compute_Frequency function is called and the frequency is computed as follows:
    1. If ui32_save_overflow is equal to zero, the half cycle time
      ui32_current_half_cycle_time = ui32_current_capture_count - ui32_previous_capture_count
    2. If ui32_save_overflow is not equal to zero then the half cycle time
      ui32_current_half_cycle_time = (ui32_save_overflow * 65535) + ui32_current_capture_count - ui32_previous_capture_count
    3.    Once the half cycle time is computed, the frequency is calculated as:
      ui32_frequency = 1000*(ui32_timerFreq/(ui32_current_half_cycle_time+ui32_previous_half_cycle_time))
      where ui32_timerFreq = SystemClockFrequency/(TIMER_PRESCALE_VALUE+1)  Note that 1000 is added to recover three decimal places of the frequency. 
    4. Once the frequency is computed, the previous variables are updated with the current variable values for next cycle computation: 
      ui32_previous_half_cycle_time = ui32_current_half_cycle_time            ui32_previous_capture_count = ui32_current_capture_count
      Also, the ui32_new_cap_event flag is set to 0.


The attached project shows that CT16B1_CAP0 on pin PIO1_7,   CT16B1_CAP1 on pin PIO1_18 and CT16B0_CAP0 on pin PIO0_2 are configured since three frequencies are to be measured. 


Any help regarding this would be highly appreciated. 



Akhilesh Sreedharan