This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

How to read Clock Frequency

Other Parts Discussed in Thread: MSP430F5338

Hi everyone,

 I am working in MSP430F5338 with XT2 (20MHz) as source frequency for SMCLK,MCLK,ACLK . Everyone is aware to develop timer interrupt code the source clock frequency is need. For that i make the sourece clock frequence as a constant in my code like

 SourceClock  = 20000000

 Is there any sample code or logic is available to read the clock frequency by the software itself at run time instaed of giving as manually.

Thanks in Advance

  • Hi Balaganhatharan,

    Just for confirmation, are you going to get the frequency that the MCU run at? or yo just want to measure the frequency of a clock (such as SMCLK or ACLK) in the MCU?

    Thank you for posting

    Eric Fu

  • In order to measure the frequency of something with any precision, you need an accurate timebase to begin with.

    You could do something like use a timer with another clock as the trigger source and capture the count between triggers.

    But, you have to ask yourself - is the accuracy/jitter/drift etc of the reference clock (say, a 32768 Hz crystal) good enough to measure a 20 MHz clock? What if the answer you get is 19.678 MHz? What are you going to do with that? How do you know whether the 20 MHz clock is off or it is do to inaccuracy of the reference timebase?

    Perhaps you could re-frame your question in terms of what you actually need to accomplish...

  • To calculate the timer clock we used the formula as    

    f(timer) = f(source) / ID;

    where the f(source) is the clock source frequency.

    In my code to calculate the f(timer) i wrote the f(source) value as constant like

     SourceClock  = 20000000

    My question is how do we get the clock source value at run time instead of writing as constant?

  • NO

    That's the short answer.

    The proper answer is "It depends"

    Peter

  • Prakash Balagangatharan said:
    My question is how do we get the clock source value at run time instead of writing as constant?

    I think has been answered. The answer is "you can't", unless you have some other hardware and are willing to live with inaccurate answers.

  • Brian, this reminds me of the stories of Baron Muenchhausen. In one of them he pulled himself out of a swamp on his own hair.

     

    Balagangatharan, ‘frequency’ means ‘number of oscillations per second’. How can you measure the frequency if you don’t know when a second is over? You can’t, unless you have a second, known frequency, you can use as reference.
    In your code, you simply tell the code that source clock is 2MHz. If you’re using an external crystal, you should know which one you use, and so this approach is the easiest.

    The 5x family has the factory-trimmed REFO. However, it is not too precise, only +-3.5% (worst case).
    You can use it to count the source clock cycles during one reference clock cycle to get a estimate source clock value. If you know the source is a multiple of 1MHz, then you can this way determine how many MHz it is. IIRC, the USB BSL does it this way (or similar) to detect whether the USB crystal is 4, 8 or 12MHz (to set-up the 48MHz PLL)

    For a precision frequency detection, you’ll need a precision clock reference. And need to know its exact frequency.

**Attention** This is a public forum