Forum Discussion

AShar42's avatar
AShar42
Icon for New Contributor rankNew Contributor
7 years ago

My code is making a random LED generator using LSFR. But, I want to turn on only one LED at a time, but my program is randomly generating 1-3 LED at a time.

module Memorygame

#(parameter BITS = 5)

(clk, rst_n, out);

input clk;

input rst_n;

output reg [3:0] out;

reg [3:0] out_next;

integer count=0;

reg clk_1hz;

always @ (posedge clk) begin // slows our clock from 50mhz to 1hz

if (count<25000000)

count = count +1;

else begin

clk_1hz = ~clk_1hz;

count = 0;

end

end

always @* begin // responsible for determing the blinking of the leds

out_next = out;

repeat(BITS) begin

out_next = {out_next[2:0],(out_next[3]^out_next[0])};

end

end

always @(posedge clk_1hz or negedge rst_n) begin

if(!rst_n)

out <= 4'h1f;

else

out <= out_next;

end

endmodule

1 Reply

  • Abe's avatar
    Abe
    Icon for Frequent Contributor rankFrequent Contributor

    There are a couple of issues.. check out this code and try if it works.. this is the correct way to implement a LFSR.

    `timescale 1ns/1ns
    module Memorygame 
    	(
    	   input wire       clk, 
    	   input wire       rst_n, 
    	   output reg [3:0] out
      	); 
     
    integer   count;
    reg       clk_1hz;
    wire     lf;  // linear feedback of XORed bits.
     
    always @ (posedge clk or negedge rst_n) begin // slows our clock from 50mhz to 1hz 
      if(!rst_n)  begin
        count = 0;
        clk_1hz <= 1'b0;
      end
      else begin
        if (count<250000)
          count <= count + 1; 
        else begin 
          clk_1hz <= ~clk_1hz;
          count <= 0;
        end 
      end 
    end //always
     
    assign lf =  !(out[3] ^ out[0]);
    always @(posedge clk_1hz or negedge rst_n) begin
       if(!rst_n)
         out <= 4'b0000;
       else begin
         out <= {out[2],out[1],
                 out[0], lf}; 
        end
    end //always