Facebook
From Toxic Water Vole, 4 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 217
  1. library IEEE;
  2. use IEEE.STD_LOGIC_1164.ALL;
  3. use IEEE.numeric_std.ALL;
  4.  
  5. entity OneWire is
  6.     Port ( clk : in  STD_LOGIC; -- 1MHz
  7.                           reset : in  STD_LOGIC;
  8.            start : in  STD_LOGIC;
  9.                           command : in  STD_LOGIC; --no ze wybor instrukcji
  10.                           temperature_out : out         STD_LOGIC_VECTOR(15 downto 0);
  11.            --led1 : out  STD_LOGIC;
  12.            onewire : inout  STD_LOGIC);
  13. end OneWire;
  14.  
  15. architecture Behavioral of OneWire is
  16.  
  17. CONSTANT 1US : positive := 50;
  18. CONSTANT DEL_RST_LOW  : positive := 500 * 1US;
  19. CONSTANT DEL_RST_70   : positive := 70  * 1US;
  20. --CONSTANT DEL_WAIT  : positive := 100 * 1US;
  21. CONSTANT CONV_T_STRONG_PULLUP_TIME  : positive := 750000 * 1US;
  22. CONSTANT DEL_READ_BIT  : positive := 9 * 1US;
  23. CONSTANT DEL_NEXT_READ_TIME_SLOT  : positive := 40 * 1US;
  24. CONSTANT DEL_58  : positive := 58 * 1US;
  25. CONSTANT DEL_2  : positive := 2 * 1US;
  26.  
  27. constant SKIP_ROM : std_logic_vector(7 downto 0):= "11001100";
  28. constant CONVERT_T : std_logic_vector(7 downto 0) := "01000100";
  29. constant READ_SCRATCHPAD : std_logic_vector(7 downto 0) := "10111110";
  30.  
  31. TYPE STATE_TYPE is (S_START, S_RESET, S_PRESENCE, S_WAIT, S_SKIP_ROM, S_SEND_BYTE, S_WRITE_LOW, S_WRITE_HIGH, S_WYBOR, S_CONVERT_T, S_CONVERT_T_ANSWER, S_READ_SCRATCHPAD, S_READ_BYTES, S_READ_TIME_SLOT, S_READ_BIT, S_SEND_TEMPERATURE);
  32. SIGNAL state : STATE_TYPE := S_START;
  33.  
  34. SIGNAL timer : INTEGER RANGE 0 TO 38000000;
  35.  
  36. SIGNAL IO : STD_LOGIC := '0'; -- jak jeden to wysyłaj
  37. SIGNAL IS_ROM_COMMAND : STD_LOGIC := '1';
  38. SIGNAL TEMPERATURE : STD_LOGIC_VECTOR(15 downto 0) := "0000000000000000"; -- przechowuje temperature
  39. SIGNAL SEND_COMMAND : STD_LOGIC_VECTOR(7 downto 0);
  40. SIGNAL COMMAND_CHOICE : STD_LOGIC := '0'; --wybor funkcji convert_t i read scratchpad
  41. signal bit_counter : integer range 0 to 24;
  42.  
  43. begin
  44.  
  45.         Process(clk)
  46.         begin
  47.                 if(rising_edge(clk)) then
  48.          if(timer > 0) then timer <= timer - 1; end if;
  49.                         case state is
  50.                                 when S_START =>
  51.                                         --answer <= '0';
  52.                                         if(start='1') then
  53.                                                 timer <= 500 * 1US; --DEL_RST_LOW ;
  54.                                                 state <= S_RESET;
  55.                                                 IO <= '0';
  56.                                         end if;
  57.                                
  58.                                 when S_RESET =>
  59.                                         if(timer = 0) then
  60.                   IO <= '1';
  61.                                                 timer <= 70  * 1US; -- DEL_RST_70;
  62.                                                 state <= S_PRESENCE;
  63.                                         end if;
  64.                                
  65.                                 when S_PRESENCE =>
  66.                                         if(timer = 0) then
  67.                                                 --answer <= not onewire;
  68.                                                 --state <= S_WAIT;
  69.                                                 state <= S_SKIP_ROM;
  70.                                                 --timer <= DEL_WAIT;
  71.                                         end if;
  72.                                 ---------------------------------
  73.                                 when S_SKIP_ROM =>
  74.                                         SEND_COMMAND <= SKIP_ROM;
  75.                                         IS_ROM_COMMAND <= '1';
  76.                                         bit_counter <= 0;
  77.                                         timer <= 0;
  78.                                         state <= S_SEND_BYTE;
  79.                                        
  80.                                 when S_SEND_BYTE =>
  81.                                                 if(timer = 0) then
  82.                                                         if(bit_counter < 8) then
  83.                                                                 if(SEND_COMMAND(bit_counter) = '0') then
  84.                                                                         timer <= 58 * 1US; --DEL_58;
  85.                                                                         IO <= '0';
  86.                                                                         state <= S_WRITE_LOW;
  87.                                                                 else
  88.                                                                         timer <= 2 * 1US; --DEL_2;
  89.                                                                         IO <= '0';
  90.                                                                         state <= S_WRITE_HIGH;
  91.                                                                 end if;
  92.                                                         else
  93.                                                                 state <= S_WYBOR;
  94.                                                         end if;
  95.                                                 end if;
  96.                                                                        
  97.                                 when S_WRITE_LOW =>
  98.                                         if(timer = 0) then
  99.                                                 timer <= 2 * 1US; --DEL_2;
  100.                                                 IO <= '1';
  101.                                                 bit_counter <= bit_counter + 1;
  102.                                                 state <= S_SEND_BYTE;
  103.                                         end if;
  104.                                        
  105.                                 when S_WRITE_HIGH =>
  106.                                         if(timer = 0) then
  107.                                                 timer <= 58 * 1US; -- DEL_58;
  108.                                                 IO <= '1';
  109.                                                 bit_counter <= bit_counter + 1;
  110.                                                 state <= S_SEND_BYTE;
  111.                                         end if;
  112.                                 ---------------------------------------------------------------------          
  113.                                 when S_WYBOR =>
  114.                                         if(IS_ROM_COMMAND = '1') then
  115.                                                                         case command is
  116.                                                                                 when '1' =>
  117.                                                                                         state <= S_CONVERT_T;
  118.                                                                                         COMMAND_CHOICE <= command;
  119.                                                                                         IS_ROM_COMMAND <= '0';
  120.                                                                                 when '0' =>
  121.                                                                                         state <= S_READ_SCRATCHPAD;
  122.                                                                                         COMMAND_CHOICE <= command;
  123.                                                                                         IS_ROM_COMMAND <= '0';
  124.                                                                                 when others =>
  125.                                                                                         state <= S_RESET;
  126.                                                                         end case;
  127.                                                                 else
  128.                                                                         case COMMAND_CHOICE is
  129.                                                                                 when '1' =>
  130.                                                                                         state <= S_CONVERT_T_ANSWER;
  131.                                                                                         timer <= 750000 * 1US; --750ms --CONV_T_STRONG_PULLUP_TIME;
  132.                                                                                         IO <= '1'; -- nie weim co to ten strong pullup
  133.                                                                            when '0' =>
  134.                                                                                         state <= S_READ_BYTES;
  135.                                                                                         timer <= 0;
  136.                                                                                         bit_counter <= 0;
  137.                                                                                 when others =>
  138.                                                                                         state <= S_RESET;
  139.                                                                         end case;
  140.                                                                 end if;
  141.                                 --------------------------------------------------------
  142.                                  when S_CONVERT_T =>
  143.                                                 SEND_COMMAND <= CONVERT_T;
  144.                                                 bit_counter <= 0;
  145.                                                 state <= S_SEND_BYTE;
  146.                                                
  147.                                 when S_CONVERT_T_ANSWER=>
  148.                                           if(timer = 0) then
  149.                                                         state <= S_WAIT;
  150.                                                 end if;
  151.                                 --------------------------------------------------------
  152.                                 when S_READ_SCRATCHPAD =>
  153.                                                 SEND_COMMAND <= READ_SCRATCHPAD;
  154.                                                 bit_counter <= 0;
  155.                                                 state <= S_SEND_BYTE;
  156.                                 -----------------------------------------------------------            
  157.                                 when S_READ_BYTES =>
  158.                                         if(timer = 0) then
  159.                                                 if(bit_counter < 16) then
  160.                                                         timer <= 1US; -- 1us
  161.                                                         IO <= '0'; --wysyla zero
  162.                                                         state <= S_READ_TIME_SLOT;
  163.                                                 else           
  164.                                                         state <= S_SEND_TEMPERATURE;
  165.                                                 end if;
  166.                                         end if;
  167.                                        
  168.                                 when S_READ_TIME_SLOT =>
  169.                                         if(timer = 0) then
  170.                                                 timer <= DEL_READ_BIT; --9us
  171.                                                 state <= S_READ_BIT;
  172.                                         end if;
  173.                                
  174.                                 when S_READ_BIT =>
  175.                                         if(timer = 0) then
  176.                                                 timer <= DEL_NEXT_READ_TIME_SLOT; --40us
  177.                                                 TEMPERATURE(bit_counter) <= onewire;
  178.                                                 bit_counter <= bit_counter + 1;
  179.                                                 state <= S_READ_BYTES;
  180.                                         end if;
  181.                                 when S_SEND_TEMPERATURE =>
  182.                                         temperature_out <= TEMPERATURE;
  183.                                         state <= S_WAIT;
  184.                                 --------------------------------------------------------
  185.                                 when S_WAIT =>
  186.                  if(reset = '1') then      --if(answer != '1') then
  187.                     state <= S_RESET;    --state <= S_START;
  188.                  end if;       
  189.                         end case;
  190.                 end if;
  191.         end process;
  192.        
  193.    
  194.  
  195.         onewire <= '0' when IO = '0' else 'Z';
  196. end Behavioral;
  197.  
  198.  
  199.