Ivan-Holm
Member level 5
![Member level 5](/styles/images/ranks/dlevel5.gif)
Code VHDL - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 ---------------------------------------------------------------------------------- -- Company: UCN T&B - IT-Teknolog - Electronics -- Engineer: Ivan & Mads -- -- Create Date: 11:13:55 04/29/2011 -- Design Name: -- Module Name: Rotary - Behavioral -- Project Name: -- Target Devices: -- Tool versions: -- Description: -- -- Dependencies: -- -- Revision: -- Revision 0.01 - File Created -- Additional Comments: -- ---------------------------------------------------------------------------------- library IEEE; use IEEE.STD_LOGIC_1164.ALL; use IEEE.STD_LOGIC_ARITH.ALL; use IEEE.STD_LOGIC_UNSIGNED.ALL; -- Uncomment the following library declaration if using -- arithmetic functions with Signed or Unsigned values --use IEEE.NUMERIC_STD.ALL; -- Uncomment the following library declaration if instantiating -- any Xilinx primitives in this code. --library UNISIM; --use UNISIM.VComponents.all; entity Rotary is Port ( rotary_a : in std_logic; rotary_b : in std_logic; rotary_press : in std_logic; clk : in std_logic); end Rotary; architecture Behavioral of Rotary is signal rotary_a_in : std_logic; -- indgang på FPGA signal rotary_b_in : std_logic; -- indgang på FPGA signal rotary_press_in : std_logic; -- indgang på FPGA signal rotary_in : std_logic_vector(1 downto 0); -- bit kombination af a og b signal rotary_q1 : std_logic; -- state 1 signal rotary_q2 : std_logic; -- state 2 signal delay_rotary_q1 : std_logic; -- signal rotary_event : std_logic; -- signal rotary_left : std_logic; -- ------------------------------------------------------------------------------------------------------------------------------------------------------------------------ -- -- Start of circuit description -- begin -- ---------------------------------------------------------------------------------------------------------------------------------- -- Interface to rotary encoder. -- Detection of movement and direction. ---------------------------------------------------------------------------------------------------------------------------------- -- -- The rotary switch contacts are filtered using their offset (one-hot) style to -- clean them. Circuit concept by Peter Alfke. -- Note that the clock rate is fast compared with the switch rate. rotary_filter: process(clk) begin if clk'event and clk='1' then --Synchronise inputs to clock domain using flip-flops in input/output blocks. rotary_a_in <= rotary_a; rotary_b_in <= rotary_b; rotary_press_in <= rotary_press; --concatinate rotary input signals to form vector for case construct. rotary_in <= rotary_b_in & rotary_a_in; case rotary_in is when "00" => rotary_q1 <= '0'; rotary_q2 <= rotary_q2; when "01" => rotary_q1 <= rotary_q1; rotary_q2 <= '0'; when "10" => rotary_q1 <= rotary_q1; rotary_q2 <= '1'; when "11" => rotary_q1 <= '1'; rotary_q2 <= rotary_q2; when others => rotary_q1 <= rotary_q1; rotary_q2 <= rotary_q2; end case; end if; end process rotary_filter; -- -- The rising edges of 'rotary_q1' indicate that a rotation has occurred and the -- state of 'rotary_q2' at that time will indicate the direction. -- direction: process(clk) begin if clk'event and clk='1' then delay_rotary_q1 <= rotary_q1; if rotary_q1='1' and delay_rotary_q1='0' then rotary_event <= '1'; rotary_left <= rotary_q2; else rotary_event <= '0'; rotary_left <= rotary_left; end if; end if; end process direction; end Behavioral; -------- INSPIRATION led_display: process(clk) begin if clk'event and clk='1' then if rotary_event='1' then if rotary_left='1' then led_pattern <= led_pattern(6 downto 0) & led_pattern(7); --rotate LEDs to left else led_pattern <= led_pattern(0) & led_pattern(7 downto 1); --rotate LEDs to right end if; end if; -- Pressing the rotary encoder will cause all LED drive bits to be inverted. if rotary_press_in='0' then led_drive <= led_pattern; else led_drive <= led_pattern xor "11111111"; end if; -- Ouput LED drive to the pins making use of the output flip-flops in input/output blocks. led <= led_drive; end if; end process led_display;