?? i2c.vhd
字號:
-- I2C總線是一種非常常用的串行總線,它操作簡便,占用接口少。本程序介紹操作一個I2C總線接口的EEPROM AT24C02
-- 的方法,使用戶了解I2C總線協議和讀寫方法。
-- 實驗過程是:按動開發板鍵盤某個鍵CPLD將撥碼開關的數據寫入EEPROM的某個地址,按動另外一個鍵,將剛寫入的數據
-- 讀回CPLD,并在數碼管上顯示。( 按sw0寫撥碼 開關值入24c02,按sw1讀出數值在數碼管上顯示
-- 為了更好的理解程序,用戶應該仔細閱讀光盤中的AT24C02的手冊
--
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
ENTITY i2c IS
PORT (
clk : IN std_logic;
rst : IN std_logic;
data_in : IN std_logic_vector(3 DOWNTO 0);
scl : OUT std_logic; --I2C時鐘線
sda : INOUT std_logic; --I2C數據線
wr_input : IN std_logic; --撥碼開關輸入想寫入EEPROM的數據
rd_input : IN std_logic; --要求寫的輸入
lowbit : OUT std_logic; --要求讀的輸入
en : OUT std_logic_vector(1 DOWNTO 0); --輸出一個低電平給矩陣鍵盤的某一行
seg_data : OUT std_logic_vector(7 DOWNTO 0)); -- 數碼管使能
END i2c;
ARCHITECTURE translated OF i2c IS
SIGNAL seg_data_buf : std_logic_vector(7 DOWNTO 0);
SIGNAL cnt_scan : std_logic_vector(11 DOWNTO 0);
SIGNAL sda_buf : std_logic; --sda輸入輸出數據緩存
SIGNAL link : std_logic; -- sda輸出標志
--一個scl時鐘周期的四個相位階段,將一個scl周期分為4段
--phase0對應scl的上升沿時刻,phase2對應scl的下降沿時刻,phase1對應從scl高電平的中間時刻,phase2對應從scl低電平的中間時刻,
SIGNAL phase0 : std_logic;
SIGNAL phase1 : std_logic;
SIGNAL phase2 : std_logic;
SIGNAL phase3 : std_logic;
--phase0對應scl的上升沿時刻,phase2對應scl的下降沿時刻,phase1對應從scl高電平的中間時刻,phase2對應從scl低電平的中間時刻,
SIGNAL clk_div : std_logic_vector(7 DOWNTO 0); --分頻計數器
SIGNAL main_state : std_logic_vector(1 DOWNTO 0);
SIGNAL i2c_state : std_logic_vector(2 DOWNTO 0);--對i2c操作的狀態
SIGNAL inner_state : std_logic_vector(3 DOWNTO 0);--i2c每一操作階段內部狀態
SIGNAL cnt_delay : std_logic_vector(19 DOWNTO 0); --按鍵延時計數器
SIGNAL start_delaycnt : std_logic; --按鍵延時開始
SIGNAL writeData_reg : std_logic_vector(7 DOWNTO 0);--要寫的數據的寄存器
SIGNAL readData_reg : std_logic_vector(7 DOWNTO 0);--讀回數據的寄存器
SIGNAL addr : std_logic_vector(7 DOWNTO 0);--被操作的EEPROM字節的地址
CONSTANT div_parameter : std_logic_vector(7 DOWNTO 0) := "01100100";--分頻系數,AT24C02最大支持400K時鐘速率
CONSTANT start : std_logic_vector(3 DOWNTO 0) := "0000"; --開始
CONSTANT first : std_logic_vector(3 DOWNTO 0) := "0001"; --第1位
CONSTANT second : std_logic_vector(3 DOWNTO 0) := "0010"; --第2位
CONSTANT third : std_logic_vector(3 DOWNTO 0) := "0011"; --第3位
CONSTANT fourth : std_logic_vector(3 DOWNTO 0) := "0100"; --第4位
CONSTANT fifth : std_logic_vector(3 DOWNTO 0) := "0101"; --第5位
CONSTANT sixth : std_logic_vector(3 DOWNTO 0) := "0110"; --第6位
CONSTANT seventh : std_logic_vector(3 DOWNTO 0) := "0111"; --第7位
CONSTANT eighth : std_logic_vector(3 DOWNTO 0) := "1000"; --第8位
CONSTANT ack : std_logic_vector(3 DOWNTO 0) := "1001"; --確認位
CONSTANT stop : std_logic_vector(3 DOWNTO 0) := "1010"; --結束位
CONSTANT ini : std_logic_vector(2 DOWNTO 0) := "000"; --初始化EEPROM狀態
CONSTANT sendaddr : std_logic_vector(2 DOWNTO 0) := "001"; --發送地址狀態
CONSTANT write_data : std_logic_vector(2 DOWNTO 0) := "010"; --寫數據狀態
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -