⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 iicrd.vhd

📁 用FPGA实现IIC通讯的主控端
💻 VHD
字号:
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
entity iicrd is
port(      reset_1:                  in       std_logic;   
           clkmain1:                 in       std_logic;  
           xiicask_1:                in       std_logic; 
           xiichange_1:              in       std_logic; 
           xiicdatain_3:             in       std_logic_vector(7 downto 0); 

           iicend_p1_321d:           out      std_logic; 
           iicdataout_p1_322u:       buffer   std_logic_vector(7 downto 0); 

           scl_p1_322u:              out      std_logic; 
           sda_z2_322u:              inout    std_logic); 

end iicrd;

architecture behav of iicrd is   
signal iicask_s1_321d:           std_logic; 
signal iicwork_s1_322u:          std_logic; 
signal temp_s0_321d:             std_logic_vector(2 downto 0);  
signal temp_s0_322u:             std_logic_vector(4 downto 0); 
signal iicdata_s3_322d:          std_logic_vector(7 downto 0); 
signal iiclength_s3_321d:        std_logic_vector(7 downto 0); 
signal iiclecoun_s3_321d:        std_logic_vector(7 downto 0); 
signal counter_s0_322u:          std_logic_vector(2 downto 0); 
-------------------------------------------------------------------------------  
begin 
  proc321:process(clkmain1,reset_1)                                               
        begin   
          if(reset_1='0')then  
            iicask_s1_321d<='1';  
            iicend_p1_321d<='1';                            
            temp_s0_321d<="000"; 
          elsif(clkmain1'event and clkmain1='0')then  
            if(temp_s0_321d="000")then                         -----------length------------
              if(xiicask_1='0')then
                if(iicwork_s1_322u='1' and xiichange_1='0')then 
                  iiclength_s3_321d<=xiicdatain_3;   
                  iiclecoun_s3_321d<="00000000";                             
                  iicask_s1_321d<='0';    
                  temp_s0_321d<="001";
                else
                  iicend_p1_321d<='1';                            
                  iicask_s1_321d<='1';
                  temp_s0_321d<="000"; 
                end if;
              else
                iicask_s1_321d<='1';
                iicend_p1_321d<='1';                            
                temp_s0_321d<="000"; 
              end if;
            elsif(temp_s0_321d="001")then  
              if(iicwork_s1_322u='0')then
                iicask_s1_321d<='1';    
                iicend_p1_321d<='0';                                                 
                temp_s0_321d<="010"; 
              else
                temp_s0_321d<="001"; 
              end if;
            elsif(temp_s0_321d="010")then 
              if(iicwork_s1_322u='1')then 
                iicend_p1_321d<='1';                                                    
                temp_s0_321d<="011";  
              else
                temp_s0_321d<="010"; 
              end if;
            elsif(temp_s0_321d="011")then                       --------------id-addr-data-------
              if(xiicask_1='0')then
                if(iicwork_s1_322u='1' and xiichange_1='0')then
                  iiclecoun_s3_321d<=iiclecoun_s3_321d+'1';                             
                  iicask_s1_321d<='0';
                  temp_s0_321d<="100";
                else
                  temp_s0_321d<="011";
                end if;
              else
                iicask_s1_321d<='1';
                iicend_p1_321d<='1';                            
                temp_s0_321d<="110"; 
              end if;
            elsif(temp_s0_321d="100")then                          
              if(iicwork_s1_322u='0')then
                iicask_s1_321d<='1';    
                iicend_p1_321d<='0';                                                 
                temp_s0_321d<="101"; 
              else
                temp_s0_321d<="100";
              end if;
            elsif(temp_s0_321d="101")then      
              if(iicwork_s1_322u='1')then
                iicend_p1_321d<='1';  
                if(iiclecoun_s3_321d>=iiclength_s3_321d)then                                                    
                  temp_s0_321d<="110"; 
                else
                  temp_s0_321d<="011"; 
                end if;
              else
                temp_s0_321d<="101"; 
              end if;
            elsif(temp_s0_321d="110")then                            -------------end----------------
              if(iicwork_s1_322u='1')then
                iicask_s1_321d<='0';                         
                temp_s0_321d<="111";
              else
                temp_s0_321d<="110";
              end if;
            elsif(temp_s0_321d="111")then
              if(iicwork_s1_322u='0')then
                iicask_s1_321d<='1';
                temp_s0_321d<="000";
              else
                temp_s0_321d<="111"; 
              end if;
            end if;
          end if; 
        end process proc321; 

  proc322:process(clkmain1,reset_1)                                        
        begin   
          if(reset_1='0')then
            temp_s0_322u<="00000";
            counter_s0_322u<="000";
            iicwork_s1_322u<='1';
            scl_p1_322u<='1';
            sda_z2_322u<='1';
          elsif(clkmain1'event and clkmain1='1')then     
            if(temp_s0_322u="00000")then
              if(iicask_s1_321d='0')then
                if(temp_s0_321d<"011")then                                   ------------------start
                  sda_z2_322u<='1';
                  scl_p1_322u<='1';
                  iicwork_s1_322u<='0';  
                  temp_s0_322u<="00001";
                elsif(temp_s0_321d>="011" and temp_s0_321d<"110")then      --------------ID(wr) 
                  iicdata_s3_322d<=xiicdatain_3;
                  counter_s0_322u<="000";
                  iicwork_s1_322u<='0';  
                  if(iiclecoun_s3_321d="00000001")then  
                    temp_s0_322u<="01000";              ---goto wr 
                  elsif(iiclecoun_s3_321d="00000010")then
                    temp_s0_322u<="01000";              ---goto wr 
                  elsif(iiclecoun_s3_321d>="00000011")then
                    temp_s0_322u<="10000";            ---goto rd
                    sda_z2_322u<='Z';
                  end if;
                elsif(temp_s0_321d>="110")then                              -------------------end
                  iicwork_s1_322u<='0';  
                  sda_z2_322u<='0';
                  temp_s0_322u<="11000";    
                end if;
              else
                iicwork_s1_322u<='1';
                temp_s0_322u<="00000";
              end if;
            elsif(temp_s0_322u>="00001" and temp_s0_322u<"01000")then    -----------------------start-------
              if(temp_s0_322u="00001")then
                sda_z2_322u<='1';
                scl_p1_322u<='1';
                temp_s0_322u<="00010";
              elsif(temp_s0_322u="00010")then             
                sda_z2_322u<='0';
                temp_s0_322u<="00011";
              elsif(temp_s0_322u="00011")then
                scl_p1_322u<='0';
                sda_z2_322u<='0'; 
                temp_s0_322u<="00100";
              elsif(temp_s0_322u>="00100")then
                scl_p1_322u<='0';
                sda_z2_322u<='1'; 
                iicwork_s1_322u<='1';  
                temp_s0_322u<="00000";
              end if;
            elsif(temp_s0_322u>="01000" and temp_s0_322u<"10000")then   ----------------------wr data---------
              if(temp_s0_322u="01000")then  
                sda_z2_322u<=iicdata_s3_322d(7);  
                temp_s0_322u<="01001";
              elsif(temp_s0_322u="01001")then 
                iicdata_s3_322d(7)<=iicdata_s3_322d(6);
                iicdata_s3_322d(6)<=iicdata_s3_322d(5);
                iicdata_s3_322d(5)<=iicdata_s3_322d(4);
                iicdata_s3_322d(4)<=iicdata_s3_322d(3);
                iicdata_s3_322d(3)<=iicdata_s3_322d(2);
                iicdata_s3_322d(2)<=iicdata_s3_322d(1);
                iicdata_s3_322d(1)<=iicdata_s3_322d(0); 
                scl_p1_322u<='1';
                temp_s0_322u<="01010";
              elsif(temp_s0_322u="01010")then
                scl_p1_322u<='0';
                if(counter_s0_322u>="111")then
                  temp_s0_322u<="01011";
                else
                  counter_s0_322u<=counter_s0_322u+'1';
                  temp_s0_322u<="01000";
                end if;
              elsif(temp_s0_322u="01011")then                        ---------wait ack
                iicwork_s1_322u<='1';  
                sda_z2_322u<='Z';
                temp_s0_322u<="01100";
              elsif(temp_s0_322u="01100")then
                scl_p1_322u<='1';
                sda_z2_322u<='Z';
                temp_s0_322u<="01101";
              elsif(temp_s0_322u="01101")then 
                scl_p1_322u<='0';
                sda_z2_322u<='1';
                temp_s0_322u<="00000"; 
              end if;
            elsif(temp_s0_322u>="10000" and temp_s0_322u<"11000")then ------------------------rd data-------
              if(temp_s0_322u="10000")then
                iicdataout_p1_322u(7)<=iicdataout_p1_322u(6);
                iicdataout_p1_322u(6)<=iicdataout_p1_322u(5);  
                iicdataout_p1_322u(5)<=iicdataout_p1_322u(4);
                iicdataout_p1_322u(4)<=iicdataout_p1_322u(3);
                iicdataout_p1_322u(3)<=iicdataout_p1_322u(2);
                iicdataout_p1_322u(2)<=iicdataout_p1_322u(1);
                iicdataout_p1_322u(1)<=iicdataout_p1_322u(0);
                iicdataout_p1_322u(0)<=sda_z2_322u;
                scl_p1_322u<='1';                         
                sda_z2_322u<='Z';
                temp_s0_322u<="10001";
              elsif(temp_s0_322u="10001")then 
                sda_z2_322u<='Z';
                scl_p1_322u<='0';
                if(counter_s0_322u>="111")then 
                  temp_s0_322u<="10010";
                else
                  counter_s0_322u<=counter_s0_322u+'1'; 
                  temp_s0_322u<="10000";
                end if;
              elsif(temp_s0_322u="10010")then    
                iicwork_s1_322u<='1';                
                temp_s0_322u<="10011";
                if(iiclecoun_s3_321d>=iiclength_s3_321d)then     ----------no ack
                  sda_z2_322u<='1';
                else
                  sda_z2_322u<='0';                              ----------ack
                end if;
              elsif(temp_s0_322u="10011")then 
                scl_p1_322u<='1';
                temp_s0_322u<="10100";
              elsif(temp_s0_322u>="10100")then 
                scl_p1_322u<='0';
                temp_s0_322u<="00000";
              end if;
            elsif(temp_s0_322u>="11000")then                            -------------------------stop-----------
              if(temp_s0_322u="11000")then
                scl_p1_322u<='1';
                temp_s0_322u<="11001";
              elsif(temp_s0_322u="11001")then    
                sda_z2_322u<='1';
                temp_s0_322u<="11010";
              elsif(temp_s0_322u>="11010")then
                iicwork_s1_322u<='1'; 
                scl_p1_322u<='1';
                sda_z2_322u<='1';
                temp_s0_322u<="00000";
              end if;
            end if;
          end if;
        end process proc322;

end behav;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -