library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
--动态显示模块,主要目的实现数码管和发光二极管同时显示
entity dynamic_display is
Port ( clk : in std_logic; --系统时钟
reset: in std_logic; --复位信号
datain1,datain2: in std_logic_vector(3 downto 0); --倒计时的数据输入
data_ledfa:in std_logic_vector(7 downto 0 ); --交通灯的亮灭信号输入
cs:out std_logic_vector(1 downto 0); --数码管和发光二极管的选通信号
shift: out std_logic_vector(3 downto 0);--点亮数码管的位选信号
led : out std_logic_vector(7 downto 0)); --送去显示的数据输出
end dynamic_display;
architecture Behavioral of dynamic_display is
signal clk_shift:std_logic;
signal datain11,datain22: std_logic_vector(7 downto 0);
begin
process(clk) --分频器;
variable cnt : integer range 0 to 5000;
begin
if clk'event and clk='1' then cnt:=cnt+1;
if cnt<2500 then clk_shift<='1';
elsif cnt<5000 then clk_shift<='0';
else cnt:=0;clk_shift<='0';
end if;
end if;
end process;
process(clk_shift,datain1) --译码,把十进制数译成数码管显示的段码
begin
if clk_shift'event and clk_shift='1' then
case datain1 is
when"0000"=>datain11<="01000000";--0
when"0001"=>datain11<="01111001";--1
when"0010"=>datain11<="00100100";--2
when"0011"=>datain11<="00110000";--3
when"0100"=>datain11<="00011001";--4
when"0101"=>datain11<="00010010";--5
when"0110"=>datain11<="00000010";--6
when"0111"=>datain11<="01111000";--7
when"1000"=>datain11<="00000000";--8
when"1001"=>datain11<="00010000";--9
when others=>datain11<="11111111";--No signal;
end case;
end if;
end process;
process(clk_shift,datain2) --译码,把十进制数译成数码管显示的段码
begin
if clk_shift'event and clk_shift='1' then
case datain2 is
when"0000"=>datain22<="01000000";--0
when"0001"=>datain22<="01111001";--1
when"0010"=>datain22<="00100100";--2
when"0011"=>datain22<="00110000";--3
when"0100"=>datain22<="00011001";--4
when"0101"=>datain22<="00010010";--5
when"0110"=>datain22<="00000010";--6
when"0111"=>datain22<="01111000";--7
when"1000"=>datain22<="00000000";--8
when"1001"=>datain22<="00010000";--9
when others=>datain22<="11111111";--No signal;
end case;
end if;
end process;
process(clk_shift,reset)
variable cnt : std_logic_vector(2 downto 0);
begin
if reset='1' then
cnt:="000";
elsif clk_shift'event and clk_shift='1' then
cnt:=cnt+1;
if cnt="001" then
cs<="11";
shift<="1111";
led<="11111111"; --发光二极管、数码管全灭;
elsif cnt="010" then
cs<="00";
shift<="1111";
led<="11111111";
elsif cnt="011" then --数码管显示‘0’;
cs<="01";
shift<="0111";
led<="01000000";
elsif cnt="100" then --数码管显示‘0’;
cs<="01";
shift<="1011";
led<="01000000";
elsif cnt="101" then --数码管显示datain2输入的数据;
cs<="01";
shift<="1101";
led<=datain22;
elsif cnt="110" then --数码管显示datain1输入的数据;
cs<="01";
shift<="1110";
led<=datain11;
elsif cnt="111" then --用发光二极管来代替交通灯显示
cs<="10";
shift<="1111";
led<=data_ledfa;
end if;
end if;
end process;
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL; --读成功
entity read_from_24c02 is
Port ( sysclk,reset : in std_logic;
sda,scl : inout std_logic;
cs:out std_logic;
led:out std_logic_vector(8 downto 1));
end read_from_24c02;
architecture Behavioral of read_from_24c02 is
type state is (prepare,start,transmit_slave_address,check_ack1,transmit_sub_address,check_ack2,
nack,start1,transmit_read,check_ack3,read_data,stop,idel); --定义状态机的各子状态;
signal current_state:state; --定义信号;
signal clock:std_logic;
begin
pulse:process(sysclk,reset) --进程1,分频得到周期为0.1s的时钟信号
variable count:integer range 0 to 5000000;
begin
if reset='0' then count:=0;
elsif rising_edge(sysclk) then
count:=count+1;
if count=2500000 then clock<='1';
elsif count=5000000 then clock<='0';count:=0; --frequency:10Hz time:0.1s
end if;
end if;
end process pulse;
statemachine:process(clock,reset) --进程2,状态机的转换
variable slave_address,sub_address:std_logic_vector(8 downto 1);
variable cnt:std_logic_vector(6 downto 0);
variable cnt1:integer range 0 to 8;
variable count1:integer range 0 to 40;
begin
if reset='0' then count1:=0;cnt:="0000000";cnt1:=8;cs<='1';
sda<='1';scl<='1';slave_address:="10100000";sub_address:="00000011";
led<="11111111";
current_state<=prepare;
elsif rising_edge(clock) then
case current_state is
when prepare=>cnt:=cnt+1;-- --准备状态,等各个器件复位
if cnt="0000010" then cnt:="0000000";current_state<=start;
else current_state<=prepare;
end if;
when start=>count1:=count1+1; led<="00000001"; --起始信号产生状态
case count1 is
when 1=>sda<='1';
when 3=>scl<='1';
when 5=>sda<='0';
when 7=>scl<='0';
when 9=>count1:=0;current_state<=transmit_slave_address;
when others=>null;
end case;
when transmit_slave_address=>count1:=count1+1;led<="00000010"; --发送器件从地址
case count1 is
when 1=>sda<=slave_address(cnt1);
when 3=>scl<='1';
when 6=>scl<='0';
when 8=>cnt1:=cnt1-1;count1:=0;
if cnt1=0 then cnt1:=8;
current_state<=check_ack1;
else current_state<=transmit_slave_address;
end if;
when others=>null;
end case;
when check_ack1=>count1:=count1+1;led<="00000100"; --查询应答信号
case count1 is
when 3=>sda<='0';
when 6=>scl<='1';
when 8=>scl<='0';
when 10=>
current_state<=transmit_sub_address;
count1:=0;
when others=>null;
end case;
when transmit_sub_address=>count1:=count1+1;led<="00001000"; --发送器件子地址
case count1 is
when 1=>sda<=sub_address(cnt1);
when 3=>scl<='1';
when 6=>scl<='0';
when 9=>cnt1:=cnt1-1;count1:=0;
if cnt1=0 then cnt1:=8;
current_state<=check_ack2;
else current_state<=transmit_sub_address;
end if;
when others=>null;
end case;
when check_ack2=>count1:=count1+1;led<="00010000"; --查询应答信号
case count1 is
when 3=>sda<='0';
when 6=>scl<='1';
when 8=>scl<='0';
when 10=>
current_state<=start1;
count1:=0;
when others=>null;
end case;
when start1=>count1:=count1+1;led<="01000000"; --重新起始信号产生状态
case count1 is
when 1=>sda<='1';
when 3=>scl<='1';
when 6=>sda<='0';
when 8=>scl<='0';
when 10=>count1:=0;current_state<=transmit_read;
slave_address:="10100001";
when others=>null;
end case;
when transmit_read=>count1:=count1+1;led<="10000000"; --发送器件从地址
case count1 is
when 1=>sda<=slave_address(cnt1);
when 4=>scl<='1';
when 6=>scl<='0';
when 9=>cnt1:=cnt1-1;count1:=0;
if cnt1=0 then cnt1:=8;
current_state<=check_ack3;
else current_state<=transmit_read;
end if;
when others=>null;
end case;
when check_ack3=>count1:=count1+1;led<="00000000"; --查询应答信号
case count1 is
when 3=>sda<='0';
when 6=>scl<='1';
when 8=>scl<='0';
when 10=>
current_state<=read_data;
count1:=0;
when others=>null;
end case;
when read_data=>count1:=count1+1; --读操作
case count1 is
when 1=>sda<='Z';
when 4=>scl<='1';
when 8=>led(cnt1)<=sda;
when 10=>scl<='0';
when 12=>cnt1:=cnt1-1;count1:=0;
if cnt1=0 then cnt1:=8;
current_state<=stop;
else current_state<=read_data;
end if;
when others=>null;
end case;
when stop=>count1:=count1+1; --产生停止信号
case count1 is
when 1=>sda<='0';
when 3=>scl<='1';
when 6=>sda<='1';
when 8=>count1:=0;current_state<=idel;
when others=>null;
end case;
when idel=>sda<='1';scl<='1';current_state<=idel;
when others=>null;
end case;
end if;
end process;
end Behavioral;
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL; --写成功
entity write_to_24c02 is
Port ( sysclk,reset : in std_logic;
sda,scl : inout std_logic;
cs: out std_logic;
s:out std_logic_vector(7 downto 0)
);
end write_to_24c02;
architecture Behavioral of write_to_24c02 is
type state is (prepare,start,transmit_slave_address,check_ack1,transmit_sub_address,check_ack2,
transmit_data,check_ack3,stop,idel); --定义状态机的各子状态;
signal current_state:state; --定义信号;
signal clock:std_logic;
begin
pulse:process(sysclk,reset) --进程1,分频得到周期为0.1s的时钟信号
variable count:integer range 0 to 5000000;
begin
if reset='0' then count:=0;
elsif rising_edge(sysclk) then
count:=count+1;
if count=2500000 then clock<='1';
elsif count=5000000 then clock<='0';count:=0; --frequency:10Hz time:0.1s
end if;
end if;
end process pulse;
statemachine:process(clock,reset) --进程2,状态机的转换
variable slave_address,sub_address,data:std_logic_vector(8 downto 1);
variable cnt:std_logic_vector(6 downto 0);
variable cnt1:integer range 0 to 8;
variable count1:integer range 0 to 40;
begin
if reset='0' then count1:=0;cnt:="0000000";cnt1:=8;cs<='1';
sda<='1';scl<='1';slave_address:="10100000";sub_address:="00000011";
current_state<=prepare;data:="00000111";
s<="11111111";
elsif rising_edge(clock) then
case current_state is
when prepare=>cnt:=cnt+1;-- --准备状态,等各个器件复位
if cnt="0000010" then cnt:="0000000";current_state<=start;
else current_state<=prepare;
end if;
when start=>count1:=count1+1; --起始信号产生状态
case count1 is
when 1=>sda<='1';
when 2=>scl<='1';
when 3=>sda<='0';
when 4=>scl<='0';
when 5=>count1:=0;current_state<=transmit_slave_address;
when others=>null;
end case;
when transmit_slave_address=>count1:=count1+1;s<="01111111"; --发送器件从地址
case count1 is
when 1=>sda<=slave_address(cnt1);
when 2=>scl<='1';
when 3=>scl<='0';
when 4=>cnt1:=cnt1-1;count1:=0;
if cnt1=0 then cnt1:=8;
current_state<=check_ack1;
else current_state<=transmit_slave_address;
end if;
when others=>null;
end case;
when check_ack1=>count1:=count1+1;s<="10111111"; --查询应答信号
case count1 is
when 1=>sda<='0';
when 2=>scl<='1';
when 3=>scl<='0';
when 4=>
current_state<=transmit_sub_address;
count1:=0;
when others=>null;
end case;
when transmit_sub_address=>count1:=count1+1;s<="11011111"; --发送器件子地址
case count1 is
when 1=>sda<=sub_address(cnt1);
when 2=>scl<='1';
when 3=>scl<='0';
when 4=>cnt1:=cnt1-1;count1:=0;
if cnt1=0 then cnt1:=8;
current_state<=check_ack2;
else current_state<=transmit_sub_address;
end if;
when others=>null;
end case;
when check_ack2=>count1:=count1+1; s<="11101111"; --查询应答信号
case count1 is
when 1=>sda<='0';
when 2=>scl<='1';
when 3=>scl<='0';
when 4=>current_state<=transmit_data;
count1:=0;
when others=>null;
end case;
when transmit_data=>count1:=count1+1;s<="11110111"; --发送数据
case count1 is
when 1=>sda<=data(cnt1);
when 2=>scl<='1';
when 3=>scl<='0';
when 4=>cnt1:=cnt1-1;count1:=0;
if cnt1=0 then cnt1:=8;
current_state<=check_ack3;
else current_state<=transmit_data;
end if;
when others=>null;
end case;
when check_ack3=>count1:=count1+1;s<="11111011"; --查询应答信号
case count1 is
when 1=>sda<='0';
when 2=>scl<='1';
when 3=>scl<='0';
when 4=>
current_state<=stop;
count1:=0;
when others=>null;
end case;
when stop=>count1:=count1+1; s<="11111101"; --产生停止信号
case count1 is
when 1=>sda<='0';
when 3=>scl<='1';
when 10=>sda<='1';
when 15=>count1:=0;current_state<=idel;
when others=>null;
end case;
when idel=>sda<='1';scl<='1';current_state<=idel; s<="11111110";
when others=>null;
end case;
end if;
end process;
end Behavioral;