2022年VHDL编写IIC程序 .pdf
《2022年VHDL编写IIC程序 .pdf》由会员分享,可在线阅读,更多相关《2022年VHDL编写IIC程序 .pdf(8页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、如题所示,本文是使用VHDL语言编写的IIC 总线的 24C02 的读写例程,程序加了中文注释便于想我一样的初学者理解,写使用的写一个字节,读使用的随机读,具体参考 24c02 的手册library IEEE; use IEEE.std_logic_1164.all; use IEEE.std_logic_arith.all; use IEEE.std_logic_unsigned.all; entity iic_com is port( clk: in STD_LOGIC; rst_n: in STD_LOGIC; sw1_en: in STD_LOGIC; - 读使能sw2_en: in
2、STD_LOGIC; - 写使能scl: out STD_LOGIC; sda: inout STD_LOGIC; dis_data: out STD_LOGIC_VECTOR (7 downto 0) ); end entity iic_com; architecture iic_communication of iic_com is signal sw_state: STD_LOGIC; signal cnt_delay: STD_LOGIC_VECTOR (8 downto 0); signal scl_pos: STD_LOGIC; signal scl_hig: STD_LOGIC;
3、 signal scl_neg: STD_LOGIC; signal scl_low: STD_LOGIC; signal db_r: STD_LOGIC_VECTOR (7 downto 0); signal read_data: STD_LOGIC_VECTOR (7 downto 0); signal sda_r: STD_LOGIC; signal sda_in: STD_LOGIC; signal sda_link: STD_LOGIC; signal num: STD_LOGIC_VECTOR (3 downto 0); constant DEVICE_READ: STD_LOGI
4、C_VECTOR (7 downto 0) := 10100001;-器件地址读constant DEVICE_WRITE: STD_LOGIC_VECTOR (7 downto 0) := 10100000;-器件地址写constant WRITE_DATA: STD_LOGIC_VECTOR (7 downto 0) := 11000011; -写入的数据constant BYTE_ADDR: STD_LOGIC_VECTOR (7 downto 0) := 00000011;-写入的地址type state is (IDLE,START1,ADD1,ACK1,ADD2,ACK2,STAR
5、T2,ADD3,ACK3,DATA,ACK4,STOP1,STOP2); signal cstate: state; signal temp_sw1,temp_sw2:Std_LOGIC; begin - process(clk,rst_n) 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 8 页 - - - - - - - - - begin if (rst_n = 0) then sw_state = 0; elsif (clkevent AND clk = 1) t
6、hen if(sw1_en = 1) then sw_state = 0; elsif (sw2_en = 1) then sw_state = 1; end if; end if; end process; - process(clk,rst_n) begin if (rst_n = 0) then cnt_delay = 0 & x00; elsif (clkevent AND clk = 1) then if(cnt_delay = 10#499#) then -相当于 500 分频,得到100K 时钟cnt_delay = 0 & x00; else cnt_delay = cnt_d
7、elay+1; end if; end if; end process; scl_pos = 1 when (cnt_delay = 10#499#) else 0; -IIC时钟上升沿scl_hig = 1 when (cnt_delay = 10#124#) else 0; -IIC时钟高电平scl_neg = 1 when (cnt_delay = 10#249#) else 0; -IIC时钟下降沿scl_low = 1 when (cnt_delay = 10#374#) else 0; -IIC时钟低电平process(clk,rst_n) begin if (rst_n = 0)
8、 then scl = 0; elsif (clkevent AND clk = 1) then if(scl_pos = 1) then scl = 1; elsif(scl_neg = 1) then scl = 0; end if; end if; end process; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 2 页,共 8 页 - - - - - - - - - - process(clk,rst_n) begin if (rst_n = 0) then csta
9、te = IDLE; sda_r = 1; sda_link = 0; num = x0; read_data sda_link = 1; sda_r = 1; if (sw1_en/=temp_sw1)or(sw2_en/=temp_sw2) then -当 sw1_en 和 sw2_en 变化一次,只读或写一次,避免多次读写temp_sw1=sw1_en;temp_sw2=sw2_en; if (sw1_en = 1) OR (sw2_en = 1) then db_r = DEVICE_WRITE; cstate = START1; else cstate = IDLE; end if;
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 2022年VHDL编写IIC程序 2022 VHDL 编写 IIC 程序
限制150内