注意,此代码是错误代码,并不能实现想要的结果。

之所以留着,因为里面的enable 是独立开来的思想值得借鉴。就是控制单元和运算单元分开(我也是借鉴别人的实现思想)。具体用verilogHDL实现IIC通讯在http://www.cnblogs.com/sepeng/p/3258705.html 里会做到。

 //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 MEASURE `define START
`define Re
`define Se
`define STOP //`default_nettype none module HMC5883_IIC (
clock,reset,
sda,scl,
out_seg, sel_seg
); input clock,reset;
inout sda,scl;
output reg [:]out_seg; output reg [:]sel_seg; reg [:] SEND_buffer;
reg [:] Re_buffer ;
reg sda_reg;
reg scl_reg;
reg sda_enable;
reg scl_enable; reg IC_state;
reg [:]state; //当前状态寄存器
reg state_finish_flag ; // reg [:] clk_counter;
reg [:] step_counter;
reg [:] clk_temp ; //-------------------------时钟控制模块-------------------------------start------------------
always @ (posedge clock or negedge reset)
if (!reset)
begin
clk_temp <= 'd0;
clk_counter <= 'd0;
end
else if (clk_temp[])
begin
clk_temp <= 'd0;
if (state_finish_flag) clk_counter <= 'd0;
else if (clk_counter == 'b111_111) //这里是一个保护机制,可以设置一个flag
begin
clk_counter <= 'b111_111;
end
else clk_counter <= clk_counter + ;
end
else clk_temp <= clk_temp + ;
//-------------------------时钟控制模块-----------------------------------end------------------ //-----------------状态机控制模块------------------------------------------start-------------------
always @ (posedge clock or negedge reset)
if(!reset)
IC_state <= `INITIAL;
else IC_state <= `MEASURE; always @ (posedge clock or negedge reset)
if (!reset)
begin
step_counter <= 'd0;
SEND_buffer <= 'd0;
end
else if(IC_state == `INITIAL)
case (step_counter)
: begin state <= `START; step_counter <= step_counter + ;end
: begin SEND_buffer <= 'h3c;state <= `Se; step_counter <= step_counter + 1; end
: begin SEND_buffer <= 'h02;state <= `Se; step_counter <= step_counter + 1; end
: begin SEND_buffer <= 'h00;state <= `Se; step_counter <= step_counter + 1; end
: begin SEND_buffer <= 'h00;state <= `Se; step_counter <= step_counter + 1; end
default step_counter <= 'd0;
endcase
else if (IC_state == `MEASURE)
case (step_counter)
: begin state <= `START; step_counter <= step_counter + ;end
: begin SEND_buffer <= 'h3c;state <= `Se; step_counter <= step_counter + 1; end
: begin SEND_buffer <= 'h03;state <= `Se; step_counter <= step_counter + 1; end
: begin state <= `START; step_counter <= step_counter + ;end
: begin SEND_buffer <= 'h3d;state <= `Se; step_counter <= step_counter + 1; end : begin state <= `Re; step_counter <= step_counter + ; end
: begin state <= `Re; step_counter <= step_counter + ; end
: begin state <= `Re; step_counter <= step_counter + ; end
: begin state <= `Re; step_counter <= step_counter + ; end
: begin state <= `Re; step_counter <= step_counter + ; end
: begin state <= `Re; step_counter <= step_counter + ; end
: begin state <= `Re; step_counter <= step_counter + ; end
: begin state <= `Re; step_counter <= step_counter + ; end : begin state <= `STOP; step_counter <= 'd0; end
default step_counter <= 'd0;
endcase //-----------------状态机控制模块-------------------------------------------end--------------------- //------------------------------------------使能控制模块--------------------------start---------------
always @ (posedge clk_counter or negedge reset)
if (!reset)
begin
sda_enable <= 'd0;
scl_enable <= 'd0;
end
else case (state)
`START : begin
if(clk_counter >= 'd4)
begin
sda_enable <= 'd0;
scl_enable <= 'd0;
end
else
begin
sda_enable <= 'd1;
scl_enable <= 'd1;
end
end `STOP : begin
if(clk_counter >= 'd4)
begin
sda_enable <= 'd0;
scl_enable <= 'd0;
end
else
begin
sda_enable <= 'd1;
scl_enable <= 'd1;
end
end `Se : begin
if(clk_counter >= 'd36) //send 完成之后释放掉使能
begin
sda_enable <= 'd0;
scl_enable <= 'd0;
end
else
begin
sda_enable <= 'd1;
scl_enable <= 'd1;
end
end `Re : begin
if(clk_counter >= 'd32) //receive 完成之后释放掉使能
begin
sda_enable <= 'd0;
scl_enable <= 'd0;
end
else
begin
sda_enable <= 'd1;
scl_enable <= 'd1;
end
end
endcase
//------------------------------------------使能控制模块--------------------------end--------------- //------------------------------------------------外部数据线 控制模块-----------------start---------------
always @ (posedge clk_counter or negedge reset)
if(!reset)
begin
sda_reg <= 'd0;
scl_reg <= 'd0;
state_finish_flag <= 'd0;
Re_buffer <= 'd0;
end
else case (state)
`START : begin
case (clk_counter)
: begin sda_reg <= 'd1; scl_reg <= 1'd0; state_finish_flag <= 'd0; end
: begin sda_reg <= 'd1; scl_reg <= 1'd0; state_finish_flag <= 'd0; end
: begin sda_reg <= 'd1; scl_reg <= 1'd1; state_finish_flag <= 'd0; end
: begin sda_reg <= 'd0; scl_reg <= 1'd1; state_finish_flag <= 'd0; end
default begin sda_reg <= 'dz; scl_reg <= 1'dz; state_finish_flag <= 'd1; end
endcase
end `STOP : begin
case (clk_counter)
: begin sda_reg <= 'd0; scl_reg <= 1'd0; state_finish_flag <= 'd0; end
: begin sda_reg <= 'd0; scl_reg <= 1'd0; state_finish_flag <= 'd0; end
: begin sda_reg <= 'd0; scl_reg <= 1'd1; state_finish_flag <= 'd0; end
: begin sda_reg <= 'd1; scl_reg <= 1'd1; state_finish_flag <= 'd0; end
default begin sda_reg <= 'dz; scl_reg <= 1'dz; state_finish_flag <= 'd1; end
endcase
end `Se : begin
case (clk_counter)
: begin sda_reg <= 'd0; scl_reg <= 1'd0; end
: begin sda_reg <= SEND_buffer[]; scl_reg <= 'd0; state_finish_flag <= 1'd0; end
: begin sda_reg <= SEND_buffer[]; scl_reg <= 'd1; state_finish_flag <= 1'd0; end
: begin sda_reg <= SEND_buffer[]; scl_reg <= 'd1; state_finish_flag <= 1'd0; end
: begin scl_reg <= 'd0; state_finish_flag <= 1'd0; end
: begin sda_reg <= SEND_buffer[]; scl_reg <= 'd0; state_finish_flag <= 1'd0; end
: begin sda_reg <= SEND_buffer[]; scl_reg <= 'd1; state_finish_flag <= 1'd0; end
: begin sda_reg <= SEND_buffer[]; scl_reg <= 'd1; state_finish_flag <= 1'd0; end
: begin scl_reg <= 'd0; state_finish_flag <= 1'd0; end
: begin sda_reg <= SEND_buffer[]; scl_reg <= 'd0; state_finish_flag <= 1'd0; end
: begin sda_reg <= SEND_buffer[]; scl_reg <= 'd1; state_finish_flag <= 1'd0; end
: begin sda_reg <= SEND_buffer[]; scl_reg <= 'd1; state_finish_flag <= 1'd0; end
: begin scl_reg <= 'd0; state_finish_flag <= 1'd0; end
: begin sda_reg <= SEND_buffer[]; scl_reg <= 'd0; state_finish_flag <= 1'd0; end
: begin sda_reg <= SEND_buffer[]; scl_reg <= 'd1; state_finish_flag <= 1'd0; end
: begin sda_reg <= SEND_buffer[]; scl_reg <= 'd1; state_finish_flag <= 1'd0; end
: begin scl_reg <= 'd0; state_finish_flag <= 1'd0; end
: begin sda_reg <= SEND_buffer[]; scl_reg <= 'd0; state_finish_flag <= 1'd0; end
: begin sda_reg <= SEND_buffer[]; scl_reg <= 'd1; state_finish_flag <= 1'd0; end
: begin sda_reg <= SEND_buffer[]; scl_reg <= 'd1; state_finish_flag <= 1'd0; end
: begin scl_reg <= 'd0; state_finish_flag <= 1'd0; end
: begin sda_reg <= SEND_buffer[]; scl_reg <= 'd0; state_finish_flag <= 1'd0; end
: begin sda_reg <= SEND_buffer[]; scl_reg <= 'd1; state_finish_flag <= 1'd0; end
: begin sda_reg <= SEND_buffer[]; scl_reg <= 'd1; state_finish_flag <= 1'd0; end
: begin scl_reg <= 'd0; state_finish_flag <= 1'd0; end
: begin sda_reg <= SEND_buffer[]; scl_reg <= 'd0; state_finish_flag <= 1'd0; end
: begin sda_reg <= SEND_buffer[]; scl_reg <= 'd1; state_finish_flag <= 1'd0; end
: begin sda_reg <= SEND_buffer[]; scl_reg <= 'd1; state_finish_flag <= 1'd0; end
: begin scl_reg <= 'd0; state_finish_flag <= 1'd0; end
: begin sda_reg <= SEND_buffer[]; scl_reg <= 'd0; state_finish_flag <= 1'd0; end
: begin sda_reg <= SEND_buffer[]; scl_reg <= 'd1; state_finish_flag <= 1'd0; end
: begin sda_reg <= SEND_buffer[]; scl_reg <= 'd1; state_finish_flag <= 1'd0; end : begin /*state_finish_flag <= sda;*/ scl_reg <= 'd0; /*state_finish_flag <= 1'd0; */ end
: begin /*state_finish_flag <= sda;*/ scl_reg <= 'd0; /*state_finish_flag <= 1'd0; */ end
: begin /*state_finish_flag <= sda;*/ scl_reg <= 'd1; /*state_finish_flag <= 1'd0; */ end
: begin state_finish_flag <= sda; scl_reg <= 'd1; /*state_finish_flag <= 1'd0; */ end default begin scl_reg <= 'dz; state_finish_flag <= sda; end //等待响应
endcase
end `Re : begin
case (clk_counter)
: begin scl_reg <= 'd0; state_finish_flag <= 1'd0; end
: begin scl_reg <= 'd0; state_finish_flag <= 1'd0; end
: begin scl_reg <= 'd1; state_finish_flag <= 1'd0; end
: begin Re_buffer[] <= sda; scl_reg <= 'd1; state_finish_flag <= 1'd0; end
: begin scl_reg <= 'd0; state_finish_flag <= 1'd0; end
: begin scl_reg <= 'd0; state_finish_flag <= 1'd0; end
: begin scl_reg <= 'd1; state_finish_flag <= 1'd0; end
: begin Re_buffer[] <= sda; scl_reg <= 'd1; state_finish_flag <= 1'd0; end : begin scl_reg <= 'd0; state_finish_flag <= 1'd0; end
: begin scl_reg <= 'd0; state_finish_flag <= 1'd0; end
: begin scl_reg <= 'd1; state_finish_flag <= 1'd0; end
: begin Re_buffer[] <= sda; scl_reg <= 'd1; state_finish_flag <= 1'd0; end
: begin scl_reg <= 'd0; state_finish_flag <= 1'd0; end
: begin scl_reg <= 'd0; state_finish_flag <= 1'd0; end
: begin scl_reg <= 'd1; state_finish_flag <= 1'd0; end
: begin Re_buffer[] <= sda; scl_reg <= 'd1; state_finish_flag <= 1'd0; end
: begin scl_reg <= 'd0; state_finish_flag <= 1'd0; end
: begin scl_reg <= 'd0; state_finish_flag <= 1'd0; end
: begin scl_reg <= 'd1; state_finish_flag <= 1'd0; end
: begin Re_buffer[] <= sda; scl_reg <= 'd1; state_finish_flag <= 1'd0; end
: begin scl_reg <= 'd0; state_finish_flag <= 1'd0; end
: begin scl_reg <= 'd0; state_finish_flag <= 1'd0; end
: begin scl_reg <= 'd1; state_finish_flag <= 1'd0; end
: begin Re_buffer[] <= sda; scl_reg <= 'd1; state_finish_flag <= 1'd0; end
: begin scl_reg <= 'd0; state_finish_flag <= 1'd0; end
: begin scl_reg <= 'd0; state_finish_flag <= 1'd0; end
: begin scl_reg <= 'd1; state_finish_flag <= 1'd0; end
: begin Re_buffer[] <= sda; scl_reg <= 'd1; state_finish_flag <= 1'd0; end
: begin scl_reg <= 'd0; state_finish_flag <= 1'd0; end
: begin scl_reg <= 'd0; state_finish_flag <= 1'd0; end
: begin scl_reg <= 'd1; state_finish_flag <= 1'd0; end
: begin Re_buffer[] <= sda; scl_reg <= 'd1; state_finish_flag <= 1'd0; end // 32:begin sda_reg <= 1'd0; scl_reg <= 1'd0; state_finish_flag <= 1'd0; end //NA
// 33:begin sda_reg <= 1'd0; scl_reg <= 1'd0; state_finish_flag <= 1'd0; end
// 34:begin sda_reg <= 1'd0; scl_reg <= 1'd1; state_finish_flag <= 1'd0; end
// 35:begin sda_reg <= 1'd0; scl_reg <= 1'd1; state_finish_flag <= 1'd0; end default begin sda_reg <= 'dz; scl_reg <= 1'dz; state_finish_flag <= 'd1; end
endcase
end
endcase
//------------------------------------------------外部数据线 控制模块-----------------end--------------- assign sda = sda_enable ? sda_reg : 'dz ;
assign scl = scl_enable ? scl_reg : 'dz ; always @ (posedge clk_counter 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)
'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通讯的更多相关文章

  1. Verilog实现IIC通讯第二版

    HMC5883三轴磁力传感器IIC通讯模块的VerilogHDL的实现 上一版并没有实现我想要的功能 0.0.1版   正在修订中   2013/9/2 //date :2013/7/7 //desi ...

  2. LPC1768的iic通讯

    LPC1768有三路IIC,其中IIC0支持高速模式和plus模式,另外两路是普通IIC,使用IIC的过程如下 首先依然是打开IIC时钟,同时打开GPIO时钟 然后配置引脚为IIC功能 另外,因为ii ...

  3. STM32作为主设备,Arduino作为从设备进行IIC通讯的注意要点

    近日公司的项目重心要往米思齐的Arduino图形化编程上转移了,需要我将STM32和Arduino的IIC通讯调通.之前Arduino并没怎么使用过,仅仅是将超声波的代码移植成TOF激光测距而已.网上 ...

  4. 基于esp32的IIC通讯

    本文源码地址在:http://download.csdn.net/download/noticeable/9962029 IIC 通讯应该是当代比较常用的几种通讯方式之一,其无需特殊的IO接口,连线方 ...

  5. iic通讯 FPGA实现 mpu6050为例

    IIC最常用的通讯协议,但普遍用于单片机.arm这些,用FPGA实现大材小用,但对于菜鸡水平练练手很不错,考验串并转换和时序的控制.今天我就以mpu6050陀螺仪为例,实现FPGA的iic通信. 1. ...

  6. 「STM32 」IIC通讯原理及其实验

    I2C两线式串行总线通讯协议,它是由飞利浦开发的,主要用于连接微控制器及其外围设备之间,它是由数据线SDA和信号线SCL构成的,可发送和接收数据即在MUC和I2C设备之间,I2C和I2C之间进行全双工 ...

  7. IIC通讯协议(非原创,转载他人,用于学习)

    I2C协议:1.空闲状态 2.开始信号 3.停止信号 4.应答信号 5.数据的有效性 6.数据传输 IIC详解 1.I2C总线具有两根双向信号线,一根是数据线SDA,另一根是时钟线SCL 2.IIC总 ...

  8. IIC通讯程序

    IIC程序 IIC起始信号 void IIC_Start(void) { SDA_OUT();//sda设为输出 IIC_SDA=; IIC_SCL=; delay_us();//延时一段时间,具体时 ...

  9. 填坑-关于IIC通讯

    ​01.概述 在之前的文章中<STM32IIC详解>中详细讲解了IIC协议,并且使用是NXP的官方手册,demo示例使用IIC读取RTC芯片,运行正常,没有任何问题.并且更新了<II ...

随机推荐

  1. LintCode- 删除排序数组中的重复数字

    题目描述: 给定一个排序数组,在原数组中删除重复出现的数字,使得每个元素只出现一次,并且返回新的数组的长度. 不要使用额外的数组空间,必须在原地没有额外空间的条件下完成. 样例 给出数组A =[1,1 ...

  2. 12个高矮不同的人,排成两排(catalan数)

    问题描述: 12个高矮不同的人,排成两排,每排必须是从矮到高排列,而且第二排比对应的第一排的人高,问排列方式有多少种? 这个笔试题,很YD,因为把某个递归关系隐藏得很深. 问题分析: 我们先把这12个 ...

  3. Android开发之TextView高级应用

    Android开发之TextView高级应用 我们平时使用TextView往往让它作为一个显示文字的容器,但TextView的功能并不局限于此.以下就和大家分享一下TextView的一些使用技巧. A ...

  4. phpmyadmin出现空password登录被禁止

    在Windows或者Linux下mysql安装后默认的password为空,又当我们又安装了mysql的管理工具 phpmyadmin后登陆时出现"空password登陆呗禁止(參见同意pa ...

  5. Objective-c Category(类别)

    category是Objective-c里面最常用的功能之一. category可以为已经存在的类增加方法,而不需要增加一个子类. 类别接口的标准语法格式如下: #import "类名.h& ...

  6. C# ikvm 运行htmlunit Provider com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl not found

    在使用 ikvm 去运行 htmlunit 中的 webclient Getpage的时候  报错说com.sun.org.apache.xerces.internal.jaxp.DocumentBu ...

  7. [置顶] 浅谈大型web系统架构

    转载原文:http://blog.csdn.net/dinglang_2009/article/details/6863697 分类: 大规模Web 2.0架构 2011-10-11 18:27 12 ...

  8. java线程之停止线程

         在Java中有以下3种方法可以终止一个正在运行的线程:      1.使用退出标志,是线程正常退出,也就是run方法完成后线程终止.      2.使用stop方法强制终止线程,但不推荐使用 ...

  9. Enze Third day(c#中选择结构【if...else】)

    哈喽,又到了我总结课堂知识的时间了.今天在云和学院学的是C#中的“选择结构”下的If语句.下面就来总结一下今天所学的吧. 理论:If语句是最常用的选择结构语句.它主要根据所给定的条件(常由关系表达式和 ...

  10. Socket 基础解析使用ServerSocket建立聊天服务器

    很简单的教程哦! 1.socket 简介 Socket 又称"套接字",应用程序通常通过"套接字"向网络发出请求或者应答网络请求.ServerSocket 用于 ...