Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.


Help with 5 bit counter with JK Flip Flops

Help with 5 bit counter with JK Flip Flops

Hey guys, my teacher asked me to make a 5 bit counter using JK Flip Flops, and he said the counter has to be an inout port. The thing is, I know almost nothing of verilog, just the basics, and I have no idea what's wrong with my code. I searched the web for the circuit, but for some reason my counter only displays "xxxxx"...

Here's the site where I found the circuit:

Here's my code:


module FlipFlopJK(J, K, clock, Q);
    input J, K, clock;
    output Q;
    reg Q;
    reg Qm;
    reg Qn;

    always @(posedge clock)
        if (J == 1 && K == 0)
            Qm <= 1;
        else if (J == 0 && K == 1)
            Qm <= 0;
        else if (J == 1 && K == 1)
            Qm <= ~Qm;
    always @(negedge clock)
        Q <= Qm;

module Counter(count, clock, signal, reset, enable);

    input clock, signal, reset, enable;
    inout [0:4] count;
    wire clock, signal, reset, enable;
    wire [0:4] count;
    wire A, B, C, D, E;
    wire auxC, auxB, auxA;
    assign count[0] = A;
    assign count[1] = B;
    assign count[2] = C;
    assign count[3] = D;
    assign count[4] = E;
    assign count = (reset) ? 00000 : count;
    FlipFlopJK e (1, 1, clock, E);
    FlipFlopJK d (E, E, clock, D);
    and a1 (auxC, E, D);
    FlipFlopJK c (auxC, auxC, clock, C);
    and a2 (auxB, E, D, C);
    FlipFlopJK b (auxB, auxB, clock, B);
    and a3 (auxA, E, D, C, B);
    FlipFlopJK a (auxA, auxA, clock, A);


module testBench();

    wire [0:4] count;
    reg clock, reset, enable, signal;

    Counter c (count, clock, signal, reset, enable);    

    initial begin: Init

        // Zera a chave
        clock = 1;
        enable = 0;
        reset = 0;
        signal = 1;
        // Escreve no arquivo .vcd
        #2 reset = 1;
        #2 reset = 0;
        #2 enable = 1;
        #10 enable = 0;
        #2 $finish;

    always begin
        #1 clock = ~clock;
        $display("Time\t Count\t Clock");
        $monitor("%0d\t %b\t %b\t", $time, count, clock);


RE: Help with 5 bit counter with JK Flip Flops

In Verilog, all 'reg' types initialize to x.
So, all 5 of your JK outputs start as x, and stay as
x.  You could initialize the Qm reg in all 5 in
your testbench as follows:


initial {c.e.Qm, c.d.Qm, c.c.Qm, c.b.Qm, c.a.Qm,} = 5'b00000;

The value should not matter: 5'b10101 should be fine too.

Another problem is that you have 2 assigns to each count bit.


    assign count = (reset) ? 5'b00000 : {A, B, C, D, E};

and get rid of:


    assign count[0] = A;
    assign count[1] = B;
    assign count[2] = C;
    assign count[3] = D;
    assign count[4] = E;
    assign count = (reset) ? 00000 : count;

RE: Help with 5 bit counter with JK Flip Flops

Thank you, although I already handed it to my teacher, I did some ugly stuff, and it wasn't fully functional, but that's past now =]

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close