安德鲁

[笔记].等占空比分频器的几种写法.[Verilog]

0
阅读(2764)

1 偶数分频

(1)2的幂分频

案例I 二分频

i: div_2.v

01 module div_2(
02   input  i_clk,
03   input  i_rst_n,
04   
05   output o_clk
06 );
07  
08 reg [0:0] cnt;
09  
10 always @ (posedge i_clk, negedge i_rst_n)
11   if (!i_rst_n)
12     cnt <= 0;
13   else
14     cnt <= cnt + 1'b1;
15  
16 assign o_clk = cnt[0];
17  
18 endmodule

ii:RTL视图(QII综合结果)


图1 二分频的RTL视图

iii:仿真波形(QII仿真结果)


图2 二分频的仿真波形

案例II 四分频

i:div_4.v

01 module div_4(
02   input  i_clk,
03   input  i_rst_n,
04   
05   output o_clk
06 );
07  
08 reg [1:0] cnt;
09  
10 always @ (posedge i_clk, negedge i_rst_n)
11   if (!i_rst_n)
12     cnt <= 0;
13   else
14     cnt <= cnt + 1'b1;
15  
16 assign o_clk = cnt[1];
17  
18 endmodule

 ii:RTL视图(QII综合结果)


图3 四分频的RTL视图

iii:仿真波形(QII仿真结果)


图4 四分频的仿真波形

(2)不是2的幂分频

案例I 六分频

i:div_6.v

01 module div_6(
02   input      i_clk,
03   input      i_rst_n,
04   
05   output reg o_clk
06 );
07  
08 // log2(6) = 2.5850 <= 3
09 reg [2:0] cnt;
10  
11 // 6 bit counter: 0 ~ 5
12 // 5 = 6 - 1
13 always @ (posedge i_clk, negedge i_rst_n)
14 begin
15   if (!i_rst_n)
16     cnt <= 0;
17   else
18   begin
19     if (cnt == 5)
20       cnt <= 0;
21     else
22       cnt <= cnt + 1'b1;
23     end
24 end
25  
26 // 0 ~ 2  -> 1     
27 // 2 ~ 5  -> 0
28 // 2 = 6>>1 - 1
29 // 5 = 6    - 1
30 always @ (posedge i_clk, negedge i_rst_n)
31 begin
32   if (!i_rst_n)
33     o_clk <= 0;
34   else
35   begin
36     if (cnt <= 2)
37       o_clk <= 1;
38     else
39       o_clk <= 0;
40   end
41 end
42  
43 endmodule

ii:RTL视图(QII综合结果)


图5 六分频的RTL视图

gt;iii:仿真波形(QII仿真结果)


图6 六分频的仿真波形

案例II 十分频

i:div_10.v

01 module div_10(
02   input      i_clk,
03   input      i_rst_n,
04   
05   output reg o_clk
06 );
07  
08 // log2(10) = 3.3219 <= 4
09 reg [3:0] cnt;
10  
11 // 10 bit counter: 0 ~ 9
12 // 9 = 10 - 1
13 always @ (posedge i_clk, negedge i_rst_n)
14 begin
15   if (!i_rst_n)
16     cnt <= 0;
17   else
18   begin
19     if (cnt == 9)
20       cnt <= 0;
21     else
22       cnt <= cnt + 1'b1;
23     end
24 end
25  
26 // 0 ~ 4  -> 1     
27 // 4 ~ 9  -> 0
28 // 4 = 10>>1 - 1
29 // 9 = 10    - 1
30 always @ (posedge i_clk, negedge i_rst_n)
31 begin
32   if (!i_rst_n)
33     o_clk <= 0;
34   else
35   begin
36     if (cnt <= 4)
37       o_clk <= 1;
38     else
39       o_clk <= 0;
40   end
41 end
42  
43 endmodule

ii:RTL视图(QII综合结果)


图7 十分频的RTL视图

iii:仿真波形(QII仿真结果)


图8 十分频的仿真波形

2 奇数分频

案例I 三分频

i. div_3.v

01 module div_3(
02   input  i_clk,
03   input  i_rst_n,
04   
05   output o_clk
06 );
07  
08 // log2(3) = 1.5850 <= 2 
09 reg [1:0] cnt_p;                        // 上升沿计数子
10  
11 // 3位上升沿计数器: 0 ~ 2
12 // 2 = 3 - 1
13 always @ (posedge i_clk, negedge i_rst_n)
14 begin
15   if (!i_rst_n)
16     cnt_p <= 0;
17   else
18     begin
19     if (cnt_p == 2)
20       cnt_p <= 0;
21     else
22       cnt_p <= cnt_p + 1'b1;
23     end
24 end
25  
26 // log2(3) = 1.5850 <= 2 
27 reg [1:0] cnt_n;                        // 下降沿计数子
28  
29 // 3位下降沿计数器: 0 ~ 2
30 // 2 = 3 - 1
31 always @ (negedge i_clk, negedge i_rst_n)
32 begin
33   if (!i_rst_n)
34     cnt_n <= 0;
35   else
36   begin
37     if (cnt_n == 2)
38       cnt_n <= 0;
39     else
40       cnt_n <= cnt_n + 1'b1;
41   end
42 end
43  
44  
45 reg o_clk_p;                            // 上升沿时钟输出寄存器
46  
47 // 输出上升沿时钟
48 // 0     ~ 1 ↑-> 1
49 // (1+1) ~ 2 ↑-> 0
50 // 1 = 3>>1
51 // 2 = 3 - 1
52 always @ (posedge i_clk, negedge i_rst_n)
53 begin
54   if (!i_rst_n)
55     o_clk_p <= 0;
56   else
57   begin
58     if (cnt_p <= 1)                     // 1 = 3>>1
59       o_clk_p <= 1;
60     else
61       o_clk_p <= 0;
62   end
63 end
64  
65 reg o_clk_n;                            // 下降沿时钟输出寄存器
66  
67 // 输出下降沿时钟
68 // 0     ~  1 ↓-> 1
69 // (1+1) ~  2 ↓-> 0
70 // 1 = 3>>1
71 // 2 = 3 - 1
72 always @ (negedge i_clk, negedge i_rst_n)
73 begin
74   if (!i_rst_n)
75     o_clk_n <= 0;
76   else
77   begin
78     if (cnt_n <= 1)                     // 1 = 3>>1
79       o_clk_n <= 1;
80     else
81       o_clk_n <= 0;
82   end
83 end
84  
85 assign o_clk = o_clk_n & o_clk_p;       // 按位与(作用:掩码)
86  
87 endmodule

ii:RTL视图(QII综合结果)


图9 三分频的RTL视图

iii:仿真波形(QII仿真结果)


图10 三分频的仿真波形

案例II 五分频

i. div_5.v

01 module div_5(
02   input  i_clk,
03   input  i_rst_n,
04   
05   output o_clk
06 );
07  
08 // log2(5) = 2.3219 <= 3 
09 reg [2:0] cnt_p;                        // 上升沿计数子
10  
11 // 5位上升沿计数器: 0 ~ 4
12 // 4 = 5 - 1
13 always @ (posedge i_clk, negedge i_rst_n)
14 begin
15   if (!i_rst_n)
16     cnt_p <= 0;
17   else
18     begin
19     if (cnt_p == 4)
20       cnt_p <= 0;
21     else
22       cnt_p <= cnt_p + 1'b1;
23     end
24 end
25  
26 // log2(5) = 2.3219 <= 3 
27 reg [2:0] cnt_n;                        // 下降沿计数子
28  
29 // 5位下降沿计数器: 0 ~ 4
30 // 4 = 5 - 1
31 always @ (negedge i_clk, negedge i_rst_n)
32 begin
33   if (!i_rst_n)
34     cnt_n <= 0;
35   else
36   begin
37     if (cnt_n == 4)
38       cnt_n <= 0;
39     else
40       cnt_n <= cnt_n + 1'b1;
41   end
42 end
43  
44  
45 reg o_clk_p;                            // 上升沿时钟输出寄存器
46  
47 // 输出上升沿时钟
48 // 0     ~ 2 ↑-> 1
49 // (2+1) ~ 4 ↑-> 0
50 // 2 = 5>>1
51 // 4 = 5 - 1
52 always @ (posedge i_clk, negedge i_rst_n)
53 begin
54   if (!i_rst_n)
55     o_clk_p <= 0;
56   else
57   begin
58     if (cnt_p <= 2)                     // 2 = 5>>1
59       o_clk_p <= 1;
60     else
61       o_clk_p <= 0;
62   end
63 end
64  
65 reg o_clk_n;                            // 下降沿时钟输出寄存器
66  
67 // 输出下降沿时钟
68 // 0     ~ 2 ↓-> 1
69 // (2+1) ~ 4 ↓-> 0
70 // 2 = 5>>1
71 // 4 = 5 - 1
72 always @ (negedge i_clk, negedge i_rst_n)
73 begin
74   if (!i_rst_n)
75     o_clk_n <= 0;
76   else
77   begin
78     if (cnt_n <= 2)                     // 2 = 5>>1
79       o_clk_n <= 1;
80     else
81       o_clk_n <= 0;
82   end
83 end
84  
85 assign o_clk = o_clk_n & o_clk_p;       // 按位与(作用:掩码)
86  
87 endmodule

ii:RTL视图(QII综合结果)


图11 五分频的RTL视图

iii:仿真波形(QII仿真结果)


图12 五分频的仿真波形