2

I want that an LED should turn on at any random time within 15 seconds. For example after pressing a button it should turn on after 4 seconds or 7 seconds, that is randomly.

I came up with the code to produce a 15 second delay, but I cant figure out a way to select a random time between this.

Below is my code for the 15 second delay:

always @ (posedge clock or posedge reset)
begin
 if(reset)

  ticker <= 0;

 else if(ticker == 750000000) //if it reaches the desired max value that equates 15 second reset it
  ticker <= 0;
 else if(start) //only start if the input is set high
  ticker <= ticker + 1;
end

assign click = ((ticker == 750000000)?1'b1:1'b0); //click to be assigned high every 0.1 second

Also I want a synthesizable solution please.

ipunished
  • 684
  • 2
  • 6
  • 22

2 Answers2

2

You need a hardware way to create a random number. You have $random for simulation but not hardware.

I would suggest creating an lfsr which runs and when a button is pressed, you capture the current value which you count up to, or preset a counter and count down to 0.

Morgan
  • 19,934
  • 8
  • 58
  • 84
  • Thank you. Ive been looking into LFSR as advised and am greatly confused by the selection of taps. What determines which feedback bits must be used? Also I can find a consistent example on it. Thanks – ipunished Jan 23 '13 at 08:15
  • 1
    This might be best done by starting another question. You need to decide how many random numbers you need (feedback terms), Or maybe just the (bit) width of a random number. An example [Verilog lfsr](https://github.com/rcastle/verilog_lfsr/blob/master/fibonacci_lfsr.v) For [choosing taps](http://www.ece.cmu.edu/~koopman/lfsr/index.html) make sure you have a maximal LFSR. – Morgan Jan 23 '13 at 08:42
  • Thanks. I just need a 30 bit wide pseudo random number from this, I care not for the number of random terms before it starts repeating. I was trying to understand the link you posted, I understand that ``"iterations per value"`` show the random numbers before it starts repeating. Am I right? But what is the start and end value? As it does not show the tap arrangement. Can I just use any tap arrangement and get away with it? Thank you – ipunished Jan 24 '13 at 07:17
  • 1
    @ipunished, I have added this Q&A http://stackoverflow.com/questions/14497877/how-to-implement-a-sudo-hardware-random-number-generator/ Please add comments there if I have missed anything. – Morgan Jan 24 '13 at 09:29
  • Dude, you are a legend. A clear detailed explanation was greatly needed regarding LFSR. Thanks a lot :) – ipunished Jan 25 '13 at 06:32
2

If you need a random number in hardware, you can use a Linear feedback shift register (LFSR) circuit to generates a pseudorandom number.

LFSRs are easy to implement and you'll find many examples online.

When you get a button press, you can capture the current value of the LFSR to a register (you may need to scale in some way for your application) and use that value to count seconds until you turn on the LED.

dwikle
  • 6,820
  • 1
  • 28
  • 38
  • Thank you. Ive been looking into LFSR as advised and am greatly confused by the selection of taps. What determines which feedback bits must be used? Thanks – ipunished Jan 23 '13 at 08:14