nullnull9.1 键盘接口设计
9.2 LED驱动器设计
9.3 D/A接口设计
9.4 A/D接口设计
第9章 FPGA在微机工程中实践应用 null教学目标
教学重点
教学过程null了解键盘接口原理及设计方法
理解LED驱动器设计原理
理解和掌握D/A接口设计的主要
内容
财务内部控制制度的内容财务内部控制制度的内容人员招聘与配置的内容项目成本控制的内容消防安全演练内容
理解和掌握A/D接口设计的主要内容
教学目标教学重点教学重点理解LED驱动器设计
理解和掌握D/A接口设计的主要内容
理解和掌握A/D接口设计的主要内容键盘工作原理 键盘工作原理 键盘是由一组规则排列的按键组成,一个按键实际上是一个开关元件,它是微型计算机最常用的输入设备。
微机键盘通常使用机械触点式按键开关,其主要功能是把机械上的通断转换为电气上的逻辑关系。也就是说,它能提供标准的TTL逻辑电平,以便与通用数字系统的逻辑电平相容。 9.1键盘接口设计去抖动措施去抖动措施在触点抖动期间检测按键的通与断状态,可能导致判断出错。即按键一次按下或释放被错误地认为是多次操作,这种情况是不允许出现的。为了克服按键触点机械抖动所致的检测误判,必须采取去抖动措施,可从硬件、软件两方面予以考虑。
硬件去抖动措施:在键数较少时,可采用硬件去抖
软件去抖动措施: 当键数较多时采用软件去抖。矩阵式键盘的结构及原理 矩阵式键盘的结构及原理 该类型键盘由行线和列线组成,按键位于行、列线的交叉点上。由图9-2可知,一个4×4的行、列结构可以构成一个含有16个按键的键盘,但是只占用8个I/O口线,因此,矩阵式键盘较之独立式按键键盘可以节省很多I/O口。 图9-2矩阵式键盘按键的识别 矩阵式键盘按键的识别 识别按键的方法很多,其中,最常见的方法是扫描法。
以下是以8号键的识别为例来介绍扫描法:
8号键按下时,第2行一定为低电平,然而,第2行为低电平时,能否肯定是8号键按下呢?回答是否定的,因为9、10、11号键按下同样使第2行为低电平。 接下页null为进一步确定具体键,不能使所有列线在同一时刻都处在低电平,可在某一时刻只让一条列线处于低电平,其余列线均处于高电平,另一时刻,让下一列处在低电平,依此循环,这种依次轮流每次选通一列的工作方式称为键盘扫描。采用键盘扫描后,再来观察8号键按下时的工作过程,当第0列处于低电平时,第2行处于低电平,而第1、2、3列处于低电平时,第2行却处在高电平,由此可判定按下的键应是第2行与第0列的交叉点,即8号键。 null对于矩阵式键盘,按键的位置由行号和列号唯一确定,因此可分别对行号和列号进行二进制编码,然后将两值合成一个字节,高4位是行号,低4位是列号。如图9.2中的8号键,它位于第2行,第0列,因此,其键盘编码应为20H。采用上述编码对于不同行的键离散性较大,不利于散转指令对按键进行处理。因此,可采用依次排列键号的方式对安排进行编码。以图9-2中的4×4键盘为例,可将键号编码为:01H、02H、03H…0EH、0FH、10H等16个键号。编码相互转换可通过计算或查表的方法实现。键盘的编码矩阵式按键FPGA描述 矩阵式按键FPGA描述 键盘接口顶层模块 nulllibrary IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
entity key is
Port ( clk : in std_logic;
keyin : in std_logic_vector(3 downto 0);
keyvalue : out std_logic_vector(3 downto 0);
keypressed : out std_logic;
keydrv : inout std_logic_vector(3 downto 0));
end key;
architecture Behavioral of key is接下页nullcomponent keyscan
port(
clk_scan: in std_logic; --扫描时钟
key_drv: out std_logic_vector(3 downto 0)--扫描信号);
end component;
component clkgen
port(
clk : in std_logic;
clk_scan : out std_logic);
end component; 接下页nullcomponent keydecoder
port( key_in: in std_logic_vector(3 downto 0); --键盘输入
key_drv: in std_logic_vector(3 downto 0);--键盘输出
clk: in std_logic; --全局时钟
clk_scan: in std_logic;--扫描时钟
keyvalue: out std_logic_vector(3 downto 0);--键盘的键值
key_pressed: out std_logic);
end component;
signal clk_scan:std_logic;
begin接下页nullkeyscan1:keyscan
port map(clk_scan=>clk_scan,
key_drv=>keydrv);
clkgen1:clkgen
port map( clk=>clk,
clk_scan=>clk_scan);
keydecoder1:keydecoder
port map(
key_in=>keyin,接下页null key_drv=>keydrv,
clk=>clk,
clk_scan=>clk_scan,
keyvalue=>keyvalue,
key_pressed=>keypressed);
end Behavioral;
null键盘扫描电路模块 接下页nulllibrary IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
entity keyscan is
Port ( clk_scan: in std_logic; --扫描时钟
key_drv: out std_logic_vector(3 downto 0));
end keyscan;
architecture Behavioral of keyscan is
constant s0:STD_LOGIC_VECTOR(3 DOWNTO 0):="1110";
constant s1:STD_LOGIC_VECTOR(3 DOWNTO 0):="1101";
constant s2:STD_LOGIC_VECTOR(3 DOWNTO 0):="1011";接下页null
constant s3:STD_LOGIC_VECTOR(3 DOWNTO 0):="0111";
signal present_state:STD_LOGIC_VECTOR(3 DOWNTO 0);--当前状态机
signal next_state:STD_LOGIC_VECTOR(3 DOWNTO 0);--下次状态机
begin
process(clk_scan)
begin
if(clk_scan'event and clk_scan='1') then
present_state<=next_state;--状态机在上升沿时翻转
end if;
end process;接下页nullprocess(present_state)
begin
case present_state is
when s0=>next_state<=s1;
when s1=>next_state<=s2;
when s2=>next_state<=s3;
when s3=>next_state<=s0;
when others=>next_state<=s0;
end case;
end process;
key_drv<=present_state; --输出译码信号
end Behavioral;null键盘译码电路模块 接下页nulllibrary IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
entity keydecoder is
Port (
key_in: in std_logic_vector(3 downto 0); --键盘输入
key_drv: in std_logic_vector(3 downto 0);--键盘输出
clk: in std_logic; --全局时钟
clk_scan: in std_logic; --扫描时钟
keyvalue: out std_logic_vector(3 downto 0)--键盘的键值
);接下页nullkey_pressed: out std_logic); --有键盘按下去的标志
end keydecoder;
architecture Behavioral of keydecoder is
signal temp:STD_LOGIC_VECTOR(7 DOWNTO 0); --键盘的输入信号
signal temp_preseed:STD_LOGIC;--键盘按下标志位
signal temp1,temp2,temp3,temp4,temp5,temp6:STD_LOGIC; --同步化信号
signal key_pressed_asy:STD_LOGIC;
begin
temp<=key_drv&key_in;
process(temp)
begin接下页nullcase temp is
when "11101110"=>keyvalue<=CONV_STD_LOGIC_VECTOR(1,4);
temp_preseed<='1';
when "11101101"=>keyvalue<=CONV_STD_LOGIC_VECTOR(2,4);
temp_preseed<='1';
when "11101011"=>keyvalue<=CONV_STD_LOGIC_VECTOR(3,4);
temp_preseed<='1';
when "11100111"=>keyvalue<=CONV_STD_LOGIC_VECTOR(4,4);
temp_preseed<='1';
when "11011110"=>keyvalue<=CONV_STD_LOGIC_VECTOR(5,4);
temp_preseed<='1';
when "11011101"=>keyvalue<=CONV_STD_LOGIC_VECTOR(6,4);
temp_preseed<='1';
when "11011011"=>keyvalue<=CONV_STD_LOGIC_VECTOR(7,4);
temp_preseed<='1';接下页nullwhen "11010111"=>keyvalue<=CONV_STD_LOGIC_VECTOR(8,4);
temp_preseed<='1';
when "10111110"=>keyvalue<=CONV_STD_LOGIC_VECTOR(9,4);
temp_preseed<='1';
when "10111101"=>keyvalue<=CONV_STD_LOGIC_VECTOR(10,4);
temp_preseed<='1';
when "10111011"=>keyvalue<=CONV_STD_LOGIC_VECTOR(11,4);
temp_preseed<='1';
when "10110111"=>keyvalue<=CONV_STD_LOGIC_VECTOR(12,4);
temp_preseed<='1';
when "01111110"=>keyvalue<=CONV_STD_LOGIC_VECTOR(13,4);
temp_preseed<='1';
when "01111101"=>keyvalue<=CONV_STD_LOGIC_VECTOR(14,4);
temp_preseed<='1';接下页nullwhen "01111011"=>keyvalue<=CONV_STD_LOGIC_VECTOR(15,4);
temp_preseed<='1';
when "01110111"=>keyvalue<=CONV_STD_LOGIC_VECTOR(16,4);
temp_preseed<='1';
when others=>temp_preseed<='0';
end case;
end process;接下页nullprocess(clk_scan)
begin
if(clk_scan'event and clk_scan='1') then
temp1<=temp_preseed;
temp2<=temp1;
temp3<=temp2;
temp4<=temp3;
end if;
key_pressed_asy<=temp1 OR temp2 OR temp3 OR temp4;
end process;接下页nullprocess(clk)
begin
if(clk'event and clk='1')then
temp5<=key_pressed_asy;
temp6<=temp5;
end if;
key_pressed<=temp5 AND (NOT(temp6));
end process;
end Behavioral;null时钟电路产生模块 接下页nulllibrary IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
entity clkgen is
Port (
clk : in std_logic;
clk_scan : out std_logic
);
end clkgen;
architecture Behavioral of clkgen is接下页nullsignal cnt:INTEGER range 0 to 199;
begin
process(clk)
begin
if(clk'event and clk = '1')then
if(cnt=199)then
cnt<=0;
else
cnt<=cnt+1;
end if;
end if;
end process;接下页nullprocess(cnt,clk)
begin
if(clk'event and clk='1') then
if(cnt>=199/2) then
clk_scan<='1';
else
clk_scan<='0';
end if;
end if;
end process;
end Behavioral;