I2C Verilog的实现(一)
- <span style="font-size:14px;">`timescale 1ns / 1ps
- module test(
- sda
- );
- reg scl;
- inout sda;
- reg sda_out;
- wire sda_in;
- reg [7:0] data;
- reg start_flag, stop_flag;
- assign sda = sda_out ? 1'bz : 1'b0;
- assign sda_in = sda;
- pullup( sda );
- I2CTEST testmine(.SDA(sda), .SCL(scl));
- initial
- begin
- scl = 0;
- sda_out = 0;
- data = 8'h27;
- start_flag = 0;
- #160000;
- start ( );
- end
- always
- begin
- #50000 scl = ~scl;
- end
- always @ (posedge start_flag)
- begin
- repeat (8)
- begin
- wait ( scl == 0 );
- #20000;
- sda_out = data[7];
- #40000;
- data = data << 1;
- end
- wait (~ scl);
- #20000;
- sda_out = 1;
- #160000;
- stop ( );
- end
- always @ ( posedge stop_flag)
- begin
- // sda_out = 0;
- // #50000;
- sda_out = 1;
- end
- task start;
- begin
- wait (scl == 0);
- #20000;
- sda_out = 1;
- wait ( scl == 1 );
- #20000;
- sda_out = 0;
- start_flag = 1;
- end
- endtask
- task stop;
- begin
- wait ( scl == 0 );
- #20000;
- sda_out = 0;
- wait ( scl ==1 );
- #20000;
- sda_out = 1;
- stop_flag = 1;
- end
- endtask
- endmodule</span>
I2C程序
- <span style="font-family:Arial;">`timescale 1ns / 1ps
- module I2CTEST(
- SDA, SCL
- );
- input SCL;
- inout SDA;
- // The 7-bits address that we want for our I2C slave
- parameter I2C_ADR = 7'h13;
- //---------------------------------------------
- //start,stop condition judgement
- //---------------------------------------------
- wire start, stop;
- reg sda1, sda2;
- reg sda11;
- always @ ( posedge SCL )
- sda1 <= SDA;
- always @ ( negedge SCL )
- sda2 <= SDA;
- always @ ( negedge SCL )
- sda11 <= sda1;
- assign start = sda11 & (!sda2);
- assign stop = sda2 & ( !sda11 );
- //----------------------------------------------
- //count setting
- //----------------------------------------------
- reg [3:0] bitcont;
- wire bit_ack = bitcont[3];
- always @ ( posedge SCL or posedge start)
- begin
- if ( start )
- bitcont <= 4'h6;
- else
- begin
- if (bit_ack)
- bitcont <= 4'h6;
- else
- bitcont <= bitcont -4'h1;
- end
- end
- //-------------------------------------
- //get sda using posedge scl
- //-------------------------------------
- reg sdar;
- always @ ( posedge SCL ) sdar <= SDA;
- //----------------------------------------
- //address match
- //----------------------------------------
- reg addr_match, op_read;
- always @ ( negedge SCL or posedge start )
- begin
- if ( start )
- begin
- addr_match <= 1'h1;
- op_read <= 1'h0;
- end
- else
- begin
- if( (bitcont == 6) & (sdar != I2C_ADR[6])) addr_match <= 1'h0;
- if( (bitcont == 5) & (sdar != I2C_ADR[5])) addr_match <= 1'h0;
- if( (bitcont == 4) & (sdar != I2C_ADR[4])) addr_match <= 1'h0;
- if( (bitcont == 3) & (sdar != I2C_ADR[3])) addr_match <= 1'h0;
- if( (bitcont == 2) & (sdar != I2C_ADR[2])) addr_match <= 1'h0;
- if( (bitcont == 1) & (sdar != I2C_ADR[1])) addr_match <= 1'h0;
- if( (bitcont == 0) & (sdar != I2C_ADR[0])) addr_match <= 1'h0;
- if( bitcont == 0 ) op_read <= sdar;
- end
- end
- //-----------------------------------------------------------------------
- //send ack
- //-----------------------------------------------------------------------
- reg ack_assert;
- always @ ( negedge SCL )
- begin
- if ( bit_ack & addr_match & op_read )
- ack_assert <= 1'h1;
- else
- ack_assert <= 1'h0;
- end
- //-------------------------------------------------------------------------
- //control SDA line
- //-------------------------------------------------------------------------
- assign SDA = ack_assert ? 1'h0 : 1'hz;
- pullup ( SDA );
- endmodule
I2C Verilog的实现(一)的更多相关文章
- I2C Verilog的实现(二)
1. 起始结束信号的判断 //--------------------------------------------- //start,stop condition judgement //---- ...
- Verilog之i2c合约
说明:i2c乔布斯.有这么多的事情在网上参考. 时刻:2014年5一个月6周二星期 1.问题叙述性说明: 正如图.已知的时钟clk为100k,rst为复位信号.上升沿有效,基于Verilog HDL或 ...
- i2c状态机方法设计-verilog
2010-09-05 21:04:00 verilog语言基础学的差不多了.接着就是看看华为的语言编写规范.状态机设计方法是fpga的重要设计方法.所以我要记上一笔. 只要会FSM方法,用fpga编写 ...
- I2C控制器的Verilog建模之三(完结版)
前言:终于到了测试篇,不过悲剧了一下.按照之前<二>里面的思路,在顶层用一个复用器驱动读写独立模块的I2C总线确实失败.虽然综合过去了,不过警告里已经说明:底层的2个原本是inout三态口 ...
- I2C控制器的Verilog建模之二
前言:接着上一篇的I2C写操作,今天要实现一个I2C的读操作.虽然在ADV7181B配置内部寄存器时没有必要使用到读操作,但是为了进一步确认寄存器是否在I2C写模块下被正确配置,这一步是必不可少的. ...
- I2C控制器的Verilog建模之一
前言:之前申请了ADI公司的一款ADV7181CBSTZ的视频解码芯片,正好原装DE2板子安的是同系列的ADV7181BBSTZ.虽然都是ADV7181的宗出,但是寄存器配置等等还是有些诧异,引脚也不 ...
- verilog中24LC04B iic(i2c)读写通信设计步骤,以及程序常见写法错误。
板子使用的是黑金的是xilinx spartan-6开发板,首先准备一份24LC04B芯片资料,读懂资料后列出关键参数. 如下: 1.空闲状态为SDA和SCL都为高电平 2.开始状态为:保持SCL,S ...
- 学习笔记一:I2C协议学习和Verilog实现
////////////////////////////////////////////////// //clk = 20 MHz ,一个周期50ns //sck = 100 kHz (scl) ,一 ...
- I2C三态门Verilog
http://www.blogbus.com/uyarotxb-logs/206932748.html inout作为输出端口时三态门为选通状态,inout作为输入端口时三态门为高阻态,可通过 ...
随机推荐
- Linux Kernel ‘exitcode_proc_write()’函数本地缓冲区溢出漏洞
漏洞名称: Linux Kernel ‘exitcode_proc_write()’函数本地缓冲区溢出漏洞 CNNVD编号: CNNVD-201311-061 发布时间: 2013-11-07 更新时 ...
- ubunt下的MinimalCD
ubuntu有MinimalCD,水平高的衍生版制作者基于MinimalCD安装并编译,定制出独特风格的ubuntu衍生版,如 crunchbang.水平不高的个人用户可以从Alternate(文字安 ...
- VS2010如何调试IIS上的网站
通常,我们在Visual Studio里调试ASP.NET网站,都是加个断点,然后按F5,在VS自带的虚拟服务器下调试的.但有时候,VS自带的服务器弱爆了,无法满足一些特定情况的要求,我们必须把网站放 ...
- 谈谈分布式事务之三: System.Transactions事务详解[下篇]
在前面一篇给出的Transaction的定义中,信息的读者应该看到了一个叫做DepedentClone的方法.该方法对用于创建基于现有Transaction对 象的“依赖事务(DependentTra ...
- c pvr转存pvr.ccz格式
pvr.ccz 是把pvr用zlib算法压缩后的图像格式,其优点是可以提升文件读取效率. 大多数情况下我们可以用一些工具来将pvr压缩到pvr.ccz ,下面提供一个c++方法来完成这个过程 int ...
- 自定义控制器的View(loadView)及其注意点
*:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...
- 【CSS3】Advanced5:At Rules:@import, @media, and @font-face
1.@import bolt another stylesheet onto your existing one. @import url(**.css); must be placed at the ...
- leetcode之Palindrome Partitioning
方法一:DFS递归,判断每一个是否为回文数 1,首先要有一个判断字符串是否是回文的函数.容易实现,字符串从两边同时往中间走,看字符是否相同; 2,深度优先搜索思想对字符串进行遍历.得到结果.例如,s ...
- BestCoder Round #81 (div.1)A
水题...就是n的三进制后m位 #include<cstdio> #include<cstring> #include<cstdlib> #include<i ...
- va_start、va_end、va_list的使用
1:当无法列出传递函数的所有实参的类型和数目时,可用省略号指定参数表void foo(...);void foo(parm_list,...); 2:函数参数的传递原理函数参数是以数据结构:栈的形式存 ...