Verilog实现IIC通讯第二版
HMC5883三轴磁力传感器IIC通讯模块的VerilogHDL的实现
上一版并没有实现我想要的功能
0.0.1版 正在修订中 2013/9/2
//date :2013/7/7
//designer :pengxiaoen
//synthesizer:QuartusII 12.1
//function : IIC实现HMC5883的通讯 50M /(400k × 4)= 32 `define WriteAddress 'h3c
`define ReadAddress 'h3d `define RegAAddress 'h00 //配置寄存器A
`define RegBAddress 'h01 //配置寄存器B
`define ModelAddress 'h02 //模式寄存器
`define X_MSBAddress 'h03 //X MSB寄存器
`define X_LSBAddress 'h04
`define Z_MSBAddress 'h05
`define Z_LSBAddress 'h06
`define Y_MSBAddress 'h07
`define Y_LSBAddress 'h08
`define STATEAddress 'h09 //状态寄存器
`define IdentifyAAddress 'h10 //识别寄存器A
`define IdentifyBAddress 'h10
`define IdentifyCAddress 'h10 `define INITIAL
`define DELAY
`define MEASURE `define START
`define Re
`define Se
`define STOP //`default_nettype none module HMC5883_2 (
clock,reset,
sda,scl,
out_seg, sel_seg,
IIC_result
); input clock,reset;
inout sda,scl;
output reg [:]out_seg;
output reg IIC_result; output reg [:]sel_seg; reg [:] SEND_buffer;
reg [:] Re_buffer ;
reg sda_reg;
reg scl_reg;
reg ack_reg;
reg sda_enable;
reg scl_enable; reg IC_state;
reg n_IC_state;
reg [:]state; //当前状态寄存器
reg state_finish_flag ; // reg [:] scl_4;
reg [:] step_counter;
reg [:] clk_temp ; //==============================================================================================
//-------------时钟控制模块------------------start---------------
always @ (posedge clock or negedge reset)
if (!reset)
begin
clk_temp <= 'd0;
scl_4 <= 'd0;
end
else if (clk_temp=='d15)
begin
clk_temp <= 'd0;
if (state_finish_flag) scl_4 <= 'd0;
else if (scl_4 == 'b111_111) //这里是一个保护机制,可以设置一个flag
scl_4 <= 'b111_111;
else scl_4 <= scl_4 + ;
end
else clk_temp <= clk_temp + ;
//----------时钟控制模块----------end----------- //--------一个检测的pin---------start-----------
always @(posedge clock or negedge reset)
if(!reset)
IIC_result <= 'd0;
else if(scl_4 == 'b111_111)
IIC_result <= 'd1;
else ;
//-----------一个检测的pin-------end---------- //-------延时模块----5us------start--------
reg [:] delay_counter;
reg delay_enable;
always @(posedge clock )
if(!delay_enable)
begin
delay_counter <= 'd0;
state_finish_flag <= 'd0;
end
else if(delay_counter == 'd250)
begin
state_finish_flag <= 'd1;
delay_counter <= 'd0;
end
else begin
delay_counter <= delay_counter + ;
state_finish_flag <= 'd0;
end
//--------------延时模块-------end--------------- //----------状态机控制模块---------start-------
always @ (posedge clock or negedge reset)
if(!reset)
IC_state <= `INITIAL;
else IC_state <= n_IC_state; //------------------------------------------
always @ (posedge state_finish_flag or negedge reset)
if (!reset)
begin
SEND_buffer <= 'd0;
n_IC_state <= 'd0;
end
else if(IC_state == `INITIAL)
case (step_counter)
: begin state <= `START;end
: begin state <= `Se; SEND_buffer <= `WriteAddress; end
: begin state <= `Se; SEND_buffer <= `ModelAddress; end
: begin state <= `Se; SEND_buffer <= `RegAAddress; end
: begin state <= `STOP; end : begin n_IC_state <= `MEASURE; end
default state <= `START;
endcase
else if (IC_state == `MEASURE)
case (step_counter)
: begin state <= `START; end
: begin state <= `Se; SEND_buffer <= `WriteAddress; end
: begin state <= `Se; SEND_buffer <= `X_MSBAddress; end
: begin state <= `START; end
: begin state <= `Se; SEND_buffer <= `ReadAddress;end
: begin state <= `Re; end
: begin state <= `Re; end
: begin state <= `Re; end
: begin state <= `Re; end
: begin state <= `Re; end
: begin state <= `Re; end
: begin state <= `STOP; end :begin n_IC_state <= `INITIAL; end
default state <= `START;
endcase
else ;
//----------状态机控制模块----------end--- --- //----------执行步骤计数--------start------------
always @ (posedge clock or negedge reset)
if (!reset) begin
step_counter <= 'd0; end
else if((IC_state == `INITIAL)&& (state_finish_flag)) begin
if(step_counter == 'd5) step_counter <= 4'd0;
else step_counter <= step_counter + ; end
else if((IC_state == `MEASURE) && (state_finish_flag)) begin
if(step_counter == 'd12 ) step_counter <= 4'd0;
else step_counter <= step_counter + ; end
else ;
//----------执行步骤计数--------end------------ //-----------外部数据线 控制模块----start------
always @ (posedge clock or negedge reset)
if(!reset)
begin
Re_buffer <= 'd0;
sda_enable <= 'd0;
scl_enable <= 'd0;
delay_enable <= 'd0;
end
else case (state)
`START : case (scl_4)
: begin sda_enable <= 'd1; scl_enable <= 1'd1; delay_enable <= 'd0;
sda_reg <= 'd1; scl_reg <= 1'd0; end
: begin sda_reg <= 'd1; scl_reg <= 1'd0; end
: begin sda_reg <= 'd1; scl_reg <= 1'd1; end
: begin sda_reg <= 'd0; scl_reg <= 1'd1; end
: delay_enable <= 'd1;
default begin sda_reg <= 'dz; scl_reg <= 1'dz; end
endcase
`STOP : case (scl_4)
: begin sda_enable <= 'd1; scl_enable <= 1'd1; delay_enable <= 'd0;
sda_reg <= 'd0; scl_reg <= 1'd0; end
: begin sda_reg <= 'd0; scl_reg <= 1'd0; end
: begin sda_reg <= 'd0; scl_reg <= 1'd1; end
: begin sda_reg <= 'd1; scl_reg <= 1'd1; end
: delay_enable <= 'd1;
default begin sda_reg <= 'dz; scl_reg <= 1'dz; end
endcase
`Se : case (scl_4)
: begin sda_enable <= 'd1; scl_enable <= 1'd1; delay_enable <= 'd0;
sda_reg <= 'd0; scl_reg <= 1'd0; end
: begin sda_reg <= SEND_buffer[]; scl_reg <= 'd0; end
: begin sda_reg <= SEND_buffer[]; scl_reg <= 'd1; end
: begin sda_reg <= SEND_buffer[]; scl_reg <= 'd1; end
: begin scl_reg <= 'd0; end
: begin sda_reg <= SEND_buffer[]; scl_reg <= 'd0; end
: begin sda_reg <= SEND_buffer[]; scl_reg <= 'd1; end
: begin sda_reg <= SEND_buffer[]; scl_reg <= 'd1; end
: begin scl_reg <= 'd0; end
: begin sda_reg <= SEND_buffer[]; scl_reg <= 'd0; end
: begin sda_reg <= SEND_buffer[]; scl_reg <= 'd1; end
: begin sda_reg <= SEND_buffer[]; scl_reg <= 'd1; end
: begin scl_reg <= 'd0; end
: begin sda_reg <= SEND_buffer[]; scl_reg <= 'd0; end
: begin sda_reg <= SEND_buffer[]; scl_reg <= 'd1; end
: begin sda_reg <= SEND_buffer[]; scl_reg <= 'd1; end
: begin scl_reg <= 'd0; end
: begin sda_reg <= SEND_buffer[]; scl_reg <= 'd0; end
: begin sda_reg <= SEND_buffer[]; scl_reg <= 'd1; end
: begin sda_reg <= SEND_buffer[]; scl_reg <= 'd1; end
: begin scl_reg <= 'd0; end
: begin sda_reg <= SEND_buffer[]; scl_reg <= 'd0; end
: begin sda_reg <= SEND_buffer[]; scl_reg <= 'd1; end
: begin sda_reg <= SEND_buffer[]; scl_reg <= 'd1; end
: begin scl_reg <= 'd0; end
: begin sda_reg <= SEND_buffer[]; scl_reg <= 'd0; end
: begin sda_reg <= SEND_buffer[]; scl_reg <= 'd1; end
: begin sda_reg <= SEND_buffer[]; scl_reg <= 'd1; end
: begin scl_reg <= 'd0; end
: begin sda_reg <= SEND_buffer[]; scl_reg <= 'd0; end
: begin sda_reg <= SEND_buffer[]; scl_reg <= 'd1; end
: begin sda_reg <= SEND_buffer[]; scl_reg <= 'd1; end
//此时序之后必须释放掉sda控制权 等待响应
: begin sda_enable <= 'd0; scl_enable <= 1'd0; delay_enable <= 'd1;
ack_reg <= 'd0; scl_reg <= 1'd0; end //这里出现了一个警告,因为ack_reg没有利用到
: begin ack_reg <= sda; scl_reg <= 'd0; end
: begin ack_reg <= sda; scl_reg <= 'd1; end
: begin ack_reg <= sda; scl_reg <= 'd1; end
//此时IC有可能在一个时钟下来不及响应,从而导致主机没有接收到响应信号
default begin scl_reg <= 'dz; end
endcase `Re : case (scl_4)
: begin sda_enable <= 'd0; scl_enable <= 1'd1; delay_enable <= 'd0;
scl_reg <= 'd0; end
: begin scl_reg <= 'd0; end
: begin scl_reg <= 'd1; end
: begin Re_buffer[] <= sda; scl_reg <= 'd1; end
: begin scl_reg <= 'd0; end
: begin scl_reg <= 'd0; end
: begin scl_reg <= 'd1; end
: begin Re_buffer[] <= sda; scl_reg <= 'd1; end
: begin scl_reg <= 'd0; end
: begin scl_reg <= 'd0; end
: begin scl_reg <= 'd1; end
: begin Re_buffer[] <= sda; scl_reg <= 'd1; end
: begin scl_reg <= 'd0; end
: begin scl_reg <= 'd0; end
: begin scl_reg <= 'd1; end
: begin Re_buffer[] <= sda; scl_reg <= 'd1; end
: begin scl_reg <= 'd0; end
: begin scl_reg <= 'd0; end
: begin scl_reg <= 'd1; end
: begin Re_buffer[] <= sda; scl_reg <= 'd1; end
: begin scl_reg <= 'd0; end
: begin scl_reg <= 'd0; end
: begin scl_reg <= 'd1; end
: begin Re_buffer[] <= sda; scl_reg <= 'd1; end
: begin scl_reg <= 'd0; end
: begin scl_reg <= 'd0; end
: begin scl_reg <= 'd1; end
: begin Re_buffer[] <= sda; scl_reg <= 'd1; end
: begin scl_reg <= 'd0; end
: begin scl_reg <= 'd0; end
: begin scl_reg <= 'd1; end
: begin Re_buffer[] <= sda; scl_reg <= 'd1; end
//此时序之后主机夺回sda控制权,延时开始
: begin sda_enable <= 'd1; delay_enable <= 1'd1;
sda_reg <= ; scl_reg <= 'd0; end
: begin sda_reg <= ; scl_reg <= 'd0; end
: begin sda_reg <= ; scl_reg <= 'd1; end
: begin sda_reg <= ; scl_reg <= 'd1; end
default begin sda_reg <= 'dz; scl_reg <= 1'dz;end
endcase
endcase
//-------外部数据线 控制模块----------end--------------- assign sda = sda_enable ? sda_reg : 'dz ;
assign scl = scl_enable ? scl_reg : 'dz ;
//==================================================================================== //--------------数码管显示部分=======只显示了一部分,有待优化============================
always @ ( posedge clock or negedge reset)
if (!reset)
begin
sel_seg <= 'b111110;
end
else if(state_finish_flag)
begin
sel_seg <= {sel_seg[:],sel_seg[]};
end
//------------------------------------------------
always @(posedge clock or negedge reset)
if (!reset) out_seg <= 'd0;
else
begin
case (Re_buffer[:]) //刚开始这里没有[3:0] 居然没有报错,连个警告都没有,这是为什么呢
'b0000 : out_seg<=8'b1100_0000;//0000_0011
'b0001 : out_seg<=8'b1111_1001;//1001_1111
'b0010 : out_seg<=8'b1010_0100;//0010_0101
'b0011 : out_seg<=8'b1011_0000;//0000_1101
'b0100 : out_seg<=8'b1001_1001;//1001_1001
'b0101 : out_seg<=8'b1001_0010;//0100_1001
'b0110 : out_seg<=8'b1000_0010;//0100_0001
'b0111 : out_seg<=8'b1111_1000;//0001_1111
'b1000 : out_seg<=8'b1000_0000;//0000_0001
'b1001 : out_seg<=8'b1001_1000;//0001_1001
'b1010 : out_seg<=8'b1000_1000;//0001_0001
'b1011 : out_seg<=8'b1000_0011;//1100_0001
'b1100 : out_seg<=8'b1100_0110;//0110_0011
'b1101 : out_seg<=8'b1010_0001;//1000_0101
'b1110 : out_seg<=8'b1000_0110;//0110_0001
'b1111 : out_seg<=8'b1000_1110;//0111_0001
endcase
end
//-------------------------------================================= endmodule
Verilog实现IIC通讯第二版的更多相关文章
- Verilog笔记——Verilog数字系统设计(第二版)夏宇闻
本片记录Verilog学习笔记,主要是和以往用的C语言的不同之处,以例子.代码的形式记录.学习以<Verilog数字系统设计>(第二版)为参考资料,援助作者夏宇闻. 1. C语言和Veri ...
- 用Verilog实现IIC通讯
注意,此代码是错误代码,并不能实现想要的结果. 之所以留着,因为里面的enable 是独立开来的思想值得借鉴.就是控制单元和运算单元分开(我也是借鉴别人的实现思想).具体用verilogHDL实现II ...
- 一套简单的web即时通讯——第二版
前言 接上一版,这一版的页面与功能都有所优化,具体如下: 1.优化登录拦截 2.登录后获取所有好友并区分显示在线.离线好友,好友上线.下线都有标记 3.将前后端交互的值改成用户id.显示值改成昵称ni ...
- python核心编程第二版笔记
python核心编程第二版笔记由网友提供:open168 python核心编程--笔记(很详细,建议收藏) 解释器options:1.1 –d 提供调试输出1.2 –O 生成优化的字节码(生成 ...
- 读书笔记:JavaScript DOM 编程艺术(第二版)
读完还是能学到很多的基础知识,这里记录下,方便回顾与及时查阅. 内容也有自己的一些补充. JavaScript DOM 编程艺术(第二版) 1.JavaScript简史 JavaScript由Nets ...
- 《selenium2 Java 自动化测试实战(第二版)》 更新2016.5.3
java 版来了!! 本文档在<selenium2 Python 自动化测试实战>的基础上,将代码与实例替换为java ,当然,部分章节有变更.这主要更语言本身的特点有关.集合和java下 ...
- 《Java程序设计与数据结构教程(第二版)》学习指导
<Java程序设计与数据结构教程(第二版)>学习指导 欢迎关注"rocedu"微信公众号(手机上长按二维码) 做中教,做中学,实践中共同进步! 原文地址:http:// ...
- (转载)持续集成(第二版)[来自:Martin Fowler]
转载自:iTech的博客 持续集成(第二版) 作者:Martin Fowler 译者:雷镇 持续集成 是一种软件开发实践.在持续集成中,团队成员频繁集成他们的工作成果,一般每人每天至少集成一次,也可以 ...
- Learning ROS for Robotics Programming - Second Edition(《学习ROS机器人编程-第二版》)
Learning ROS for Robotics Programming - Second Edition <学习ROS机器人编程-第二版> ----Your one-stop guid ...
随机推荐
- [LeetCode]题解(python):099-Recover Binary Search Tree
题目来源: https://leetcode.com/problems/recover-binary-search-tree/ 题意分析: 二叉搜索树中有两个点错了位置,恢复这棵树. 题目思路: 如果 ...
- Android Launcher label和Main Activity保持不一致
最近在开发一个库存管理的App,AndroidMainifest.xml中是这样设定的: <application android:allowBackup="true" an ...
- Mysql的Debug模式实现
前一段领导开发了一个内核的模块,测试的过程中,发现导致MYSQL客户端无法连接服务器. 经过查询文档,追根溯源,终于找到了MYSQL实现链接客户端的代码,在源文件sql-common/client.c ...
- 推荐一些socket工具,TCP、UDP调试、抓包工具 推荐一些socket工具,TCP、UDP调试、抓包工具
还记得我在很久很久以前和大家推荐的Fiddler和Charles debugger么?他们都是HTTP的神器级调试工具,非常非常的好用.好工具能让你事半功倍,基本上,我是属于彻头彻尾的工具控. 假如有 ...
- 【opengl】OpenGL中三维物体显示在二维屏幕上显示的变换过程
转自:http://blog.sina.com.cn/s/blog_957b9fdb0100zesv.html 为了说明在三维物体到二维图象之间,需要经过什么样的变换,我们引入了相机(Camera)模 ...
- STC12C5201AD AD采样+串口发送模板
#include<reg52.h> sfr ADC_CONTR = 0xBC; //ADC control register sfr ADC_RES = 0xBD; //ADC 8-bit ...
- vs linq to db template
linq to db template 支持sqlite. mysql .db2. accress. oracle. Firebird等多种数据库以linq方式操作数据. NuGet NuGet 是 ...
- Python学习入门基础教程(learning Python)--2.2 Python下的变量基础
变量的基本概念,变量可以这样去理解,变量是一个值,这个值存储在计算机的内存里.以网购为例,您在选购傻商品的时候,是在不同页面里选不同的商品,选好一件点击“放入购物车”,选完了再点击去结帐,这些商品的价 ...
- 定制ToolChain for ARM
**************************************************************************编写:王卫无,北京讯业互联科技有限公司版本号:V1. ...
- C# Best Practices - Specify Clear Method Parameters
Improve parameters parameter order public OperationResult PlaceOrder(Product product, int quantity, ...