- library IEEE;
- use IEEE.STD_LOGIC_1164.ALL;
- use IEEE.numeric_std.ALL;
- entity OneWire is
- Port ( clk : in STD_LOGIC; -- 1MHz
- reset : in STD_LOGIC;
- start : in STD_LOGIC;
- command : in STD_LOGIC; --no ze wybor instrukcji
- temperature_out : out STD_LOGIC_VECTOR(15 downto 0);
- --led1 : out STD_LOGIC;
- onewire : inout STD_LOGIC);
- end OneWire;
- architecture Behavioral of OneWire is
- CONSTANT 1US : positive := 50;
- CONSTANT DEL_RST_LOW : positive := 500 * 1US;
- CONSTANT DEL_RST_70 : positive := 70 * 1US;
- --CONSTANT DEL_WAIT : positive := 100 * 1US;
- CONSTANT CONV_T_STRONG_PULLUP_TIME : positive := 750000 * 1US;
- CONSTANT DEL_READ_BIT : positive := 9 * 1US;
- CONSTANT DEL_NEXT_READ_TIME_SLOT : positive := 40 * 1US;
- CONSTANT DEL_58 : positive := 58 * 1US;
- CONSTANT DEL_2 : positive := 2 * 1US;
- constant SKIP_ROM : std_logic_vector(7 downto 0):= "11001100";
- constant CONVERT_T : std_logic_vector(7 downto 0) := "01000100";
- constant READ_SCRATCHPAD : std_logic_vector(7 downto 0) := "10111110";
- 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);
- SIGNAL state : STATE_TYPE := S_START;
- SIGNAL timer : INTEGER RANGE 0 TO 38000000;
- SIGNAL IO : STD_LOGIC := '0'; -- jak jeden to wysyłaj
- SIGNAL IS_ROM_COMMAND : STD_LOGIC := '1';
- SIGNAL TEMPERATURE : STD_LOGIC_VECTOR(15 downto 0) := "0000000000000000"; -- przechowuje temperature
- SIGNAL SEND_COMMAND : STD_LOGIC_VECTOR(7 downto 0);
- SIGNAL COMMAND_CHOICE : STD_LOGIC := '0'; --wybor funkcji convert_t i read scratchpad
- signal bit_counter : integer range 0 to 24;
- begin
- Process(clk)
- begin
- if(rising_edge(clk)) then
- if(timer > 0) then timer <= timer - 1; end if;
- case state is
- when S_START =>
- --answer <= '0';
- if(start='1') then
- timer <= 500 * 1US; --DEL_RST_LOW ;
- state <= S_RESET;
- IO <= '0';
- end if;
- when S_RESET =>
- if(timer = 0) then
- IO <= '1';
- timer <= 70 * 1US; -- DEL_RST_70;
- state <= S_PRESENCE;
- end if;
- when S_PRESENCE =>
- if(timer = 0) then
- --answer <= not onewire;
- --state <= S_WAIT;
- state <= S_SKIP_ROM;
- --timer <= DEL_WAIT;
- end if;
- ---------------------------------
- when S_SKIP_ROM =>
- SEND_COMMAND <= SKIP_ROM;
- IS_ROM_COMMAND <= '1';
- bit_counter <= 0;
- timer <= 0;
- state <= S_SEND_BYTE;
- when S_SEND_BYTE =>
- if(timer = 0) then
- if(bit_counter < 8) then
- if(SEND_COMMAND(bit_counter) = '0') then
- timer <= 58 * 1US; --DEL_58;
- IO <= '0';
- state <= S_WRITE_LOW;
- else
- timer <= 2 * 1US; --DEL_2;
- IO <= '0';
- state <= S_WRITE_HIGH;
- end if;
- else
- state <= S_WYBOR;
- end if;
- end if;
- when S_WRITE_LOW =>
- if(timer = 0) then
- timer <= 2 * 1US; --DEL_2;
- IO <= '1';
- bit_counter <= bit_counter + 1;
- state <= S_SEND_BYTE;
- end if;
- when S_WRITE_HIGH =>
- if(timer = 0) then
- timer <= 58 * 1US; -- DEL_58;
- IO <= '1';
- bit_counter <= bit_counter + 1;
- state <= S_SEND_BYTE;
- end if;
- ---------------------------------------------------------------------
- when S_WYBOR =>
- if(IS_ROM_COMMAND = '1') then
- case command is
- when '1' =>
- state <= S_CONVERT_T;
- COMMAND_CHOICE <= command;
- IS_ROM_COMMAND <= '0';
- when '0' =>
- state <= S_READ_SCRATCHPAD;
- COMMAND_CHOICE <= command;
- IS_ROM_COMMAND <= '0';
- when others =>
- state <= S_RESET;
- end case;
- else
- case COMMAND_CHOICE is
- when '1' =>
- state <= S_CONVERT_T_ANSWER;
- timer <= 750000 * 1US; --750ms --CONV_T_STRONG_PULLUP_TIME;
- IO <= '1'; -- nie weim co to ten strong pullup
- when '0' =>
- state <= S_READ_BYTES;
- timer <= 0;
- bit_counter <= 0;
- when others =>
- state <= S_RESET;
- end case;
- end if;
- --------------------------------------------------------
- when S_CONVERT_T =>
- SEND_COMMAND <= CONVERT_T;
- bit_counter <= 0;
- state <= S_SEND_BYTE;
- when S_CONVERT_T_ANSWER=>
- if(timer = 0) then
- state <= S_WAIT;
- end if;
- --------------------------------------------------------
- when S_READ_SCRATCHPAD =>
- SEND_COMMAND <= READ_SCRATCHPAD;
- bit_counter <= 0;
- state <= S_SEND_BYTE;
- -----------------------------------------------------------
- when S_READ_BYTES =>
- if(timer = 0) then
- if(bit_counter < 16) then
- timer <= 1US; -- 1us
- IO <= '0'; --wysyla zero
- state <= S_READ_TIME_SLOT;
- else
- state <= S_SEND_TEMPERATURE;
- end if;
- end if;
- when S_READ_TIME_SLOT =>
- if(timer = 0) then
- timer <= DEL_READ_BIT; --9us
- state <= S_READ_BIT;
- end if;
- when S_READ_BIT =>
- if(timer = 0) then
- timer <= DEL_NEXT_READ_TIME_SLOT; --40us
- TEMPERATURE(bit_counter) <= onewire;
- bit_counter <= bit_counter + 1;
- state <= S_READ_BYTES;
- end if;
- when S_SEND_TEMPERATURE =>
- temperature_out <= TEMPERATURE;
- state <= S_WAIT;
- --------------------------------------------------------
- when S_WAIT =>
- if(reset = '1') then --if(answer != '1') then
- state <= S_RESET; --state <= S_START;
- end if;
- end case;
- end if;
- end process;
- onewire <= '0' when IO = '0' else 'Z';
- end Behavioral;