I looked all over the net for a code that will allow me to convert Binary to BCD. 8-O
Most circuits were either in Verilog or if VHDL it was a combinational circuit. :-(
yes, yes… combinational is faster.. :roll: but my professor asked me to code a Binary to BCD sequential vhdl code.
I know its stupid, tell me about it.. :-?

Binary to BCD Converter

Shift and Add-3 Algorithm (for 8 bits binary number)

    The principle is simple 

  1. Check the 4 bit numbers in the Hundreds, Tens, and Units. If they are greater or equal to 5, then add 3 to it.
  2. Shift the binary number left one bit.
  3. Repeat from the top. (if you are converting a 8 bit binary number, you do it 8 times; if 12 bit then 12 times. You get the idea, right? :-D )


Operation Hundreds Tens Units Binary
HEX F F
Start 1 1 1 1 1 1 1 1

Example 1: Convert hex E to BCD


Example 2: Convert hex FF to BCD

OPERATION H T U BINARY
HEX F F
Start 1 1 1 1 1 1 1 1
shift 1 0001 1111 1110
shift 2 0011 1111 1100
shift 3 0111 1111 1000
add 3 1010 1111 0000
shift 4 0001 0101 1111 0000
add 3 0001 1000 1111 0000
shift 5 0011 0001 1110 0000
shift 6 0110 0011 1100 0000
add 3 1001 0011 1100 0000
shift 7 0001 0010 0111 1000 0000
add 3 0001 0010 1010 1000 0000
shift 8 0010 0101 0101 0000 0000
BCD 2 5 5 0000 0000




LIBRARY ieee;
USE ieee.std_logic_1164.all;
USE ieee.numeric_std.all;
entity calculatorF_v_2 is
port ( CLK : in std_logic;
 
--PUSH BUTTONS PIN ASSIGNMENT
pbutt :in std_logic_vector(3 downto 0);
--SWITCH PIN ASSIGNMENT 
sw0 :in std_logic_vector(3 downto 0);
sw1 :in std_logic_vector(3 downto 0);
sw2 :in std_logic_vector(3 downto 0);
sw3 :in std_logic_vector(3 downto 0);
sw4 :in std_logic_vector(1 downto 0);
--YOU COULD HAVE ALSO WRITTEN 
-- sw: in std_logic_vector(17 downto 0);
--   that would have worked too. But I did another project, SO I just kept it like that. Quartus is a pain in the butt when assigning pin. Its so time consuming and stupid :P
 
 
--7SEGMENT PIN ASSIGNMENT
hex0:out std_logic_vector(6 downto 0);
hex1:out std_logic_vector(6 downto 0);
hex2:out std_logic_vector(6 downto 0);
hex3:out std_logic_vector(6 downto 0);
hex4:out std_logic_vector(6 downto 0);
hex5:out std_logic_vector(6 downto 0);
hex6:out std_logic_vector(6 downto 0);
hex7:out std_logic_vector(6 downto 0);
 
--LED PIN ASSIGNMENT
led :out std_logic_vector(17 downto 0) := "000000000000000000" );
 
end calculatorF_v_2;
 
architecture behavior of calculatorF_v_2 is
begin
 
--You cannot have variables here
my_proc: process (CLK)
--Variable assignment
variable random: std_logic_vector(17 downto 0) := "000000000000000000"; --18 BITS 
 
variable one: std_logic_vector(3 downto 0) := "0000";
variable ten: std_logic_vector(3 downto 0) := "0000";
variable hund: std_logic_vector(3 downto 0) := "0000";
variable thou: std_logic_vector(3 downto 0) := "0000";
variable tthou: std_logic_vector(3 downto 0) := "0000";
variable hthou: std_logic_vector(3 downto 0) := "0000";
 
begin
--ON RISING EDGE OF CLOCK, DO THIS FUNCTION
if (rising_edge(CLK)) then
 
	if (pbutt(2) = '0') then
			random := sw4 & sw3 & sw2 & sw1 & sw0;
 
			led <= random;
 
			for i in 0 to 17 loop
			--CHECKING IF THE NUMBER IS GREATER OR EQUAL TO 5
			-- IF YES, THEN ADD 3
			-- NOTE: THIS IS NOT THE MOST EFFICIENT WAY TO DO IT. But who cares :P!
			if (hthou >= "0101") then 
			hthou := std_logic_vector (unsigned(hthou) +3);
			end if;
 
			if (tthou >= "0101") then 
			tthou := std_logic_vector (unsigned(tthou) +3);
			end if;
 
			if (thou >= "0101") then 
			thou := std_logic_vector (unsigned(thou) +3);
			end if;
 
			if (hund >= "0101") then 
			hund := std_logic_vector (unsigned(hund) +3);
			end if;
 
			if (ten >= "0101") then 
			ten := std_logic_vector (unsigned(ten) +3);
			end if;
 
 
			if (one >= "0101") then 
			one := std_logic_vector (unsigned(one) +3);
			end if;		
 
		-- HERE I AM DOING THE SHIFTING WORK
			hthou := hthou(2 downto 0) & tthou(3);
			tthou := tthou(2 downto 0) & thou(3);
			thou := thou(2 downto 0)& hund(3);
			hund := hund(2 downto 0)& ten(3);
			ten := ten(2 downto 0)& one(3);
			one := one(2 downto 0)& random(17);
			random := std_logic_vector(unsigned(random) sll 1);
			end loop;
 
 
		--DISPLAYING THE VALUE ON THE 7SEG
		-- NOTE I KEPT THE WHOLE TABLE FOR HEXADECIMAL CONVERSION IF YOU NEED IT IN THE FUTURE OR SOMETHING
			CASE one IS  
			when "0000" => hex0 <= "1000000" ; -- 0
			when "0001" => hex0 <= "1111001" ; -- 1
			when "0010" => hex0 <= "0100100" ; -- 2 
			when "0011" => hex0 <= "0110000" ; -- 3
			when "0100" => hex0 <= "0011001" ; -- 4
			when "0101" => hex0 <= "0010010" ; -- 5
			when "0110" => hex0 <= "0000010" ; -- 6
			when "0111" => hex0 <= "1111000" ; -- 7
			when "1000" => hex0 <= "0000000" ; -- 8
			when "1001" => hex0 <= "0010000" ; -- 9
			when "1010" => hex0 <= "0001000" ; -- 10 or A
			when "1011" => hex0 <= "0000011" ; -- 11 or B
			when "1100" => hex0 <= "1000110" ; -- 12 or C
			when "1101" => hex0 <= "0100001" ; -- 13 or D
			when "1110" => hex0 <= "0000110" ; -- 14 or E
			when "1111" => hex0 <= "0001110" ; -- 15 or F 
			when others => hex0 <= "1111111" ; -- JUST TURN ON ALL LIGHTS
			end case;
 
			CASE ten IS 
			when "0000" => hex1 <= "1000000" ; 
			when "0001" => hex1 <= "1111001" ;
			when "0010" => hex1 <= "0100100" ;
			when "0011" => hex1 <= "0110000" ;
			when "0100" => hex1 <= "0011001" ;
			when "0101" => hex1 <= "0010010" ;
			when "0110" => hex1 <= "0000010" ;
			when "0111" => hex1 <= "1111000" ;
			when "1000" => hex1 <= "0000000" ;
			when "1001" => hex1 <= "0010000" ;
			when "1010" => hex1 <= "0001000" ;
			when "1011" => hex1 <= "0000011" ;
			when "1100" => hex1 <= "1000110" ;
			when "1101" => hex1 <= "0100001" ;
			when "1110" => hex1 <= "0000110" ;
			when "1111" => hex1 <= "0001110" ;
			when others => hex1 <= "1000000" ;
			end case;
 
			CASE hund IS 
			when "0000" => hex2 <= "1000000" ; 
			when "0001" => hex2 <= "1111001" ;
			when "0010" => hex2 <= "0100100" ;
			when "0011" => hex2 <= "0110000" ;
			when "0100" => hex2 <= "0011001" ;
			when "0101" => hex2 <= "0010010" ;
			when "0110" => hex2 <= "0000010" ;
			when "0111" => hex2 <= "1111000" ;
			when "1000" => hex2 <= "0000000" ;
			when "1001" => hex2 <= "0010000" ;
			when "1010" => hex2 <= "0001000" ;
			when "1011" => hex2 <= "0000011" ;
			when "1100" => hex2 <= "1000110" ;
			when "1101" => hex2 <= "0100001" ;
			when "1110" => hex2 <= "0000110" ;
			when "1111" => hex2 <= "0001110" ;
			when others => hex2 <= "1000000" ;
			end case;
 
			CASE thou IS 
			when "0000" => hex3 <= "1000000" ; 
			when "0001" => hex3 <= "1111001" ;
			when "0010" => hex3 <= "0100100" ;
			when "0011" => hex3 <= "0110000" ;
			when "0100" => hex3 <= "0011001" ;
			when "0101" => hex3 <= "0010010" ;
			when "0110" => hex3 <= "0000010" ;
			when "0111" => hex3 <= "1111000" ;
			when "1000" => hex3 <= "0000000" ;
			when "1001" => hex3 <= "0010000" ;
			when "1010" => hex3 <= "0001000" ;
			when "1011" => hex3 <= "0000011" ;
			when "1100" => hex3 <= "1000110" ;
			when "1101" => hex3 <= "0100001" ;
			when "1110" => hex3 <= "0000110" ;
			when "1111" => hex3 <= "0001110" ;
			when others => hex3 <= "1000000" ;
			end case;
 
			CASE tthou IS 
			when "0000" => hex4 <= "1000000" ; 
			when "0001" => hex4 <= "1111001" ;
			when "0010" => hex4 <= "0100100" ;
			when "0011" => hex4 <= "0110000" ;
			when "0100" => hex4 <= "0011001" ;
			when "0101" => hex4 <= "0010010" ;
			when "0110" => hex4 <= "0000010" ;
			when "0111" => hex4 <= "1111000" ;
			when "1000" => hex4 <= "0000000" ;
			when "1001" => hex4 <= "0010000" ;
			when "1010" => hex4 <= "0001000" ;
			when "1011" => hex4 <= "0000011" ;
			when "1100" => hex4 <= "1000110" ;
			when "1101" => hex4 <= "0100001" ;
			when "1110" => hex4 <= "0000110" ;
			when "1111" => hex4 <= "0001110" ;
			when others => hex4 <= "1000000" ;
			end case;
 
			CASE hthou IS 
			when "0000" => hex5 <= "1000000" ; 
			when "0001" => hex5 <= "1111001" ;
			when "0010" => hex5 <= "0100100" ;
			when "0011" => hex5 <= "0110000" ;
			when "0100" => hex5 <= "0011001" ;
			when "0101" => hex5 <= "0010010" ;
			when "0110" => hex5 <= "0000010" ;
			when "0111" => hex5 <= "1111000" ;
			when "1000" => hex5 <= "0000000" ;
			when "1001" => hex5 <= "0010000" ;
			when "1010" => hex5 <= "0001000" ;
			when "1011" => hex5 <= "0000011" ;
			when "1100" => hex5 <= "1000110" ;
			when "1101" => hex5 <= "0100001" ;
			when "1110" => hex5 <= "0000110" ;
			when "1111" => hex5 <= "0001110" ;
			when others => hex5 <= "1000000" ;
			end case;
 
one := "0000";
 ten:= "0000";
 hund := "0000";
 thou := "0000";
 tthou:= "0000";
 hthou:= "0000";				
	end if;
 
end if;
end process ;
 
end behavior;


AND if the code has helped you,

as a sign of gratitude, please press on an advertisement on this page. :oops: I promise you that there is NO virus or adware. They are simple google ads. It will bring me 5cent/click at absolute no cost to you.
The earnings will go into running the webserver. If there is any excess money, I usually send it off to an orphanage in Bangladesh.
Thankyou.