Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- library ieee;
- use ieee.std_logic_1164.all;
- use ieee.numeric_std.all;
- entity Selector is
- port ( clk : in std_logic; --klock puls
- reset_n : in std_logic; --reset på 0
- wait_ready : in std_logic; --vänta tills ready insignal
- ena : out std_logic; --skriva enable -till s2c
- addr : out std_logic_vector(6 downto 0); --slave addressen -till s2c
- rw : out std_logic; --read/write output -till s2c
- data_wr : out std_logic_vector(7 downto 0); --skrivdata till slave -till s2c
- busy : out std_logic; --kollar om data överförst -till s2c
- data_rd : in std_logic_vector(7 downto 0); --läsa data från slave -till s2c
- ack_error : buffer std_logic; --errorflagga från slave -till s2c
- sda : inout std_logic; --serial data buss -till s2c
- scl : inout std_logic; --serial clk buss -till s2c
- data_rd_rh : out std_logic_vector (15 downto 0); --output från rh mätning
- data_rd_temp: out std_logic_vector (15 downto 0)); --output från temp mätning
- end entity Selector;
- architecture rtl of Selector is
- --Types--
- type t_main_state is ( s_idle,
- s_get_data,
- s_calculate);
- type t_secondary_state is ( s0,
- s1,
- s2,
- s3,
- s4,
- s5,
- s6,
- s7,
- s8);
- --Signals--
- signal busy_prev : std_logic;
- signal busy_i2c : std_logic;
- signal busy_cnt : std_logic;
- signal state : t_main_state;
- signal state_s : t_secondary_state;
- begin
- p_main : process (clk, reset_n)
- begin
- if (reset_n = '0') then -- reset
- state <= s_idle;
- ena <= '0';
- busy_cnt <= '0';
- elsif (rising_edge(clk)) then --initiera state machine
- case state is
- when s_idle =>
- if (wait_ready = '1') then --väntar på redo för att initiera state machine
- state <= s_get_data; --initierar state machine
- end if;
- when s_get_data =>
- busy_prev <= busy_i2c; --fångar det föregågna busy signalen
- case state_s is
- when s0 =>
- ena <= '1'; --initierar transaktionen
- addr <= "1000000"; --deklarerar addressen till 'slave'
- rw <= '0'; --sätter read/write till write
- data_wr <= "11100101"; --kommando för att läsa luftfuktighet
- if (busy_prev = '0' and busy_i2c = '1') then --kollar när i2c blir hög
- state_s <= s1; --skifta state
- end if;
- when s1 =>
- rw <= '1'; --sätter till read för att få in första halvan av data
- if (busy_prev = '0' and busy_i2c = '1') then --kollar när i2c blir hög
- state_s <= s2; --skifta state
- end if;
- when s2 =>
- rw <= '0'; --write
- if (busy_i2c = '0') then
- data_rd_rh(15 downto 8) <= data_rd; --sparar ner första halvan data
- state_s <= s3; --skiftar state
- end if;
- when s3 =>
- rw <= '1'; --read
- if (busy_prev = '0' and busy_i2c = '1') then --kollar när i2c blir hög
- state_s <= s4; --skifta state
- end if;
- when s4 =>
- rw <= '0'; --write
- if (busy_i2c = '0') then
- data_rd_rh(7 downto 0) <= data_rd; --sparar ner andra halvan data
- state_s <= s5; --skiftar state
- end if;
- when s5 =>
- rw <= '1'; --read
- if (busy_prev = '0' and busy_i2c = '1') then --kollar när i2c blir hög
- state_s <= s6; --skifta state
- end if;
- when s6 =>
- rw <= '0'; --write
- if (busy_i2c = '0') then
- data_rd_temp(15 downto 8) <= data_rd; --sparar ner första halvan data
- state_s <= s7; --skiftar state
- end if;
- when s7 =>
- rw <= '1'; --read
- if (busy_prev = '0' and busy_i2c = '1') then --kollar när i2c blir hög
- state_s <= s8; --skifta state
- end if;
- when s8 =>
- ena <= '0'; --avslutar transaktionen
- if (busy_i2c = '0') then
- data_rd_temp(7 downto 0) <= data_rd; --sparar ner andra halvan data
- state_s <= s1; --återställer state
- state <= s_idle; --återställe state
- end if;
- when others => null;
- end case;
- when others => null;
- end case;
- end if;
- end process;
- end architecture;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement