A Gray code is an encoding of numbers so that adjacent numbers have a single digit differing by 1. The term Gray code is often used to refer to a Binary Reflected Gray Code. We can implement a gray code counter in a different ways. Consider the following table carefully.

B : 000, 001, 010, 011, 100, 101, 110, 111

G: 000, 001, 011, 010, 110, 111, 101, 100

Binary to gray code can be achieved by

B : 000, 001, 010, 011, 100, 101, 110, 111

G: 000, 001, 011, 010, 110, 111, 101, 100

To convert a binary number d1,d2,..,d(n-1),dn to its corresponding Binary Reflected Gray Code, start at the right with the digit dn (the LSB). If the d(n-1) is 1, replace dn by (1-dn); otherwise, leave it unchanged. Then proceed to d(n-1). Continue up to the first d1, which is kept the same. The resulting number g1,g2,..,g(n-1),gn is the Reflected Binary Gray Code.

The most common Gray code is where the lower half of the sequence is exactly the mirror image of first half with only the MSB inverted. We illustrate the 3-bit binary Gray code as an example.

Binary to gray code can be achieved by

gray[2] = binary[2];

gray[1] = binary[2] ^ binary[1];

gray[0] = binary[1] ^ binary[0];

A simple verilog code to implement this function is given by

assign gray = (binary>> 1) ^ binary; // Right shift by 1 and EX-OR with binary.

module gray_cntr (

clock_in,

rst_n,

enable_in,

cnt_out

);

// I/O Declarations

input clock_in, rst_n, enable_in;

output [ 2:0] cnt_out;

wire [2:0] cnt_out;

reg [2:0] cnt;

always @ (posedge clock_in or negedge rst_n)

if (!rst_n)

cnt <= 1'b0;

else if (enable_in)

cnt <= cnt + 1'b1;

assign cnt_out = { cnt[2], (^cnt[2:1]), (^cnt[1:0]) };

endmodule

## 7 comments:

this was very useful to me....

You need not explicitly declare cnt_out as wire if you already declared it as an output...All Verilog input/output/inout are implicitly declared as wires...

I could not refrain from commenting. Perfectly written!

Here is my web-site::diet plan to Lose weightAn outstanding share! I have just forwarded this onto a coworker who has been conducting a little research on this. And he actually ordered me breakfast because I stumbled upon it for him... lol. So allow me to reword this.... Thanks for the meal!! But yeah, thanx for spending the time to discuss this subject here on your web page.

After going over a number of the blog posts on your web site, I really appreciate your way of writing a blog.

I saved it to my bookmark site list and will

be checking back soon. Please check out my web site as well and let me know your opinion.

My page: diets that work

My web page-safe dietsGreat post. I wаѕ checking continuοuslу this blog аnd I am impressed!

Very usеful infoгmation spеcially the last part :) I care for

such informаtiοn muсh. I was looking fοr this partіcular infoгmation fοr a very

long time. Thank you anԁ best of luck.

Alѕo viѕit mу site: www.tanak.kr

For latest information you have to go tο see world ωіde web and on the wеb

I founԁ this websіte аs a bеst web page

for most up-to-date updates.

my web sitе :: augen lasern

Post a Comment