Advertisement
Guest User

Untitled

a guest
Dec 1st, 2015
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.80 KB | None | 0 0
  1. library ieee;
  2. use ieee.std_logic_1164.all;
  3. use ieee.numeric_std.all;
  4.  
  5. entity Selector is
  6. port ( clk : in std_logic; --klock puls
  7. reset_n : in std_logic; --reset på 0
  8. wait_ready : in std_logic; --vänta tills ready insignal
  9. ena : out std_logic; --skriva enable -till s2c
  10. addr : out std_logic_vector(6 downto 0); --slave addressen -till s2c
  11. rw : out std_logic; --read/write output -till s2c
  12. data_wr : out std_logic_vector(7 downto 0); --skrivdata till slave -till s2c
  13. busy : out std_logic; --kollar om data överförst -till s2c
  14. data_rd : in std_logic_vector(7 downto 0); --läsa data från slave -till s2c
  15. ack_error : buffer std_logic; --errorflagga från slave -till s2c
  16. sda : inout std_logic; --serial data buss -till s2c
  17. scl : inout std_logic; --serial clk buss -till s2c
  18.  
  19. data_rd_rh : out std_logic_vector (15 downto 0); --output från rh mätning
  20. data_rd_temp: out std_logic_vector (15 downto 0)); --output från temp mätning
  21. end entity Selector;
  22.  
  23. architecture rtl of Selector is
  24. --Types--
  25. type t_main_state is ( s_idle,
  26. s_get_data,
  27. s_calculate);
  28. type t_secondary_state is ( s0,
  29. s1,
  30. s2,
  31. s3,
  32. s4,
  33. s5,
  34. s6,
  35. s7,
  36. s8);
  37.  
  38. --Signals--
  39. signal busy_prev : std_logic;
  40. signal busy_i2c : std_logic;
  41. signal busy_cnt : std_logic;
  42. signal state : t_main_state;
  43. signal state_s : t_secondary_state;
  44.  
  45. begin
  46.  
  47. p_main : process (clk, reset_n)
  48. begin
  49. if (reset_n = '0') then -- reset
  50. state <= s_idle;
  51. ena <= '0';
  52. busy_cnt <= '0';
  53. elsif (rising_edge(clk)) then --initiera state machine
  54. case state is
  55. when s_idle =>
  56. if (wait_ready = '1') then --väntar på redo för att initiera state machine
  57. state <= s_get_data; --initierar state machine
  58. end if;
  59. when s_get_data =>
  60. busy_prev <= busy_i2c; --fångar det föregågna busy signalen
  61.  
  62. case state_s is
  63. when s0 =>
  64. ena <= '1'; --initierar transaktionen
  65. addr <= "1000000"; --deklarerar addressen till 'slave'
  66. rw <= '0'; --sätter read/write till write
  67. data_wr <= "11100101"; --kommando för att läsa luftfuktighet
  68.  
  69. if (busy_prev = '0' and busy_i2c = '1') then --kollar när i2c blir hög
  70. state_s <= s1; --skifta state
  71. end if;
  72.  
  73. when s1 =>
  74. rw <= '1'; --sätter till read för att få in första halvan av data
  75.  
  76. if (busy_prev = '0' and busy_i2c = '1') then --kollar när i2c blir hög
  77. state_s <= s2; --skifta state
  78. end if;
  79.  
  80. when s2 =>
  81. rw <= '0'; --write
  82. if (busy_i2c = '0') then
  83. data_rd_rh(15 downto 8) <= data_rd; --sparar ner första halvan data
  84. state_s <= s3; --skiftar state
  85. end if;
  86.  
  87. when s3 =>
  88. rw <= '1'; --read
  89.  
  90. if (busy_prev = '0' and busy_i2c = '1') then --kollar när i2c blir hög
  91. state_s <= s4; --skifta state
  92. end if;
  93.  
  94. when s4 =>
  95. rw <= '0'; --write
  96. if (busy_i2c = '0') then
  97. data_rd_rh(7 downto 0) <= data_rd; --sparar ner andra halvan data
  98. state_s <= s5; --skiftar state
  99. end if;
  100.  
  101. when s5 =>
  102. rw <= '1'; --read
  103.  
  104. if (busy_prev = '0' and busy_i2c = '1') then --kollar när i2c blir hög
  105. state_s <= s6; --skifta state
  106. end if;
  107.  
  108. when s6 =>
  109. rw <= '0'; --write
  110. if (busy_i2c = '0') then
  111. data_rd_temp(15 downto 8) <= data_rd; --sparar ner första halvan data
  112. state_s <= s7; --skiftar state
  113. end if;
  114.  
  115. when s7 =>
  116. rw <= '1'; --read
  117.  
  118. if (busy_prev = '0' and busy_i2c = '1') then --kollar när i2c blir hög
  119. state_s <= s8; --skifta state
  120. end if;
  121.  
  122. when s8 =>
  123. ena <= '0'; --avslutar transaktionen
  124. if (busy_i2c = '0') then
  125. data_rd_temp(7 downto 0) <= data_rd; --sparar ner andra halvan data
  126. state_s <= s1; --återställer state
  127. state <= s_idle; --återställe state
  128. end if;
  129. when others => null;
  130. end case;
  131. when others => null;
  132. end case;
  133. end if;
  134. end process;
  135. end architecture;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement