Verilog Examples - LED blinkning by clock divider
We will now extend out clock Divide by 2N code to blink and LED. Basically we will set the parameters of the clock divider. We will also examine the issue with large data generated in test bench.
Problem - Write verilog code that has a 50 MHz clock and a reset as input. It has an output that can be called clk_out. The clk_out is also a clock that has a frequency of 2.5 Hz of time period of 400 ms. Extend the parameter to calculate the values for the parameter for 1 Hz clk_out.
Solution -
This is the main code ledblink.v
|
Here is the test bench ledblinktb.v
|
The waveform generated by this code is here
Explanation
The value of the divider 2N to generate 2.5 Hz clock is 20,000,000 which gives N = 10,000,000. In hexadecimal it is 24'h989680. We calculated hex value to find the number of bits required for the register.
module clk_div #( parameter WIDTH = 24, parameter [WIDTH-1:0] N = 24'h989680 ) |
In test bench we did monitor clk as it will generate huge output without much value. We can run it just once to make sure that it is 50 MHz. Also note that we have set the timescale to 10 ns which is half clock period.
$monitor($time, " reset=%b,clk_out=%b",reset,clk_out); |
In the $dumpvars we are dumping only the clk_out as in
$dumpvars(1,clk_out); |
If we would have done
$dumpvars(0,clkdiv2n_tb); |
It would have dumped all variables including the clk which will make the size of the dump file unnecessarily large. The reset of the code should be easy to understand