十二位数分割模块

思想

有三个状态 S0S1S2
其中每个状态对应灯开关4位数
复位信号生效时,初始状态为 S0
若复位信号失效,则每次脉冲都会切换到下一个状态
S0 -> S1 -> S2 -> S0 -> …
an采用大端法


API

  • clk : 脉冲,每次脉冲切换一次(循环切换)
  • rst : 复位信号,==1 则 输出低位,否则循环切换
  • num : 要被分割的数,12位
  • result : 被分割后的四位数
  • an : 循环切换开关

sub.v

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// 将 12 位数切成 3 个 4 位数
module sub (
input clk, // 脉冲
input rst, // 复位
input [11:0] num, // 12 位结果
output reg [3:0] result, // 输出 4 位结果
output reg [2:0] an // 输出当前选择的 灯
);

reg [1:0] status, nextstatus;

always @(posedge clk) begin
// 描述当前状态
if (rst == 1'b1) begin
status <= 2'b00;
end else begin
status <= nextstatus;
end
end

always @(*) begin
// 描述状态转移
case (status)
2'b00 : nextstatus <= 2'b01;
2'b01 : nextstatus <= 2'b10;
2'b10 : nextstatus <= 2'b00;
endcase
end

always @(*) begin
case (status)
2'b00 : begin
an = 3'b001;
result = num[3:0];
end
2'b01 : begin
an = 3'b010;
result = num[7:4];
end
2'b10 : begin
an = 3'b100;
result = num[11:8];
end
endcase
end

endmodule

十二位数分割模块
http://yjh-2860674406.github.io/2022/09/08/编程/Verilog/十二位数分割模块/
Author
Ye JinHua
Posted on
September 8, 2022
Licensed under