(二)加上程序,字数超了。这一篇是上一篇的续,加了一个“垃圾型”的这个实验的代码。
程序代码:
-- 状态机 A/D采样控制电路 2012.07.26 mige
library ieee;
use IEEE.STD_LOGIC_1164.all;
entity ADCINT is
PORT(D : IN STD_LOGIC_VECTOR(7 DOWNTO 0); --来自0809转换好的8位数据
CLK : IN STD_LOGIC; --状态机工作时钟
EOC : IN STD_LOGIC; --转换状态指示,低电平表示正在转换
ALE : OUT STD_LOGIC; --8个模拟信号通道地址锁存信号
START : OUT STD_LOGIC; --转换开始信号
OE : OUT STD_LOGIC; --数据输出3态控制信号
ADDA : OUT STD_LOGIC; --信号通道最低位控制信号
LOCK0 : OUT STD_LOGIC; --观察数据锁存时钟
Q1 , Q2 : OUT STD_LOGIC_VECTOR(3 DOWNTO 0)); --8位数据输出 分高低四位
END ADCINT;
ARCHITECTURE mige OF ADCINT IS
type states is (st0, st1, st2, st3,st4) ; --定义各状态子类型
SIGNAL current_state, next_state: states := st0 ;
SIGNAL MM: STD_LOGIC_VECTOR(7 DOWNTO 0);
signal REGL1,REGL2 : STD_LOGIC_VECTOR(3 DOWNTO 0); -- 将AD转换完成输出的8位二进制 分别分四位存储 译码
SIGNAL LOCK: STD_LOGIC; -- 转换后数据输出锁存时钟信号
BEGIN
ADDA <= '1';--当ADDA<='0',模拟信号进入通道IN0;当ADDA<='1',则进入通道IN1
Q1<= REGL1;Q2<= REGL2; LOCK0 <= LOCK ; --分高低四位
COM: PROCESS(current_state,EOC) BEGIN --规定各状态转换方式
CASE current_state IS
WHEN st0=>ALE<='0';START<='0';LOCK<='0';OE<='0';
next_state <= st1; --0809初始化
WHEN st1=>ALE<='1';START<='1';LOCK<='0';OE<='0';
next_state <= st2; --启动采样
WHEN st2=> ALE<='0';START<='0';LOCK<='0';OE<='0';
IF (EOC='1') THEN next_state <= st3; --EOC=1表明转换结束
ELSE next_state <= st2; END IF ; --转换未结束,继续等待
WHEN st3=> ALE<='0';START<='0';LOCK<='0';OE<='1';
next_state <= st4;--开启OE,输出转换好的数据
WHEN st4=> ALE<='0';START<='0';LOCK<='1';OE<='1'; next_state <= st0;
WHEN OTHERS => next_state <= st0;
END CASE ;
END PROCESS COM ;
REG: PROCESS (CLK) --第二个进程
BEGIN
IF (CLK'EVENT AND CLK='1') THEN current_state<=next_state; END IF;
END PROCESS REG ; -- 由信号current_state将当前状态值带出此进程:REG
LATCH1: PROCESS (LOCK) -- 此进程中,在LOCK的上升沿,将转换好的数据锁入
BEGIN
IF LOCK='1' AND LOCK'EVENT THEN MM <= D ;
CASE MM IS --译码输出 对应每种输入十六进制 得到输出的十进制 电压
WHEN "00000000"=> REGL2<="0000";REGL1<="0000"; WHEN "00000101"=> REGL2<="0000";REGL1<="0001";--
WHEN "00001010"=> REGL2<="0000";REGL1<="0010"; WHEN "00001111"=> REGL2<="0000";REGL1<="0011";
WHEN "00010100"=> REGL2<="0000";REGL1<="0100"; WHEN "00011001"=> REGL2<="0000";REGL1<="0101";
WHEN "00011110"=> REGL2<="0000";REGL1<="0110"; WHEN "00100011"=> REGL2<="0000";REGL1<="0111";
WHEN "00101000"=> REGL2<="0000";REGL1<="1000"; WHEN "00101100"=> REGL2<="0000";REGL1<="1001";
WHEN "00110010"=> REGL2<="0001";REGL1<="0000"; WHEN "00110111"=> REGL2<="0001";REGL1<="0001";--
WHEN "00111100"=> REGL2<="0001";REGL1<="0010"; WHEN "01000001"=> REGL2<="0001";REGL1<="0011";
WHEN "01000110"=> REGL2<="0001";REGL1<="0100"; WHEN "01001011"=> REGL2<="0001";REGL1<="0101";
WHEN "01010000"=> REGL2<="0001";REGL1<="0110"; WHEN "01010101"=> REGL2<="0001";REGL1<="0111";
WHEN "01011010"=> REGL2<="0001";REGL1<="1000"; WHEN "01011111"=> REGL2<="0001";REGL1<="1001";
WHEN "01100100"=> REGL2<="0010";REGL1<="0000"; WHEN "01101001"=> REGL2<="0010";REGL1<="0001";--
WHEN "01101110"=> REGL2<="0010";REGL1<="0010"; WHEN "01110011"=> REGL2<="0010";REGL1<="0011";
WHEN "01111000"=> REGL2<="0010";REGL1<="0100"; WHEN "01111100"=> REGL2<="0010";REGL1<="0101";
WHEN "10000010"=> REGL2<="0010";REGL1<="0110"; WHEN "10000111"=> REGL2<="0010";REGL1<="0111";
WHEN "10001100"=> REGL2<="0010";REGL1<="1000"; WHEN "10010001"=> REGL2<="0010";REGL1<="1001";
WHEN "10010110"=> REGL2<="0011";REGL1<="0000"; WHEN "10011011"=> REGL2<="0011";REGL1<="0001";--
WHEN "10100000"=> REGL2<="0011";REGL1<="0010"; WHEN "10100101"=> REGL2<="0011";REGL1<="0011";
WHEN "10101010"=> REGL2<="0011";REGL1<="0100"; WHEN "10101111"=> REGL2<="0011";REGL1<="0101";
WHEN "10110100"=> REGL2<="0011";REGL1<="0110"; WHEN "10111001"=> REGL2<="0011";REGL1<="0111";
WHEN "10111110"=> REGL2<="0011";REGL1<="1000"; WHEN "11000011"=> REGL2<="0011";REGL1<="1001";
WHEN "11001000"=> REGL2<="0100";REGL1<="0000"; WHEN "11001100"=> REGL2<="0100";REGL1<="0001";--
WHEN "11010010"=> REGL2<="0100";REGL1<="0010"; WHEN "11010111"=> REGL2<="0100";REGL1<="0011";
WHEN "11011100"=> REGL2<="0100";REGL1<="0100"; WHEN "11100001"=> REGL2<="0100";REGL1<="0101";
WHEN "11100100"=> REGL2<="0100";REGL1<="0110"; WHEN "11101011"=> REGL2<="0100";REGL1<="0111";
WHEN "11110000"=> REGL2<="0100";REGL1<="1000"; WHEN "11110101"=> REGL2<="0100";REGL1<="1001";
WHEN "11111010"=> REGL2<="0101";REGL1<="0000";
when others=>null;
END CASE;
END IF;
END PROCESS LATCH1 ;
END mige ;
这里面,代码实现的具体细节,我大概解释了一下,然后做为总结发给邹老师了(每做一个小小实验,邹老师都要求自己写总结发给他),这里我觉得需要说的是,后面那个看起来臃肿的 CASE 语句,是十六进制到十进制的译码程序,这个我真没怎么弄明白,没弄明白的是因为0809输出的是二进制,现在需要译码成十进制进行显示,是不是只有这样的一个最烂的 CASE语句实现啊? 暂时放着吧,后面总能遇到、、
用户1707737 2012-9-16 21:54
用户1707737 2012-9-15 13:08
zhangqinguili_957239374 2012-9-15 10:23