myfirst_niosii.vhd 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. library IEEE;
  2. use IEEE.STD_LOGIC_1164.ALL;
  3. use IEEE.numeric_std.ALL;
  4. use IEEE.STD_LOGIC_UNSIGNED.ALL;
  5. entity myfirst_niosii is port (
  6. clk: in std_logic;
  7. rst: in std_logic;
  8. pio_led: out std_logic_vector(31 downto 0);
  9. buttons: in std_logic_vector(7 downto 0);
  10. matrix_rows: out std_logic_vector(11 downto 0);
  11. matrix_cols: out std_logic_vector(7 downto 0);
  12. lcd_16207_ext_RS : out std_logic; -- RS
  13. lcd_16207_ext_RW : out std_logic; -- RW
  14. lcd_16207_ext_data : inout std_logic_vector(7 downto 0) := (others => 'X'); -- data
  15. lcd_16207_ext_E : out std_logic
  16. );
  17. type matrix_t is array(integer range 0 to 7) of std_logic_vector(11 downto 0);
  18. end myfirst_niosii;
  19. architecture behav of myfirst_niosii is
  20. component nios2_uc is port (
  21. clk_clk : in std_logic := 'X'; -- clk
  22. reset_reset_n : in std_logic := 'X'; -- reset_n
  23. lcd_16207_ext_RS : out std_logic; -- RS
  24. lcd_16207_ext_RW : out std_logic; -- RW
  25. lcd_16207_ext_data : inout std_logic_vector(7 downto 0) := (others => 'X'); -- data
  26. lcd_16207_ext_E : out std_logic; -- E
  27. pio_led_ext_conn_export : out std_logic_vector(31 downto 0); -- export
  28. pio_button_ext_conn_export : in std_logic_vector(7 downto 0) := (others => 'X'); -- export
  29. pio_matrix_ext_conn_export : out std_logic_vector(19 downto 0) -- export
  30. );
  31. end component nios2_uc;
  32. signal button_states: std_logic_vector(7 downto 0);
  33. signal button_timer: integer range 0 to 2**20-1 := 0;
  34. signal matrix_timer: integer range 0 to 2**15-1 := 0;
  35. signal matrix_col_index: integer range 0 to 8 := 0;
  36. signal matrix_s: matrix_t;
  37. signal pio_matrix_s: std_logic_vector(19 downto 0);
  38. begin
  39. u0: component nios2_uc
  40. port map (
  41. clk_clk => clk,
  42. pio_led_ext_conn_export => pio_led,
  43. reset_reset_n => rst,
  44. pio_matrix_ext_conn_export => pio_matrix_s,
  45. pio_button_ext_conn_export => button_states
  46. );
  47. matrix_set: process(clk, rst)
  48. variable col_id : integer range 0 to 7;
  49. begin
  50. if rst = '0' then
  51. matrix_s <= (
  52. "111110011111",
  53. "000100000101",
  54. "010000000111",
  55. "111110000000",
  56. "000000011111",
  57. "111110010001",
  58. "101010011111",
  59. "111010000000"
  60. );
  61. elsif rising_edge(clk) then
  62. col_id := to_integer(unsigned(pio_matrix_s(15 downto 12)));
  63. if col_id > 0 then
  64. matrix_s(col_id-1) <= pio_matrix_s(11 downto 0);
  65. end if;
  66. end if;
  67. end process;
  68. matrix_multiplex: process(clk, rst)
  69. begin
  70. if rst = '0' then
  71. matrix_rows <= "111111111111";
  72. matrix_cols <= "11111111";
  73. matrix_timer <= 0;
  74. matrix_col_index <= 0;
  75. elsif rising_edge(clk) then
  76. if matrix_timer = 2**15-1 then
  77. matrix_timer <= 0;
  78. if matrix_col_index = 7 then
  79. matrix_col_index <= 0;
  80. else
  81. matrix_col_index <= matrix_col_index + 1;
  82. end if;
  83. matrix_cols <= (others => '0');
  84. matrix_rows <= (others => '0');
  85. elsif matrix_timer = 2**11-1 then
  86. matrix_cols(matrix_col_index) <= '1';
  87. matrix_rows <= matrix_s(matrix_col_index);
  88. matrix_timer <= matrix_timer + 1;
  89. else
  90. matrix_timer <= matrix_timer + 1;
  91. end if;
  92. end if;
  93. end process;
  94. button_debounce: process(clk, rst)
  95. begin
  96. if rst = '0' then
  97. button_timer <= 0;
  98. elsif rising_edge(clk) then
  99. if button_timer = 2**20-1 then
  100. button_timer <= 0;
  101. for id in 0 to 7 loop
  102. if buttons(id) = not button_states(id) then
  103. button_states(id) <= buttons(id);
  104. end if;
  105. end loop;
  106. else
  107. button_timer <= button_timer + 1;
  108. end if;
  109. end if;
  110. end process;
  111. end behav;