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

📄 ds1307.vhd

📁 通过IIC总线读写实时时钟DS1307
💻 VHD
字号:
--DS1307的写地址 0xd0   11010000
--DS1307的读地址 0xd1	11010001

library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;

ENTITY DS1307 IS
   PORT (
      clk                     : IN std_logic;   
      rst_n                   : IN std_logic;   
	  cnt_inc_n				  : IN std_logic;			--数据递增按键
      wr_input_n              : IN std_logic:='0';     --要求写的输入
      scl                     : OUT std_logic;    --I2C时钟线
      sda                     : INOUT std_logic;  --I2C数据线 
	  out_sec	  			  : out std_logic_vector(7 downto 0);
	  out_min	  			  : out std_logic_vector(7 downto 0);
	  out_hour	  			  : out std_logic_vector(7 downto 0)
	     );
END DS1307;

ARCHITECTURE translated OF DS1307 IS

   SIGNAL rst                      :  std_logic;   
   SIGNAL cnt_inc				   :  std_logic;
   SIGNAL wr_input                 :  std_logic;     
   SIGNAL rd_input                 :  std_logic;     
   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 cnt_div				   :  std_logic_vector(22 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字节的地址
   SIGNAL addr_rd                  :  std_logic_vector(7 DOWNTO 0);
   SIGNAL addr_wr                  :  std_logic_vector(7 DOWNTO 0); 
   SIGNAL rd_flag				   :  std_logic;
   SIGNAL rd_star				   :  std_logic;
   SIGNAL rd_done				   :  std_logic;
   SIGNAL wr_flag				   :  std_logic;
   SIGNAL wr_star				   :  std_logic;
   SIGNAL wr_done				   :  std_logic;
   CONSTANT  div_parameter         :  std_logic_vector(7 DOWNTO 0) := "01110100";--分频系数,AT24C02最大支持400K时钟速率  "01100100"  
   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 + -