题解 | #格雷码计数器#

格雷码计数器

https://www.nowcoder.com/practice/311754bcd45d42eb8d981eeddbdf1e43

`timescale 1ns/1ns

module gray_counter(
   input   clk,
   input   rst_n,

   output  reg [3:0] gray_out
);
reg [3:0] bit_num;
reg flag;

always@(posedge clk or negedge rst_n)  begin
    if(!rst_n)
        flag <= 1'b0;
    else
        flag <= ~flag;
end
always@(posedge flag or negedge rst_n)  begin
    if(!rst_n)
        bit_num <= 'd0;
    else
        bit_num <= bit_num + 1'b1;
end
always@(posedge clk or negedge rst_n)  begin
    if(!rst_n)
        gray_out <= 'd0;
    else
        gray_out <= bit_num ^(bit_num >> 1);
end
//assign gray_out = bit_num;
endmodule

全部评论

相关推荐

心中的变压器:简历需要突出重点
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务