Advertisement
Sebgg

uppgift21b

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