Electronics - Verilog - Blinking a LED
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.
Explanation
This tutorial has been made with the Altera DE1 board.
In Verilog, every program starts by a module.
This module will declare ports as:
- input;
- output;
- inout.
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[1] will be always lit and the LEDG[0] 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.
Why 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[0] will stay high (switched on) or low (switched off) we've to do:
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.
Then:
Approximately 0.04 second.
Now to know how many times the LEDG[0] will blink per second, we've just to do:
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:
Consequently:
And:
Our period is approximately 2.7.
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[0] 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).
Code
Quick blinking
Slow blinking
- GPIO_0
- GPIO_1
As you can see from the video, there are two green LEDs plugged to the breadboard (on the right).
Each green LED is paired with a 150 ohm resistor.
The black wire is the GND that I've taken from GPIO_0[11] and the red wire is the VCC (+3V) taken from GPIO_0[28].
This first green LED is just here to say there is a current through the breadboard.
Then I've taken a green wire to connect the + of the second LED to the GPIO_1[0].
It could be a bit difficult to understand that, especially because on the DE1_Schematics (that you can find easily on Internet) the first pin starts from 1 and not 0.
Furthermore we use CLOCK_24[1] instead of the famous CLOCK_50.
沒有留言:
張貼留言