?? infrared_receive.vhd
字號(hào):
---------------------------------------頂層程序-----------------------------------------
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
entity infrared_receive is
port(clk,infrared_in: in std_logic;
key_in: in std_logic_vector(3 downto 0);
ringout,ledout: out std_logic;
light: out std_logic_vector(1 downto 0);
data_result: out std_logic_vector(3 downto 0);
user_num: out std_logic_vector(2 downto 0));
end infrared_receive;
architecture behav of infrared_receive is
signal clr: std_logic;
signal data: std_logic_vector(11 downto 0);
signal dataout,key_out: std_logic_vector(7 downto 0);
signal red_ring,red_led,key_ring,key_led: std_logic;
signal key_first: std_logic;
component check is
Port(clk,infrared_in:in std_logic;
clr:out std_logic);
end component;
component coding is
port(clk,infrared_in,clr: in std_logic;
data: out std_logic_vector(11 downto 0);
user_num: out std_logic_vector(2 downto 0);
red_ring: out std_logic);
end component;
component decode is
port(clk: in std_logic;
data: in std_logic_vector(11 downto 0);
data_out: out std_logic_vector(7 downto 0);
red_led: out std_logic);
end component;
component display is
port(clk,key_first,infrared_in: in std_logic;
data_out: in std_logic_vector(7 downto 0);
key_out: in std_logic_vector(7 downto 0);
data_result: out std_logic_vector(3 downto 0);
light: out std_logic_vector(1 downto 0));
end component;
component ring_led is
port(clk,red_ring,key_ring,red_led,key_led: in std_logic;
ringout,ledout: out std_logic);
end component;
component keyboard is
port(clk: in std_logic;
key_in: in std_logic_vector(3 downto 0);
key_out: out std_logic_vector(7 downto 0);
key_first,key_ring,key_led: out std_logic);
end component;
begin
u1:check port map(clk,infrared_in,clr);
u2:coding port map(clk,infrared_in,clr,data,user_num,red_ring);
u3:decode port map(clk,data,dataout,red_led);
u4:display port map(clk,key_first,infrared_in,dataout,key_out,data_result,light);
u5:ring_led port map(clk,red_ring,key_ring,red_led,key_led,ringout,ledout);
u6:keyboard port map(clk,key_in,key_out,key_first,key_ring,key_led);
end behav;
-----------------------------------1:檢測(cè)----------------------------------------
Library ieee;
Use ieee.std_logic_1164.all;
Use ieee.std_logic_unsigned.all; --要用到'+'運(yùn)算,故包含此程序包
Entity check is
Port(clk,infrared_in:in std_logic;
clr:out std_logic);
End check;
Architecture behav of check is
Type states is(T0,T1);
Signal state:states;
Signal cnt:std_logic_vector(4 downto 0); --用于高電平計(jì)數(shù)
Begin
Process(infrared_in,clk)
Begin
if (clk'event and clk='1')then
Case state is
when T0=>
clr<='0';
cnt<="00000";
if(infrared_in='1') then
state<=T1;
else
state<=T0;
end if;
when T1=>
cnt<=cnt+1; --如果高電平個(gè)數(shù)持續(xù)16個(gè)時(shí)鐘周期
if(infrared_in='1') then --就產(chǎn)生一個(gè)清零信號(hào)clr
if(cnt="10000") then --使12位寄存器清零
clr<='1';
state<=T0;
else state<=T1;
end if;
else state<=T0;
end if;
End Case;
End if;
End process;
End behav;
-----------------------------------2:編碼---------------------------------------
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
entity coding is
port(clk,infrared_in,clr: in std_logic;
data: out std_logic_vector(11 downto 0);
user_num: out std_logic_vector(2 downto 0);
red_ring: out std_logic);
end coding;
architecture behav of coding is
type states is(T0,T1,T2,T3,T4,T5);
signal state: states;
signal datareg12: std_logic_vector(11 downto 0);
signal cnt1: std_logic_vector(3 downto 0); --用于低電平計(jì)數(shù)
signal cnt2: std_logic_vector(3 downto 0); --用于接收紅外編碼位數(shù)計(jì)數(shù)
begin
process(clk,infrared_in,clr)
begin
if(clr='1') then
data<="000000000000"; --如有clr='1',則編碼輸出全為0
state<=T0;
elsif(clk'event and clk='1') then
case state is
when T0=> --開(kāi)始狀態(tài)
datareg12<="000000000000";
cnt1<="0000";
cnt2<="0000";
red_ring<='0';
if(infrared_in='0') then --有信號(hào)輸入,跳到T1狀態(tài)
state<=T1;
else
state<=T0; --沒(méi)有信號(hào)輸入則狀態(tài)不變
end if;
when T1=>
cnt1<=cnt1+1;
if(infrared_in='1') then --紅外輸入變?yōu)楦唠娖浇Y(jié)束計(jì)數(shù)
if(cnt1>=8) then --判斷0和1
state<=T3;
else
state<=T2;
end if;
else
state<=T1;
end if;
when T2=>
cnt2<=cnt2+1;
datareg12<=datareg12(10 downto 0)&'0'; --接收的是0
if(cnt2=11) then
state<=T4;
else
state<=T5;
end if;
when T3=>
cnt2<=cnt2+1;
datareg12<=datareg12(10 downto 0)&'1'; --接收的是1
if(cnt2=11) then --判斷是否接收完12位數(shù)據(jù)
state<=T4;
else
state<=T5;
end if;
when T4=>
data<=datareg12(11 downto 0); --輸出數(shù)據(jù)
user_num<=datareg12(11 downto 9) xor "111"; --顯示用戶碼
red_ring<='1'; --響鈴
state<=T0;
when T5 => --沒(méi)有接收完12位數(shù)據(jù),cnt1清零準(zhǔn)備接收
cnt1<="0000"; --下一位數(shù)據(jù),如有數(shù)據(jù)輸入跳到T1狀態(tài)
datareg12<=datareg12;
if(infrared_in='1') then
state<=T5;
else
state<=T1;
end if;
end case;
end if;
end process;
end behav;
-----------------------------------3:解碼---------------------------------------
library ieee;
use ieee.std_logic_1164.all;
entity decode is
port(clk: in std_logic;
data: in std_logic_vector(11 downto 0);
data_out: out std_logic_vector(7 downto 0);
red_led: out std_logic);
end decode;
architecture rtl of decode is
signal bcd_code: std_logic_vector(7 downto 0);
signal led_reg: std_logic;
begin
process(clk)
begin
if(clk'event and clk='1') then
case data is
when "111100100000" =>bcd_code<="00000001"; led_reg<='0'; --接收1,以此類推至18
when "111100010000" =>bcd_code<="00000010"; led_reg<='0';
when "111100001000" =>bcd_code<="00000011"; led_reg<='0';
when "111100000100" =>bcd_code<="00000100"; led_reg<='0';
when "111100000010" =>bcd_code<="00000101"; led_reg<='0';
when "111100000001" =>bcd_code<="00000110"; led_reg<='0';
when "111010100000" =>bcd_code<="00000111"; led_reg<='1'; --接收7
when "111010010000" =>bcd_code<="00001000"; led_reg<='1';
when "111010001000" =>bcd_code<="00001001"; led_reg<='1';
when "111010000100" =>bcd_code<="00010000"; led_reg<='1';
when "111010000010" =>bcd_code<="00010001"; led_reg<='1';
when "111010000001" =>bcd_code<="00010010"; led_reg<='1';
when "111001100000" =>bcd_code<="00010011"; led_reg<='1'; --接收13
when "111001010000" =>bcd_code<="00010100"; led_reg<='1';
when "111001001000" =>bcd_code<="00010101"; led_reg<='1';
when "111001000100" =>bcd_code<="00010110"; led_reg<='1';
when "111001000010" =>bcd_code<="00010111"; led_reg<='1';
when "111001000001" =>bcd_code<="00011000"; led_reg<='1';
when others =>bcd_code<=null; led_reg<='1';
end case;
end if;
end process;
data_out<=bcd_code(7 downto 0); --輸出8位BCD碼
red_led<=led_reg; --輸出亮燈信號(hào)
end rtl;
-----------------------------------4:數(shù)碼顯示---------------------------------------
library ieee;
use ieee.std_logic_1164.all;
entity display is
port(clk,key_first,infrared_in: in std_logic;
data_out: in std_logic_vector(7 downto 0);
key_out: in std_logic_vector(7 downto 0);
data_result: out std_logic_vector(3 downto 0);
light: out std_logic_vector(1 downto 0));
end display;
architecture behav of display is
signal cnt4: integer range 0 to 2; --用于循環(huán)顯示計(jì)數(shù)
signal data8: std_logic_vector(7 downto 0);--當(dāng)前要顯示的8位BCD碼
begin
p1: process(clk,key_first,infrared_in,key_out) is
begin
if(key_first='1') then --鍵盤(pán)優(yōu)先顯示
data8<=key_out(7 downto 0);
elsif(clk'event and clk='1') then
if(infrared_in='1') then
data8<=data_out(7 downto 0);
end if;
end if;
end process p1;
p2: process(clk,cnt4,data8) is
begin
if(clk'event and clk='1') then
if(cnt4=0) then
data_result<=data8(7 downto 4); --顯示十位
light<="10";
cnt4<=cnt4+1;
elsif(cnt4=1) then
data_result<=data8(3 downto 0); --顯示個(gè)位
light<="01";
cnt4<=cnt4+1;
elsif(cnt4=2) then
cnt4<=0; --回到初狀態(tài)為顯示下一個(gè)
end if; --數(shù)據(jù)作準(zhǔn)備
end if;
end process p2;
end behav;
------------------------------------5:響鈴及亮燈-----------------------------------
library ieee;
use ieee.std_logic_1164.all;
entity ring_led is
port(clk,red_ring,key_ring,red_led,key_led: in std_logic;
ringout,ledout: out std_logic);
end ring_led;
architecture behav of ring_led is
type states is(T0,T1);
signal state: states;
signal ledout_reg,ringout_reg: std_logic;
begin
process(clk,red_ring,red_led)
variable cnt3: integer range 0 to 50; --用于延時(shí)計(jì)數(shù)
begin
if(clk'event and clk='1') then
case state is
when T0=> --不響鈴,不亮燈狀態(tài)
ringout_reg<='0';
cnt3:=0;
ledout_reg<='1';
if(red_ring='1') then
state<=T1;
else
state<=T0;
end if;
when T1=> --響鈴和亮燈狀態(tài)
ringout_reg<='1';
if(red_led='0') then
ledout_reg<='0';
end if;
cnt3:=cnt3+1;
if(cnt3=49) then --判斷延時(shí)是否結(jié)束
state<=T0;
else
state<=T1;
end if;
end case;
end if;
end process;
ringout<=ringout_reg or key_ring; --響鈴輸出
ledout<=ledout_reg and key_led; --亮燈輸出
end behav;
---------------------------------6:鍵盤(pán)輸入---------------------------------------
library ieee;
use ieee.std_logic_1164.all;
entity keyboard is
port(clk: in std_logic;
key_in: in std_logic_vector(3 downto 0);
key_out: out std_logic_vector(7 downto 0);
key_first,key_ring,key_led: out std_logic);
end keyboard;
architecture rtl of keyboard is
signal key_bcd: std_logic_vector(7 downto 0);
signal key_H: std_logic;
signal key_ring_reg: std_logic;
signal key_led_reg: std_logic;
begin
process(clk,key_in)
begin
if(clk'event and clk='1') then
case key_in is
when "0111" => --按鍵是1
key_bcd<="00000001"; --輸出1的BCD碼
key_H<='1'; --鍵盤(pán)優(yōu)先標(biāo)志
key_ring_reg<='1'; --響鈴
key_led_reg<='0'; --亮燈
when "1011" =>
key_bcd<="00000010"; --按鍵是2
key_H<='1';
key_ring_reg<='1';
key_led_reg<='0';
when "1101" =>
key_bcd<="00000011"; --按鍵是3
key_H<='1';
key_ring_reg<='1';
key_led_reg<='0';
when "1110" =>
key_bcd<="00000100"; --按鍵是4
key_H<='1';
key_ring_reg<='1';
key_led_reg<='0';
when others =>
key_bcd<=null; --其它狀態(tài),輸出0且不響鈴,不亮燈
key_H<='0';
key_ring_reg<='0';
key_led_reg<='1';
end case;
end if;
end process;
key_out<=key_bcd(7 downto 0); --輸出要顯示的BCD碼
key_first<=key_H;
key_ring<=key_ring_reg;
key_led<=key_led_reg;
end rtl;
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -