题解 | #Johnson Counter#

Johnson Counter

https://www.nowcoder.com/practice/7ee6e9ed687c40c3981d7586a65bc22d

//方法1
`timescale 1ns/1ns

module JC_counter(
   input                clk ,
   input                rst_n,
 
   output reg [3:0]     Q  
);


    always@(posedge clk or negedge rst_n)
        begin
            if(!rst_n)
                begin
                    Q <= 4'd0;
                end
            else
                begin
                    Q <= {!Q[0],Q[3:1]};//低位取反右移
                end
        end
endmodule
//方法2
`timescale 1ns/1ns

module JC_counter(
   input                clk ,
   input                rst_n,
 
   output reg [3:0]     Q  
);

    reg     [2:0]       cnt;
    always@(posedge clk or negedge rst_n)
        begin
            if(!rst_n)
                cnt <= 3'd0;
            else 
                cnt <= cnt + 1'b1;
        end

    always@(*)
        begin
            if(!rst_n)
                Q <= 3'd0;
            else 
                case(cnt)
                    3'd0:Q <= 4'b0000;
                    3'd1:Q <= 4'b1000;
                    3'd2:Q <= 4'b1100;
                    3'd3:Q<= 4'b1110;
                    3'd4:Q <= 4'b1111;
                    3'd5:Q <= 4'b0111;
                    3'd6:Q <= 4'b0011;
                    3'd7:Q <= 4'b0001;
                    default:Q <= 4'b0000;
                endcase
        end
endmodule

全部评论

相关推荐

06-17 00:26
门头沟学院 Java
程序员小白条:建议换下项目,智能 AI 旅游推荐平台:https://github.com/luoye6/vue3_tourism_frontend 智能 AI 校园二手交易平台:https://github.com/luoye6/vue3_trade_frontend GPT 智能图书馆:https://github.com/luoye6/Vue_BookManageSystem 选项目要选自己能掌握的,然后最好能自己拓展的,分布式这种尽量别去写,不然你只能背八股文了,另外实习的话要多投,尤其是学历不利的情况下,多找几段实习,最好公司title大一点的
无实习如何秋招上岸
点赞 评论 收藏
分享
程序员饺子:正常 我沟通了200多个 15个要简历 面试2个 全投的成都的小厂。很多看我是27直接不会了😅
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-01 11:27
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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