Blinking a LED, a basic step.
But without this first step, there won't be a second.
And without second, no third and so on.
This simple tutorial will explain basics in order to program a blinking system.
Thus this timer example in Verilog could be seen as an Hello World exercise.
Let's get started.
This tutorial has been made with the Altera DE1 board.
In Verilog, every program starts by a module.
This module will declare ports as:
A clock is an input because the pin where the clock is connected has to receive data from this clock.
An output will send data from the program to the pin.
So it's normal to set a LED as an output.
Our board has four green LEDs.
For our example we're going to use only two of them.
LEDG will be always lit and the LEDG will blink.
The reg element is there to hold a value.
1'b means that it'll be only 1 binary digit in this reg element.
So we declare three reg elements, one for a data holding a value of 1 in binary, the second to create a counter and the third to have a state.
Of course this state will change during the process.
Indeed, in the always@ block, we tell the process that for each clock's edge, the counter will be incremented by 1.
For the first code, called Quick blinking, we also tell the process that the state value will change everytime the counter reach 2^20.
Because in binary this value is: 100000000000000000000.
We've twenty zeros, so we've 2^20 in binary.
In a decimal base it will be 1,048,576.
Our clock is at 50 MHz, meaning fifty million oscillations per second (it's huge, yes).
If we would like to know how long the LEDG will stay high (switched on) or low (switched off) we've to do:
1,048,576 / 50,000,000 = 0.02097152
So approximately 0.02 second.
This time represents the moment when the LED is high (1) or when it's low (0).
So to retrieve the period (a cycle) we've to multiply this result by 2.
0.02097152* 2 = 0.04194304
Approximately 0.04 second.
Now to know how many times the LEDG will blink per second, we've just to do:
1 / 0.04194304 = 23.8418579102
So approximately 24 times per second, that is 24 Hz.
This frequency is a bit too quick.
Let's reduce it with the second code, called Slow blinking.
For that we've to change the counter-27th's bit (so 2^26).
Same as earlier:
2^26 = 67,108,864
67,108,864 / 50,000,000 = 1.34217728
1.34217728 * 2 = 2.68435456
Our period is approximately 2.7.
1 / 2.68435456 = 0.37252902984.
Then the frequency is approximately 0.37 Hz.
And if you check the LED after sending the HDL on the board, you should see the LEDG blinking once every 1.3 second (from low to high or high to low) and 2.7 seconds for a complete cycle (from low to low or high to high).
// Quick blinking a LED /* module */ module blinking ( input CLOCK_50, output [1:0] LEDG ); /* reg */ reg data1 = 1'b1; reg [32:0] counter; reg state; /* assign */ assign LEDG = state; assign LEDG = data1; /* always */ always @ (posedge CLOCK_50) begin counter <= counter + 1; state <= counter; // <------ data to change end endmodule
// Slow blinking a LED /* module */ module blinking ( input CLOCK_50, output [1:0] LEDG ); /* reg */ reg data1 = 1'b1; reg [32:0] counter; reg state; /* assign */ assign LEDG = state; assign LEDG = data1; /* always */ always @ (posedge CLOCK_50) begin counter <= counter + 1; state <= counter; // <------ data to change end endmodule
A simple timer is a good way to start learning Verilog.
In this tiny code there were a lot of things to know and you are now aware about that.
Well done, you've made it.