Reputation: 694
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.
Upvotes: 2
Views: 240
Reputation: 20514
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.
Upvotes: 2
Reputation: 6978
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.
Upvotes: 2