Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
16 years ago

Dynamic Counter

Hi,

Is it possible to load a counter dynamically(run time). The no. of bits to be counted is fixed (16 bit). so Is it possible to change the counter max. value on fly that can lie with in 16 bits.

Thank you

1 Reply

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    module my_counter(
        input               clk,
        input               reset_n,
        input               load,
        input         new_count,
        
        output reg          event
    )
    reg   count_val;
    reg   counter;
    always @(posedge clk)
        if(load)            count_val   <= new_count;
    always @(posedge clk or negedge reset_n)
        if(!reset_n)        counter <= 16'd0;
        else if(!counter)   counter <= count_val;
        else                counter <= counter - 16'd1;
    endmodule
    

    Jake