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