Need help on Clock

The favorite HDL language in North America

Need help on Clock

Postby yongkiat » Wed Apr 21, 2010 3:28 am

i need a code where the clock generate on its own.
now what i have is everytime i push a button the clock run.
need hlp from here pls. here is the code

module LFSR (clk, RESET, DIN, LOAD, SHIFT, Q);
input clk;
input RESET;
input DIN;
input LOAD;

input SHIFT;

output [4:0] Q;

reg [4:0] Q;
reg [4:0] Q_N;
wire TAP;





always @ (negedge clk or negedge RESET)
begin
if (~RESET)
Q <= 5'b11111;
else
Q <= Q_N;
end




assign TAP = Q[2] ^ Q[0];


always @ (Q or SHIFT or LOAD or DIN or TAP)
begin
Q_N = Q;

if (LOAD)
Q_N = {DIN, Q[4:1]};
else
if (SHIFT)
Q_N = {TAP, Q[4:1]};




end

endmodule
yongkiat
 
Posts: 4
Joined: Wed Apr 21, 2010 3:11 am

Postby Yassen » Wed Apr 21, 2010 7:03 am

Hi!

I will give you a hint. You need an 'enable' signal attached to your button. When you press the button, the code runs, atherwise - the code doesn't run.
Or if you want to use the button as a tack signal, you should connect the CLK with the button.
Bear in mind, that first you have to debounce the button, as mechanical commutating elements have the so called contact bounce. I can give you the code of the debounce module, tested on 50MHz Spartan board:
Code: Select all
module debounce (reset, clk, noisy, clean);
   input reset, clk, noisy;
   output clean;
   
   reg [15:0] count;
   reg xnew, clean;
   
   always @(posedge clk)
      if(reset)
         begin
            xnew <= noisy;
            clean <= noisy;
            count <= 0;
         end
      else if(noisy != xnew)
         begin
            xnew <= noisy;
            count <= 0;
         end
      else if(&count)
         clean <= xnew;
      else
         count <= count+1;

endmodule

Find more explanations in this Great web-site: Debouncer.

Hope this helps.

Yassen
Yassen
 
Posts: 70
Joined: Thu Jun 08, 2006 6:46 pm

Postby yongkiat » Wed Apr 21, 2010 9:56 am

can i have a simpler code.
i only studied this for 5weeks.
yongkiat
 
Posts: 4
Joined: Wed Apr 21, 2010 3:11 am

Postby Yassen » Thu Apr 22, 2010 10:57 am

Hi!

I'm not sure that using LFSR is the best for first-steps-in-Verilog. Anyway, you can simulate your circuit first and then implement it. You have to learn to write testbenches. And you have to write it by yourself ;-)

Here is a version of the module, but it is not tested!!! Here the button is first debounced (to eliminate the contact noise) and that the clean signal is connected to the Shift input of your register. You should connect all your ports to your test environment. Note, that if you try to simulate with testbench, you better test only the LFSR, as the debouncer may require too much clock cycles and is not convenient for simulation where you don't have any contact bounce; the debouncer is needed only for implementation.

Code: Select all
module TOP(
    input clk,
    input RESET,
    input btnTact,
    input DIN,
    input LOAD,
    input SHIFT,
    output [4:0] Q);

    // instantiate debouncer
    debounce BTN_CLEANER (.reset(RESET), .clk(clk), .noisy(btnTact), .clean(btnTactClean));

    // instantiate register
    LFSR UUT (.clk(clk), .RESET(RESET), .DIN(DIN), .LOAD(LOAD), .SHIFT(btnTactClean), .Q(Q));

endmodule


Yassen
Yassen
 
Posts: 70
Joined: Thu Jun 08, 2006 6:46 pm


Return to Verilog HDL