一、2ASK調製與解調
-
2ASK調製
module modulate_2ASK(clk , rst , x, y);
input clk,rst;
input x;
reg [1:0] cnt;
reg carry = 0;
output y; //wire類型
//第一步:分頻得到載波信號序列:carry,(4分頻)
always@(posedge clk)
begin
if(!rst) //rst低電平有效:置位爲0可以重置cnt、carry初值
begin
cnt <= 0;
carry <= 0;
end
else
begin //高電平:3、0;低電平:1、2
if(cnt == 3 ) //先判斷cnt是否爲3: 令carry =1
begin
cnt <= 0;
carry <= 1;
end
else if(cnt == 0) //0: 令carry = 1
begin
carry <= 1;
cnt <= cnt + 1;
end
else //1、2: 令carry = 0
begin
cnt = cnt + 1;
carry <= 0;
end
end
end
//第二步:基帶信號x、載波信號carry,&&
assign y = x && carry;
endmodule
-
2ASK解調
module demodulate_2ASK( clk,rst , y, x );
input clk,rst;
input y; //調製信號
output reg x; //解調後信號,原始基帶信號
reg [3:0] cnt;
reg [2:0] m; //記錄採樣過程中"1"的個數
reg yy; //yy
//計數器 0 ~ 11,一直循環累加
always@(posedge clk)
begin
yy <= y;
if(!rst)
begin
cnt <= 0;
end
else
begin
if(cnt == 11)
cnt <= 0;
else
cnt <= cnt +1; // 000、001、···、007
end
end
//採樣判決
always@(posedge clk)
begin
if(cnt == 11) //cnt=11,m重置爲0
begin
m <= 0;
end
else
begin
if(cnt == 10) //cnt=11,開始採樣判決
begin
if(m <= 2)
x <= 0;
else //只要 m 超過 2, 就判決爲1
x <= 1;
end
else if(yy == 1)
m <= m + 1; //cnt:0 ~ 5,都在累加
end
end
endmodule
二、2FSK調製解調
-
2FSK調製
module modulate_2FSK(clk,rst , x, y);
input clk, rst;
input x;
output y;
reg [1:0] cnt1;
reg cnt2;
reg y_f1, y_f2; // 2個不同頻率的信號
// 利用2位計數器cnt1實現4分頻,分頻之後的信號爲y_f1
always@(posedge clk or negedge rst)
begin
if(!rst) // rst是低電平有效
begin
cnt1 <= 0;
y_f1 <= 0;
end
else
begin
if(cnt1 == 2'b11) //0、1、2、3、 0、1、2、3 、···
begin
cnt1 <= 0;
y_f1 <= ~y_f1;
end
else
cnt1 <= cnt1 + 1;
end
end
// 利用1位計數器cnt2實現2分頻,分頻之後的信號爲f2
always@(posedge clk or negedge rst)
begin
if(!rst) //rst是低電平有效
begin
cnt2 <= 0;
y_f2 <= 0;
end
else
begin
if(cnt2 == 2'b1)
begin
cnt2 <= 0;
y_f2 <= ~y_f2;
end
else
cnt2 <= cnt2 + 1;
end
end
assign y = (x==1)? y_f1 : y_f2;
endmodule
-
2FSK解調
module demodulate_2FSK(clk,rst , x, y);
input clk, rst;
input y;
output reg x;
reg clk1;
reg temp;
reg [3:0] cnt1;
reg [3:0] cnt2;
//計數器,cnt1計數變量:記錄clk下調製信號‘0’電平個數
always@(posedge clk or negedge rst)
begin
if(!rst)
cnt1 <= 0;
else
begin
if(!y)
cnt1 <= cnt1 + 1;
else
cnt1 <= 0;
end
end
//比較器,temp變量:記錄‘0’電平個數大於6的數量,採樣
always@(posedge clk or negedge rst)
begin
if(!rst)
temp <= 0;
else
begin
if(cnt1 > 6) //若clk時鐘下調製信號中的‘1’電平個數大於6
temp <= 1; //temp置爲‘1’
else
temp <= 0;
end
end
//20分頻作爲採樣頻率
always@(posedge clk or negedge rst)
begin
if(!rst)
begin
cnt2 <= 0;
clk1 <= 0;
end
else
begin
if(cnt2 == 9)
begin
clk1 <= ~clk1;
cnt2 <= 0;
end
else
cnt2 <= cnt2 + 1;
end
end
//調製信號是在clk1的上升沿下的temp狀態
always@(posedge clk1 or negedge rst)
begin
if(!rst)
x <= 0;
else
x <= temp;
end
endmodule