that_one_nigga_you_k

codigo da maquina de estados

Jul 25th, 2024
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module main(
  2.     input wire clk,
  3.     input wire ButtonIniciar,
  4.     input wire ButtonReset,
  5.     input wire ButtonContar,
  6.     input wire ButtonPausar,
  7.     input wire ButtonParar,
  8.     output wire [6:0] segCentena,
  9.     output wire [6:0] segDezena,
  10.     output wire [6:0] segUnidade,
  11.     output wire [6:0] segDecimo
  12. );
  13.  
  14. reg [31:0] Contador1 = 0;
  15. reg [3:0] Centena = 0;
  16. reg [3:0] Dezena = 0;
  17. reg [3:0] Unidade = 0;
  18. reg [3:0] Decimo = 0;
  19. reg Ativo = 0;        // Flag para controlar a contagem
  20. reg Display = 0;      // Flag para ativar/desativar o display
  21.  
  22. parameter RESET = 3'b000,
  23.           COUNT = 3'b001,
  24.           PAUSE = 3'b010,
  25.           STOP  = 3'b011;
  26.  
  27. reg [2:0] state = RESET;
  28. reg [3:0] reg_aux;
  29. reg ButtonReset_prev, ButtonContar_prev, ButtonPausar_prev, ButtonParar_prev;
  30.  
  31. always @(posedge clk) begin
  32.             // state <= state;
  33.  
  34.             // Atualizando os estados anteriores dos botões
  35.             ButtonReset_prev <= ButtonReset;
  36.             ButtonContar_prev <= ButtonContar;
  37.             ButtonPausar_prev <= ButtonPausar;
  38.             ButtonParar_prev <= ButtonParar;
  39.  
  40.             // Atualizando o estado atual
  41.         // else state <= RESET;
  42.     end
  43.      
  44.  
  45.  
  46.  
  47. Botton botao (
  48.     .clk(clk),
  49.     .ButtonIniciar(ButtonIniciar),
  50.     .ButtonReset(ButtonReset),
  51.     .ButtonContar(ButtonContar),
  52.     .ButtonPausar(ButtonPausar),
  53.     .ButtonParar(ButtonParar),
  54.     // .state(state)
  55. );
  56. always @(posedge clk) begin
  57.  
  58.         case (state)
  59.             RESET: begin // RESET
  60.                 Contador1 <= 0;
  61.                 Centena <= 0;
  62.                 Dezena <= 0;
  63.                 Unidade <= 0;
  64.                 Decimo <= 0;
  65.                 Ativo <= 0;
  66.                 Display <= 1;
  67.                      //state <= COUNT;
  68.                      
  69.                      if (ButtonIniciar) begin
  70.                         // Detecção da borda de subida dos botões
  71.                         if (ButtonReset && !ButtonReset_prev) begin
  72.                              state = RESET;
  73.                         end else if (ButtonContar && !ButtonContar_prev) begin
  74.                              state = COUNT;
  75.                         end else if (ButtonPausar && !ButtonPausar_prev) begin
  76.                              state = PAUSE;
  77.                         end else if (ButtonParar && !ButtonParar_prev) begin
  78.                              state = STOP;
  79.                         end
  80.                     end
  81.          
  82.             end
  83.             COUNT: begin // COUNT
  84.                 Ativo <= 1;
  85.                 Display <= 1;
  86.                      //state <= PAUSE;
  87.                      if (ButtonIniciar) begin
  88.             // Detecção da borda de subida dos botões
  89.             if (ButtonReset && !ButtonReset_prev) begin
  90.                 state = RESET;
  91.             end else if (ButtonContar && !ButtonContar_prev) begin
  92.                 state = COUNT;
  93.             end else if (ButtonPausar && !ButtonPausar_prev) begin
  94.                 state = PAUSE;
  95.             end else if (ButtonParar && !ButtonParar_prev) begin
  96.                 state = STOP;
  97.             end
  98.         end
  99.             end
  100.             PAUSE: begin // PAUSE
  101.                 Ativo <= 1;
  102.                 Display <= 0;
  103.                      //state <= STOP;
  104.                      if (ButtonIniciar) begin
  105.             // Detecção da borda de subida dos botões
  106.             if (ButtonReset && !ButtonReset_prev) begin
  107.                 state = RESET;
  108.             end else if (ButtonContar && !ButtonContar_prev) begin
  109.                 state = COUNT;
  110.             end else if (ButtonPausar && !ButtonPausar_prev) begin
  111.                 state = PAUSE;
  112.             end else if (ButtonParar && !ButtonParar_prev) begin
  113.                 state = STOP;
  114.             end
  115.         end
  116.             end
  117.             STOP: begin // STOP
  118.                 Ativo <= 0;
  119.                 Display <= 0;
  120.                      //state <= RESET;
  121.                      if (ButtonIniciar) begin
  122.             // Detecção da borda de subida dos botões
  123.             if (ButtonReset && !ButtonReset_prev) begin
  124.                 state = RESET;
  125.             end else if (ButtonContar && !ButtonContar_prev) begin
  126.                 state = COUNT;
  127.             end else if (ButtonPausar && !ButtonPausar_prev) begin
  128.                 state = PAUSE;
  129.             end else if (ButtonParar && !ButtonParar_prev) begin
  130.                 state = STOP;
  131.             end
  132.         end
  133.             end
  134.         endcase
  135.  
  136.     if (Ativo == 1) begin // Apenas conta se estiver no estado COUNT
  137.         Contador1 <= Contador1 + 1;
  138.         if (Contador1 == 5000000) begin
  139.             Contador1 <= 0;
  140.             Decimo <= Decimo + 1;
  141.             if (Decimo == 9) begin
  142.                 Decimo <= 0;
  143.                 Unidade <= Unidade + 1;
  144.                 if (Unidade == 9) begin
  145.                     Unidade <= 0;
  146.                     Dezena <= Dezena + 1;
  147.                     if (Dezena == 9) begin
  148.                         Dezena <= 0;
  149.                         Centena <= Centena + 1;
  150.                     end
  151.                 end
  152.             end
  153.         end
  154.     end
  155. end
  156.  
  157. Display7 display (
  158.     .centena(Centena),
  159.     .dezena(Dezena),
  160.     .unidade(Unidade),
  161.     .decimo(Decimo),
  162.     .segCentena(segCentena),
  163.     .segDezena(segDezena),
  164.     .segUnidade(segUnidade),
  165.     .segDecimo(segDecimo),
  166.     .displayativo(Display)
  167. );
  168.  
  169. endmodule
  170.  
Add Comment
Please, Sign In to add comment