GrayCode for state machine
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的更多相关文章
- Finite State Machine 是什么?
状态机(Finite State Machine):状态机由状态寄存器和组合逻辑电路构成,能够根据控制信号按照预先设定的状态进行状态转移,是协调相关信号动 作.完成特定操作的控制中心. 类 ...
- State Machine.(状态机)
What is a State Machine? Any device that changes its state from one to another due to some actions a ...
- Finite State Machine
Contents [hide] 1 Description 2 Components 3 C# - FSMSystem.cs 4 Example Description This is a Dete ...
- Qt: The State Machine Framework 学习
State Machine,即为状态机,是Qt中一项非常好的框架.State Machine包括State以及State间的Transition,构成状态和状态转移.通过状态机,我们可以很方便地实现很 ...
- Android4.0中蓝牙适配器state machine(状态机)的分析
今天晓东和大家来一起看一下Android4.0中蓝牙适配器(Bluetooth Adapter)的状态机变化的过程.首先,我们需要了解一下,蓝牙适配器究竟有哪些状态,从代码可以清晰地看到(framew ...
- 控制结构(3) 状态机(state machine)
// 上一篇:卫语句(guard clause) // 下一篇:局部化(localization) 基于语言提供的基本控制结构,更好地组织和表达程序,需要良好的控制结构. 前情回顾 上次分析了guar ...
- 控制结构(3): 状态机(state machine)
// 上一篇:卫语句(guard clause) // 下一篇:局部化(localization) 基于语言提供的基本控制结构,更好地组织和表达程序,需要良好的控制结构. 前情回顾 上次分析了guar ...
- 【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 ...
- 【翻译】What is State Machine Diagram(什么是状态机图)?
[翻译]What is State Machine Diagram(什么是状态机图)? 写在前面 在上一篇学习类图的时候将这个网站上的类图的一篇文章翻译了出来,感觉受益良多,今天来学习UML状态机图, ...
随机推荐
- 第一课 第一个nodejs程序
这就是我们的第一个程序了 在控制台会输出:hello 我们需要运行该文件 开始->运行 cmd 进入我们的程序目录 我的是D:/nodejs/hello.js 进入程序目录cd D:/nodej ...
- Apache Maven pom文件
Welcome to Apache Maven Apache Maven is a software project management and comprehension tool. Based ...
- LookAndFeelInfo
在读zap源码的时候碰到:UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());以下来自百度知道UIManager.s ...
- python数据分析之:数据聚合与分组运算
在数据库中,我们可以对数据进行分类,聚合运算.例如groupby操作.在pandas中同样也有类似的功能.通过这些聚合,分组操作,我们可以很容易的对数据进行转换,清洗,运算.比如如下图,首先通过不同的 ...
- 2014阿里实习生面试题——MySQL如何实现索引的
这是2014阿里实习生北京站二面的一道试题: 在MySQL中,索引属于存储引擎级别的概念,不同存储引擎对索引的实现方式是不同的,比如MyISAM和InnoDB存储引擎. MyISAM索引实现: MyI ...
- codeforces 686B
题意:给出一个序列,只允许进行相邻的两两交换,给出使序列变为非降序列的操作方案. 思路:关键点是操作次数不限,冒泡排序. #include<iostream> #include<cs ...
- jsonpath对json数据进行分析校验做接口测试
在做接口测试的时候, 我们需要对返回的数据进行分析校验, 一般返回的都是json格式的数据, 怎么来解析校验呢? 之前有看过使用递归遍历json数据的, 然后找到了jsonpath, 可以很方便的对j ...
- mongodb同步
mongodb同步 py-mongo-sync MongodbSync mongodb 的一个同步工具,具备将一个数据源上的数据,同步到其它 mongodb 上,支持: mongos -> (m ...
- LINQ 学习路程 -- 查询操作 ElementAt, ElementAtOrDefault
Element Operators (Methods) Description ElementAt 返回指定索引的元素,如果索引超过集合长度,则抛出异常 ElementAtOrDefault 返回指定 ...
- scala基本学习
def addOne(f: Int => Int, arg: Int) = f(arg) + 1,意思是 addOne要两个参数一个是:传一个整数的参数且返回一个整形的方法的参数,第二个参数就是 ...