# Algorithm For Calculating P & Q values

Version 1
Question: How can I calculate optimal P and Q counter values for the CY22150 without using CyberClocks?

If the application requires on-the-fly frequency changes and the Cypress CyberClocks software is unavailble, use an algorithm to find the best P and Q counter values.

The following equation shows the VCO output frequency as a function of the reference input frequency and the P and Q counters:

VCO = Ref * P / Q

The output frequency is calculated by:

CLKOUT = VCO / Divider

Therefore the final equation is:

CLKOUT = (Ref * P / Q) / Divider

Note: For best performance, have the VCO running as fast as possible and use a low Q value.

Here is an example of how to find the P and Q values:

1. Multiply the desired CLKOUT value as high as possible before the VCO maximum frequency is violated. VCO = CLKOUT * Divider. VCO < VCOMAX, where VCOMAX is 400MHz for commercial devices and 333.33MHz for industrial devices.

2. With the required VCO frequency, start a nested loop with the Q value (possible Q values range from 2 to 129) in the inner loop and the P value in the outer loop
(P is between 16 and 1023). Verify that Ref / Q, the phase detector frequency, is greater than or equal to 250 kHz. Calculate Ref * P / Q for each iteration of the loop. Calculate the ppm error between the desired VCO frequency and Ref * P / Q.
If the ppm error is 0 exit the loop. Otherwise, compare the current ppm error to previously saved ppm error.
If current ppm is less than saved ppm, save the current ppm value. Note: If there are no combinations that give an acceptable ppm value, go back to step 1 and change the desired VCO frequency.

3. With the P and Q values established, convert them to the base values used by the device. P = (2 * (Pbase+4)) + Po (Po can be 0 or 1 and is used for odd numbers of P). Q = Qbase + 2.

Pbase and Qbase are used in the JEDEC file

4. Use Table 9 in page 6 of the  datasheet to use the correct charge pump setting according to the computed P counter (not Pbase) value.