在线咨询
eetop公众号 创芯大讲堂 创芯人才网
切换到宽版

EETOP 创芯网论坛 (原名:电子顶级开发网)

手机号码,快捷登录

手机号码,快捷登录

找回密码

  登录   注册  

快捷导航
搜帖子
查看: 6690|回复: 21

[转帖]i2c写程序

[复制链接]
发表于 2003-11-11 13:59:55 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?注册

x
LBSALE[1000]LBSALE
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
entity i2c_write is
port(
    clk,
    nreset:  in std_logic;
    d:in std_logic_vector(7 downto 0);
    start,
    stop,
    read,
    write: in std_logic;
   -- load,
  --  shift: out std_logic;
    dout: out std_logic;   
    SCL,
    SDA: out std_logic
);
end i2c_write;
architecture arc_i2c_core of i2c_write is

component p2s_altera is
port(
clk,
clkih,
stld,
ser: in std_logic;
d :in std_logic_vector(0 to 7);
q,
nq: out std_logic
);
end component p2s_altera;

--signal for i2c_core
signal sclo,sdao:std_logic;
signal din:std_logic;
signal  dcnt:unsigned(3 downto 0);
signal iload,ishift:std_logic;
type cmds is (idle, start_a, start_b, start_c, start_d, stop_a, stop_b, stop_c, rd_a, rd_b, rd_c, rd_d, wr_a, wr_b, wr_c, wr_d,ack_a,ack_b,ack_c,ack_d,nack_a,nack_b,nack_c,nack_d);
--state:     00000  00001    00010    00011    00100   00101    00110  00111  01000  01001 01010  01011 01100 01101 01110 01111 10000 10001 10010 10011 10100 10101  10110   10111
signal state:cmds;
--signal for p2s_altera
signal  iclkih,  istld, iser:std_logic;
signal do :std_logic;
signal flag: std_logic;
signal iflag:std_logic;

--signal for shift_control process
type shift_type is(s1,s2,s3);
signal shift_state: shift_type;

begin
-- generate  parallel to serial
shift: p2s_altera port map(clk,iclkih,istld,iser,d,din,do);
dout<=din;

