Adjunto el codigo de un convertidor binario a bcd de 12 bits,
el programa es un contador de 12 bits cada que se pulsa un push botton aumenta su valor en uno
la valor de la cuenta es mostrada en 4 display, el programa esta en vhdl
se utiliza la tarjeta basys 2 , para cualquier duda envienme un correo a tico_x@live.com.mx
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.Std_logic_arith.all;
use IEEE.std_logic_unsigned.all;
use IEEE.numeric_std.all;
entity bcd is
Port ( mclk,kl,up,clr : in STD_LOGIC;
punto : out std_logic;
y : out std_logic_vector (3 downto 0);
d : out STD_LOGIC_VECTOR (6 downto 0));
end bcd;
architecture Behavioral of bcd is
signal b : STD_LOGIC_VECTOR (11 downto 0);
signal unidades,decenas,centenas,millares,sal : STD_LOGIC_VECTOR (3 downto 0);
signal clk2 : std_logic;
signal qq : STD_LOGIC_VECTOR (10 downto 0);
signal count : STD_LOGIC_VECTOR (1 downto 0);
signal dd : STD_LOGIC_VECTOR (7 downto 0);
--********contador de 12 bits rango 0-40955**********
begin
process(kl, clr)
begin
if clr = '1' then
b <= (others => '0');
elsif rising_edge(kl) then
if (up='0')then
b <= b + 1;
else
b <= b - 1;
end if;
end if;
end process;
--**********convertidor binario a bcd para vizualizar en 4 display unidades decenas centenas millares**********
process(
variable z: STD_LOGIC_VECTOR (27 downto 0);
begin
for i in 0 to 27 loop
z(i) := '0';
end loop;
z(14 downto 3) := B;
for i in 0 to 8 loop
if z(15 downto 12) > 4 then
z(15 downto 12) := z(15 downto 12) + 3;
end if;
if z(19 downto 16) > 4 then
z(19 downto 16) := z(19 downto 16) + 3;
end if;
if z(23 downto 20) > 4 then
z(23 downto 20) := z(23 downto 20) + 3;
end if;
if z(27 downto 24) > 4 then
z(27 downto 24) := z(27 downto 24) + 3;
end if;
z(27 downto 1) := z(26 downto 0);
end loop;
unidades <= z(27 downto 24);
decenas <= z(23 downto 20);
centenas <= z(19 downto 16);
millares <= z(15 downto 12);
end process ;
process(mclk) begin
if rising_edge(mclk) then
qq <= qq + 1;
end if;
end process;
--***********bcd a 7 segmentos**********
process(sal) begin
case sal is
when X"0" => dd <= x"01"; --0
when X"1" => dd <= x"4f"; --1
when X"2" => dd <= x"12"; --2
when X"3" => dd <= x"06"; --3
when X"4" => dd <= x"4c"; --4
when X"5" => dd <= x"24"; --5
when X"6" => dd <= x"20"; --6
when X"7" => dd <= x"0d"; --7
when X"8" => dd <= x"00"; --8
when others => dd <= x"04"; --9
end case;
end process;
--***************multiplexion del display***********
process(count,unidades,decenas,centenas,millares,clk2)
begin
if rising_edge(clk2) then
count <= count + 1;
end if;
case count is
when "00" => sal <= unidades;
y <= x"7";
when "01" => sal <= decenas;
y <= x"b";
when "10" => sal <= centenas;
y <= x"d";
when others => sal <= millares;
y <= x"e";
end case;
end process;
clk2 <= qq(10);
punto <= '1';
d <=dd(6 downto 0 );
end Behavioral;