目录
一、VGA介绍
(一) VGA协议
(二) VGA端口介绍
(三) 色彩原理
(四)VGA显示原理
VGA通信协议:
VGA时序解析
时钟分频
二、实现
1.彩条显示
2.字符显示
3.图片显示
三、代码
1.vga驱动模块
2.显示数据生成模块
3.按键消抖模块
4.顶层模块
四、效果
RTL视图
顶层编辑
vga_drive编辑
data_drive
视频
五 、参考
什么是VGA?VGA不是用来显示的那块屏幕,而是用来传输信号的接口。VGA全称是Video Graphics Array,即视频图形阵列,是模拟信号的一种视频传输标准。不⽀持热插拔,不⽀持⾳频传输。对于⼀些嵌⼊式VGA显示系统,可以在不使⽤VGA显卡和计算机的 情况下,实现VGA图像的显示和控制。VGA显示器具有成本低、结构简单、应⽤灵活的优点。
VGA端口是视频输出端口,端口一共包含15个管脚,如下图
VGA接口是一种D型接口,上面共有15针孔,分成三排,每排五个。 其中,除了2根NC(Not Connect)信号、3根显示数据总线和5个GND信号,比较重要的是3根RGB彩色分量信号和2根扫描同步信号HSYNC和VSYNC针。VGA接口中彩色分量采用RS343电平标准。RS343电平标准的峰值电压为1V。VGA接口是显卡上应用最为广泛的接口类型,多数的显卡都带有此种接口。有些不带VGA接口而带有DVI(Digital Visual Interface数字视频接口)接口的显卡,也可以通过一个简单的转接头将DVI接口转成VGA接口,通常没有VGA接口的显卡会附赠这样的转接头。
在通常使用的连接方法里面,15个管脚里面的5个是最重要的,他们包括3个基本红、绿、蓝三条基本色彩和水平与垂直两条控制线。
大多数计算机与外部显示设备之间都是通过模拟VGA接口连接,计算机内部以数字方式生成的显示图像信息,被显卡中的数字/模拟转换器转变为R、G、B三原色信号和行、场同步信号,信号通过电缆传输到显示设备中。对于模拟显示设备,如模拟CRT显示器,信号被直接送到相应的处理电路,驱动控制显像管生成图像。而对于LCD、DLP等数字显示设备,显示设备中需配置相应的A/D(模拟/数字)转换器,将模拟信号转变为数字信号。在经过D/A和A/D两次转换后,不可避免地造成了一些图像细节的损失。VGA接口应用于CRT显示器无可厚非,但用于连接液晶之类的显示设备,则转换过程的图像损失会使显示效果略微下降。
而且可以从接口处来判断显卡是独显还是集成显卡,VGA接口竖置的说明是集成显卡,VGA接口横置说明是独立显卡(一般的台式主机都可以用此方法来查看)。
三基⾊是指通过其他颜⾊的混合⽆法得到的“基本 ⾊”由于⼈的⾁眼有感知红、绿、蓝三种不同颜⾊的锥体细胞,因此⾊彩空间通常可以由三种基本⾊来表达
设计RGB信号时,既可以R信号、G信号和B信号独⽴的赋值,最后连到端⼝上,也可以直接⽤RGB当做⼀个整体信号,RGB信号在使⽤时的位宽有三种常见格式,以你的VGA解码芯⽚的配置有关。
- RGB_8,R:G:B = 3:3:2,即RGB332
- RGB_16,R:G:B = 5:6:5,即RGB565
- RGB_24,R:G:B = 8:8:8,即RGB888
依次对应8、16、24位位宽,位宽越高,图像越清晰
VGA通过引脚的模拟电压(0V-0.714V)显示红绿蓝三种颜色,不同的电压值对应不同的颜色。
VGA驱动显示器用的是扫描的方式,一般是逐行扫描。
逐行扫描是扫描从屏幕左上角一点开始,从左像右逐点扫描,每扫描完一行,电子束回到屏幕的左边下一行的起始位置,在这期间,CRT对电子束进行消隐,每行结束时,用行同步信号进行同步;
当扫描完所有的行,形成一帧后,用场同步信号进行场同步,并使扫描回到屏幕左上方,同时进行场消隐,开始下一帧。
FPGA芯片驱动VGA显示,需要先产生模拟信号,这就要借助数模转换器D/A,利用D/A产生模拟信号,输出至VGA的RED、GREEN、BLUE基色数据线。另一种方法是利用电阻网络分流模拟D/A实现的。
具体颜色对应的电压值:
利用电阻网络分流模拟D/A:
参考:https://blog.csdn.net/qq_40147893/article/details/108342484
VS:帧时序
帧时序的四个部分别是:同步脉冲(Sync o)、显示后沿(Back porch p)、显示时序段(Display interval q)和显示前沿(Front porchr)。其中同步脉冲(Sync o)、显示后沿(Back porch p)和显示前沿(Front porch r)是消隐区,RGB信号无效,屏幕不显示数据。显示时序段(Display interval q)是有效数据区。
HS:行时序
行时序的四个部分分别是:同步脉冲(Sync a)、显示后沿(Back porch b)、显示时序(Display interval c)和显示前沿(Front porchd)。其中同步脉冲(Sync a)、显示后沿(Back porch b)和显示前沿(Front porch d)是消隐区,RGB信号无效,屏幕不显示数据。显示时序段(Display interval c)是有效数据区。
不同分辨率对应参数
举例:分别使用640×480 60HZ和800×600 72HZ,对应时钟分别为25M和50M,需要使用PLL进行分频 时钟频率 = 行帧长 × 列帧长 * 刷新率,640 ×480 60HZ对应时钟频率= 800 ×525 × 60 = 25.2M,1040x666x72=49.9M
IP核里面找到ALTPLL
基础时钟选择50M
c0默认输出50M即可,c1分频到25M,如需其他时钟频率可以自己进行设置
根据当前行地址判断需要显示的颜色即可。
在子模提取工具里面输入需要显示的字符并设置字符大小为64*64,点阵大小为64*64
再点击选项按如下参数设置
最后点击生成字符并保存字符为文本文件
注意:char_line[0]这一行全赋值为0,也就是说增加了第一行0,其余不变
之所以要转变成BMP图片格式,是因为否则生成的都是单个字模,位数会不统一,为了位数长度统一,代码中384=64*6,也就是一个字符宽度64,因为显示6个字符,因此位宽384
使用工具BMP2MifV1.0把图片转为HEX文件
在转换图片之前需要先修改图片的格式,使用电脑自带的画图软件打开此图片
注意:保存类型一定要选择为24位位图(*.bmp;*.dib),否则加载不成功
然后开始转换
选择输出图像格式RGB565 文件类型Hex
由于图片数据太多需要使用ROM来存储数据
设置位宽度为16位,大小为图片大小128×78 = 9984
取消勾选下列选项
勾选以下选项后直接finsh即可
数据的读取和使用参考后面的代码部分
基于EP4CE6F17C8型号芯片
module vga_dirve (input wire clk, //系统时钟
input wire rst_n, //复位
input wire [ 15:0 ] rgb_data, //16位RGB对应值
output wire vga_clk, //vga时钟 25M
output reg h_sync, //行同步信号
output reg v_sync, //场同步信号
output reg [ 11:0 ] addr_h, //行地址
output reg [ 11:0 ] addr_v, //列地址
output wire [ 4:0 ] rgb_r, //红基色
output wire [ 5:0 ] rgb_g, //绿基色
output wire [ 4:0 ] rgb_b //蓝基色
);
// 640 * 480 60HZ
localparam H_FRONT = 16; // 行同步前沿信号周期长
localparam H_SYNC = 96; // 行同步信号周期长
localparam H_BLACK = 48; // 行同步后沿信号周期长
localparam H_ACT = 640; // 行显示周期长
localparam V_FRONT = 11; // 场同步前沿信号周期长
localparam V_SYNC = 2; // 场同步信号周期长
localparam V_BLACK = 31; // 场同步后沿信号周期长
localparam V_ACT = 480; // 场显示周期长
// 800 * 600 72HZ
// localparam H_FRONT = 40; // 行同步前沿信号周期长
// localparam H_SYNC = 120; // 行同步信号周期长
// localparam H_BLACK = 88; // 行同步后沿信号周期长
// localparam H_ACT = 800; // 行显示周期长
// localparam V_FRONT = 37; // 场同步前沿信号周期长
// localparam V_SYNC = 6; // 场同步信号周期长
// localparam V_BLACK = 23; // 场同步后沿信号周期长
// localparam V_ACT = 600; // 场显示周期长
localparam H_TOTAL = H_FRONT + H_SYNC + H_BLACK + H_ACT; // 行周期
localparam V_TOTAL = V_FRONT + V_SYNC + V_BLACK + V_ACT; // 列周期
reg [ 11:0 ] cnt_h ; // 行计数器
reg [ 11:0 ] cnt_v ; // 场计数器
reg [ 15:0 ] rgb ; // 对应显示颜色值
// 对应计数器开始、结束、计数信号
wire flag_enable_cnt_h ;
wire flag_clear_cnt_h ;
wire flag_enable_cnt_v ;
wire flag_clear_cnt_v ;
wire flag_add_cnt_v ;
wire valid_area ;
// 25M时钟 行周期*场周期*刷新率 = 800 * 525* 60
wire clk_25 ;
// 50M时钟 1040 * 666 * 72
wire clk_50 ;
//PLL
pll pll_inst (
.areset ( ~rst_n ),
.inclk0 ( clk ),
.c0 ( clk_50 ), //50M
.c1 ( clk_25 ), //25M
);
//根据不同分配率选择不同频率时钟
assign vga_clk = clk_25;
// 行计数
always @( posedge vga_clk or negedge rst_n ) begin
if ( !rst_n ) begin
cnt_h <= 0;
end
else if ( flag_enable_cnt_h ) begin
if ( flag_clear_cnt_h ) begin
cnt_h <= 0;
end
else begin
cnt_h <= cnt_h + 1;
end
end
else begin
cnt_h <= 0;
end
end
assign flag_enable_cnt_h = 1;
assign flag_clear_cnt_h = cnt_h == H_TOTAL - 1;
// 行同步信号
always @( posedge vga_clk or negedge rst_n ) begin
if ( !rst_n ) begin
h_sync <= 0;
end
else if ( cnt_h == H_SYNC - 1 ) begin // 同步周期时为1
h_sync <= 1;
end
else if ( flag_clear_cnt_h ) begin // 其余为0
h_sync <= 0;
end
else begin
h_sync <= h_sync;
end
end
// 场计数
always @( posedge vga_clk or negedge rst_n ) begin
if ( !rst_n ) begin
cnt_v <= 0;
end
else if ( flag_enable_cnt_v ) begin
if ( flag_clear_cnt_v ) begin
cnt_v <= 0;
end
else if ( flag_add_cnt_v ) begin
cnt_v <= cnt_v + 1;
end
else begin
cnt_v <= cnt_v;
end
end
else begin
cnt_v <= 0;
end
end
assign flag_enable_cnt_v = flag_enable_cnt_h;
assign flag_clear_cnt_v = cnt_v == V_TOTAL - 1;
assign flag_add_cnt_v = flag_clear_cnt_h;
// 场同步信号
always @( posedge vga_clk or negedge rst_n ) begin
if ( !rst_n ) begin
v_sync <= 0;
end
else if ( cnt_v == V_SYNC - 1 ) begin
v_sync <= 1;
end
else if ( flag_clear_cnt_v ) begin
v_sync <= 0;
end
else begin
v_sync <= v_sync;
end
end
// 对应有效区域行地址 1-640
always @( posedge vga_clk or negedge rst_n ) begin
if ( !rst_n ) begin
addr_h <= 0;
end
else if ( valid_area ) begin
addr_h <= cnt_h - H_SYNC - H_BLACK + 1;
end
else begin
addr_h <= 0;
end
end
// 对应有效区域列地址 1-480
always @( posedge vga_clk or negedge rst_n ) begin
if ( !rst_n ) begin
addr_v <= 0;
end
else if ( valid_area ) begin
addr_v <= cnt_v -V_SYNC - V_BLACK + 1;
end
else begin
addr_v <= 0;
end
end
// 有效显示区域
assign valid_area = cnt_h >= H_SYNC + H_BLACK && cnt_h <= H_SYNC + H_BLACK + H_ACT && cnt_v >= V_SYNC + V_BLACK && cnt_v <= V_SYNC + V_BLACK + V_ACT;
// 显示颜色
always @( posedge vga_clk or negedge rst_n ) begin
if ( !rst_n ) begin
rgb <= 16'h0;
end
else if ( valid_area ) begin
rgb <= rgb_data;
end
else begin
rgb <= 16'b0;
end
end
assign rgb_r = rgb[ 15:11 ];
assign rgb_g = rgb[ 10:5 ];
assign rgb_b = rgb[ 4:0 ];
endmodule // vga_dirve
module data_drive (input wire vga_clk,
input wire rst_n,
input wire [ 11:0 ] addr_h,
input wire [ 11:0 ] addr_v,
input wire [ 2:0 ] key,
output reg [ 15:0 ] rgb_data);
localparam red = 16'd63488;
localparam orange = 16'd64384;
localparam yellow = 16'd65472;
localparam green = 16'd1024;
localparam blue = 16'd31;
localparam indigo = 16'd18448;
localparam purple = 16'd32784;
localparam white = 16'd65503;
localparam black = 16'd0;
reg [ 383:0 ] char_line[ 64:0 ];
localparam states_1 = 1; // 彩条
localparam states_2 = 2; // 字符
localparam states_3 = 3; // 图片
parameter height = 78; // 图片高度
parameter width = 128; // 图片宽度
reg [ 1:0 ] states_current ; // 当前状态
reg [ 1:0 ] states_next ; // 下个状态
reg [ 13:0 ] rom_address ; // ROM地址
wire [ 15:0 ] rom_data ; // 图片数据
wire flag_enable_out1 ; // 文字有效区域
wire flag_enable_out2 ; // 图片有效区域
wire flag_clear_rom_address ; // 地址清零
wire flag_begin_h ; // 图片显示行
wire flag_begin_v ; // 图片显示列
//状态转移
always @( posedge vga_clk or negedge rst_n ) begin
if ( !rst_n ) begin
states_current <= states_1;
end
else begin
states_current <= states_next;
end
end
//状态判断
always @( posedge vga_clk or negedge rst_n ) begin
if ( !rst_n ) begin
states_next <= states_1;
end
else if ( key[ 0 ] ) begin
states_next <= states_1;
end
else if ( key[ 1 ] ) begin
states_next <= states_2;
end
else if ( key[ 2 ] ) begin
states_next <= states_3;
end
else begin
states_next <= states_next;
end
end
//状态输出
always @( * ) begin
case ( states_current )
states_1 : begin
if ( addr_h == 0 ) begin
rgb_data = black;
end
else if ( addr_h >0 && addr_h <81 ) begin
rgb_data = red;
end
else if ( addr_h >80 && addr_h <161 ) begin
rgb_data = orange;
end
else if ( addr_h >160 && addr_h <241 ) begin
rgb_data = yellow;
end
else if ( addr_h >240 && addr_h <321 ) begin
rgb_data = green;
end
else if ( addr_h >320 && addr_h <401 ) begin
rgb_data = blue;
end
else if ( addr_h >400 && addr_h <481 ) begin
rgb_data = indigo;
end
else if ( addr_h >480 && addr_h <561 ) begin
rgb_data = purple;
end
else if ( addr_h >560 && addr_h <641 ) begin
rgb_data = white;
end
else begin
rgb_data = black;
end
end
states_2 : begin
if ( flag_enable_out1 ) begin
rgb_data = char_line[ addr_v-208 ][ 532 - addr_h ]? white:black;
end
else begin
rgb_data = black;
end
end
states_3 : begin
if ( flag_enable_out2 ) begin
rgb_data = rom_data;
end
else begin
rgb_data = black;
end
end
default: begin
case ( addr_h )
0 : rgb_data = black;
1 : rgb_data = red;
81 : rgb_data = orange;
161: rgb_data = yellow;
241: rgb_data = green;
321: rgb_data = blue;
401: rgb_data = indigo;
481: rgb_data = purple;
561: rgb_data = white;
default: rgb_data = rgb_data;
endcase
end
endcase
end
assign flag_enable_out1 = states_current == states_2 && addr_h > 148 && addr_h < 533 && addr_v > 208 && addr_v < 273 ;
assign flag_begin_h = addr_h > ( ( 640 - width ) / 2 ) && addr_h < ( ( 640 - width ) / 2 ) + width + 1;
assign flag_begin_v = addr_v > ( ( 480 - height )/2 ) && addr_v <( ( 480 - height )/2 ) + height + 1;
assign flag_enable_out2 = states_current == states_3 && flag_begin_h && flag_begin_v;
//ROM地址计数器
always @( posedge vga_clk or negedge rst_n ) begin
if ( !rst_n ) begin
rom_address <= 0;
end
else if ( flag_clear_rom_address ) begin //计数满清零
rom_address <= 0;
end
else if ( flag_enable_out2 ) begin //在有效区域内+1
rom_address <= rom_address + 1;
end
else begin //无效区域保持
rom_address <= rom_address;
end
end
assign flag_clear_rom_address = rom_address == height * width - 1;
//初始化显示文字
always@( posedge vga_clk or negedge rst_n ) begin
if ( !rst_n ) begin
char_line[ 0 ] = 384'h000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000;
char_line[ 1 ] = 384'h000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000;
char_line[ 2 ] = 384'h000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000;
char_line[ 3 ] = 384'h000000000000000000000002000000000000000000000000000000300000000000000000000000000000000000000000;
char_line[ 4 ] = 384'h0000000000000000000000078000000000000800000004000000001E0000000000000000000100000000200000800000;
char_line[ 5 ] = 384'h000000000000000000000007C000000000000E00000003000000000F80000000000180000003800000003C0000E00000;
char_line[ 6 ] = 384'h000000000000000000000007E000000000000F80000003C000000007C00000000001FFFFFFFFE00000003F0000F80000;
char_line[ 7 ] = 384'h00000000000000000000000F8000000000000F00000003C000000003E00000000001FFFFFFFFE00000003E0000F00000;
char_line[ 8 ] = 384'h00000000000000000000000FC000000000000E000000038000000003E00000000001E0000003C00000003C0000E00000;
char_line[ 9 ] = 384'h00000000000008000000001F4000000000000E000000038000600001E00000000001E0000003C00000003C0000E00000;
char_line[ 10 ] = 384'h0000000000001C000000001E6000000000000E000000038000600000C00002000001E0000003C00000003C0000E00000;
char_line[ 11 ] = 384'h0000000000003E000000003E3000000000000E003000038000600000C00007000001E0000003C00000003C0000E00000;
char_line[ 12 ] = 384'h01FFFFFFFFFFFF000000003C3800000000000E0078300380007FFFFFFFFFFF800001E0000003C00000003C0000E00000;
char_line[ 13 ] = 384'h00FFFFFFFFFFFF8000000078180000000FFFFFFFFC3C0380007FFFFFFFFFFFC00001FFFFFFFFC00000003C0000E00000;
char_line[ 14 ] = 384'h0060000000000000000000F80C00000007FFFFFFFE3E038000E0000000000FE00001FFFFFFFFC00000803C0000E00000;
char_line[ 15 ] = 384'h0000000000000000000000F00E00000000000E00003E038000E0018000000F800001E0000003C00000E03C0000E00000;
char_line[ 16 ] = 384'h0000000000000000000001E00700000000000E00003C038001E001C000001E000001E0000003C00000F83C0000E00400;
char_line[ 17 ] = 384'h0000000000000000000003E00380000000000E00003C038003E001F000001C000001E0000003C00000F03C0C00E00700;
char_line[ 18 ] = 384'h0000000000000000000003C003C0000000000E00003C038003E003F8000030000001E0000003C00000F03C0FFFFFFF80;
char_line[ 19 ] = 384'h00000000000000000000078001E0000000000E00003C038007C007E0000060000001E0000003C00000F03C0FFFFFFF80;
char_line[ 20 ] = 384'h000000000000000000000F0000F0000000000E00403C0380078007C0003040000001FFFFFFFFC00000F03C0E00E00F00;
char_line[ 21 ] = 384'h000000000000000000001E000078000000400E00603C038000000FFFFFF800000001FFFFFFFFC00000F03C0E00E00F00;
char_line[ 22 ] = 384'h000000000000000000003E02003E0000007FFFFFF03C038000000FFFFFFC00000001E0000003C00000F03C0E00E00F00;
char_line[ 23 ] = 384'h000000000000000000007C03801F0000007FFFFFF83C038000001F00007E00000001E0000003800000F03C0E00E00F00;
char_line[ 24 ] = 384'h00000000000000000000F803E01FC00000700E00F03C038000003C8000F000000001C0000002030000F03C0E00E00F00;
char_line[ 25 ] = 384'h00000000000000000001E003E007E00000700E00E03C0380000078C001E00000000000000000078000F03C0E00E00F00;
char_line[ 26 ] = 384'h00000000000000000003C003C003F80000700E00E03C03800000706003C000000000000000000FC000F03C0E00E00F00;
char_line[ 27 ] = 384'h000000000000000000078003C001FF0000700E00E03C03800000E030078000001FFFFFFFFFFFFFE000F03C0E00E00F00;
char_line[ 28 ] = 384'h0000000000000000000F0003C000FFE000700E00E03C03800001C0180F0000000FFFFFFFFFFFFFF000F03C0E00E00F00;
char_line[ 29 ] = 384'h0000000000000000001C0003C0007FF800700E00E03C03800003800C1E000000000F001E0000000000F03C0E00E00F00;
char_line[ 30 ] = 384'h000000000001000000780003C0001FE000700E00E03C03800007000F3C000000000F001E0000000000F0380E00E00F00;
char_line[ 31 ] = 384'h000000000003800000E00003C0000F8000700E00E03C0380000E0007F8000000000F001E0000080000F0380E00E00F00;
char_line[ 32 ] = 384'h000000000007C00003800003C000030000700E00E03C0380001C0003F0000000000F001E00000C0000F0380E00E00F00;
char_line[ 33 ] = 384'h001FFFFFFFFFE0000E000003C000000000700E00E03C038000300001F0000000000F001FFFFFFF0000F0380E00E00F00;
char_line[ 34 ] = 384'h000FFFFFFFFFF00038000003C000000000700E00E03C038000600007FC000000000FFFFEFFFFFF8000F0380E00E00F00;
char_line[ 35 ] = 384'h000600000000000000000003C000000000700E00E03C03800080000FBF000000000FFFFE44001E0000F0380E00E00F00;
char_line[ 36 ] = 384'h000000000000000000000003C000000000700E00E03C03800000003E0FC00000000F001E04003C0000F0380E00E00F00;
char_line[ 37 ] = 384'h000000000000000000000003C000000000701E7FE03C03800000007C03F80000000F001E06003C0000F0380E00E00F00;
char_line[ 38 ] = 384'h000000000000000000000003C000000000703E0FE03C0380000001F001FF0000000F001E0200380000F0780E00E00F00;
char_line[ 39 ] = 384'h000000000000000000000003C000000000703E07C03C0380000007C0007FF800000F001E0300780000F0780E00E00F00;
char_line[ 40 ] = 384'h000000000000000000000003C000000000607F03803C038000001F00001FFFF8000F001E0300700000F0700E00E00F00;
char_line[ 41 ] = 384'h000000000000000000000003C000000000007F82003C038000007C000007FFF0000FFFFE0180F00000F0700E00E00F00;
char_line[ 42 ] = 384'h000000000000000000000003C00000000000FEE0003C03800001F000000EFFC0000FFFFE0180E00000F0700E00E00F00;
char_line[ 43 ] = 384'h000000000000000000000003C00000000001EE38003C0380000FFFFFFFFF1F80000F001E01C1E00000C0F00E00E00F00;
char_line[ 44 ] = 384'h000000000000000000000003C00000000003CE1E003C0380007E7FFFFFFF8300000F001E00C1C0000000E00E00E00F00;
char_line[ 45 ] = 384'h000000000000000000000003C000000000038E0F003C038003E07800000F0000000F001E00E3C0000000E00E00E00F00;
char_line[ 46 ] = 384'h000000000000000000000003C000000000070E07C03C03801E007800000E0000000F001E006780000001E00E00E7FF00;
char_line[ 47 ] = 384'h000000000000000000000003C0000000000E0E03E03C038000007800000E0000000F001E1C7700000001C00E00E1FE00;
char_line[ 48 ] = 384'h000000000000000000000003C0000000001C0E01F03C038000007800000E0000000F001FF03F00000001C00E00E07E00;
char_line[ 49 ] = 384'h000000000000000000000003C000000000380E00F830038000007800000E0000000F00FF803E00000003800C00E03C00;
char_line[ 50 ] = 384'h000000000000000000000003C000000000700E007800038000007800000E0000000F3FFE001E00000003800800E01800;
char_line[ 51 ] = 384'h000000000000018000000003C000000000E00E007800038000007800000E0000000FFFDE003F00000007000000E00000;
char_line[ 52 ] = 384'h00000000000003C000000003C000000000C00E003800038000007800000E000000FFFC1E00778000000E000000E00000;
char_line[ 53 ] = 384'h00000000000007E000000003C000000001800E001000038000007800000E00000FFFC01E00E3C000000E000000E00000;
char_line[ 54 ] = 384'h3FFFFFFFFFFFFFF000000003C000000003000E000000038000007800000E000007FE001E01C3F000001C000000E00000;
char_line[ 55 ] = 384'h1FFFFFFFFFFFFFF800000003C00000000E000E000000038000007800000E000007E0001E0380FC000038000000E00000;
char_line[ 56 ] = 384'h0C0000000000000000000003C000000018000E00000FCF8000007FFFFFFE00000380001E07007F000070000000E00000;
char_line[ 57 ] = 384'h000000000000000000000003C000000030000E000007FF8000007FFFFFFE00000200001E0E003FF800E0000000E00000;
char_line[ 58 ] = 384'h000000000000000000000003C000000000000E000001FF8000007800000E00000000001E18001FE001C0000000E00000;
char_line[ 59 ] = 384'h000000000000000000000003C000000000000E0000007F0000007800000E00000000001E700007800300000000E00000;
char_line[ 60 ] = 384'h000000000000000000000003C000000000000E0000001E0000007000000C00000000001EC00003000600000000E00000;
char_line[ 61 ] = 384'h0000000000000000000000030000000000000E0000000C0000004000000000000000001F800000000800000000E00000;
char_line[ 62 ] = 384'h000000000000000000000002000000000000080000000000000000000000000000000010000000000000000000800000;
char_line[ 63 ] = 384'h000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000;
char_line[ 64 ] = 384'h000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000;
end
end
//实例化ROM
ROM1_port ROM1_port_inst (
.address ( rom_address ),
.clock ( vga_clk ),
.q ( rom_data )
);
endmodule // data_drive
module key_debounce(
input wire clk,
input wire rst_n,
input wire key,
output reg flag,// 0抖动, 1抖动结束
output reg key_value//key抖动结束后的值
);
parameter MAX_NUM = 20'd1_000_000;
reg [19:0] delay_cnt;//1_000_000
reg key_reg;//key上一次的值
always @(posedge clk or negedge rst_n) begin
if(!rst_n) begin
key_reg <= 1;
delay_cnt <= 0;
end
else begin
key_reg <= key;
//当key为1 key 为0 表示按下抖动,开始计时
if(key_reg != key ) begin
delay_cnt <= MAX_NUM ;
end
else begin
if(delay_cnt > 0)
delay_cnt <= delay_cnt -1;
else
delay_cnt <= 0;
end
end
end
//当计时完成,获取key的值
always @(posedge clk or negedge rst_n) begin
if(!rst_n) begin
flag <= 0;
key_value <= 1;
end
else begin
// 计时完成 处于稳定状态,进行赋值
if(delay_cnt == 1) begin
flag <= 1;
key_value <= key;
end
else begin
flag <= 0;
key_value <= key_value;
end
end
end
endmodule
module vga_top (input wire clk,
input wire rst_n,
input wire [ 2:0 ] key,
output wire vga_clk,
output wire h_sync,
output wire v_sync,
output wire [ 4:0 ] rgb_r,
output wire [ 5:0 ] rgb_g,
output wire [ 4:0 ] rgb_b,
output reg [ 3:0 ] led);
reg [ 27:0 ] cnt ;
wire [ 11:0 ] addr_h ;
wire [ 11:0 ] addr_v ;
wire [ 15:0 ] rgb_data ;
wire [ 2:0 ] key_flag ;
wire [ 2:0 ] key_value ;
//vga模块
vga_dirve u_vga_dirve(
.clk ( clk ),
.rst_n ( rst_n ),
.rgb_data ( rgb_data ),
.vga_clk ( vga_clk ),
.h_sync ( h_sync ),
.v_sync ( v_sync ),
.rgb_r ( rgb_r ),
.rgb_g ( rgb_g ),
.rgb_b ( rgb_b ),
.addr_h ( addr_h ),
.addr_v ( addr_v )
);
//数据模块
data_drive u_data_drive(
.vga_clk ( vga_clk ),
.rst_n ( rst_n ),
.addr_h ( addr_h ),
.addr_v ( addr_v ),
.key ( {key_value[ 2 ] && key_flag[ 2 ], key_value[ 1 ] && key_flag[ 1 ], key_value[ 0 ] && key_flag[ 0 ] } ),
.rgb_data ( rgb_data )
);
//按键消抖
key_debounce u_key_debounce0(
.clk ( vga_clk ),
.rst_n ( rst_n ),
.key ( key[ 0 ] ),
.flag ( key_flag[ 0 ] ),
.key_value ( key_value[ 0 ] )
);
key_debounce u_key_debounce1(
.clk ( vga_clk ),
.rst_n ( rst_n ),
.key ( key[ 1 ] ),
.flag ( key_flag[ 1 ] ),
.key_value ( key_value[ 1 ] )
);
key_debounce u_key_debounce2(
.clk ( vga_clk ),
.rst_n ( rst_n ),
.key ( key[ 2 ] ),
.flag ( key_flag[ 2 ] ),
.key_value ( key_value[ 2 ] )
);
// led
always @( posedge clk or negedge rst_n ) begin
if ( !rst_n ) begin
cnt <= 0;
end
else if ( cnt == 50_000_000 - 1 ) begin
cnt <= 0;
end
else begin
cnt <= cnt + 1;
end
end
always @( posedge clk or negedge rst_n ) begin
if ( !rst_n ) begin
led <= 4'b0000;
end
else if ( cnt == 50_000_000 -1 )begin
led <= ~led;
end
else begin
led <= led;
end
end
endmodule // vga_top
通过按键切换状态,根据不同状态分别输出彩条、字符、图片。
640 ×480分辨率下拍摄 点击下方链接可观看
https://live.csdn.net/v/230012
截图如下
https://blog.csdn.net/qq_47281915/article/details/125134764