数字锁相环verilog代码
时间: 2023-05-14 11:02:36 浏览: 256
数字锁相环(Digital Phase-Locked Loop, DPLL)是一种广泛应用于数字通信、计算机网络和数字信号处理等领域的数字信号处理器件。在变频调制、时钟同步、数据解调等方面有着重要的作用。数字锁相环的核心是锁相环电路,其Verilog代码如下。
module DPLL(
input clk, //输入时钟
input rstn, //复位信号
input [7:0] dat_in, //输入数据
output reg clk_out, //输出时钟
input locked_in //同步信号
);
reg [7:0] phase = 0;
reg [7:0] phase_prev = 0;
reg locked_out = 0;
parameter Kp = 8'h3e; //比例系数
parameter ki = 8'h05; //积分系数
parameter kd = 8'h00; //微分系数
parameter count_limit = 8'hcc; //计数器上限值
reg [7:0] filter_out = 0;
reg [7:0] proc_out = 0;
reg [7:0] count = 0;
reg [7:0] phase_diff = 0;
parameter PI_SIZE = 8;
reg [PI_SIZE-1:0] p = 0;
reg [PI_SIZE-1:0] i = 0;
reg [PI_SIZE-1:0] d = 0;
always @(posedge clk or negedge rstn) begin
if(~rstn) begin
phase <= 0;
phase_prev <= 0;
locked_out <= 0;
count <= 0;
clk_out <= 0;
filter_out <= 0;
proc_out <= 0;
p <= 0;
i <= 0;
d <= 0;
end else begin
phase <= phase + dat_in;
phase_prev <= phase;
if(locked_in == 1) begin //已同步
locked_out <= 1;
phase_diff <= phase - phase_prev;
filter_out <= filter_out + (phase_diff - filter_out) * Kp;
proc_out <= filter_out + i + d;
if(proc_out >= count_limit) begin
count <= count + 1;
proc_out <= proc_out - count_limit;
end
if(count >= count_limit) begin
count <= 0;
clk_out <= ~clk_out;
end
p <= filter_out;
i <= i + phase_diff * ki;
d <= phase_diff * kd;
end else begin //未同步
locked_out <= 0;
phase_diff <= 0;
filter_out <= 0;
proc_out <= 0;
count <= 0;
clk_out <= 0;
p <= 0;
i <= 0;
d <= 0;
end
end
end
endmodule
该代码实现了包括比例积分微分(PID)控制、计数器和状态机等在内的全部数字锁相环功能。其中,输入数据dat_in为采样到的相位差,phase为当前相位,phase_prev为上一个时刻的相位,locked_in为同步信号。locked_out为输出的锁定信号,proc_out为计算得出的比例积分微分控制量,count为计数器,clk_out为输出时钟频率。通过对比例积分微分系数的调整,可实现多种锁相环功能。
阅读全文