CLKFREQ

Command: Current System Clock frequency; the frequency at which each cog is running.

((PUBPRI))
   CLKFREQ


Returns: Current System Clock frequency, in Hz.

Explanation

The value returned by CLKFREQ is the actual System Clock frequency as determined by the current clock mode (oscillator type, gain, and PLL settings) and the external XI pin frequency, if any. Objects use CLKFREQ to determine the proper time delays for time-sensitive operations. For example:

waitcnt(clkfreq / 10 + cnt)      'wait for .1 seconds (100 ms) 

This statement divides CLKFREQ by 10 and adds the result to CNT (the current System Counter value) then waits (WAITCNT) until the System Counter reaches the result value. Since CLKFREQ is the number of cycles per second, a divide by 10 yields the number of clock cycles per 0.1 seconds, or 100 ms. So, disregarding the time it takes to process the expression, this statement pauses the cog's program execution for 100 ms. The table below shows more examples of System Clock tick versus Time calculations.

System Clock Ticks Vs. Time Calculations

Expression

Result

clkfreq / 10

Clock ticks per 0.1 seconds (100 ms)

clkfreq / 100

Clock ticks per 0.01 seconds (10 ms)

clkfreq / 1_000

Clock ticks per 0.001 seconds (1 ms)

clkfreq / 10_000

Clock ticks per 0.0001 seconds (100 µs)

clkfreq / 100_000

Clock ticks per 0.00001 seconds (10 µs)

clkfreq / 9600

Clock ticks per serial bit period at 9,600 baud (~ 104 µs)

clkfreq / 19200

Clock ticks per serial bit period at 19,200 baud (~ 52 µs)

The value that CLKFREQ returns is actually read from long 0 (the first location in RAM) and that value can change whenever the application changes the clock mode, either manually or via the CLKSET command. Objects that are time-sensitive should check CLKFREQ at strategic points in order to adjust to new settings automatically.

CLKFREQ vs. _CLKFREQ

CLKFREQ is related to, but not the same as, _CLKFREQ. CLKFREQ is command that returns the current System Clock frequency whereas _CLKFREQ is an application-defined constant that contains the application's System Clock frequency at startup. In other words, CLKFREQ is the current clock frequency and _CLKFREQ is the original clock frequency; they both may happen to be the same value but they certainly can be different.

Unless otherwise noted, content on this site is licensed under the
Creative Commons Attribution-ShareAlike 4.0 International License.