Error in frequency measurement using capture inputs of LPC11U68

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

Error in frequency measurement using capture inputs of LPC11U68

1,826 Views
akhilesh_sreedh
Contributor III

Hello,

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. 

Thanks,

Akhilesh Sreedharan

0 Kudos
12 Replies

1,390 Views
akhilesh_sreedh
Contributor III

Dear Jeremy,

Just use one capturing pins of CT16B0, and the pin will trigger an interrupt on rising edge, in the interrupt, reset the PC courter when detecting the first rising edge and read the capture value on second rising edge.

Does this mean that both the capture inputs of CT16B0 or CT16B1of LPC11U68 cannot be used simultaneously since we have a requirement of using atleast 3 captures available on 16-bit Timer?

Also, since you are suggesting to make the timer counter zero on every rising edge, I feel it will still not solve the problem of overflow handling. This is because the tick of the timer is approx 20ns and the timer will expire in 20nsx65535 = 1.3ms. So in a half cycle span of 10ms atleast 6-7 overflows will be expected and to handle that a match will have to be configured. 

Thanks 

Akhilesh

0 Kudos

1,390 Views
jeremyzhou
NXP Employee
NXP Employee

Hi Akhilesh Sreedharan,
Of course,  CT16B0 and CT16B1 can work simultaneously, I 'd recommend you to use one, as I suspect that repeated interrupt handlers are not necessary and it will make the code become more complicated.
Yes, I made a mistake, it needs to add overflow handling when detecting the frequency of PWM, such as 50 Hz, etc.
Have a great day,
TIC


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

0 Kudos

1,390 Views
akhilesh_sreedh
Contributor III

Dear Jeremy,

Of course,  CT16B0 and CT16B1 can work simultaneously, I 'd recommend you to use one, as I suspect that repeated interrupt handlers are not necessary and it will make the code become more complicated.

My question was, since I have a requirement of using any 3 capture channels available with 16-bit timer. Can I achieve the correct functionality by using CT16B0_CAP0 capture input available with CT16B0 and capture inputs CT16B1_CAP0, CT16B1_CAP1 avaialble along with CT16B1 simultaneously along with overflow handling? 

Thanks,

Akhilesh Sreedharan

0 Kudos

1,390 Views
jeremyzhou
NXP Employee
NXP Employee

Hi Akhilesh Sreedharan,
As I shared before, one capturing pins of CT16B0 is capable of measuring the frequency in my way, definitely, you can use three pins with the same way simultaneously.
Please giving it a try.
Have a great day,
TIC

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

0 Kudos

1,390 Views
anilkumarraj200
Contributor I

Hi,

I am using the lpc11u6x, Can anybody can share the logic for calculating the gpio pulse width (Hight to low transition) using time32 without ISR.

I want to count the pulse using timer counter when i am in while loop for checking the condition pulse as high to low or low to high.period in msec or any....

Regards

0 Kudos

1,390 Views
jeremyzhou
NXP Employee
NXP Employee

Hi anil kumar,

I've attached a 32-bit timer driver, please refer to it for details.
Have a great day,
TIC

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

0 Kudos

1,391 Views
akhilesh_sreedh
Contributor III

Hi Jeremy, 

The internal pull-up was added to prevent the capture interrupt from getting triggered due to any noise. Intially, I had not enabled the internal pull-up in my code and I was getting garbage  frequency readings even when the inputs square wave was not applied. Enabling the internal pull-up solved that problem. 

Also, regarding the testing process I had added the following to the end of the while (1) in the main:

if(channels[0].ui32_frequency>51000 ||channels[0].ui32_frequency<49000)
 {
 isFreq_Wrong = TRUE;
 }‍‍‍‍

isFreq_Wrong is a boolean variable defined in begining of main. 

To do the testing I had applied the input and after some time (after few cycles had passed) breakpoint was placed on isFreq_Wrong statement in the main. The code was found to encounter the breakpoint, few seconds (approx 30s to 1min) after the breakpoint was placed. The frequency which was read on the watch window did not match the applied value of 50Hz. 

I had repeated this on all the channels and the same problem was observed.

Thanks,
Akhilesh.

0 Kudos

1,391 Views
jeremyzhou
NXP Employee
NXP Employee

Hi Akhilesh Sreedharan,
Thanks for your reply.
I'd like to simplify your code for testing, just use one timer to measure the frequency and another timer is used to generating 50Hz PWM, and I will respond to you later.
Have a great day,
TIC

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

0 Kudos

1,391 Views
jeremyzhou
NXP Employee
NXP Employee

Hi Akhilesh Sreedharan,

Hi Akhilesh Sreedharan,
I've replicated the phenomenon and I'd like to share some suggestion for improvement.
1. Just use one capturing pins of CT16B0, and the pin will trigger an interrupt on rising edge, in the interrupt, reset the PC courter when detecting the first rising edge and read the capture value on second rising edge.
This way will simplify the code with overflow handling and allow much more time to compute the frequency, it also avoids interrupts interfering.
2. Adding a coupling capacitance, get closer to the capture pin would be better, to discard random noise.
Have a great day,
TIC

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

0 Kudos

1,391 Views
jeremyzhou
NXP Employee
NXP Employee

Hi Akhilesh Sreedharan,
Thank you for your interest in NXP Semiconductor products and for the opportunity to serve you.
Firstly, I think the computing algorithm of frequency can work, secondly, I was wondering if you can introduce what kind of the problem happen in details.

Have a great day,
TIC

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

0 Kudos

1,391 Views
akhilesh_sreedh
Contributor III

Dear Jeremy, 

Most of the times the frequency read in ui32_frequency variable is 50000 when 50.000Hz is applied at the input. However, at times it is observed that the some random values of frequencies are read. I have observed values like 53000, 62000, 82000 etc. being read. The error doesn't persist and it gets corrected very soon.

I would like to know what is causing this problem. Is this the result of some wrong configurations in timer or the capture? 

Thanks 

Akhilesh. 

0 Kudos

1,391 Views
jeremyzhou
NXP Employee
NXP Employee

Hi Akhilesh Sreedharan,

Thanks for your reply.

Please giving a try after reomve pull-up setting when configure the capture pin.

pastedImage_1.png

In addition, I was wondering if you can introudce the testing process in details.
Have a great day,
TIC

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

0 Kudos