Advertisement
Sebgg

lab4c21

May 17th, 2016
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.93 KB | None | 0 0
  1. library IEEE;
  2. use IEEE.STD_LOGIC_1164.ALL;
  3. use IEEE.STD_LOGIC_UNSIGNED.ALL;
  4.  
  5. entity timer is
  6. Port(
  7. clk, mux, time : in std_logic;
  8. start_in, reset_in : in std_logic;
  9. time_prec : in std_logic;
  10. a, b, c, d, e, f, g, dp, dspA, dspB : out std_logic);
  11. end timer;
  12.  
  13. architecture atimer of timer is
  14. signal en_p, en_t, en_p_plus, en_t_plus, start, reset, start_plus, reset_plus : std_logic := '0';
  15. signal adress, seconds_1, seconds_10, minutes_1, minutes_10 : std_logic_vector(3 downto 0) := "0000";
  16. signal mseconds_1, mseconds_10, mseconds_100 : std_logic_vector(3 downto 0) := "0000";
  17. signal t_prec : std_logic := '0';
  18. signal count_enable : std_logic := '0';
  19. signal display : std_logic_vector(1 downto 0) := "00";
  20. signal data : std_logic_vector(6 downto 0) := "0000000";
  21.  
  22. begin
  23.  
  24. -- Klocka insignaler
  25. process(clk) begin
  26. if rising_edge(clk) then
  27. en_p <= en_p_plus;
  28. en_t <= en_t_plus;
  29. en_p_plus <= mux;
  30. en_t_plus <= time;
  31. start <= start_plus;
  32. reset <= reset_plus;
  33. start_plus <= start_in;
  34. reset_plus <= reset_in;
  35. t_prec <= time_prec;
  36. end if;
  37. end process;
  38.  
  39. -- Räkna upp
  40. process(clk) begin
  41. if rising_edge(clk) then
  42. if (start='0' and start_plus='1') then
  43. count_enable <= not count_enable;
  44. elsif (reset='0' and reset_plus='1') then
  45. seconds_1 <= "0000";
  46. seconds_10 <= "0000";
  47. minutes_1 <= "0000";
  48. minutes_10 <= "0000";
  49. count_enable <= '0';
  50. elsif (en_t='0' and en_t_plus='1' and count_enable='1') then
  51. if (t_prec = '0') then
  52. seconds_1 <= seconds_1 + 1;
  53. if (seconds_1 = 9) then
  54. seconds_10 <= seconds_10 + 1;
  55. seconds_1 <= "0000";
  56. end if;
  57. if (seconds_10 = 5 and seconds_1 = 9) then
  58. minutes_1 <= minutes_1 + 1;
  59. seconds_10 <= "0000";
  60. end if;
  61. if (minutes_1 = 9 and seconds_10 = 5 and seconds_1 = 9) then
  62. minutes_10 <= minutes_10 + 1;
  63. minutes_1 <= "0000";
  64. end if;
  65. if (minutes_10 = 5 and minutes_1 = 9 and seconds_10 = 5 and seconds_1 = 8) then
  66. count_enable <= '0';
  67. end if;
  68. end if;
  69. elsif (en_p = '0' and en_p_plus = '1' and count_enable = '1') then
  70. if (t_prec = '1') then
  71. mseconds_1 <= mseconds_1 + 1;
  72. if (mseconds_100 = 9 and mseconds_10 = 9 and mseconds_1 = 8 and seconds_1 = 9 and seconds_10 = 5 and minutes_1 = 9 and minutes_10 = 5) then
  73. count_enable <= '0';
  74. elsif (mseconds_100 = 9 and mseconds_10 = 9 and mseconds_1 = 9 and seconds_1 = 9 and seconds_10 = 5 and minutes_1 = 9) then
  75. minutes_10 <= minutes_10 + 1;
  76. minutes_1 <= "0000";
  77. seconds_10 <= "0000";
  78. seconds_1 <= "0000";
  79. mseconds_100 <= "0000";
  80. mseconds_10 <= "0000";
  81. mseconds_1 <= "0000";
  82. elsif (mseconds_100 = 9 and mseconds_10 = 9 and mseconds_1 = 9 and seconds_1 = 9 and seconds_10 = 5) then
  83. minutes_1 <= minutes_1 + 1;
  84. seconds_10 <= "0000";
  85. seconds_1 <= "0000";
  86. mseconds_100 <= "0000";
  87. mseconds_10 <= "0000";
  88. mseconds_1 <= "0000";
  89. elsif (mseconds_100 = 9 and mseconds_10 = 9 and mseconds_1 = 9 and seconds_1 = 9) then
  90. seconds_10 <= seconds_10 + 1;
  91. seconds_1 <= "0000";
  92. mseconds_100 <= "0000";
  93. mseconds_10 <= "0000";
  94. mseconds_1 <= "0000";
  95. elsif (mseconds_100 = 9 and mseconds_10 = 9 and mseconds_1 = 9) then
  96. seconds_1 <= seconds_1 + 1;
  97. mseconds_100 <= "0000";
  98. mseconds_10 <= "0000";
  99. mseconds_1 <= "0000";
  100. elsif (mseconds_10 = 9 and mseconds_1 = 9) then
  101. mseconds_100 <= mseconds_100 + 1;
  102. mseconds_10 <= "0000";
  103. mseconds_1 <= "0000";
  104. elsif(mseconds_1 = 9) then
  105. mseconds_10 <= mseconds_10 + 1;
  106. mseconds_1 <= "0000";
  107. end if;
  108. end if;
  109. end if;
  110. end if;
  111. end process;
  112.  
  113. -- Sätt adress för PROMen samt välja display-segment
  114. process(clk) begin
  115. if rising_edge(clk) then
  116. if (en_p='0' and en_p_plus='1') then
  117. display <= display + 1;
  118. dp <= '0';
  119. if (display = 0) then
  120. if (t_prec = '0') then
  121. if (minutes_10 > 0) then
  122. dspA <= '1';
  123. dspB <= '1';
  124. end if;
  125. adress <= seconds_1;
  126. elsif (t_prec = '1') then
  127. if (seconds_10 > 0) then
  128. dspA <= '1';
  129. dspB <= '1';
  130. end if;
  131. adress <= mseconds_10;
  132. end if;
  133. elsif (display = 1) then
  134. if (t_prec = '0') then
  135. dspA <= '0';
  136. dspB <= '0';
  137. adress <= seconds_10;
  138. elsif(t_prec = '1') then
  139. dspA <= '0';
  140. dspB <= '0';
  141. adress <= mseconds_100;
  142. end if;
  143. elsif (display = 2) then
  144. if (t_prec = '0') then
  145. if ((minutes_1 >= 0 and seconds_10 > 0) or (seconds_10 > 0)) then
  146. dspA <= '1';
  147. dspB <= '0';
  148. end if;
  149. adress <= minutes_1;
  150. elsif(t_prec = '1') then
  151. if ((seconds_1 >= 0 and mseconds_100 > 0) or (mseconds_100 > 0)) then
  152. dspA <= '1';
  153. dspB <= '0';
  154. end if;
  155. adress <= seconds_1;
  156. end if;
  157. elsif (display = 3) then
  158. dp <= '1';
  159. if (t_prec = '0') then
  160. if ((minutes_1 >= 0 and minutes_10 > 0) or (minutes_1 > 0)) then
  161. dspA <= '0';
  162. dspB <= '1';
  163. end if;
  164. adress <= minutes_10;
  165. elsif(t_prec = '1') then
  166. if ((seconds_1 >= 0 and seconds_10 > 0) or (seconds_1 > 0)) then
  167. dspA <= '0';
  168. dspB <= '1';
  169. end if;
  170. adress <= seconds_10;
  171. end if;
  172. display <= "00";
  173. end if;
  174. end if;
  175. end if;
  176. end process;
  177.  
  178. -- Översätt tal till display
  179. with adress select
  180. data <= "1111110" when "0000",
  181. "0110000" when "0001",
  182. "1101101" when "0010",
  183. "1111001" when "0011",
  184. "0110011" when "0100",
  185. "1011011" when "0101",
  186. "1011111" when "0110",
  187. "1110000" when "0111",
  188. "1111111" when "1000",
  189. "1111011" when "1001",
  190. "-------" when others;
  191.  
  192. -- Sätt outputs
  193. process(clk) begin
  194. if (en_p='0' and en_p_plus='1') then
  195. if rising_edge(clk) then
  196. a <= data(6);
  197. b <= data(5);
  198. c <= data(4);
  199. d <= data(3);
  200. e <= data(2);
  201. f <= data(1);
  202. g <= data(0);
  203. end if;
  204. end if;
  205. end process;
  206.  
  207. -- Lagra föregående klockpuls
  208.  
  209. end atimer;
  210.  
  211. -- vsim timer
  212. -- add wave sim:/timer/*
  213. -- restart -force
  214. -- force -freeze sim:/timer/clk 0 0, 1 4 -r 8
  215. -- force -freeze sim:/timer/mux 0 0, 1 50 -r 100
  216. -- force -freeze sim:/timer/time 0 0, 1 500 -r 1000
  217. -- force -freeze sim:/timer/start_in 0 0, 1 100
  218. -- force -freeze sim:/timer/reset_in 0 0
  219. -- force -freeze sim:/timer/time_prec 0 0
  220. -- run 500000
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement