I2C VHDL程序

http://blog.sina.com.cn/s/blog_9bd80b7601012o9y.html

library ieee ;

use ieee.std_logic_1164.all ;

use ieee.std_logic_arith.all ;

use ieee.std_logic_unsigned.all ;

entity I2C_Control is

              port (

      CLKI      :IN     Std_logic ;

      RST    :IN     Std_logic ;

      Slave_add  :in  std_logic_vector(3 downto 0);

      addr   :in  std_logic_vector(7 downto 0);

      wr_rd   :IN     Std_logic ;

      DAT1   :in  std_logic_vector(7 downto 0);

      Delay_Time  :in  integer range 0 to 255;

      A    :buffer std_logic_vector(2 downto 0);

      WP    :buffer std_logic;

      SCL    :buffer std_logic;

      SDA    :buffer std_logic

                   ) ;

end I2C_Control ;

architecture Arch of I2C_Control is

  type state1 is (Idle,Idle1, I1,I2,I3,I4,I5,I6,I7,I8,I9,I10,Iok);

  signal current_state,next_state : state1;

  signal I2C_Sent  :std_logic_vector(26 downto 0);

begin

I2C_Sent <= Slave_add & A & wr_rd &'0'& addr &'0'& DAT1&'0' ;

process(clki,RST)

begin

  if RST = '1' then

   current_state <= Idle;

  elsif clki'event and clki = '0' then

   current_state <= next_state;

  end if;

end process;

 

process(clki)

variable i            :integer range 0 to 63;

variable cnt          :integer range 0 to 1023;

begin

  if clki'event and clki = '1' then

      A<="111";

   case current_state is

    when Idle =>  

        SCL <= '1'; SDA <= '1';

        WP <= '1';

        next_state <= Idle1;

    when Idle1 =>  

        SCL <= '1'; SDA <= '1';

        WP <= '0';

        next_state <= I1;       

       

    when I1   =>

        SDA <= '0';

        next_state <= I2;

    when I2   =>

        SCL <= '0';

        i :=26;

        next_state <= I3;

    when I3   =>

                    SCL <= '0';

        SDA <= I2C_Sent(i);

        next_state <= I4;

    when I4   =>

        SCL <= '1';

        next_state <= I5;

    when I5   =>

        if i= 0 then

         next_state <= I6;

        else

         i := i-1;

         next_state <= I3;

        end if;

    when I6   =>

        SCL <= '0';

        next_state <= I7;

       

          when I7   =>

          SCL<= '1';

        next_state <= I8;

    when I8   =>

          SDA <= '1';

        next_state <= I9;

    when I9   =>

        WP<= '1';  --1

        next_state <= I10;

    WHEN I10 =>

        if cnt = Delay_Time then

         cnt := 0;

         next_state <= Idle;

        else

         cnt := cnt +1;

        end if;    

    when Iok  =>

        SCL <='1';

        SDA <= '1';

        next_state <= Iok;

    when others =>

        next_state <= Iok;

   end case;

  end if;

end process;

end Arch;

 

转载于:https://www.cnblogs.com/xinjie/p/5823612.html

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值