Experiment 1: Write VHDL Code For Realize All Logic Gates.
Experiment 1: Write VHDL Code For Realize All Logic Gates.
a) AND Gate: A Logic circuit whose output is logic ‘1’ if and only if all of its inputs
are logic ‘1’.
Truth table Logic diagram
Inputs Output
A 2
A B Y
1 Y
0 0 0 B 3
0 1 0
1 0 0 AND2
1 1 1
Y = A AND B
= A.B
y
b)OR Gate: A logic gate whose output is logic ‘0’ if and only if all of its inputs are
logic ‘0’.
Truth table Logic diagram
Inputs Output
A 2
A B Y 1 Y
0 0 0 B 3
0 1 1
1 0 1 OR2
1 1 1
Y = A OR B
=A+B
VHDL Code for OR Gate:
-------------------------------------------------------------------------------
-- File : orgate.vhd
-- Entity : orgate
-------------------------------------------------------------------------------
-- University : Vishweswaraia Technological University
Belgaum,Karnataka
-- Simulators : Mentor Graphics Modelsim OR Active HDL
-- Synthesizers : Xilinx ISE
-- Target Device : XC4000 Series
-------------------------------------------------------------------------------
-- Description : VHDL code to realize OR gate functionality
-------------------------------------------------------------------------------
--The IEEE standard 1164 package, declares std_logic, etc.
library IEEE;
use IEEE.std_logic_1164.all;
use IEEE.std_logic_arith.all;
use IEEE.std_logic_unsigned.all;
---------------------------------- Entity Declarations -------------------------
entity orgate is
Port( A : in std_logic;
B : in std_logic;
Y : out std_logic
);
end orgate;
y
c) NOT Gate: A logic gate whose input is complement of its input.
Input Output
A Y A Y
0 1
1 0 INV
Y = NOT A
y
d) NAND Gate: A logic gate which gives logic ‘0’ output if and only if all of its
inputs are logic ‘1’
Truth table Logic diagram
Inputs Output
A B Y A 2
1 Y
0 0 0 B 3
0 1 1
1 0 1 NAND2
1 1 1
Y= A NAND B
=(A. B)\
y
e) NOR Gate: A logic gate whose output logic ‘1’ if and only if all of its inputs are
logic ‘0’
Truth table Logic diagram
Inputs Output
A B Y A 2
0 0 0 1 Y
0 1 1 B 3
1 0 1 NOR2
1 1 1
Y= A NOR B
=(A+ B)\
y
f) EX-OR (Exclusive OR): A logic gate whose output is logic ‘0’ when all the inputs
are equal and logic ‘1’ when they are un equal.
Truth table Logic diagram
Inputs Output
A B Y A 2
0 0 0 1 Y
0 1 1 B 3
1 0 1 XOR2
1 1 0
Y= A EX-OR B
= A (+)B
= A.B\ + A\.B
y
g) EX-NOR (Exclusive -NOR) gate: A logic gate that prodices a logic ‘1’ only when
the two inputs are equal
Truth table Logic diagram
Inputs Output
A B Y A 2
0 0 0 1 Y
0 1 1 B 3
1 0 1
XNOR2
1 1 0
Y= A XNOR B
= (A (+)B)\
= (A.B)\ + A.B
y
Experiment 2: Write a VHDL program for the following combinational designs.
a) 2 to 4 decoder: A decoder is a digital logic circuit that converts n-bits binary input
code in to M output lines. OR It is a logic circuit that decodes from binary to octal,
decimal, Hexa-decimal or any other code such as 7-segment etc.
EN SEL(1) SEL(0)
INST1 INST2
INV INV
U5
2
3
4
1 D0
NAND3
U6
2
3
4
1 D1
NAND3
U8
2
3
4
1 D3
NAND3
Logic Diagram of 2:4 Decoder
EN Inputs Output
Sel(1) Sel(0) D
1 X X 0
0 0 0 D0
0 0 1 D1
0 1 0 D2
0 1 1 D3
Truth table
-------------------------------------------------------------------------------
-- File : decoder24.vhd
-- Entity : decoder24
-------------------------------------------------------------------------------
-- University : Vishweswaraia Technological University
Belgaum,Karnataka
-- Simulators : Mentor Graphics Modelsim Or Active HDL
-- Synthesizers : Xilinx ISE
-- Target Device : XC4000 Series
-------------------------------------------------------------------------------
-- Description : 2 to 4 DECODER
-------------------------------------------------------------------------------
--The IEEE standard 1164 package, declares std_logic, etc.
library IEEE;
use IEEE.std_logic_1164.all;
use IEEE.std_logic_arith.all;
use IEEE.std_logic_unsigned.all;
---------------------------------- Entity Declarations -------------------------
entity decoder24 is
generic(
N: INTEGER :=2;
M: INTEGER :=4 );
port (
EN : in STD_LOGIC;
SEL: in STD_LOGIC_VECTOR (N-1 downto 0);
D: out STD_LOGIC_VECTOR (M-1 downto 0) );
end decoder24;
architecture decoder24_arch of decoder24 is
signal aux: INTEGER;
begin
aux<=conv_integer(SEL);
process(EN,aux)
begin
if (EN=' 1'
) then
for i in 0 to M-1 loop
if aux=i then
D(i)<=' 1';
else
D(i)<=' 0';
end if;
end loop;
else
for i in 0 to M-1 loop
D(i)<=' 0';
end loop;
end if;
end process;
end decoder24_arch;
Simulator Waveforms for 2:4 Decoder:
500 10 0 0 15 0 0 2000 2500 ns
EN
SEL 0 1 2 3 0 1 2 3 0 1 2
D (3 )
D (2 )
D (1 )
D (0 )
b) 8 to 3 (Encoder without & with priority)
Encoder: A logic circuit that produces coded binary outputs from uncoded inputs.
Priority encoder: Whenever two or more inputs are applied at a time, internal
hardware will check this condition and if the priority is set such that higher numbered
input should be taken into account and remaining are considered as don’t care then
output code will be appear will be “higher numbered input”.
x 05 0A 0F 14 19 1E 23 28 2D 32
x(7)
x(6)
x(5)
x(4)
x(3)
x(2)
x(1)
x(0)
y 2 3 4 5
y(2)
y(1)
y(0)
A
c) 8 :1 Multiplexer: The multiplexer is a combinational circuit which accepts several data
inputs and allows only one of them AT A TIME to get through to the output.
EN SEL(2) SEL(1) SEL(0)
ENABLE INV1
INV3 INV2
2 U1
3
4 1
D0
5
D0 6
AND5
D1 0 EN
2 U2
3
4 1
1
5
D1 6
D2 2 2
3
AND5
U3
D3 3 D2
4
5
6
1
O/PData Output
AND5
Data D4 4 2 U4
8:1
3 2 U9
Inputs 5 Y 4 1 3
D5
5 4
D3 6 5
Y
6 AND5 1
6
D6
2 U5 7
7
3 8
4 1 9
5
D4 6
OR8
D7 2
3
AND5
U6
4 1
5
D5 6
AND5
2 U7
3
SEL0
4 1
5
D6 6
AND5
SEL1 2
3
4
5
U8
D7
SEL2
6
AND5
Control Inputs
Block Diagram of 8:1 Mux Logic Diagram
end mux8_1_arch;
Simulator Waveforms for 8:1 Multiplexer:
D(7)
D(6)
D(5)
D(4)
D(3)
D(2)
D(1)
D(0)
EN
SEL 0 1 2 3 4 5 6 7
Y
d) 4-bit Binary to Gray converter.
Binary –It is a number system, which has only two states ‘0’ (high) and ‘1’ (low)
Gray- In Gray code “ Every new code differs from the previous interms of single
bit” only one bit changes between successive numbers.
Decimal
Binary Gray B3 G3
0 0000 0000 U3
1 0001 0001
2 0010 0011 B2
2
3
1 G2
3 0011 0010
4 0100 0110 XOR2
5 0101 0111 U2
6
7
0110
0111
0101
0100 B1
2
3
1 G1
8 1000 1100
XOR2
9 1001 1101
10 1010 1111 U1
11 1011 1110
B0
2
1 G0
12 1100 1010 3
13 1101 1011 XOR2
14 1110 1001
15 1111 1000
------------------------------------------------------------------------------
-- File :b2g.vhd
-- Entity :b2g
-------------------------------------------------------------------------------
-- University :Vishweswaraia Technological University
Belgaum,Karnataka
-- Simulators :Mentor Graphics Modelsim
-- Synthesizers :Xilinx ISE
-- Target Device :XC4000 Series
-------------------------------------------------------------------------------
-- Description : 4-BIT BINARY TO GRAY CONVERTOR
-------------------------------------------------------------------------------
begin
G3<=B3;
G2<=B2 xor B3;
G1<=B1 xor B2;
G0<=B0 xor B1;
end b2g_arch;
B 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111
G 0000 0001 0011 0010 0110 0111 0101 0100 1100 1101 1111 1110 1010 1011 1001 1000
e) Multiplexer, Demultiplexer, comparator.
0 D0 2
3
4
U1
1 D0
EN 1 D1 Y 5
6
AND5
2 D2
2 U2
D1
3
4 1
5
D3
6
U3
D2
3
Demux 4 D4 4
5
1
Y
6
AND5
5 D5 2
3
4
U4
1 D3
D6
5
6 6
AND5
D7
2 U5
7 D4
3
4 1
5
6
AND5
2 U6
3
4 1 D5
Data Outputs 5
SEL0
6
AND5
2 U7
3
D6
SEL1 4
5
6
AND5
1
SEL2 2
3
4
U8
1 D7
5
Control Input 6
AND5
Truth Table
EN CONTROL INPUTS OUTPUTS
SEL(3) SEL(3) SEL(3)
0 X X X 0
1 0 0 0 D0=Y
1 0 0 1 D1=Y
1 0 1 0 D2=Y
1 0 1 1 D3=Y
1 1 0 0 D4=Y
1 1 0 1 D5=Y
1 1 1 0 D6=Y
1 1 1 1 D7=Y
-------------------------------------------------------------------------------
-- File : demux.vhd
-- Entity : demux
-------------------------------------------------------------------------------
-- University : Vishweswaraia Technological University
Belgaum,Karnataka
-- Simulators : Mentor Graphics Modelsim
-- Synthesizers : Xilinx ISE
-- Target Device : XC4000 Series
-------------------------------------------------------------------------------
-- Description : 1:8 DEMULTIPLEXOR
-------------------------------------------------------------------------------
--The IEEE standard 1164 package, declares std_logic, etc.
library IEEE;
use IEEE.std_logic_1164.all;
use IEEE.std_logic_arith.all;
use IEEE.std_logic_unsigned.all;
---------------------------------- Entity Declarations -------------------------
entity demux8_1 is
port ( Y : in STD_LOGIC;
EN : in STD_LOGIC;
SEL : in STD_LOGIC_VECTOR (2 downto 0);
D : out STD_LOGIC_VECTOR (7 downto 0) );
end demux8_1;
end demux8_1_arch;
Simulator Waveforms for 1: Demultiplexer:
EN
SEL 0 1 2 3 4 5 6 7
D 00 02 0A 2A AA
D(7)
D(6)
D(5)
D(4)
D(3)
D(2)
D(1)
D(0)
Comparator: A circuit that compares two numbers and produces an output
indicating whether they are equal. It may also indicate which number is greater if they
are unequal. Ex: ‘1’ bit comparator
Truth table:
Comparing Outputs
inputs
A B Y=(A>B) Y=(A<B) Y=(A=B)
0 0 0 0 1
0 1 0 1 0
1 0 1 0 0
1 1 0 0 1
A 2
U1
1
AB\
Y(A<B)
3
INV1
AND2
U3
2
3
1 Y(A=B)
NOR2
U2
B
2
3
1 Y(A>B)
AND2 A\B
INV2
-------------------------------------------------------------------------------
-- File : bitcomp.vhd
-- Entity : bitcomp
-------------------------------------------------------------------------------
-- University : Vishweswaraia Technological University
Belgaum,Karnataka
-- Simulators : Mentor Graphics Modelsim
-- Synthesizers : Xilinx ISE
-- Target Device : XC4000 Series
-------------------------------------------------------------------------------
-- Description : SINGLE BIT MAGNITUDE COMPARATOR.
-------------------------------------------------------------------------------
end bitcomp_arch;
Simulator Waveforms for SINGLE BIT MAGNITUDE
COMPARATOR:
500 1000 1500 2000 2500 3000 3500 ns
sel 0 1 2 3 0
Truth table for Full adder Truth table for Half adder
AND2 OR3
U4
2
1 A.B + B.Cin + A.Cin
3
AND2
-- File : HA.vhd
-- Entity : HA
-- Architecture : HA_arch
-------------------------------------------------------------------------------
-- University : Vishweswaraia Technological University
Belgaum,Karnataka
-- Simulators : Mentor Graphics Modelsim
-- Synthesizers : Xilinx ISE
-- Target Device : XC4000 Series
-------------------------------------------------------------------------------
-- Description : HALF ADDER.
-------------------------------------------------------------------------------
entity HA is
port(
A,B : in STD_LOGIC;
S,CY : out STD_LOGIC
);
end HA;
architecture HA_arch of HA is
begin
S<= A XOR B;
CY<= A AND B;
end HA_arch;
VHDL Code for FULL ADDER
-- File : FA.vhd
-- Entity : FA
-- Architecture : FA_arch
-------------------------------------------------------------------------------
-- University : Vishweswaraia Technological University
Belgaum,Karnataka
-- Simulators : Mentor Graphics Modelsim
-- Synthesizers : Xilinx ISE
-- Target Device : XC4000 Series
-------------------------------------------------------------------------------
-- Description : FULL ADDER.
-------------------------------------------------------------------------------
architecture STRUCTURAL of FA is
signal sum1,cy1,cy2:std_logic;
component HA
port(
A,B : in STD_LOGIC;
S,CY : out STD_LOGIC
);
end component ;
begin
u1: HA port map(A=>A, B=>B, S=>SUM1, CY=>CY1);
u2: HA port map(A=>SUM1, B=>Cin, S=>SUM, CY=>CY2);
end STRUCTURAL;
-- DATAFLOW MODELING-A set of concurrent assignment statements.
architecture DATAFLOW of FA is
begin
SUM<= A XOR B XOR Cin;
CARRY<= (A AND B) OR (Cin AND A)OR (Cin AND B);
end DATAFLOW;
architecture BEHAVIOR of FA is
begin
process(A,B,Cin)
begin
SUM<= A XOR B XOR Cin;
CARRY<= (A AND B) OR (Cin AND A)OR (Cin AND B);
end process;
end BEHAVIOR;
Simulator waveforms of HALF ADDER
1 2 3 4 5 6 7 8 9 us
SUM
CARRY
Cin
SUM
CARRY
Experiment 4: Write a model for 32 bit ALU using the schematic diagram
Shown below example
ALU should use the combinational logic to calculate an output based on the four
bit Opcode input.
ALU should pass the result to the out bit when enable line is high and tri-state
when low enable.
ALU should decode the 4-bit op-code according to the given in example below
Opcode ALU operation
1 A+B
2 A–B
3 A Complement
4 A*B
5 A AND B
6 A OR B
7 A NAND B
8 A XOR B
ALU is logic circuit which is able to perform different arithmetic and logical
function basically ALU is the heart of central processing unit (CPU).
Mode
Note: For A*B, A&B lower 16 bit [15:0] can be taken in to consideration.
VHDL Code for 8_bit ALU:
-- File : alu.vhd
-- Entity : alu
-------------------------------------------------------------------------------
-- University : Vishweswaraia Technological University
Belgaum,Karnataka
-- Simulators : Mentor Graphics Modelsim
-- Synthesizers : Xilinx ISE
-- Target Device : XC4000 Series
-------------------------------------------------------------------------------
-- Description : 8-bit ALU.
-------------------------------------------------------------------------------
--The IEEE standard 1164 package, declares std_logic, etc.
library IEEE;
use IEEE.Std_Logic_1164.all;
use IEEE.NUMERIC_STD.all;
---------------------------------- Entity Declarations -------------------------
-- NOTE : JUST BY CHANGING THE WIDTH OF INPUT AND OUTPUT
OF (31 DOWNTO O)
-- WILL BECOME 32-BIT ALU
--------------------------------------------------------------------------------
entity Alu is
port( Clk : in Std_Logic;
MODE,EN: in Std_Logic;
A,B : in Std_Logic_Vector(7 downto 0);
OPCODE : in Std_Logic_Vector(3 downto 0);
Y : out Std_Logic_Vector(7 downto 0));
end Alu;
process
begin
wait until Clk'
event and Clk = '
1';
y <= Std_Logic_Vector(C_s);
end process ;
end Alu_a;
Clk
MODE
EN
A 08
B 04
OPCODE 0 1 2 3 4 3 4 5 6 7 8 9 A B C D
Y UU 04 20 00 F7 FB 00 FF 0C F3 0C F3 00
Experiment 5: Develop the VHDL code for the following flip-flops SR, D, JK & T.
Set Set
? Q ? Q
o Clk
Clk
? Q\ ? Q\
Rst Rst
o o
Positive
Edge-Triggered
-
Negative Edge Triggered
In a memory device set and Reset is often required for synchronization of the
device in such case S-R Flip-flop is need & this is refereed as clocked set-reset.
S R Q+ Action
0 0 Q No
Change
0 1 0 Reset
1 0 1 Set
1 1 - Illegal
VHDL Code for SR Flip-Flop:
-------------------------------------------------------------------------------
-- File : SRFF.vhd
-- Entity : SRFF
-------------------------------------------------------------------------------
-- University : Vishweswaraia Technological University
Belgaum,Karnataka
-- Simulators : Mentor Graphics Modelsim Or Active HDL
-- Synthesizers : Xilinx ISE
-- Target Device : XC4000 Series
-------------------------------------------------------------------------------
-- Description : SR Flip-Flop
-------------------------------------------------------------------------------
--The IEEE standard 1164 package, declares std_logic, etc.
library IEEE;
use IEEE.std_logic_1164.all;
use IEEE.std_logic_arith.all;
use IEEE.std_logic_unsigned.all;
---------------------------------- Entity Declarations -------------------------
entity SRFF is
port (
CLK, RST, S, R : in std_logic;
Q, Qn : out std_logic);
end SRFF;
architecture RTL of SRFF is
signal FF : std_logic;
begin
process (CLK, RST)
variable SR : std_logic_vector(1 downto 0);
begin
if (RST = ' 0') then
FF <= ' 0';
elsif (CLK' event and CLK = ' 1'
) then
SR := S & R;
case SR is
when "01" => FF <= ' 0';
when "10" => FF <= ' 1';
when "11" => FF <= ' Z' ;
when others => FF <= FF;
end case;
end if;
end process;
Q <= FF ;
Qn <= not FF ;
end RTL;
Simulator waveforms for SR Flip-Flop:
200 400 600 800 1000 1200 1400 ns
CLK
RST
Qn
0 0 Reset
1 1 Set
clk
data
reset
QB
c) J.K Flip-flop:
The race conditions in S-R Flip-flop can be eliminated by converting it in to
J.K, the data inputs J and K are ANDed with Q\ and Q to obtain S & R inputs.
Here SR, T, or D depending on inputs.
S=J.Q\
R=K.Q
Logic diagram:
0 0 Q No Change
0 1 0 Reset
1 0 1 Set
1 1 Q Toggle
library IEEE;
use IEEE.std_logic_1164.all;
use IEEE.std_logic_arith.all;
use IEEE.std_logic_unsigned.all;
---------------------------------- Entity Declarations -------------------------
entity JKFF is
port (
CLK, RST, J, K : in std_logic;
Q, Qn : out std_logic);
end JKFF;
architecture RTL of JKFF is
signal FF : std_logic;
begin
process (CLK, RST)
variable JK : std_logic_vector(1 downto 0);
begin
if (RST = '0') then
FF <= ' 0';
elsif (CLK'event and CLK = ' 1') then
JK := J & K;
case JK is
when "01" => FF <= ' 0'
;
when "10" => FF <= ' 1'
;
when "11" => FF <= not FF;
when others => FF <= FF;
end case;
end if;
end process;
Q <= FF ;
Qn <= not FF ;
end RTL;
CLK
RSTn
Qn
d) T-Flip-flop (Toggle Flip-flop): On every change in clock pulse the output ‘Q’
changes its state (Toggle). A Flip-flop with one data input which changes state for
every clock pulse.(J=K=’1’ in JQK Flip-flop the resulting output is ‘T’ Flip-flop).
T-F/F Truth table
Q+
T Action
0 Q No Change
1 Q Toggle
library IEEE;
use IEEE.std_logic_1164.all;
use IEEE.std_logic_arith.all;
use IEEE.std_logic_unsigned.all;
---------------------------------- Entity Declarations -------------------------
entity tff is
port (T, CLK, RST : in std_logic;
Q,QB : out std_logic);
end tff;
CLK
RST
QB
U5
2 U3
1 S 2 U1
J 3 1 2
3 1 Q
AND2 3
INST1 NAND2
C1 NAND2
CLK
R2
R1 INV U2
U4 2
U6 2 1 Q\
K 2 1 3
1 R 3
3 NAND2
NAND2
AND2
COUNTER: Counter is a digital circuit that can counts the member of pulse for building
the counters, Flip-flop are used.
Relation between number of Flip-flop used and number of state of counter is
(Regular/binary counter).
Synchronous Counter:
end graycnt;
Waveforms for 4-Bit Binary Up Counter
200 400 600 800 1000 1200 1400 1600 1800 2000 ns
clock
reset
qout 0 1 2 3 4 5 6 7 8 9 A B C D E F 0 1 2 3 4
qout(3)
qout(2)
qout(1)
qout(0)
clock
reset
qout 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
qout(3)
qout(2)
qout(1)
qout(0)
clock
reset
binaryout 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111
grayout 0000 0001 0011 0010 0110 0111 0101 0100 1100 1101 1111 1110 1010 1011 1001 1000
Experiment 7: Write VHDL code to display messages on the given seven-
segment display interface.
7-Segment display can display the digits 0-9 and the hex extension (A-F). A
signal-character displays bring out leads for 7-segments & the common elect code (Common
cathode & common anode).Here in FPGA/CPLD board to interface one 7-segment LED
display whose elements are connected to any I/O pins of the FPGA/CPLD.
Here we can consider common-anode 7-segment LED displays. The user can
then ON by driving associated signal low.
a b c d e f g
Binary
0000 0 0 0 0 0 0 1
0001 1 0 0 1 1 1 1
0010 0 0 1 0 0 1 0
0011 0 0 0 0 1 1 0
0100 1 0 0 1 1 0 0
0101 0 1 0 0 1 0 0
0110 0 1 0 0 0 0 0
0111 0 0 0 1 1 0 1
1000 0 0 0 0 0 0 0
1001 0 0 0 0 1 0 0
1010 0 0 0 1 0 0 0
1011 1 1 0 0 0 0 0
1100 0 1 1 0 0 0 1
1101 1 0 0 0 0 1 0
1110 0 1 1 0 0 0 0
1111 0 1 1 1 0 0 0
Interfacing Diagram:
Turning on an LED:
Anode Current
light
+3.3V/
+1.7V +0.2V
+5V GAL output pin
R 0
LED
voltage 5 - 1.7
R= = = 220 ohms
current 15 x 10-3
(For +5V)
Note: If your Seven-segment LED is having Digit point, Then turn on with giving Logic ‘1’ to
dig.pt permanently when particular Seven-segment is enabled.
VHDL Code for 7-Segment Displays:
----------------------------------------------------------------------------------
-- File : seg7dec.vhd
-- Entity : seg7dec
-------------------------------------------------------------------------------
-- University : Vishweswaraia Technological University
-- Belgaum,Karnataka
-- Simulators : Mentor Graphics Modelsim Or Active HDL
-- Synthesizers : Xilinx ISE
-- Target Device : XC4000 Series
-------------------------------------------------------------------------------
-- Description : 7-Segment Decoder
-------------------------------------------------------------------------------
--The IEEE standard 1164 package, declares std_logic, etc.
library IEEE;
use IEEE.std_logic_1164.all;
use IEEE.std_logic_arith.all;
use IEEE.std_logic_unsigned.all;
---------------------------------- Entity Declarations -------------------------
entity seg7dec is
port ( Q : in STD_LOGIC_VECTOR(3 downto 0);
AtoG : out STD_LOGIC_VECTOR(6 downto 0)
);
end seg7dec;
architecture seg7dec_arch of seg7dec is
begin
process(Q)
begin
case Q is
when "0000" => AtoG <= "0000001";
when "0001" => AtoG <= "1001111";
when "0010" => AtoG <= "0010010";
when "0011" => AtoG <= "0000110";
when "0100" => AtoG <= "1001100";
when "0101" => AtoG <= "0100100";
when "0110" => AtoG <= "0100000";
when "0111" => AtoG <= "0001101";
when "1000" => AtoG <= "0000000";
when "1001" => AtoG <= "0000100";
when "1010" => AtoG <= "0001000";
when "1011" => AtoG <= "1100000";
when "1100" => AtoG <= "0110001";
when "1101" => AtoG <= "1000010";
when "1110" => AtoG <= "0110000";
when others => AtoG <= "0111000";
end case;
end process;
end seg7dec_arch;
Simulator waveforms for 7-Segment Displays:
10 20 30 40 50 60 70 80 90 ns
q 0 1 2 3 4 5 6 7 8 9 A B C D E F
AtoG 01 4F 12 06 4C 24 20 0D 00 04 08 60 31 42 30 38
AtoG(6)
AtoG(5)
AtoG(4)
AtoG(3)
AtoG(2)
AtoG(1)
AtoG(0)
Experiment 8: Write a VHDL codes to display messages on given LCD panel.
The LCD should be initialize before displaying any characters, the initialization procedures
for my LCD driver are:
The ASCII representations of each individual character are being entered into the program and
displayed on to the LCD screen
6 R1
1 VSS
1
7-14
CPLD/
FPGA LCD 20*4
Pin out of LCD and Description of each pins:
Pin Signal
Input/Output Function
No. Name
1 VSS Input Ground
2 VDD Input +5V
3 VEE Input Contrast Adjust
Signal to select registers
'
0': instruction register (for write)
4 RS Input Busy flag : address counter
(for read)
'
1': Data register (for read and write)
Signal to select read (R) and write (W)
5 R/W Input '
0': Write
'
1': Read
6 Enable Input Operation start signal for data read/write
8 bit bidirectional three-state data bus lines.
7-14 DB0-DB7 Input/Output Used for data transfer between FPGA and
LCD
Execution
Instruction RS R/W DB7 DB6 DB5 DB4 DB3 DB2 DB1 DB0 Description Time
(Max)
Clear entire display
Clear and sets DD RAM
0 0 0 0 0 0 0 0 0 1 1.64 ms
Display address 0 in address
counter.
Sets DD RAM
address 0 in address
counter. Also returns
Return
0 0 0 0 0 0 0 0 1 X display being shifted 1.64 ms
Home to original position.
DD RAM contents
remain unchanged.
Sets cursor move
direction and
specifies shift of
Entry
0 0 0 0 0 0 0 1 I/D S display. These 40 us
Mode Set operations are
performed during
data write and read.
Sets ON/OFF of
Display entire display (D),
On/Off 0 0 0 0 0 0 1 D C B cursor ON/OFF (C), 40 us
Control and blink of cursor
position character
(B).
• I set signal "count" count from 0 to 3. However, this value is depend on the system
clock. Make sure the LCD has enough time to execute the next instruction.
Execution time for each instruction(Follow the instruction table).
---------------------------------------------------------------------------------------------------
-- File : lcd_init.vhd
-- Entity : lcd_init
---------------------------------------------------------------------------------------------------
-- University : Vishweswaraia Technological University
Belgaum,Karnataka
-- Simulators : Mentor Graphics Modelsim
-- Synthesizers : Xilinx ISE
-- Target Device : XC4000 Series
------------------------------------------------------------------------------------------------
-- Description : This program initialize the LCD display
-----------------------------------------------------------------------------------------------
--The IEEE standard 1164 package, declares std_logic, etc.
library IEEE;
use IEEE.Std_Logic_1164.all;
use IEEE.Std_Logic_unsigned.all;
use IEEE.NUMERIC_STD.all;
---------------------------------- Entity Declarations -------------------------
entity lcd_init is
end lcd_init;
begin
running : process(state,enable,done_ack,count) is
begin
case state is
if enable = '
1'then
next_state <= i0;
else
next_state <= waiting;
end if;
when i0 =>
Qout_temp <= "00000001"; --clear display
when i1 =>
Qout_temp <= "00000010"; --clear display & returns to HOME
if count = "11" then
next_state <= i2;
else
next_state <= i1;
end if;
when i2 =>
Qout_temp <= "00111000"; --2 line display
when i3 =>
Qout_temp <= "00001110"; --truns on display with cursor at home
if done_ack = '1'then
next_state <= waiting;
else
next_state <= donestate;
end if;
end case;
timing : process(clock,reset) is
begin
if rising_edge(clock) then
Qout <= Qout_temp;
count <= count_temp;
if reset = '1'then
state <= waiting;
count_temp <= "00";
else
state <= next_state;
count_temp <= count_temp + "01";
end if;
end if;
end initial;
• Simulator waveforms for LCD display Initialization:
200 400 600 800 1000 1200 1400 1600 ns
clock
reset
enable
done_ack
done
RW
rs
lcd_e
Qout 00 01 02 38 0E 00
count U 0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3 0 1
count_temp 0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3 0 1 2
Qout_temp 00 01 02 38 0E 00
Experiment 9: Write codes to operate the given stepper motor.
CPLD/FPGA
Note: Please go through the datasheet or Voltage and Current ratings of stepper
motor and
Decide The transistor selection depends on drive current, power
dissipation, and gain.(Preferably NPN Power Darlington transistors).
The series resistors should be selected to limit the FPGA current to 8 mA
per output, 1/4 Watt resistors Value.
If step angle = 1.8, For One revolution 360/1.8=200 steps(In Full step mode)
If step angle = 0.9, For One revolution 360/0.9=400 steps(In Half step mode)
Bipolar stepper motor will not run in Half step mode for any consult the stepper
motor manufacturer.
Full step
Steps Q1 Q2 Q3 Q4
Step0 1 0 1 0
Step1 1 0 0 1
Step2 1 0 1 0
Step3 0 1 1 0
entity SMControl is
Port (Clk,Reset,E,Dir: in std_logic;
Sec : out std_logic_vector(3 downto 0)
);
end SMControl;
architecture Behavioral of SMControl is
Type States is (Step_0, Step_1, Step_2, Step_3);
Signal Next_State, Current_State : States;
begin
Process( Clk, Reset, Current_State )
Begin
if Reset = '1' then
Next_State <= Step_0;
elsif Clk'event and Clk = '1' then
Case Current_State is
When Step_0 =>
Sec <= x"A";
if E = '1' then
Next_State <= Step_0;
else
If DIR = '1' then
Next_State <= Step_1;
Else
Next_State <= Step_3;
End if;
end if;
When Step_1 =>
Sec <= x"9";
if E = '1' then
Next_State <= Step_1;
else
If DIR = '1' then
Next_State <= Step_2;
Else
Next_State <= Step_0;
End if;
end if;
When Step_2 =>
Sec <= x"5";
if E = '1' then
Next_State <= Step_2;
else
If DIR = '1' then
Next_State <= Step_3;
Else
Next_State <= Step_1;
End if;
end if;
When Step_3 =>
Sec <= x"6";
if E = '1' then
Next_State <= Step_3;
else
If DIR = '1' then
Next_State <= Step_0;
Else
Next_State <= Step_2;
End if;
end if;
When Others =>
Next_State <= Step_0;
end Case;
end if;
end Process;
Process(Clk)
Begin
if Clk'event and clk = '1' then
Current_State <= Next_State;
end if;
end process;
end Behavioral;
State machine Diagram according to step changes:
Clk
Reset
Dir
Sec 5 9 A 6 5 9 A 9 5 6
Sec(3)
Sec(2)
Sec(1)
Sec(0)
Next_State step_1 step_0 step_3 step_2 step_1 step_0 step_1 step_2 step_3 step_0
Current_State step_2 step_1 step_0 step_3 step_2 step_1 step_0 step_1 step_2 step_3 step_0