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状态机图, ...
随机推荐
- RedisTemplate访问Redis数据结构(介绍和常用命令)
Redis 数据结构简介 Redis 可以存储键与5种不同数据结构类型之间的映射,这5种数据结构类型分别为String(字符串).List(列表).Set(集合).Hash(散列)和 Zset(有序集 ...
- iOS OC和JS的交互 javaScriptCore方法封装
一.javaScriptCore javaScriptCore是一种JavaScript引擎,主要为webKit提供脚本处理能力,javaScriptCore是开源webkit的一部分,他提供了强大的 ...
- 【python】-- 协程介绍及基本示例、协程遇到IO操作自动切换、协程(gevent)并发爬网页
协程介绍及基本示例 协程,又称微线程,纤程.英文名Coroutine.一句话说明什么是协程:协程是一种用户态的轻量级线程. 协程拥有自己的寄存器上下文和栈.协程调度切换时,将寄存器上下文和栈保存到其他 ...
- c++操作flash
c++操作falsh,忘了原文在哪了,自己尝试了,直接贴代码 // SDK版本 //////////////////////////////////////////////////////////// ...
- Java语言实现简单FTP软件------>辅助功能模块FTP站点管理的实现(十二)
1.FTP站点管理 点击"FTP站点管理"按钮,弹出对话框"FTP站点管理",如下图 1) 连接站点 在FTP站点管理面板上选好要连接的站点,点击"连 ...
- TextView属性
TextView及其子类,当字符内容太长显示不下时可以省略号代替未显示的字符:省略号可以在显示区域的起始,中间,结束位置,或者以跑马灯的方式显示文字(textview的状态为被选中). 其实现只需在x ...
- 读paper:image caption with global-local attention…
最近的图片caption真的越来越火了,CVPR ICCV ECCV AAAI很多顶级会议都有此类的文章,今天我来讲一篇发表在AAAI的文章,因为我看了大量的论文,最近感觉AAAI越来越水了.所以这篇 ...
- SpringBoot学习笔记(3):静态资源处理
SpringBoot学习笔记(3):静态资源处理 在web开发中,静态资源的访问是必不可少的,如:Html.图片.js.css 等资源的访问. Spring Boot 对静态资源访问提供了很好的支持, ...
- HDU - 2709 Sumsets 【递推】
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=2709 题意 给出一个数N 要求有多少种方式 求和 能够等于N 加的数 必须是 2的幂次 思路 首先可以 ...
- 《程序员代码面试指南》第五章 字符串问题 去掉字符串中连续出现k 个0 的子串
题目 去掉字符串中连续出现k 个0 的子串 java代码 package com.lizhouwei.chapter5; /** * @Description: 去掉字符串中连续出现k 个0 的子串 ...