Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Phase-locked loop
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Implementing a digital phase-locked loop in software=== Digital phase locked loops can be implemented in hardware, using integrated circuits such as a CMOS 4046. However, with microcontrollers becoming faster, it may make sense to implement a phase locked loop in software for applications that do not require locking onto signals in the MHz range or faster, such as precisely controlling motor speeds. Software implementation has several advantages including easy customization of the feedback loop including changing the multiplication or division ratio between the signal being tracked and the output oscillator. Furthermore, a software implementation is useful to understand and experiment with. As an example of a phase-locked loop implemented using a [[phase frequency detector]] is presented in MATLAB, as this type of phase detector is robust and easy to implement. <syntaxhighlight lang="matlab"> % This example is written in MATLAB % Initialize variables vcofreq = zeros(1, numiterations); ervec = zeros(1, numiterations); % Keep track of last states of reference, signal, and error signal qsig = 0; qref = 0; lref = 0; lsig = 0; lersig = 0; phs = 0; freq = 0; % Loop filter constants (proportional and derivative) % Currently powers of two to facilitate multiplication by shifts prop = 1 / 128; deriv = 64; for it = 1:numiterations % Simulate a local oscillator using a 16-bit counter phs = mod(phs + floor(freq / 2 ^ 16), 2 ^ 16); ref = phs < 32768; % Get the next digital value (0 or 1) of the signal to track sig = tracksig(it); % Implement the phase-frequency detector rst = ~ (qsig & qref); % Reset the "flip-flop" of the phase-frequency % detector when both signal and reference are high qsig = (qsig | (sig & ~ lsig)) & rst; % Trigger signal flip-flop and leading edge of signal qref = (qref | (ref & ~ lref)) & rst; % Trigger reference flip-flop on leading edge of reference lref = ref; lsig = sig; % Store these values for next iteration (for edge detection) ersig = qref - qsig; % Compute the error signal (whether frequency should increase or decrease) % Error signal is given by one or the other flip flop signal % Implement a pole-zero filter by proportional and derivative input to frequency filtered_ersig = ersig + (ersig - lersig) * deriv; % Keep error signal for proportional output lersig = ersig; % Integrate VCO frequency using the error signal freq = freq - 2 ^ 16 * filtered_ersig * prop; % Frequency is tracked as a fixed-point binary fraction % Store the current VCO frequency vcofreq(1, it) = freq / 2 ^ 16; % Store the error signal to show whether signal or reference is higher frequency ervec(1, it) = ersig; end </syntaxhighlight> In this example, an array <code>tracksig</code> is assumed to contain a reference signal to be tracked. The oscillator is implemented by a counter, with the most significant bit of the counter indicating the on/off status of the oscillator. This code simulates the two D-type [[Flip-flop (electronics)|flip-flops]] that comprise a phase-frequency comparator. When either the reference or signal has a positive edge, the corresponding flip-flop switches high. Once both reference and signal is high, both flip-flops are reset. Which flip-flop is high determines at that instant whether the reference or signal leads the other. The error signal is the difference between these two flip-flop values. The pole-zero filter is implemented by adding the error signal and its derivative to the filtered error signal. This in turn is integrated to find the oscillator frequency. In practice, one would likely insert other operations into the feedback of this phase-locked loop. For example, if the phase locked loop were to implement a frequency multiplier, the oscillator signal could be divided in frequency before it is compared to the reference signal.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)