Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Help with Verilog

  1. Jun 7, 2008 #1
    I'm trying to program and FPGA so that it acts as a countdown timer. The problem is that I'm very new to Verilog, and I'm stuck.

    I have an Altera Cyclone II FPGA board. It has 4 SSGs (HEX0 - HEX3), 10 switches (SW0 - SW9), 4 push-buttons (KEY0 - KEY3), 8 green LEDs (LEDG0 - LEDG9), and 10 red LEDs (LEDR0 - LEDR9).

    Here's what I wanted it to do: When SW9 is in the up position (binary 1), that is "set mode", where you can set the time you want. The you set the time, is by pressing KEY0. Also, if SW0 is in the up position (binary 1), then each time you press KEY0, the counter goes 1 up. If SW0 is in the down position (binary 0), then each time you press KEY0, the counter goes 1 down.

    Once you set SW9 to the down position (binary 0), that is "go mode", at which point the counter counts down to 00 00. Once it reaches that point, all the LEDs light up.

    You can also reset everything by pressing KEY3 (the KEYs are high at binary 0 and low at binary 1)

    The SSGs display single seconds, double seconds, single minutes, and double minutes:

    DM SM DS SS
    HEX3 HEX2 HEX1 HEX0


    The problem I have is that it doesn't count down like it is supposed to. For example, I set the time to 22:22, and when I'm in "go mode", SS goes to zero and stays there, and the rest keep counting to zero, and then going to either 9 or 5 and then counting to zero again, indefinitely. I don't understand why it does that. Below I have all the code that I wrote.


    Top level instantiation:

    Code (Text):
    assign freeze = ((cr1 == 1) && (cr2 == 1) && (cr3 == 1) && (cr4 == 1));

    cnt_up_down_09 countdown1 (
                                .clk_in(EXT_CLOCK),                        
    //                          .clk_in(Cont[22]),
                                .rst_n(KEY[3]),
                                .set_go(SW[9]),
                                .set_up_down(SW[0]),
                                .buzzer(LEDG[4]),
                                .current_q(my_counter),
                                .button_0(KEY[0]),
                                .zero_in(1'b1),
                                .freeze(freeze),
                                .carrier_out(cr1)
                                                           
                                );

    cnt_up_down_05 countdown2 (
                                .clk_in(EXT_CLOCK),                        
    //                          .clk_in(Cont[22]),
                                .rst_n(KEY[3]),
                                .set_go(SW[9]),
                                .set_up_down(SW[0]),
                                .buzzer(LEDG[5]),
                                .current_q(my_counter2),
                                .button_0(KEY[0]),
                                .zero_in(cr1),
                                .freeze(freeze),
                                .carrier_out(cr2)
                               
                                );
                               
    cnt_up_down_09 countdown3 (
                                .clk_in(EXT_CLOCK),                        
    //                          .clk_in(Cont[22]),
                                .rst_n(KEY[3]),
                                .set_go(SW[9]),
                                .set_up_down(SW[0]),
                                .buzzer(LEDG[6]),
                                .current_q(my_counter3),
                                .button_0(KEY[0]),
                                .zero_in(cr2),
                                .freeze(freeze),
                                .carrier_out(cr3)
                                                           
                                );

    cnt_up_down_05 countdown4 (
                                .clk_in(EXT_CLOCK),                        
    //                          .clk_in(Cont[22]),
                                .rst_n(KEY[3]),
                                .set_go(SW[9]),
                                .set_up_down(SW[0]),
                                .buzzer(LEDG[7]),
                                .current_q(my_counter4),
                                .button_0(KEY[0]),
                                .zero_in(cr3),
                                .freeze(freeze),
                                .carrier_out(cr4)
                               
                                );

    ssg_decoder_ ssd1 (HEX0, my_counter);
    ssg_decoder_ ssd2 (HEX1, my_counter2);
    ssg_decoder_ ssd3 (HEX2, my_counter3);
    ssg_decoder_ ssd4 (HEX3, my_counter4);


    /*
    ssg_ex inst_one (
                        .clk_in(Cont[22]),
    //                  .clk_in(EXT_CLOCK),
                        .KEY0(KEY[0]),
                        .HEX0(HEX0),
                        .HEX1(HEX1),
                        .HEX2(HEX2),
                        .HEX3(HEX3),
                        .my_counter(my_counter),
                        .my_counter2(my_counter2),
                        .my_counter3(my_counter3),
                        .my_counter4(my_counter4),
                        .cr1(cr1),
                        .cr2(cr2),
                        .cr3(cr3)
                                           
    );
    cnt_up_down_09

    Code (Text):
    module cnt_up_down_09 (
            clk_in,
            rst_n,
            set_go,
            set_up_down,
            buzzer,
            current_q,
            button_0,
            zero_in,
            freeze,
            carrier_out
    );

    input clk_in;
    input rst_n;
    input set_go;       // 0 is set, 1 is go
    input set_up_down;  // 0 is down, 1 is up
    input button_0;
    input zero_in;      //0 is not zero, 1 is zero
    input freeze;

    output [3:0] current_q;
    output buzzer;
    output carrier_out;


    reg [3:0] next_d;
    reg [3:0] current_q;

    //Create FFs
    always @(posedge clk_in)
    begin
        current_q <= next_d;
    end

    //Create Combinatorical Logic
    always @(rst_n or current_q or set_up_down or button_0 or set_go or zero_in or freeze)
    begin

    if (rst_n == 0)
        next_d = 2;
       
    else if (set_go == 0)
            begin
                if ((set_up_down == 1) && (button_0 == 0) && (current_q != 9))
                    next_d = current_q + 1;
               
                else if ((set_up_down == 1) && (button_0 == 0) && (current_q == 9))
                        next_d = 0;
               
                     else if ((set_up_down == 0) && (button_0 == 0) && (current_q != 0))
                            next_d = current_q - 1;
               
                          else if ((set_up_down == 0) && (button_0 == 0) && (current_q == 0))
                                next_d = 9;
               
                               else
                                    next_d = current_q;
            end
           
           
        else if (set_go == 1)
            begin
                if (freeze == 1)
                    next_d = 0;

                else if (current_q > 0)
                        next_d = (current_q - 1);

                     else if ((current_q == 0) && (zero_in == 0))
                            next_d = 9;

                          else if ((current_q == 0) && (zero_in == 1))
                                next_d = 0;
            end
    end

    assign buzzer = (current_q == 0);
    assign carrier_out = ((current_q == 0) && (next_d == 9));

    endmodule

    cnt_up_down_05

    Code (Text):
    module cnt_up_down_05 (
            clk_in,
            rst_n,
            set_go,
            set_up_down,
            buzzer,
            current_q,
            button_0,
            zero_in,
            freeze,
            carrier_out
    );

    input clk_in;
    input rst_n;
    input set_go;       // 0 is set, 1 is go
    input set_up_down;  // 0 is down, 1 is up
    input button_0;
    input zero_in;      //0 is not zero, 1 is zero
    input freeze;


    output [3:0] current_q;
    output buzzer;
    output carrier_out;

    reg [3:0] next_d;
    reg [3:0] current_q;

    //Create FFs
    always @(posedge clk_in)
    begin
        current_q <= next_d;
    end

    //Create Combinatorical Logic
    always @(rst_n or current_q or set_up_down or button_0 or set_go or zero_in or freeze)
    begin

    if (rst_n == 0)
        next_d = 2;
       
    else if (set_go == 0)
            begin
                if ((set_up_down == 1) && (button_0 == 0) && (current_q != 5))
                    next_d = current_q + 1;
               
                else if ((set_up_down == 1) && (button_0 == 0) && (current_q == 5))
                        next_d = 0;
               
                     else if ((set_up_down == 0) && (button_0 == 0) && (current_q != 0))
                            next_d = current_q - 1;
               
                          else if ((set_up_down == 0) && (button_0 == 0) && (current_q == 0))
                                next_d = 5;
               
                               else
                                    next_d = current_q;
            end
           
           
        else if (set_go == 1)
            begin
                if (freeze == 1)
                    next_d = 0;

                else if (current_q > 0)
                        next_d = (current_q - 1);

                     else if ((current_q == 0) && (zero_in == 0))
                            next_d = 5;

                          else if ((current_q == 0) && (zero_in == 1))
                                next_d = 0;
            end
    end

    assign buzzer = (current_q == 0);
    assign carrier_out = ((current_q == 0) && (next_d == 5));

    endmodule
    I tried running a simulation, but nothing seems to be responding, so I'm pretty sure something is wrong with my code.

    Any help you be greatly appreciated.
     
  2. jcsd
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Can you help with the solution or looking for help too?
Draft saved Draft deleted



Similar Discussions: Help with Verilog
  1. Help (Replies: 8)

  2. Multimeters help (Replies: 20)

  3. Help with prototype (Replies: 8)

  4. CFD help (Replies: 1)

Loading...