题解 | #格雷码计数器#

格雷码计数器

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

`timescale 1ns/1ns

module gray_counter(
   input   clk,
   input   rst_n,

   output  reg [3:0] gray_out
);
    reg cnt_clk;
    reg [3:0] cnt;
    
    always @(posedge clk, negedge rst_n) begin
        if (~rst_n) begin
            cnt_clk <= 1'b1;
        end
        else begin
            cnt_clk <= cnt_clk + 1;
        end
    end

    always @(posedge cnt_clk, negedge rst_n) begin
        if (~rst_n) begin
            cnt <= 4'd0;
        end
        else begin
            cnt <= cnt + 1'd1;
        end
    end

    always @(*) begin
        gray_out = cnt ^ (cnt >> 1);
    end
endmodule

会二进制和格雷码互相转就行,题目没什么意义。

全部评论

相关推荐

ZywOo_求职版:谁问你了....
投递字节跳动等公司9个岗位
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
06-27 18:00
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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