(一)参考学习资料

Verilog HDL学习_1:分频器/PWM的实现-LMLPHP

(二)实际操作

1. 相关变量计算:

 

First Initial

Second Initial

Upper case

H

X

ASCII (Dec)

72

88

Lengths of the pulse

  

Mu

Mu_1

2.5*10

Mu_2

2.5*10

k : mu

ku_1 : mu_1

1.2812:3.7188

ku_2 : mu_2

1.3438:3.6562

nu

nu_1

18

nu_2

18

Ku

Ku_1

64060

Ku_2

67190

Lower case

h

x

ASCII (Dec)

104

120

Lengths of the pulse

  

Ml

Ml_1

2.5*10

Ml_2

2.5*10

k : ml

kl_1 : ml_1

1.4063:3.5937

k : ml_2

1.4688:3.5312

nl

nl_1

18

nl_2

18

Kl

Kl_1

70315

Kl_2

73440

2. 第一版:

 module Assignment2(rst, CP, Z);
input CP;
input rst; //1 for upper & 0 for lower reg turn = ;
reg [:] cyc = ; //use for the number of cycles //constant parameters
parameter k = , n = , K = ;
parameter KK = K-; //take parameters according to rst input
reg [:] M;
reg [:] MM;
reg [:] m;
//output of the lautch
output Z;
reg Z; //parameters of upper case
parameter [:] Mu [:] = {, };
parameter [:] mu [:] = {, };
//parameter Ku_1 = k*Mu_1/(k+mu_1);
parameter [:] MMu [:] = {, };
//Parameter []KKu_1 = Ku_1-1; //parameters of lower case
parameter [:] Ml [:] = {, };
parameter [:] ml [:] = {, };
parameter [:] MMl [:] = {, }; //Check rst to determine upper or lower.
//check the number of turn to determine the first two or the second.
always@(posedge CP)
begin
if(rst)
begin
M <= turn ? Mu[]:Mu[];
m <= turn ? mu[]:mu[];
MM <= turn ? MMu[]:MMu[];
end
else if(!rst)
begin
M <= turn ? Ml[]:Ml[];
m <= turn ? ml[]:ml[];
MM <= turn ? MMl[]:MMl[];
end
end //Latch
reg [n-:] Q = ;
wire ld, cz;
assign ld = Q>=MM;
assign cz = (Q<KK)|ld; always@(posedge CP)
begin
{Q,Z} <= {ld?:Q+, cz};
cyc = ld ? cyc+ : cyc; if(cyc == )
begin
turn <= ;
cyc <= ;
end
else begin
turn <= ;
end
end endmodule
  • 出现问题1:

Verilog HDL学习_1:分频器/PWM的实现-LMLPHP

解决方案:将如下部分的变量类型由reg改为了input,系统没有再次崩溃。

     //take parameters according to rst input
reg [:] M;
reg [:] MM;
reg [:] m;
//output of the lautch
output Z;
reg Z;

原因:不明

补充:reg变量不可按位赋值,在二维数组中的赋值应为下图第一种方法。

// Correct
M <=Mu[]; // Wrong
M <=Mu[][:];
  • 出现问题2:vwf文件仿真时,M没有成功赋值

解决方案:修正了对reg宽度的定义。

补充:

1. Verilog中reg类型的宽度是自定义的,若无定义则默认为1bit。reg的宽度影响变量的取值,若赋值超出reg的范围,不会产生Error,reg变量的最大值将被默认为reg宽度。修改变量时应注意该变量的取值范围。

2. reg变量只能存储整数,允许的运算为加减乘除,若要实现浮点数需要以此为基础构建相关专门的模块。

3. 第二版:

 module Assignment2(rst, CP, Z, K);
input CP;
input rst; //1 for upper & 0 for lower reg turn = ;
//use to determine whether the first group of pulses or the second one
reg [:] cyc = ;
//use for the number of pulses in one 'turn' ////constant parameters
parameter n = , M = ;
parameter MM = M-; ////take parameters according to rst input
output [n-:] K;
reg [n-:] K = ; ////output of the lautch
output Z;
reg Z; ////parameters of upper case
parameter Ku_1 = ;
parameter Ku_2 = ; ////parameters of lower case
parameter Kl_1 = ;
parameter Kl_2 = ; ////Latch
reg [n-:] Q;
wire ld, cz;
assign ld = Q>=MM;
assign cz = (Q<K-)|ld; always@(posedge CP)
begin
////Check rst to determine upper or lower.
////check the number of turn to determine the first two or the second.
case(rst)
: begin
if(turn)K <= Kl_2;
else if(!turn)K <= Kl_1;
else K <= ;
end
: begin
if(turn)K <= Ku_2;
else if(!turn)K <= Ku_1;
else K <= ;
end
default: K <= ;
endcase ////renew the state and output clock.
{Q,Z} <= {ld?:Q+, cz};
cyc = ld ? cyc+ : cyc; ////1 cyc represent a pulse
////2 cycles cause an increment in turn
////4 cycles for an entire loop
if(cyc == )
begin
turn <= ;
end
else if(cyc== && turn==)
begin
turn <= ;
cyc <= ;
end end endmodule
  • 仿真结果://K的取值有修改。Verilog HDL学习_1:分频器/PWM的实现-LMLPHP
  • 补充:在Z的第一个输出前有一小段空白期,测量为微秒级,第一个输出仍为5ms。感觉应该不影响时钟的使用,但没有经过硬件检测。
05-24 07:41