top of page

OUR FIRST CODE IN VHDL

Tuğbanur Akgün

In the other part, we touched on the following topics: What is FPGA in the other part? What is hardware language? What is VHDL? Now let's start to understand and learn the vhdl language by making VHDL language examples. At the beginning it will be difficult to understand and confusing. But keep working hard without giving up and be diligent. And enjoy exploring a very fun software world. Let's start writing our first code like 'Hello World' in other software programming languages or led blink in hardware.


QUESTION 1 )

Let's write the necessary circuit design in VHDL language to turn on / off the led using a switch on the FPGA chip.


Which parts of the Basys 3 card will we use;


We will use our zeroth led out of our 16 leds and zeroth switch out of 16 switches


CODE


library IEEE;
use IEEE.STD_LOGIC_1164.ALL;

entity sw_led is
    Port ( 
           sw  : in STD_LOGIC;
           led : out STD_LOGIC
           );
end sw_led;

architecture Behavioral of sw_led is

begin

       led <= sw;

end Behavioral;

Let's learn the code blocks before moving on to the code explanations.


VHDL DESIGN SECTIONS


It generally consists of 3 parts.




1) Library and Packages Section


As the name suggests, it is the part where we add the necessary libraries.


2) Entity


This part is the part where we define the necessary input and output ports for our circuit.


So how should we write;


1 - We must give a variable name for an input or output.

2 - We need to put a colon after the variable name.

3- We need to determine whether the variable is input or output.

4- We determine the type of the variable.


3) Architecture


This is the main part of our code. We can define the signal between architecture and start. We'll learn about it later. Then we try to write the main code.




CODE EXPLANATIONS


library IEEE;
use IEEE.STD_LOGIC_1164.ALL;

We need to include these two libraries in our code to be able to use logic gates, std_logic and std_logic_vector.


 sw  : in STD_LOGIC;
 led : out STD_LOGIC

We have defined an input named sw of type std_logic.

We have defined an output named std_logic type led.


NOTE:

As you noticed, we put a semicolon at the end of the line when the first variable is finished, but we did not put a semicolon at the end of the line before ending the existence.


       led <= sw;

In other words, we have equated the state of the switch to 1 or 0 to the status of the led. When our switch is 1, our led will turn on and when our switch is 0, our led will turn off.


We can make pin assignments in 2 ways.


1. WAY


Click on the box that says Add source.



Click Add or create restrictions.



Select the add box. Select the master3 base card pin assignment file on your computer.


Check the copy option in the lower box before saying finish. Otherwise, our original file will be corrupted.




Click on design source data constraints and check the Basys3 motherboard we added there.


Double click and create the required pins to assign.


All pins will come as comment information. Remove the comment line of the pins you will use.


Replace the variable name here with the variable name used while writing the code



## Switches
set_property PACKAGE_PIN V17 [get_ports {sw[0]}]					
	set_property IOSTANDARD LVCMOS33 [get_ports {sw[0]}]
set_property PACKAGE_PIN V16 [get_ports {sw[1]}]					
	set_property IOSTANDARD LVCMOS33 [get_ports {sw[1]}]

## LEDs
set_property PACKAGE_PIN U16 [get_ports {led[0]}]					
	set_property IOSTANDARD LVCMOS33 [get_ports {led[0]}]

NOTE:

sw[0]
led[0]

Since our sw and led are 1 bit, we need to arrange it as follows.


It means to get the bit written in expression [0]. But since our led and sw variable is only 1 bit, we only need to define it as sw and led.


## Switches
set_property PACKAGE_PIN V17 [get_ports {sw}]					
	set_property IOSTANDARD LVCMOS33 [get_ports {sw}]
set_property PACKAGE_PIN U16 [get_ports {led}]					
	set_property IOSTANDARD LVCMOS33 [get_ports {led}]

Let's select the Run implementation option from the left side section.


If we do not get an error, our code is correct and we can choose the generate bitstream option.


Now we can upload our code to the card.


We can upload our code to the card by saying Auto connect program device.


2. WAY


After writing our code without doing any of the above steps, we choose the Run implementation.


If we don't get any errors, we can skip the next steps.


If we get an error, let's correct the mistake in the code and rerun the Run implementation again.


1)



2)




If you don't see their changed names, move the cursor a little and type the pin names manually.


3)


4)



Let's choose the run implementation.

If we don't get an error, our code is correct.

Let's choose your Generate bitstream option.

Now we can upload our code to the card.

We can upload our code to the card by saying the Auto connect program device.



Basys3 card pin assignments;




Sometimes there is an error while uploading our code to the card.

Unplug the card.

Plug the card on computer again.

Select the auto connect option again and we can upload it to the card.



And we have completed our first code in VHDL language. See you in different codes in other series :)










 
 
 

Comments


SUBSCRIBE VIA EMAIL

  • Facebook
  • LinkedIn
  • Twitter
  • Instagram

Thanks for submitting!

© 2035 by Salt & Pepper. Powered and secured byWix

bottom of page