Advertisement
Momostein

blink_led_hardware.v

Oct 13th, 2020
4,543
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. `timescale 1ns / 1ps
  2. // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // //
  3. // Company:
  4. // Engineer:
  5. //
  6. // Create Date: 09/29/2020 04:11:34 PM
  7. // Design Name:
  8. // Module Name: blink_led_hardware
  9. // Project Name:
  10. // Target Devices:
  11. // Tool Versions:
  12. // Description:
  13. //
  14. // Dependencies:
  15. //
  16. // Revision:
  17. // Revision 0.01 - File Created
  18. // Additional Comments:
  19. //
  20. // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // //
  21.  
  22.  
  23. module blink_led_hardware(input wire ACLK,
  24.                           input wire control_strobe,
  25.                           input wire [1:0] control_blinkmode1,
  26.                           input wire [1:0] control_blinkmode2,
  27.                           input wire [1:0] control_blinkmode3,
  28.                           input wire [1:0] control_blinkmode4,
  29.                           input wire frequency_strobe,
  30.                           input wire [31:0] frequency_frequency,
  31.                           output reg [3:0] led);
  32.     // Signals
  33.     reg slow, fast;
  34.     reg [32:0]  fast_counter;
  35.     reg [1:0]   slow_counter;
  36.    
  37.     // Combine blinkmodes into blinkmode array
  38.     wire [1:0] modes [0:3];
  39.     assign modes[0] = control_blinkmode1;
  40.     assign modes[1] = control_blinkmode2;
  41.     assign modes[2] = control_blinkmode3;
  42.     assign modes[3] = control_blinkmode4;
  43.    
  44.     // Loop counters
  45.     integer j;
  46.    
  47.     // Drive leds according to modes
  48.     always @(*) begin
  49.         for (j = 0; j < 4; j = j+1) begin
  50.             case(modes[j])
  51.                 2'd0: led[j]    = 0;
  52.                 2'd1: led[j]    = 1;
  53.                 2'd2: led[j]    = slow;
  54.                 2'd3: led[j]    = fast;
  55.                 default: led[j] = 0;
  56.             endcase
  57.         end
  58.     end
  59.    
  60.     // Generate slow and fast signals
  61.     always @(posedge ACLK, posedge frequency_strobe) begin
  62.         if (frequency_strobe) begin
  63.             fast         <= 0;
  64.             slow         <= 0;
  65.             fast_counter <= 0;
  66.             slow_counter <= 0;
  67.         end
  68.         else begin
  69.             // The higher the frequency the faster the counter goes up
  70.             fast_counter <= fast_counter + frequency_frequency;
  71.            
  72.             // Assign the carrybit to fast
  73.             fast <= fast_counter[32];
  74.            
  75.             // Detect changes in fast
  76.             if (fast != fast_counter[32]) begin
  77.                 // Every time fast changes
  78.                 if (slow_counter >= 2) begin
  79.                     // Toggle slow every 3 changes
  80.                     slow         <= !slow;
  81.                     slow_counter <= 0;
  82.                 end
  83.                 else begin
  84.                     // Or increment the counter
  85.                     slow_counter <= slow_counter + 1;
  86.                 end
  87.             end
  88.         end
  89.     end
  90. endmodule
  91.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement