1. <span style="font-size:14px;">`timescale 1ns / 1ps
  2. module test(
  3. sda
  4. );
  5. reg scl;
  6. inout sda;
  7. reg sda_out;
  8. wire    sda_in;
  9. reg [7:0]   data;
  10. reg start_flag, stop_flag;
  11. assign sda = sda_out ? 1'bz : 1'b0;
  12. assign sda_in = sda;
  13. pullup( sda );
  14. I2CTEST testmine(.SDA(sda), .SCL(scl));
  15. initial
  16. begin
  17. scl = 0;
  18. sda_out = 0;
  19. data = 8'h27;
  20. start_flag = 0;
  21. #160000;
  22. start ( );
  23. end
  24. always
  25. begin
  26. #50000 scl = ~scl;
  27. end
  28. always @ (posedge start_flag)
  29. begin
  30. repeat (8)
  31. begin
  32. wait ( scl == 0 );
  33. #20000;
  34. sda_out = data[7];
  35. #40000;
  36. data = data << 1;
  37. end
  38. wait (~ scl);
  39. #20000;
  40. sda_out = 1;
  41. #160000;
  42. stop ( );
  43. end
  44. always @ ( posedge stop_flag)
  45. begin
  46. //     sda_out = 0;
  47. //     #50000;
  48. sda_out = 1;
  49. end
  50. task start;
  51. begin
  52. wait (scl == 0);
  53. #20000;
  54. sda_out = 1;
  55. wait ( scl == 1 );
  56. #20000;
  57. sda_out = 0;
  58. start_flag = 1;
  59. end
  60. endtask
  61. task stop;
  62. begin
  63. wait ( scl == 0 );
  64. #20000;
  65. sda_out = 0;
  66. wait ( scl ==1 );
  67. #20000;
  68. sda_out = 1;
  69. stop_flag = 1;
  70. end
  71. endtask
  72. endmodule</span>

I2C程序

    1. <span style="font-family:Arial;">`timescale 1ns / 1ps
    2. module I2CTEST(
    3. SDA, SCL
    4. );
    5. input SCL;
    6. inout SDA;
    7. // The 7-bits address that we want for our I2C slave
    8. parameter I2C_ADR = 7'h13;
    9. //---------------------------------------------
    10. //start,stop condition judgement
    11. //---------------------------------------------
    12. wire start, stop;
    13. reg sda1, sda2;
    14. reg sda11;
    15. always @ ( posedge SCL )
    16. sda1 <= SDA;
    17. always @ ( negedge SCL )
    18. sda2 <= SDA;
    19. always @ ( negedge SCL )
    20. sda11 <= sda1;
    21. assign start = sda11 & (!sda2);
    22. assign stop = sda2 & ( !sda11 );
    23. //----------------------------------------------
    24. //count setting
    25. //----------------------------------------------
    26. reg [3:0]  bitcont;
    27. wire bit_ack = bitcont[3];
    28. always @ ( posedge SCL or posedge start)
    29. begin
    30. if ( start )
    31. bitcont <=  4'h6;
    32. else
    33. begin
    34. if (bit_ack)
    35. bitcont <= 4'h6;
    36. else
    37. bitcont <= bitcont -4'h1;
    38. end
    39. end
    40. //-------------------------------------
    41. //get sda using posedge scl
    42. //-------------------------------------
    43. reg sdar;
    44. always @ ( posedge SCL ) sdar <= SDA;
    45. //----------------------------------------
    46. //address match
    47. //----------------------------------------
    48. reg addr_match, op_read;
    49. always @ ( negedge SCL or posedge start )
    50. begin
    51. if ( start )
    52. begin
    53. addr_match <= 1'h1;
    54. op_read <= 1'h0;
    55. end
    56. else
    57. begin
    58. if( (bitcont == 6) & (sdar != I2C_ADR[6])) addr_match <= 1'h0;
    59. if( (bitcont == 5) & (sdar != I2C_ADR[5])) addr_match <= 1'h0;
    60. if( (bitcont == 4) & (sdar != I2C_ADR[4])) addr_match <= 1'h0;
    61. if( (bitcont == 3) & (sdar != I2C_ADR[3])) addr_match <= 1'h0;
    62. if( (bitcont == 2) & (sdar != I2C_ADR[2])) addr_match <= 1'h0;
    63. if( (bitcont == 1) & (sdar != I2C_ADR[1])) addr_match <= 1'h0;
    64. if( (bitcont == 0) & (sdar != I2C_ADR[0])) addr_match <= 1'h0;
    65. if( bitcont == 0 ) op_read <= sdar;
    66. end
    67. end
    68. //-----------------------------------------------------------------------
    69. //send ack
    70. //-----------------------------------------------------------------------
    71. reg ack_assert;
    72. always @ ( negedge SCL )
    73. begin
    74. if ( bit_ack & addr_match & op_read )
    75. ack_assert <= 1'h1;
    76. else
    77. ack_assert <= 1'h0;
    78. end
    79. //-------------------------------------------------------------------------
    80. //control SDA line
    81. //-------------------------------------------------------------------------
    82. assign SDA = ack_assert ? 1'h0 : 1'hz;
    83. pullup ( SDA );
    84. endmodule

I2C Verilog的实现(一)的更多相关文章

  1. I2C Verilog的实现(二)

    1. 起始结束信号的判断 //--------------------------------------------- //start,stop condition judgement //---- ...

  2. Verilog之i2c合约

    说明:i2c乔布斯.有这么多的事情在网上参考. 时刻:2014年5一个月6周二星期 1.问题叙述性说明: 正如图.已知的时钟clk为100k,rst为复位信号.上升沿有效,基于Verilog HDL或 ...

  3. i2c状态机方法设计-verilog

    2010-09-05 21:04:00 verilog语言基础学的差不多了.接着就是看看华为的语言编写规范.状态机设计方法是fpga的重要设计方法.所以我要记上一笔. 只要会FSM方法,用fpga编写 ...

  4. I2C控制器的Verilog建模之三(完结版)

    前言:终于到了测试篇,不过悲剧了一下.按照之前<二>里面的思路,在顶层用一个复用器驱动读写独立模块的I2C总线确实失败.虽然综合过去了,不过警告里已经说明:底层的2个原本是inout三态口 ...

  5. I2C控制器的Verilog建模之二

    前言:接着上一篇的I2C写操作,今天要实现一个I2C的读操作.虽然在ADV7181B配置内部寄存器时没有必要使用到读操作,但是为了进一步确认寄存器是否在I2C写模块下被正确配置,这一步是必不可少的. ...

  6. I2C控制器的Verilog建模之一

    前言:之前申请了ADI公司的一款ADV7181CBSTZ的视频解码芯片,正好原装DE2板子安的是同系列的ADV7181BBSTZ.虽然都是ADV7181的宗出,但是寄存器配置等等还是有些诧异,引脚也不 ...

  7. verilog中24LC04B iic(i2c)读写通信设计步骤,以及程序常见写法错误。

    板子使用的是黑金的是xilinx spartan-6开发板,首先准备一份24LC04B芯片资料,读懂资料后列出关键参数. 如下: 1.空闲状态为SDA和SCL都为高电平 2.开始状态为:保持SCL,S ...

  8. 学习笔记一:I2C协议学习和Verilog实现

    ////////////////////////////////////////////////// //clk = 20 MHz ,一个周期50ns //sck = 100 kHz (scl) ,一 ...

  9. I2C三态门Verilog

    http://www.blogbus.com/uyarotxb-logs/206932748.html     inout作为输出端口时三态门为选通状态,inout作为输入端口时三态门为高阻态,可通过 ...

随机推荐

  1. Linux Kernel ‘exitcode_proc_write()’函数本地缓冲区溢出漏洞

    漏洞名称: Linux Kernel ‘exitcode_proc_write()’函数本地缓冲区溢出漏洞 CNNVD编号: CNNVD-201311-061 发布时间: 2013-11-07 更新时 ...

  2. ubunt下的MinimalCD

    ubuntu有MinimalCD,水平高的衍生版制作者基于MinimalCD安装并编译,定制出独特风格的ubuntu衍生版,如 crunchbang.水平不高的个人用户可以从Alternate(文字安 ...

  3. VS2010如何调试IIS上的网站

    通常,我们在Visual Studio里调试ASP.NET网站,都是加个断点,然后按F5,在VS自带的虚拟服务器下调试的.但有时候,VS自带的服务器弱爆了,无法满足一些特定情况的要求,我们必须把网站放 ...

  4. 谈谈分布式事务之三: System.Transactions事务详解[下篇]

    在前面一篇给出的Transaction的定义中,信息的读者应该看到了一个叫做DepedentClone的方法.该方法对用于创建基于现有Transaction对 象的“依赖事务(DependentTra ...

  5. c pvr转存pvr.ccz格式

    pvr.ccz 是把pvr用zlib算法压缩后的图像格式,其优点是可以提升文件读取效率. 大多数情况下我们可以用一些工具来将pvr压缩到pvr.ccz ,下面提供一个c++方法来完成这个过程 int ...

  6. 自定义控制器的View(loadView)及其注意点

    *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...

  7. 【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 ...

  8. leetcode之Palindrome Partitioning

    方法一:DFS递归,判断每一个是否为回文数 1,首先要有一个判断字符串是否是回文的函数.容易实现,字符串从两边同时往中间走,看字符是否相同; 2,深度优先搜索思想对字符串进行遍历.得到结果.例如,s ...

  9. BestCoder Round #81 (div.1)A

    水题...就是n的三进制后m位 #include<cstdio> #include<cstring> #include<cstdlib> #include<i ...

  10. va_start、va_end、va_list的使用

    1:当无法列出传递函数的所有实参的类型和数目时,可用省略号指定参数表void foo(...);void foo(parm_list,...); 2:函数参数的传递原理函数参数是以数据结构:栈的形式存 ...