-- generate i2c_core data transfer
p1 : process (nreset,clk,state,start,read,write,din)
       --variable nxt_state : cmds;
       --variable icmd_ack, ibusy, store_sda : std_logic;
       --variable itxd : std_logic;
       --variable iload,ishift:std_logic;
       variable iscl,isda:std_logic;
       variable idcnt:unsigned(3 downto 0);
   
     
   begin
   -- generate regs
       if (nReset = '0' )  then
           state <= idle;
      
           scl<='1';
           sda<='1';
           dcnt<="1000";
         
       elsif (clk'event and clk = '1') then
           
              SCL<=sclo;
              SDA<=sdao;
              
             dcnt<=idcnt;
               
       case state is   
           when idle=>
               iscl:='0';   
               isda:='0';
               if(start='1')  then
                             state<=start_a;
               elsif stop='1' then
                              state<=stop_a;
               elsif read='1'    then
                             idcnt:="1000";
                             ishift<='1';
                             state<=rd_a;
               else
                             idcnt:="1000";
                             iload<='1';
                             state<=wr_a;
              end if;
        
                              
           -- start
           when start_a =>
               iscl := '0'; -- keep SCL in same state (for repeated start)
               isda := '1'; -- set SDA high
                  state <= start_b;
            
           when start_b =>
               iscl:='1';
               isda:='1';
               
                  state <= start_c;
               
      
           when start_c =>
               iscl:='1';
               isda:='0';
               
               state <= start_d;
                           
           when start_d =>
               iscl:='0';
               isda:='0';
              -- icmd_ack := '1';
              --if core_ack='1' then
               
                              
                 if read='1' then
                              idcnt:="1000";
                              ishift<='1';
                              state<=rd_a;   
                 else
                              idcnt:="1000";
                              iload<='1';
                              state<=wr_a;
                end if;
           
           -- stop
           when stop_a =>
               iscl:='0';
               isda:='0';
               state <= stop_b;
           when stop_b =>
               iscl:='1';
               isda:='0';
               state <= stop_c;
           when stop_c =>
               iscl:='1';
               isda:='1';
--            when stop_d =>
               state <= idle;
           -- read
           when rd_a =>
               iscl:='0';
               isda:='1';--sda;
               state <= rd_b;
            
           when rd_b =>
               iscl:='1';
               isda:='0';--sda;
               state <= rd_c;
              
           when rd_c =>
               iscl:='1';
               isda:='1';--sda;
               
               state <=rd_d;
            
           when rd_d =>
               iscl:='0';
               isda:='0';--sda;
               idcnt:=idcnt-1;
               
               if stop='1' then
                            state<=nack_a;
                elsif idcnt=0  then
                           state <= ack_a;
                else   
                           state<=rd_a;
               end if;
      
          -- write
           when wr_a =>
               iscl:='0';
               isda:=din;
               iflag<='0';
               state <= wr_b;
              
           when wr_b =>
               iscl:='1';
               isda:=din;
               iflag<='0';
               state <= wr_c;

           when wr_c =>
               iscl:='1';
               isda:=din;
               iflag<='0';
               state <= wr_d;
           when wr_d =>
               iscl:='0';
               isda:=din;
               iflag<='1';
              idcnt:=idcnt-1;
              
               if(idcnt=0)  then
                               state <= ack_a;
                               iflag<='0';
               else  
                               state<=wr_a;
               end if;
               
           when ack_a=>
               iscl:='0';
               isda:='0';
               state <= ack_b;
         
           when ack_b=>
                isda:='0';
                iscl:='1';
                state <= ack_c;
   
          when ack_c=>
                iscl:='1';
                state <= ack_d;
         
          when ack_d=>
                isda:='0';
                iscl:='0';
                if  stop='1' then
                               state<=stop_a;
                elsif(write='1') then
                              state<=wr_a;
                              idcnt:="1000";
                              iload<='1';
                elsif(read='1') then
                              state<=rd_a;
                              idcnt:="1000";
                              ishift<='1';
                             
               else
                              state<=idle;
               end if;
      
          when nack_a=>
               iscl:='0';
               isda:='1';
               state <= nack_b;
              
           when nack_b=>
               isda:='1';
               iscl:='1';
               state <= nack_c;
          when nack_c=>
                isda:='1';
                iscl:='0';
                state <= nack_d;
         
          when nack_d=>
                isda:='1';
                state<=stop_a;
                        
       end case;
       sclo<=iscl;
       sdao<=isda;
end if;
         
end process p1;

shift_control: process(clk,iflag,dcnt)
begin
if(clk'event and clk='1') then
     case shift_state is
     when s1=>
            iclkih<='0';
            istld<='0';
            iser<='0';
            shift_state<=s2;
    when s2=>
            iclkih<='1';
            istld<='1';
            iser<='1';
            if iflag='1'   then
               if(dcnt=8) then
                            shift_state<=s1;
               elsif dcnt=0  then
                            shift_state<=s2;
               else
                            shift_state<=s3;
               end if;
            else
                          shift_state<=s2;
            end if;
   when s3=>
            iclkih<='0';
            istld<='1';
            iser<='1';   
            shift_state<=s2;  
   end case;
   end if;
end process shift_control;
   
end architecture arc_i2c_core;
LIBRARY altera;
USE altera.maxplus2.ALL;
LIBRARY ieee;
USE ieee.std_logic_1164.ALL;
entity p2s_altera is
port(
clk,
clkih,
stld,
ser: in std_logic;
d :in std_logic_vector(0 to 7);
q,
nq: out std_logic
);
end p2s_altera;
architecture arc_p2s2 of p2s_altera is
begin
u1: a_74165b  port map (clk, clkih, stld, ser, d,q,nq);
end arc_p2s2;

发表于 2003-11-11 14:24:21 | 显示全部楼层

[转帖]i2c写程序

green_pine兄怎么不在你自己的坛子里( copyleft)帖东西?
我看你很少在那里!
发表于 2003-11-11 14:25:47 | 显示全部楼层

[转帖]i2c写程序

刚才坏了你财路是不是。。。。。?
可不要这么小气。。。
发表于 2003-11-11 15:34:28 | 显示全部楼层

[转帖]i2c写程序

要价不低阿
发表于 2003-11-11 15:49:35 | 显示全部楼层

[转帖]i2c写程序

太黑了,呵呵
 楼主| 发表于 2003-11-11 16:39:54 | 显示全部楼层

[转帖]i2c写程序

我的空间快到期了
 楼主| 发表于 2003-11-11 16:41:23 | 显示全部楼层

[转帖]i2c写程序

因为这里正在设计iic丫,所以要高点,嘿嘿
发表于 2004-4-1 08:44:16 | 显示全部楼层

[转帖]i2c写程序

为什么sda不是双向的
发表于 2004-4-1 13:10:12 | 显示全部楼层

[转帖]i2c写程序

k!
发表于 2004-4-2 15:01:51 | 显示全部楼层

[转帖]i2c写程序

好像不能用的
您需要登录后才可以回帖 登录 | 注册

本版积分规则

关闭

站长推荐 上一条 /1 下一条

小黑屋| 手机版| 关于我们| 联系我们| 隐私声明| EETOP 创芯网
( 京ICP备:10050787号 京公网安备:11010502037710 )

GMT+8, 2025-5-14 04:03 , Processed in 0.042123 second(s), 13 queries , Gzip On, MemCached On.

eetop公众号 创芯大讲堂 创芯人才网
快速回复 返回顶部 返回列表