How & Why use Gray Code

A gray counter is a binary counter where only one bit changes at a time.

Gray code is mostly used to send values across clock domains (this way it has an uncertainty of only 1).

The easiest way to create a Gray counter is to first make a binary counter, and then convert the value to Gray.

=======================================================================

VS2013, Simulation

-------------------------------------------

#include <stdio.h>
#include <string>

template<int BITS>
std::string GetBits(unsigned int val){
    std::string str;
    for(int i = BITS-1; i >= 0; i --){
        char c = '0';
        if(val & (1 << i)) c = '1';
        str.push_back(c);
    }
    return str;
}

template<int BITS>
unsigned int GetGrayCode(){
    unsigned long mask = (1<<BITS)-1;
    static unsigned int next = 0;
    unsigned int nRtn = 0;
    nRtn = (next >> 1) ^ (next);
    next++;
    if(next > mask) next = 0;
    return nRtn;
}

void TestGrayCode(){
    //Generate 4Bit Gray Code
    const int BITS = 4;
    printf("%6s : %s \n", "Index", "GrayCode");
    printf("---------------------------------\n");
    for(int i = 0; i < 16; i++){
        std::string str = GetBits<BITS>( GetGrayCode<BITS>() );
        printf("%06d :   %s \n", i, str.c_str());
    }
    printf("---------------------------------\n");
}

void main(){
    TestGrayCode();
}

----------------------------------------------------

4Bit GrayCode Result :

/////////////////////////////////////////////////////////////////////////////////////

-------------------------------Verilog-----------------------------------

module GenerateGrayCode(CLK, RST_N, gray_code);

parameter BITS_COUNT = 4;

input CLK, RST_N;
output [BITS_COUNT-1:0] gray_code;

reg [BITS_COUNT-1:0]cnt = 1'b0;

always @(posedge CLK or negedge RST_N)
begin
    if (!RST_N) cnt <= 0;
    else
    begin
        cnt <= cnt + 1'b1;
    end
end

assign gray_code = (cnt >> 1'b1) ^ cnt;

endmodule

GrayCode for state machine的更多相关文章

  1. Finite State Machine 是什么?

    状态机(Finite State Machine):状态机由状态寄存器和组合逻辑电路构成,能够根据控制信号按照预先设定的状态进行状态转移,是协调相关信号动       作.完成特定操作的控制中心. 类 ...

  2. State Machine.(状态机)

    What is a State Machine? Any device that changes its state from one to another due to some actions a ...

  3. Finite State Machine

    Contents [hide]  1 Description 2 Components 3 C# - FSMSystem.cs 4 Example Description This is a Dete ...

  4. Qt: The State Machine Framework 学习

    State Machine,即为状态机,是Qt中一项非常好的框架.State Machine包括State以及State间的Transition,构成状态和状态转移.通过状态机,我们可以很方便地实现很 ...

  5. Android4.0中蓝牙适配器state machine(状态机)的分析

    今天晓东和大家来一起看一下Android4.0中蓝牙适配器(Bluetooth Adapter)的状态机变化的过程.首先,我们需要了解一下,蓝牙适配器究竟有哪些状态,从代码可以清晰地看到(framew ...

  6. 控制结构(3) 状态机(state machine)

    // 上一篇:卫语句(guard clause) // 下一篇:局部化(localization) 基于语言提供的基本控制结构,更好地组织和表达程序,需要良好的控制结构. 前情回顾 上次分析了guar ...

  7. 控制结构(3): 状态机(state machine)

    // 上一篇:卫语句(guard clause) // 下一篇:局部化(localization) 基于语言提供的基本控制结构,更好地组织和表达程序,需要良好的控制结构. 前情回顾 上次分析了guar ...

  8. 【UML】-NO.43.EBook.5.UML.1.003-【UML 大战需求分析】- 状态机图(State Machine Diagram)

    1.0.0 Summary Tittle:[UML]-NO.43.EBook.1.UML.1.003-[UML 大战需求分析]- 状态机图(State Machine Diagram) Style:D ...

  9. 【翻译】What is State Machine Diagram(什么是状态机图)?

    [翻译]What is State Machine Diagram(什么是状态机图)? 写在前面 在上一篇学习类图的时候将这个网站上的类图的一篇文章翻译了出来,感觉受益良多,今天来学习UML状态机图, ...

随机推荐

  1. java ScriptEngine 使用

    Java SE 6最引人注目的新功能之一就是内嵌了脚本支持.在默认情况下,Java SE 6只支持JavaScript,但这并不以为着Java SE 6只能支持JavaScript.在Java SE ...

  2. 使用weka训练一个分类器

    1 训练集数据 1.1 csv格式 5.1,3.5,1.4,0.2,Iris-setosa 4.9,3.0,1.4,0.2,Iris-setosa 4.7,3.2,1.3,0.2,Iris-setos ...

  3. Netty 100万级到亿级流量 高并发 仿微信 IM后台 开源项目实战

    目录 写在前面 亿级流量IM的应用场景 十万级 单体IM 系统 高并发分布式IM系统架构 疯狂创客圈 Java 分布式聊天室[ 亿级流量]实战系列之 -10[ 博客园 总入口 ] 写在前面 ​ 大家好 ...

  4. MySQL存储过程-通过数据库里已存在的IP查询城市

    CREATE DEFINER=`mazey`@`%` PROCEDURE `sp_find_city_by_ip`(IN `in_visitor_ip` varchar(20),OUT `out_vi ...

  5. Django 之Ajax&Json&CORS&同源策略&Jsonp用法

    什么是Json 定义: JSON(JavaScript Object Notation, JS 对象标记) 是一种轻量级的数据交换格式.它基于 ECMAScript (w3c制定的js规范)的一个子集 ...

  6. css position: relative,absolute具体解释

    关于CSS中 position在布局中非常重要,查了非常多资料都说的非常难理解.以下说说个人的理解: 语法: position: relative | absolute relative: 对象遵循常 ...

  7. android禁止横屏

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

  8. 在VS2010下打开VS2008项目的解决办法

    如何在vs2010中打开vs2008项目文件? 第一步:以记事本方式打开该项目的sln解决方案,找到这两行信息,分别如下:Microsoft Visual Studio Solution File,  ...

  9. R语言图形base系统(二)

    x<-c(1:10) y<-x z<-10/x opar<-par(no.readonly = T) par(mar=c(5,4,4,8)+0.1) plot(x,y,type ...

  10. IImage--factory

    <?php /* 实例4 */ /* 使用工厂类解析图像工作 */ interface IImage { function getWidth(); function getHeight(); f ...