设计矩阵式键盘接口,并在一个数码管上面显示按下的数字,从0到9以及小数点。
判断键盘中有无按键按下是通过行线送入扫描线好然后从列线读取状态得到的。其方法是依次给行线送低电平,检查列线的输入。如果列线全是高电平,则代表低电平信号所在的行中无按键按下;如果列线有输入为低电平,则代表低电平信号所在的行和出现低电平的列的交点处有按键按下。
整个设计程序包括三个模块:时钟分频、键盘扫描和键译码转换。
时钟分频:
由于使用的外部时钟频率为50MHz,这个频率对扫描来说太高,所以这里需要一个分频器来分得适合键盘扫描使用的频率。
键盘扫描:
由键盘的工作原理可知,要正确地完成按键输入工作必须有按键扫描电路产生KEYI信号,同时必须有按键译码电路从KEYI中和KEYOUT中读出按键的键值。
键盘扫描电路是用于产生KEYI 3~ KEYI 0 信号,其变化顺序是1110→1101→1011→0111→1110…周而复始地扫描。其停留时间大慨在10ms。 键盘译码:
键盘译码电路是从keyI3~keyI0和keyout3~keyout0信号中译码出按键值的电路。 将此按键值显示在数码管上。
FPGA_CLK 系统的主时钟 MASTER_RESET_n 主复位
Button[0]-[15] 拨码开关/白色按钮 通过 跳线Jxx选择 Seven_seg[0]-[7] 数码管的显示 Seven_seg[8]-[15] 数码管的选择 LED_DOWN[0]-[7] 8个led
Keyboard_Down[0]-[8] 键盘输入/AD输入,通过 JP8选择 低位表示行。 原理图中
Keyboard_Down[0]-[4]对应KEYI[0-4] Keyboard_Down[5]-[8]对应KEYO[0-3] 此例中
Keyboard_Down[1]-[4]对应KEYI[0-3] Keyboard_Down[5]-[8]对应KEYO[0-3]
2、交通灯的控制 课程设计原理:
在十字路口,每条道路各有一组红、黄、绿灯和倒计时显示器,用以指挥车辆和行人有序的通行。其中,红灯亮表示该道路不可以通行;黄灯亮表示停车;绿灯亮表示可以通行;倒计时显示器是用来显示允许通行或禁止通行的时间。交通灯控制器就是用于自动控制十字路口的交通灯和计时器,指挥
各种车辆和行人安全通过。 课程设计要求:
1)在十字路口设置一组红、黄、绿等,显示顺序为:红,黄,绿,红……
2)设置一组数码管,以倒计时的方式显示允许通过或禁止通过的时间,其中绿灯、黄灯、红灯的持续时间为30s,5s,35s。 状态 时间 AB绿 AB黄 AB红 CD绿 CD黄 CD红 GR YR RG RY
30s 5s 30s 5s 1 0 0 0 0 1 0 0 0 0 1 1 0 0 1 0 0 0 0 1 1 1 0 0
矩阵键盘程序
LIBRARY IEEE;
USE IEEE.STD_LOGIC_11.ALL;
USE IEEE.STD_LOGIC_UNSIGNED.ALL;
ENTITY matrikeyscan IS --实体名为matrikeyscan port( clk,rst : in std_logic; --系统时钟输入
column_scan : in std_logic_vector(3 downto 0); --列扫描输入 row_scan : out std_logic_vector(3 downto 0); --行扫描输出 display : out std_logic_vector(7 downto 0); --按键编码输出 pressed : out std_logic); --按键批示信号,’1’表示有键被按下,’0’表示没有键--被按下 end matrikeyscan; --结束实体
architecture behave of matrikeyscan is --结构体名为behave signal rowreg : std_logic_vector(3 downto 0); signal con : std_logic_vector(7 downto 0); signal cnt : std_logic_vector(15 downto 0); signal clkreg : std_logic; begin
--数字分频器,对系统时钟进行适当分频,得1~10KHz左右的时钟进行矩阵键盘扫描。 process(clk)--分频器的进程
variable cnt:integer range 0 to 500000;--499999;变量count的变化范围为0到500000 BEGIN
IF rst='0' then cnt:=0;
else if clk'event and clk='1' then IF cnt=499999 THEN clkreg<=not clkreg; cnt:=0; ELSE
cnt:=cnt+1; END IF; END IF; END IF;
end process; --结束进程
--产生行扫描信号 process(clkreg) begin
if clkreg'event and clkreg = '1' then case rowreg is
when \"1110\" => rowreg <= \"1101\"; when \"1101\" => rowreg <= \"1011\"; when \"1011\" => rowreg <= \"0111\";
when \"0111\" => rowreg <= \"1110\"; when others => rowreg <= \"1110\"; end case; end if; end process;
--对行扫描信号和列扫描信号进行组合。 row_scan <= rowreg;
con <= rowreg & column_scan;
--对行扫描信号和列扫描信号进行CASE语句判断。得到是否有键被按下,输出按键按--键指示信号和按键的编码 process(clkreg) begin
if clkreg'event and clkreg = '1' then case con is
when \"01111101\" => display <= \"11000000\"; pressed <= '1';--数码管显示0 when \"10111110\" => display <= \"11111001\"; pressed <= '1';--数码管显示1 when \"10111101\" => display <= \"10100100\"; pressed <= '1';--数码管显示2 when \"10111011\" => display <= \"10110000\"; pressed <= '1';--数码管显示3 when \"11011110\" => display <= \"10011001\"; pressed <= '1';--数码管显示4 when \"11011101\" => display <= \"10010010\"; pressed <= '1';--数码管显示5 when \"11011011\" => display <= \"10000010\"; pressed <= '1';--数码管显示6 when \"11101110\" => display <= \"11111000\"; pressed <= '1';--数码管显示7 when \"11101101\" => display <= \"10000000\"; pressed <= '1';--数码管显示8 when \"11101011\" => display <= \"10010000\"; pressed <= '1';--数码管显示9 when \"01111011\" => display <= \"01111111\"; pressed <= '1';--数码管显示A when others => null; end case; end if; end process;
end behave;
引脚配置
set_global_assignment -name FAMILY \"Cyclone II\" set_global_assignment -name DEVICE EP2C20F484C8
set_global_assignment -name TOP_LEVEL_ENTITY matrikeyscan set_global_assignment -name ORIGINAL_QUARTUS_VERSION 6.0
set_global_assignment -name PROJECT_CREATION_TIME_DATE \"19:59:24 APRIL 20, 2013\" set_global_assignment -name LAST_QUARTUS_VERSION 6.0 set_global_assignment -name DEVICE_FILTER_PACKAGE FBGA set_global_assignment -name DEVICE_FILTER_PIN_COUNT 484 set_global_assignment -name DEVICE_FILTER_SPEED_GRADE 8 set_global_assignment -name VHDL_FILE matrikeyscan.vhd
set_global_assignment -name VECTOR_WAVEFORM_FILE matrikeyscan.vwf
set_location_assignment PIN_L1 -to clk set_location_assignment PIN_B3 -to rst
set_location_assignment PIN_B15 -to display[0] set_location_assignment PIN_A15 -to display[1] set_location_assignment PIN_E7 -to display[2] set_location_assignment PIN_E3 -to display[3] set_location_assignment PIN_A3 -to display[4] set_location_assignment PIN_B4 -to display[5] set_location_assignment PIN_A4 -to display[6] set_location_assignment PIN_B5 -to display[7]
set_location_assignment PIN_M5 -to row_scan[0] set_location_assignment PIN_G6 -to row_scan[1] set_location_assignment PIN_L8 -to row_scan[2] set_location_assignment PIN_T3 -to row_scan[3] set_location_assignment PIN_Y5 -to column_scan[0] set_location_assignment PIN_Y9 -to column_scan[1] set_location_assignment PIN_W4 -to column_scan[2] set_location_assignment PIN_W3 -to column_scan[3]
交通灯控制程序
LIBRARY IEEE; --定义库
USE IEEE.STD_LOGIC_11.ALL;
USE IEEE.STD_LOGIC_UNSIGNED.ALL;
ENTITY traffic_lights IS --结构体名为traffic_lights PORT( clk:in std_logic; --时钟输入 ledlight :out std_logic_vector(7downto 0) ; --led灯显示 display:out std_logic_vector(15 downto 0) --数码管显示 );
END traffic_lights;
architecture jiaotongdeng of traffic_lights is --结构体 signal shuma:std_logic; signal red_light,yellow_light,green_light:integer range 0 to 35; signal ledlight_temp:std_logic_vector(7 downto 0);
signal ld,second:bit; begin process(clk) --时间进程 --分频部分,5000000分频 variable cnt:integer range 0 to 25000000; begin --weixuan<=\"11111111\"; if clk'event and clk='1' then if ld='1' then second<=not second; cnt:=0; ld<='0'; else cnt:=cnt+1; end if; if cnt=25000000 then ld<='1'; end if; end if; end process; process(second) begin if second='1' then if red_light<35 then ledlight_temp<=\"00100100\"; red_light<=red_light+1; end if; if red_light>30 then ledlight_temp<=\"01000100\"; end if; if red_light=35 then if green_light<30 then green_light<=green_light+1; ledlight_temp<=\"10000001\"; end if; if green_light=30 then if yellow_light<4 then ledlight_temp<=\"00100010\"; yellow_light<=yellow_light+1; end if; if yellow_light=4 then red_light<=0; yellow_light<=0; green_light<=0; end if; end if; end if;
end if;
end process; ledlight<=ledlight_temp; process(second) variable cntnum:integer range 0 to 70; variable daojishi:integer range 0 to 35; begin
if second='1' then cntnum:=cntnum+1; if cntnum<35 then daojishi:=36-cntnum; end if; if cntnum>=35 and cntnum<66 then daojishi:=65-cntnum; end if; if cntnum>=66 and cntnum<71 then daojishi:=70-cntnum; end if; if cntnum=71 then daojishi:=70-cntnum; cntnum:=0; end if; case daojishi is when 0 => display<=\"1100000011000000\"; when 1 => display<=\"1100000011111001\"; when 2 => display<=\"1100000010100100\"; when 3 => display<=\"1100000010110000\"; when 4 => display<=\"1100000010011001\"; when 5 => display<=\"1100000010010010\"; when 6 => display<=\"1100000010000010\"; when 7 => display<=\"1100000011111000\"; when 8 => display<=\"1100000010000000\"; when 9 => display<=\"1100000010010000\"; when 10 => display<=\"1111100111000000\"; when 11 => display<=\"1111100111111001\"; when 12 => display<=\"1111100110100100\"; when 13 => display<=\"1111100110110000\"; when 14 => display<=\"1111100110011001\"; when 15 => display<=\"1111100110010010\"; when 16 => display<=\"1111100110000010\"; when 17 => display<=\"1111100111111000\"; when 18 => display<=\"1111100110000000\"; when 19 => display<=\"1111100110010000\";
when 20 => display<=\"1010010011000000\"; when 21 => display<=\"1010010011111001\"; when 22 => display<=\"1010010010100100\"; when 23 => display<=\"1010010010110000\"; when 24 => display<=\"1010010010011001\"; when 25 => display<=\"1010010010010010\"; when 26 => display<=\"1010010010000010\"; when 27 => display<=\"1010010011111000\"; when 28 => display<=\"1010010010000000\"; when 29 => display<=\"1010010010010000\"; when 30 => display<=\"1011000011000000\"; when 31 => display<=\"1011000011111001\"; when 32 => display<=\"1011000010100100\"; when 33 => display<=\"1011000010110000\"; when 34 => display<=\"1011000010011001\"; when 35 => display<=\"1011000010010010\"; when others=>NUll; end case; end if; end process; end;
引脚配置
set_global_assignment -name FAMILY \"Cyclone II\" set_global_assignment -name DEVICE EP2C20F484C8
set_global_assignment -name TOP_LEVEL_ENTITY traffic_lights set_global_assignment -name ORIGINAL_QUARTUS_VERSION 6.0
set_global_assignment -name PROJECT_CREATION_TIME_DATE \"21:53:08 set_global_assignment -name LAST_QUARTUS_VERSION 6.0 set_global_assignment -name DEVICE_FILTER_PACKAGE FBGA set_global_assignment -name DEVICE_FILTER_PIN_COUNT 484 set_global_assignment -name DEVICE_FILTER_SPEED_GRADE 8 set_global_assignment -name VHDL_FILE traffic_lights.vhd
set_global_assignment -name VECTOR_WAVEFORM_FILE traffic_lights.vwf set_location_assignment PIN_L1 -to clk
set_location_assignment PIN_G7 -to ledlight[0] set_location_assignment PIN_G15 -to ledlight[1] set_location_assignment PIN_J15 -to ledlight[2] set_location_assignment PIN_J14 -to ledlight[3] set_location_assignment PIN_H15 -to ledlight[4] set_location_assignment PIN_N15 -to ledlight[5]
MAY 09, 2013\" set_location_assignment PIN_M18 -to ledlight[6] set_location_assignment PIN_W5 -to ledlight[7]
set_location_assignment PIN_B15 -to display[0] set_location_assignment PIN_A15 -to display[1] set_location_assignment PIN_E7 -to display[2] set_location_assignment PIN_E3 -to display[3] set_location_assignment PIN_A3 -to display[4] set_location_assignment PIN_B4 -to display[5] set_location_assignment PIN_A4 -to display[6] set_location_assignment PIN_B5 -to display[7] set_location_assignment PIN_A8 -to display[8] set_location_assignment PIN_D9 -to display[9] set_location_assignment PIN_D8 -to display[10] set_location_assignment PIN_A11 -to display[11] set_location_assignment PIN_D4 -to display[12] set_location_assignment PIN_D7 -to display[13] set_location_assignment PIN_V4 -to display[14] set_location_assignment PIN_A5 -to display[15]
矩阵键盘波形图
交通灯波形图
矩阵键盘程序
LIBRARY IEEE;
USE IEEE.STD_LOGIC_11.ALL;
USE IEEE.STD_LOGIC_UNSIGNED.ALL;
ENTITY matrikeyscan IS --实体名为matrikeyscan port( clk,rst : in std_logic; --系统时钟输入
column_scan : in std_logic_vector(3 downto 0); --列扫描输入 row_scan : out std_logic_vector(3 downto 0); --行扫描输出 display : out std_logic_vector(7 downto 0); --按键编码输出 pressed : out std_logic); --按键批示信号,’1’表示有键被按下,’0’表示没有键--被按下 end matrikeyscan; --结束实体
architecture behave of matrikeyscan is --结构体名为behave signal rowreg : std_logic_vector(3 downto 0); signal con : std_logic_vector(7 downto 0); signal cnt : std_logic_vector(15 downto 0); signal clkreg : std_logic; begin
--数字分频器,对系统时钟进行适当分频,得1~10KHz左右的时钟进行矩阵键盘扫描。 process(clk)--分频器的进程
variable cnt:integer range 0 to 500000;--499999;变量count的变化范围为0到500000
BEGIN
IF rst='0' then cnt:=0;
else if clk'event and clk='1' then IF cnt=499999 THEN clkreg<=not clkreg; cnt:=0; ELSE
cnt:=cnt+1; END IF; END IF; END IF;
end process; --结束进程
--产生行扫描信号 process(clkreg) begin
if clkreg'event and clkreg = '1' then case rowreg is
when \"1110\" => rowreg <= \"1101\"; when \"1101\" => rowreg <= \"1011\"; when \"1011\" => rowreg <= \"0111\";
when \"0111\" => rowreg <= \"1110\"; when others => rowreg <= \"1110\"; end case; end if; end process;
--对行扫描信号和列扫描信号进行组合。 row_scan <= rowreg;
con <= rowreg & column_scan;
--对行扫描信号和列扫描信号进行CASE语句判断。得到是否有键被按下,输出按键按--键指示信号和按键的编码 process(clkreg)
begin
if clkreg'event and clkreg = '1' then case con is
when \"01111101\" => display <= \"11000000\"; pressed <= '1';--数码管显示0 when \"10111110\" => display <= \"11111001\"; pressed <= '1';--数码管显示1 when \"10111101\" => display <= \"10100100\"; pressed <= '1';--数码管显示2 when \"10111011\" => display <= \"10110000\"; pressed <= '1';--数码管显示3 when \"11011110\" => display <= \"10011001\"; pressed <= '1';--数码管显示4 when \"11011101\" => display <= \"10010010\"; pressed <= '1';--数码管显示5 when \"11011011\" => display <= \"10000010\"; pressed <= '1';--数码管显示6 when \"11101110\" => display <= \"11111000\"; pressed <= '1';--数码管显示7 when \"11101101\" => display <= \"10000000\"; pressed <= '1';--数码管显示8 when \"11101011\" => display <= \"10010000\"; pressed <= '1';--数码管显示9 when \"01111011\" => display <= \"01111111\"; pressed <= '1';--数码管显示A when others => null; end case; end if; end process;
end behave;
引脚配置
set_global_assignment -name FAMILY \"Cyclone II\" set_global_assignment -name DEVICE EP2C20F484C8
set_global_assignment -name TOP_LEVEL_ENTITY matrikeyscan set_global_assignment -name ORIGINAL_QUARTUS_VERSION 6.0
set_global_assignment -name PROJECT_CREATION_TIME_DATE \"19:59:24 APRIL 20, 2013\" set_global_assignment -name LAST_QUARTUS_VERSION 6.0 set_global_assignment -name DEVICE_FILTER_PACKAGE FBGA set_global_assignment -name DEVICE_FILTER_PIN_COUNT 484 set_global_assignment -name DEVICE_FILTER_SPEED_GRADE 8 set_global_assignment -name VHDL_FILE matrikeyscan.vhd
set_global_assignment -name VECTOR_WAVEFORM_FILE matrikeyscan.vwf
set_location_assignment PIN_L1 -to clk set_location_assignment PIN_B3 -to rst
set_location_assignment PIN_B15 -to display[0] set_location_assignment PIN_A15 -to display[1] set_location_assignment PIN_E7 -to display[2] set_location_assignment PIN_E3 -to display[3] set_location_assignment PIN_A3 -to display[4] set_location_assignment PIN_B4 -to display[5] set_location_assignment PIN_A4 -to display[6] set_location_assignment PIN_B5 -to display[7]
set_location_assignment PIN_M5 -to row_scan[0] set_location_assignment PIN_G6 -to row_scan[1] set_location_assignment PIN_L8 -to row_scan[2] set_location_assignment PIN_T3 -to row_scan[3] set_location_assignment PIN_Y5 -to column_scan[0] set_location_assignment PIN_Y9 -to column_scan[1] set_location_assignment PIN_W4 -to column_scan[2] set_location_assignment PIN_W3 -to column_scan[3]
交通灯控制程序
LIBRARY IEEE; --定义库
USE IEEE.STD_LOGIC_11.ALL;
USE IEEE.STD_LOGIC_UNSIGNED.ALL;
ENTITY traffic_lights IS --结构体名为traffic_lights PORT( clk:in std_logic; --时钟输入 ledlight :out std_logic_vector(7downto 0) ; --led灯显示 display:out std_logic_vector(15 downto 0) --数码管显示 );
END traffic_lights;
architecture jiaotongdeng of traffic_lights is --结构体 signal shuma:std_logic; signal red_light,yellow_light,green_light:integer range 0 to 35;
signal ledlight_temp:std_logic_vector(7 downto 0); signal ld,second:bit; begin process(clk) --时间进程 --分频部分,5000000分频 variable cnt:integer range 0 to 25000000; begin --weixuan<=\"11111111\"; if clk'event and clk='1' then if ld='1' then second<=not second; cnt:=0; ld<='0'; else cnt:=cnt+1; end if; if cnt=25000000 then ld<='1'; end if; end if; end process; process(second) begin if second='1' then if red_light<35 then ledlight_temp<=\"00100100\"; red_light<=red_light+1; end if; if red_light>30 then ledlight_temp<=\"01000100\"; end if; if red_light=35 then if green_light<30 then green_light<=green_light+1; ledlight_temp<=\"10000001\"; end if; if green_light=30 then if yellow_light<4 then ledlight_temp<=\"00100010\"; yellow_light<=yellow_light+1; end if; if yellow_light=4 then red_light<=0; yellow_light<=0; green_light<=0; end if; end if;
end if; end if;
end process; ledlight<=ledlight_temp; process(second) variable cntnum:integer range 0 to 70; variable daojishi:integer range 0 to 35; begin
if second='1' then cntnum:=cntnum+1; if cntnum<35 then daojishi:=36-cntnum; end if; if cntnum>=35 and cntnum<66 then daojishi:=65-cntnum; end if; if cntnum>=66 and cntnum<71 then daojishi:=70-cntnum; end if; if cntnum=71 then daojishi:=70-cntnum; cntnum:=0; end if; case daojishi is when 0 => display<=\"1100000011000000\"; when 1 => display<=\"1100000011111001\"; when 2 => display<=\"1100000010100100\"; when 3 => display<=\"1100000010110000\"; when 4 => display<=\"1100000010011001\"; when 5 => display<=\"1100000010010010\"; when 6 => display<=\"1100000010000010\"; when 7 => display<=\"1100000011111000\"; when 8 => display<=\"1100000010000000\"; when 9 => display<=\"1100000010010000\"; when 10 => display<=\"1111100111000000\"; when 11 => display<=\"1111100111111001\"; when 12 => display<=\"1111100110100100\"; when 13 => display<=\"1111100110110000\"; when 14 => display<=\"1111100110011001\"; when 15 => display<=\"1111100110010010\"; when 16 => display<=\"1111100110000010\"; when 17 => display<=\"1111100111111000\"; when 18 => display<=\"1111100110000000\";
when 19 => display<=\"1111100110010000\"; when 20 => display<=\"1010010011000000\"; when 21 => display<=\"1010010011111001\"; when 22 => display<=\"1010010010100100\"; when 23 => display<=\"1010010010110000\"; when 24 => display<=\"1010010010011001\"; when 25 => display<=\"1010010010010010\"; when 26 => display<=\"1010010010000010\"; when 27 => display<=\"1010010011111000\"; when 28 => display<=\"1010010010000000\"; when 29 => display<=\"1010010010010000\"; when 30 => display<=\"1011000011000000\"; when 31 => display<=\"1011000011111001\"; when 32 => display<=\"1011000010100100\"; when 33 => display<=\"1011000010110000\"; when 34 => display<=\"1011000010011001\"; when 35 => display<=\"1011000010010010\"; when others=>NUll; end case; end if; end process; end;
引脚配置
set_global_assignment -name FAMILY \"Cyclone II\" set_global_assignment -name DEVICE EP2C20F484C8
set_global_assignment -name TOP_LEVEL_ENTITY traffic_lights set_global_assignment -name ORIGINAL_QUARTUS_VERSION 6.0
set_global_assignment -name PROJECT_CREATION_TIME_DATE \"21:53:08 set_global_assignment -name LAST_QUARTUS_VERSION 6.0 set_global_assignment -name DEVICE_FILTER_PACKAGE FBGA set_global_assignment -name DEVICE_FILTER_PIN_COUNT 484 set_global_assignment -name DEVICE_FILTER_SPEED_GRADE 8 set_global_assignment -name VHDL_FILE traffic_lights.vhd
set_global_assignment -name VECTOR_WAVEFORM_FILE traffic_lights.vwf set_location_assignment PIN_L1 -to clk
set_location_assignment PIN_G7 -to ledlight[0] set_location_assignment PIN_G15 -to ledlight[1] set_location_assignment PIN_J15 -to ledlight[2] set_location_assignment PIN_J14 -to ledlight[3] set_location_assignment PIN_H15 -to ledlight[4]
MAY 09, 2013\" set_location_assignment PIN_N15 -to ledlight[5] set_location_assignment PIN_M18 -to ledlight[6] set_location_assignment PIN_W5 -to ledlight[7]
set_location_assignment PIN_B15 -to display[0] set_location_assignment PIN_A15 -to display[1] set_location_assignment PIN_E7 -to display[2] set_location_assignment PIN_E3 -to display[3] set_location_assignment PIN_A3 -to display[4] set_location_assignment PIN_B4 -to display[5] set_location_assignment PIN_A4 -to display[6] set_location_assignment PIN_B5 -to display[7] set_location_assignment PIN_A8 -to display[8] set_location_assignment PIN_D9 -to display[9] set_location_assignment PIN_D8 -to display[10] set_location_assignment PIN_A11 -to display[11] set_location_assignment PIN_D4 -to display[12] set_location_assignment PIN_D7 -to display[13] set_location_assignment PIN_V4 -to display[14] set_location_assignment PIN_A5 -to display[15]
矩阵键盘波形图
交通灯波形图
因篇幅问题不能全部显示,请点此查看更多更全内容
Copyright © 2019- cepb.cn 版权所有 湘ICP备2022005869号-7
违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务