Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
12 years ago.
Is it possible to count the pulses of a 16MHz signal?
I tried capturing with Timer2 (pin 29: CAP2.0).
This works very well up to 500kHz. (I tested with PwmOut pin - which is limited to 500kHz - as oscillator source connected to the capture pin.)
But, capturing didn't work with an external 16MHz oscillator as event source.
Before I spend much time in further development: is it generally possible to count the pulses of a 16MHz signal on LPC1768 ?
2 Answers
12 years ago.
Hi Erik, thanks a lot for your (fast) reply!
Your library FastPWM worked very well to create a custom square-wave frequency output. I got up to 48 MHz! (Though, the signal accuracy depends strongly on the frequency: for 12 / 24 / 48 MHz the frequency deviation is less than 1%. For others, e.g. 20 MHz, the deviation can be 20%.)
So far, I have a good configurable test signal. ( https://mbed.org/users/geotec/code/PwmOscillator/ )
Now, to the task of counting the signal pulses - i made two approaches:
First approach:
I connected the frequency output (pin p22) directly to the Timer2 capture pin p29 (CAP2.1) in order to count the pulses. Every rising edge on CAP2.1 created an interrupt on which a counter variable was incremented. This worked very well up to 1MHz. Above, it became increasingly inaccurate. At 2 MHz it stopped counting - seemed as if it couldn't separate the edges or interrupts anymore. I couldn't resolve this and had another idea...
Second approach:
I connected the frequency output (pin p22) directly to the Timer2 capture pin p29 (CAP2.1) and configured Timer2 to use this signal as external clock. This way, the timer count (TC) is no more incremented by the peripheral clock (PCLK, derived from main clock CCLK), but by the external square-wave frequency on pin22. This means, Timer2's "timestamp" (TC) is now the counter of the pulses on pin 29. This works quite good and up to 48 MHz! ( https://mbed.org/users/geotec/code/PulseCounter/ )
I tested this with an external frequency signal (9kHz) as reference, connected to Timer2's other capture pin p30 (CAP2.0). On rising edge, it reads the TC counts out of capture register (CR0). As far as i can see, the accuracy is quite good: the mbed counted the 48MHz-pulses between the rising edges of the 9kHz reference signal. The count was 1082120, resulting in a period of 22.54 ns. With an oscilloscope i measured a period of 22.50ns. So, in this case the deviation is less than 1%.
Hooray and thanks Erik!
Good to hear, also nice to know that fastPWM is indeed capable of making high frequency square waves, it should have been in theory, but practise is not always the same as theory. That it cant make for example 20MHz is not unexpected, it can only make integer divisions of the clock (96MHz).
posted by 15 Dec 2012I tried generating a clock of 48MHz, but using this code mentioned I am not able to generate any output. The code is getting compiled and downloaded in the LPC1768 successfully. Is this because of some limitation of the Compiler or something Else. I am anyways able to generate 2.5MHz using another code. Please help me out.
posted by 19 Feb 2014Hrishikesh: I just added some example code to the library homepage. http://mbed.org/users/geotec/code/PwmOscillator/wiki/Homepage If this doesn't help you, please post your code.
posted by 19 Feb 201412 years ago.
For testing you can try http://mbed.org/users/Sissors/code/FastPWM/, I expect that to work until half the clock frequency (it is untested though for that).
From the user manual:
Quote:
Effective processing of the externally supplied clock to the counter has some limitations. Since two successive rising edges of the PCLK clock are used to identify only one edge on the CAP selected input, the frequency of the CAP input can not exceed one quarter of the PCLK clock. Consequently, duration of the high/low levels on the same CAP input in this case can not be shorter than 1/(2 PCLK).
So yes, it should work. However the duty cycle should be fairly close to 50%, and the clock frequency of timer 2 should be set at its maximum. The 16MHz source is a square wave output? And you did connect the grounds?
Please can u post your code...
posted by Divya Chinnan 29 Oct 2013I published it: https://mbed.org/users/geotec/code/PulseCounter/
posted by bunt _ 29 Oct 2013