1 //==========================================
2 // Function : Code Gray counter.
3 // Coder : Alex Claros F.
5 //=======================================
8 #(parameter COUNTER_WIDTH = 4)
10 (output reg [COUNTER_WIDTH-1:0] GrayCount_out, //'Gray' code count output.
12 input wire Enable_in, //Count enable.
13 input wire Clear_in, //Count reset.
17 /////////Internal connections & variables///////
18 reg [COUNTER_WIDTH-1:0] BinaryCount;
20 /////////Code///////////////////////
22 always @ (posedge Clk)
24 BinaryCount <= {COUNTER_WIDTH{1'b 0}} + 1; //Gray count begins @ '1' with
25 GrayCount_out <= {COUNTER_WIDTH{1'b 0}}; // first 'Enable_in'.
27 else if (Enable_in) begin
28 BinaryCount <= BinaryCount + 1;
29 GrayCount_out <= {BinaryCount[COUNTER_WIDTH-1],
30 BinaryCount[COUNTER_WIDTH-2:0] ^ BinaryCount[COUNTER_WIDTH-1:1]};