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状态机图, ...
随机推荐
- 九度OJ 1193:矩阵转置 (矩阵计算)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:1673 解决:1132 题目描述: 输入一个N*N的矩阵,将其转置后输出.要求:不得使用任何数组(就地逆置). 输入: 输入的第一行包括一个 ...
- Webpack探索【3】--- loader详解
本文主要说明Webpack的loader相关内容.
- PBR探索
原理 根据能量守恒,以及一系列光照原理得出微表面BRDF(Bidirectional Reflectance Distribution Function)公式 // D(h) F(v,h) G(l,v ...
- 我的Android进阶之旅------>Android KeyCode列表
KEYCODE列表 电话键 KEYCODE_CALL 拨号键 5 KEYCODE_ENDCALL 挂机键 6 KEYCODE_HOME 按键Home 3 KEYCODE_MENU 菜单键 82 KEY ...
- leetcode第一刷_Best Time to Buy and Sell Stock II
这道题尽管是上一道题的增强.可是反而简单了. 能够交易无数次,可是买卖必须成对的出现. 为了简单起见.我用abc三股股票来说明,且忽略掉相等的情况.三个数一共同拥有六种大小关系.注意他们之间的先后顺序 ...
- Linux备份和回复mysql数据库
备份:mysqldump -u root -p密码 数据库名>/home/data.bak mysqldump -u root -p密码 数据库名.表名>/home/data.bak ...
- linux -unrar解压缩
解压缩命令unrar的使用: $unrar --help 用法: unrar <command>-<switch 1> -<switchN> <arch ...
- 【leetcode刷题笔记】Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- P4022 [CTSC2012]熟悉的文章
题目 P4022 [CTSC2012]熟悉的文章 题目大意:多个文本串,多个匹配串,我们求\(L\),\(L\)指(匹配串中\(≥L\)长度的子串出现在文本串才为"熟悉",使得匹配 ...
- web前端框架之自定义form表单验证
自定义form验证初试 .在后端创建一个类MainForm,并且在类中自定义host ip port phone等,然后写入方法,在post方法中创建MainForm对象,并且把post方法中的sel ...