Advertisement
Sebgg

21c

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