// This is a 4-bit counter // that can either count up or down (depending on a switch) // and that counts up/down roughly every second. // The purpose of the example is to show how "human-type" speeds // can be achieved in circuits, even though they use very fast clocks // e.g. the 50 MHz clock on the DE2 board. // Author janders; November 2009 module slowcounter(clk, sw0, count4); input clk; input sw0; output reg [3:0] count4; reg [24:0] counter; reg enable; // you need to tie the clk pin to the 50 MHz clock on the DE2 board // you need to tie the count4 outputs to the LEDs on the DE2 // you need to tie the sw0 to one of the switches on the DE2 // generate an enable signal always@(posedge clk) begin counter = counter + 1'b1; if (counter == 25'h000000) enable = 1'b1; else enable = 1'b0; end // generate an up/down counter always@(posedge clk) begin if (enable == 1'b1) begin if (sw0 == 1'b0) count4 = count4 + 1'b1; else count4 = count4 - 1'b1; end end endmodule