一.任务需求
- 深入了解VGA协议,理解不同显示模式下的VGA控制时序参数(行频、场频、水平/垂直同步时钟周期、显示后沿/前沿等概念和计算方式);
- 通过Verilog编程,在至少2种显示模式下(640480@60Hz,1024768@75Hz)分别实现以下VGA显示,并对照VGA协议信号做时序分析:1)屏幕上显示彩色条纹;2)显示自定义的汉字字符(姓名-学号);
- 在Verilog代码中,将行、场同步信号中,故意分别加入一定 ms延时(用delay命令),观察会出现什么现象。
二.vga协议介绍
VGA(Video Graphics Array)视频图形阵列是IBM于1987年提出的一个使用模拟信号的电脑显示标准。VGA接口即电脑采用VGA标准输出数据的专用接口。VGA接口共有15针,分成3排,每排5个孔,显卡上应用最为广泛的接口类型,绝大多数显卡都带有此种接口。
1. VGA协议的一些关键特点和组成部分:
- 定义与应用:
VGA是使用模拟信号的一种视频传输标准,用于连接计算机和显示设备。
它不仅支持CRT(阴极射线管)显示器,也被用于LCD(液晶显示器)等现代显示技术。 - 接口结构:
VGA接口通常是一个15针的D-sub连接器,分为三排,每排五个孔。
针脚包括RGB(红绿蓝)三原色信号、水平和垂直同步信号(HSYNC和VSYNC),以及其他控制信号。 - 信号类型:
RGB信号:传输图像的颜色信息,通常有RGB 8位(RGB332)、RGB 16位(RGB565)和RGB 24位(RGB888)等格式。
同步信号:包括水平同步(HSYNC)和垂直同步(VSYNC),用于控制图像在屏幕上的显示位置。 - 扫描方式:
VGA支持逐行扫描和隔行扫描两种方式。逐行扫描可以减少屏幕闪烁,提高图像质量。 - 时序:
VGA信号的时序包括行时序和帧时序,决定了图像的刷新率和分辨率。 - 电气特性:
VGA信号的电气特性包括信号的峰值电压、阻抗匹配等,通常使用75欧姆的阻抗。 - 兼容性:
尽管现代显示技术已经发展到了数字信号传输,如HDMI和DisplayPort,但VGA仍然被广泛支持,作为最低标准存在。 - 硬件实现:
VGA接口可以通过专用的视频转换DAC芯片或R-2R电阻网络来实现模拟信号的输出。 - 软件实现:
在软件层面,需要根据VGA协议生成相应的RGB数据和同步信号,以控制图像的正确显示。 - 局限性:
VGA作为模拟信号传输方式,在长距离传输时可能会有信号衰减和干扰问题。
随着数字显示技术的发展,VGA逐渐被更高清晰度和更少干扰的数字接口所取代。
管脚 | 含义 |
---|---|
1 | 红基色 |
2 | 绿基色 |
3 | 蓝基色 |
4 | 地址码 ID Bit |
5 | 自测试 ( 各家定义不同 ) |
6 | 红地 |
7 | 绿地 |
8 | 蓝地 |
9 | 电源 |
10 | 数字地 |
11 | 地址码 |
12 | 地址码 |
13 | 行同步 |
14 | 场同步 |
15 | 地址码 |
三.开始实验
1.前期准备
tcl文件配置管脚:
set_location_assignment PIN_Y2 -to OSC_50
set_location_assignment PIN_D12 -to VGA_B[7]
set_location_assignment PIN_D11 -to VGA_B[6]
set_location_assignment PIN_C12 -to VGA_B[5]
set_location_assignment PIN_A11 -to VGA_B[4]
set_location_assignment PIN_B11 -to VGA_B[3]
set_location_assignment PIN_C11 -to VGA_B[2]
set_location_assignment PIN_A10 -to VGA_B[1]
set_location_assignment PIN_B10 -to VGA_B[0]
set_location_assignment PIN_F11 -to VGA_BLANK
set_location_assignment PIN_A12 -to VGA_CLK
set_location_assignment PIN_C9 -to VGA_G[7]
set_location_assignment PIN_F10 -to VGA_G[6]
set_location_assignment PIN_B8 -to VGA_G[5]
set_location_assignment PIN_C8 -to VGA_G[4]
set_location_assignment PIN_H12 -to VGA_G[3]
set_location_assignment PIN_F8 -to VGA_G[2]
set_location_assignment PIN_G11 -to VGA_G[1]
set_location_assignment PIN_G8 -to VGA_G[0]
set_location_assignment PIN_G13 -to VGA_HS
set_location_assignment PIN_H10 -to VGA_R[7]
set_location_assignment PIN_H8 -to VGA_R[6]
set_location_assignment PIN_J12 -to VGA_R[5]
set_location_assignment PIN_G10 -to VGA_R[4]
set_location_assignment PIN_F12 -to VGA_R[3]
set_location_assignment PIN_D10 -to VGA_R[2]
set_location_assignment PIN_E11 -to VGA_R[1]
set_location_assignment PIN_E12 -to VGA_R[0]
set_location_assignment PIN_C10 -to VGA_SYNC
set_location_assignment PIN_C13 -to VGA_VS
条纹显示代码:
module VGA_colorbar_test(
OSC_50, //原CLK2_50时钟信号
VGA_CLK, //VGA自时钟
VGA_HS, //行同步信号
VGA_VS, //场同步信号
VGA_BLANK, //复合空白信号控制信号 当BLANK为低电平时模拟视频输出消隐电平,此时从R9~R0,G9~G0,B9~B0输入的所有数据被忽略
VGA_SYNC, //符合同步控制信号 行时序和场时序都要产生同步脉冲
VGA_R, //VGA绿色
VGA_B, //VGA蓝色
VGA_G); //VGA绿色input OSC_50; //外部时钟信号CLK2_50output VGA_CLK,VGA_HS,VGA_VS,VGA_BLANK,VGA_SYNC;output [7:0] VGA_R,VGA_B,VGA_G;parameter H_FRONT = 16; //行同步前沿信号周期长parameter H_SYNC = 96; //行同步信号周期长parameter H_BACK = 48; //行同步后沿信号周期长parameter H_ACT = 640; //行显示周期长parameter H_BLANK = H_FRONT+H_SYNC+H_BACK; //行空白信号总周期长parameter H_TOTAL = H_FRONT+H_SYNC+H_BACK+H_ACT; //行总周期长耗时parameter V_FRONT = 11; //场同步前沿信号周期长parameter V_SYNC = 2; //场同步信号周期长parameter V_BACK = 31; //场同步后沿信号周期长parameter V_ACT = 480; //场显示周期长parameter V_BLANK = V_FRONT+V_SYNC+V_BACK; //场空白信号总周期长parameter V_TOTAL = V_FRONT+V_SYNC+V_BACK+V_ACT; //场总周期长耗时reg [10:0] H_Cont; //行周期计数器reg [10:0] V_Cont; //场周期计数器wire [7:0] VGA_R; //VGA红色控制线wire [7:0] VGA_G; //VGA绿色控制线wire [7:0] VGA_B; //VGA蓝色控制线reg VGA_HS;reg VGA_VS;reg [10:0] X; //当前行第几个像素点reg [10:0] Y; //当前场第几行reg CLK_25;always@(posedge OSC_50)begin CLK_25=~CLK_25; //时钟end assign VGA_SYNC = 1'b0; //同步信号低电平assign VGA_BLANK = ~((H_Cont<H_BLANK)||(V_Cont<V_BLANK)); //当行计数器小于行空白总长或场计数器小于场空白总长时,空白信号低电平assign VGA_CLK = ~CLK_to_DAC; //VGA时钟等于CLK_25取反assign CLK_to_DAC = CLK_25;always@(posedge CLK_to_DAC)beginif(H_Cont<H_TOTAL) //如果行计数器小于行总时长H_Cont<=H_Cont+1'b1; //行计数器+1else H_Cont<=0; //否则行计数器清零if(H_Cont==H_FRONT-1) //如果行计数器等于行前沿空白时间-1VGA_HS<=1'b0; //行同步信号置0if(H_Cont==H_FRONT+H_SYNC-1) //如果行计数器等于行前沿+行同步-1VGA_HS<=1'b1; //行同步信号置1if(H_Cont>=H_BLANK) //如果行计数器大于等于行空白总时长X<=H_Cont-H_BLANK; //X等于行计数器-行空白总时长 (X为当前行第几个像素点)else X<=0; //否则X为0
endalways@(posedge VGA_HS)beginif(V_Cont<V_TOTAL) //如果场计数器小于行总时长V_Cont<=V_Cont+1'b1; //场计数器+1else V_Cont<=0; //否则场计数器清零if(V_Cont==V_FRONT-1) //如果场计数器等于场前沿空白时间-1VGA_VS<=1'b0; //场同步信号置0if(V_Cont==V_FRONT+V_SYNC-1) //如果场计数器等于行前沿+场同步-1VGA_VS<=1'b1; //场同步信号置1if(V_Cont>=V_BLANK) //如果场计数器大于等于场空白总时长Y<=V_Cont-V_BLANK; //Y等于场计数器-场空白总时长 (Y为当前场第几行) else Y<=0; //否则Y为0
endreg valid_yr;always@(posedge CLK_to_DAC)beginif(V_Cont == 10'd32) //场计数器=32时valid_yr<=1'b1; //行输入激活else if(V_Cont==10'd512) //场计数器=512时valid_yr<=1'b0; //行输入冻结endwire valid_y=valid_yr; //连线 reg valid_r; always@(posedge CLK_to_DAC)beginif((H_Cont == 10'd32)&&valid_y) //行计数器=32时valid_r<=1'b1; //像素输入激活else if((H_Cont==10'd512)&&valid_y) //行计数器=512时 valid_r<=1'b0; //像素输入冻结endwire valid = valid_r; //连线assign x_dis=X; //连线Xassign y_dis=Y; //连线Y// reg[7:0] char_bit;// always@(posedge CLK_to_DAC)// if(X==10'd144)char_bit<=9'd240; //当显示到144像素时准备开始输出图像数据// else if(X>10'd144&&X<10'd384) //左边距屏幕144像素到416像素时 416=144+272(图像宽度)// char_bit<=char_bit-1'b1; //倒着输出图像信息reg[29:0] vga_rgb; //定义颜色缓存always@(posedge CLK_to_DAC) beginif(X>=0&&X<200)begin //X控制图像的横向显示边界:左边距屏幕左边144像素 右边界距屏幕左边界416像素vga_rgb<=30'hffffffffff; //白色endelse if(X>=200&&X<400)beginvga_rgb<=30'hf00ff65f1f; endelse if(X>=400&&X<600)beginvga_rgb<=30'h9563486251; endelse beginvga_rgb<=30'h5864928654; endendassign VGA_R=vga_rgb[23:16];assign VGA_G=vga_rgb[15:8];assign VGA_B=vga_rgb[7:0];
endmodule
字符显示:
module vga(
OSC_50, //原CLK2_50时钟信号
VGA_CLK, //VGA自时钟
VGA_HS, //行同步信号
VGA_VS, //场同步信号
VGA_BLANK, //复合空白信号控制信号 当BLANK为低电平时模拟视频输出消隐电平,此时从R9~R0,G9~G0,B9~B0输入的所有数据被忽略
VGA_SYNC, //符合同步控制信号 行时序和场时序都要产生同步脉冲
VGA_R, //VGA绿色
VGA_B, //VGA蓝色
VGA_G); //VGA绿色input OSC_50; //外部时钟信号CLK2_50output VGA_CLK,VGA_HS,VGA_VS,VGA_BLANK,VGA_SYNC;output [7:0] VGA_R,VGA_B,VGA_G;parameter H_FRONT = 16; //行同步前沿信号周期长parameter H_SYNC = 96; //行同步信号周期长parameter H_BACK = 48; //行同步后沿信号周期长parameter H_ACT = 640; //行显示周期长parameter H_BLANK = H_FRONT+H_SYNC+H_BACK; //行空白信号总周期长parameter H_TOTAL = H_FRONT+H_SYNC+H_BACK+H_ACT; //行总周期长耗时parameter V_FRONT = 11; //场同步前沿信号周期长parameter V_SYNC = 2; //场同步信号周期长parameter V_BACK = 31; //场同步后沿信号周期长parameter V_ACT = 480; //场显示周期长parameter V_BLANK = V_FRONT+V_SYNC+V_BACK; //场空白信号总周期长parameter V_TOTAL = V_FRONT+V_SYNC+V_BACK+V_ACT; //场总周期长耗时reg [10:0] H_Cont; //行周期计数器reg [10:0] V_Cont; //场周期计数器wire [7:0] VGA_R; //VGA红色控制线wire [7:0] VGA_G; //VGA绿色控制线wire [7:0] VGA_B; //VGA蓝色控制线reg VGA_HS;reg VGA_VS;reg [10:0] X; //当前行第几个像素点reg [10:0] Y; //当前场第几行reg CLK_25;always@(posedge OSC_50)begin CLK_25=~CLK_25; //时钟end assign VGA_SYNC = 1'b0; //同步信号低电平assign VGA_BLANK = ~((H_Cont<H_BLANK)||(V_Cont<V_BLANK)); //当行计数器小于行空白总长或场计数器小于场空白总长时,空白信号低电平assign CLK_to_DAC = CLK_25;assign VGA_CLK = ~CLK_to_DAC; //VGA时钟等于CLK_25取反always@(posedge CLK_to_DAC)beginif(H_Cont<H_TOTAL) //如果行计数器小于行总时长H_Cont<=H_Cont+1'b1; //行计数器+1else H_Cont<=0; //否则行计数器清零if(H_Cont==H_FRONT-1) //如果行计数器等于行前沿空白时间-1VGA_HS<=1'b0; //行同步信号置0if(H_Cont==H_FRONT+H_SYNC-1) //如果行计数器等于行前沿+行同步-1VGA_HS<=1'b1; //行同步信号置1if(H_Cont>=H_BLANK) //如果行计数器大于等于行空白总时长X<=H_Cont-H_BLANK; //X等于行计数器-行空白总时长 (X为当前行第几个像素点)else X<=0; //否则X为0endalways@(posedge VGA_HS)beginif(V_Cont<V_TOTAL) //如果场计数器小于行总时长V_Cont<=V_Cont+1'b1; //场计数器+1else V_Cont<=0; //否则场计数器清零if(V_Cont==V_FRONT-1) //如果场计数器等于场前沿空白时间-1VGA_VS<=1'b0; //场同步信号置0if(V_Cont==V_FRONT+V_SYNC-1) //如果场计数器等于行前沿+场同步-1VGA_VS<=1'b1; //场同步信号置1if(V_Cont>=V_BLANK) //如果场计数器大于等于场空白总时长Y<=V_Cont-V_BLANK; //Y等于场计数器-场空白总时长 (Y为当前场第几行) else Y<=0; //否则Y为0endreg valid_yr;always@(posedge CLK_to_DAC)if(V_Cont == 10'd32) //场计数器=32时valid_yr<=1'b1; //行输入激活else if(V_Cont==10'd512) //场计数器=512时valid_yr<=1'b0; //行输入冻结wire valid_y=valid_yr; //连线 reg valid_r; always@(posedge CLK_to_DAC) if((H_Cont == 10'd32)&&valid_y) //行计数器=32时valid_r<=1'b1; //像素输入激活else if((H_Cont==10'd512)&&valid_y) //行计数器=512时 valid_r<=1'b0; //像素输入冻结wire valid = valid_r; //连线wire[10:0] x_dis; //像素显示控制信号wire[10:0] y_dis; //行显示控制信号assign x_dis=X; //连线Xassign y_dis=Y; //连线Yparameterchar_line00=256'h0000000000000000000000000000000000000000000000000000000000000000,char_line01=256'h0000000000000000000000000000000000000000000000000000000000000000,char_line02=256'h0000000000000000000000000000000000000000000000000000000000C00001,char_line03=256'h07F00FE00FF0008007E01FFC07E007F007E00FE007E00FE000001C0000008000,char_line04=256'h08183018301807801818300818180818181830181818301800001FE000C00001,char_line05=256'h10003818380C0180381C2010381C1000381C3818381C381800001800FFF88000,char_line06=256'h3000001810180180300C0020300C3000300C0018300C0018000018C000CC0001,char_line07=256'h37F0006000180180300C0040300C37F0300C0060300C0060000018200C188030,char_line08=256'h380C01F000600180300C0080300C380C300C01F0300C01F0000018871FF01FFF,char_line09=256'h300C001801800180300C0180300C300C300C0018300C00180000FFF81818FFC0,char_line0a=256'h300C000C06000180300C0300300C300C300C000C300C000C0000190000C0000D,char_line0b=256'h300C380C08040180381803003818300C3818380C3818380C000018001010B000,char_line0c=256'h18183018300C01801C1003801C1018181C1030181C1030180000198000CE0019,char_line0d=256'h07E00FE03FF80FF807E0030007E007E007E00FE007E00FE00000180061F08800,char_line0e=256'h0000000000000000000000000000000000000000000000000000185F3FF10061,char_line0f=256'h0000000000000000000000000000000000000000000000000000FFFC80408700;reg[7:0] char_bit;always@(posedge CLK_to_DAC)if(X==10'd164)char_bit<=9'd256; //当显示到164像素时准备开始输出图像数据else if(X>10'd164&&X<10'd420) //左边距屏幕164像素到420像素时 420=164+256(图像宽度)char_bit<=char_bit-1'b1; //倒着输出图像信息reg[29:0] vga_rgb; //定义颜色缓存always@(posedge CLK_to_DAC) if(X>10'd164&&X<10'd420) //X控制图像的横向显示边界:左边距屏幕左边164像素 右边界距屏幕左边界420像素begin case(Y) //Y控制图像的纵向显示边界:从距离屏幕顶部160像素开始显示第一行数据10'd200:if(char_line00[char_bit])vga_rgb<=30'b1111111111_0000000000_0000000000; //如果该行有数据 则颜色为红色else vga_rgb<=30'b0000000000_0000000000_0000000000; //否则为黑色10'd201:if(char_line01[char_bit])vga_rgb<=30'b1111111111_0000000000_0000000000;else vga_rgb<=30'b0000000000_0000000000_0000000000;10'd202:if(char_line02[char_bit])vga_rgb<=30'b1111111111_0000000000_0000000000;else vga_rgb<=30'b0000000000_0000000000_0000000000;10'd203:if(char_line03[char_bit])vga_rgb<=30'b1111111111_0000000000_0000000000;else vga_rgb<=30'b0000000000_0000000000_0000000000;10'd204:if(char_line04[char_bit])vga_rgb<=30'b1111111111_0000000000_0000000000;else vga_rgb<=30'b0000000000_0000000000_0000000000; 10'd205:if(char_line05[char_bit])vga_rgb<=30'b1111111111_0000000000_0000000000;else vga_rgb<=30'b0000000000_0000000000_0000000000;10'd206:if(char_line06[char_bit])vga_rgb<=30'b1111111111_0000000000_0000000000;else vga_rgb<=30'b0000000000_0000000000_0000000000; 10'd207:if(char_line07[char_bit])vga_rgb<=30'b1111111111_0000000000_0000000000;else vga_rgb<=30'b0000000000_0000000000_0000000000;10'd208:if(char_line08[char_bit])vga_rgb<=30'b1111111111_0000000000_0000000000;else vga_rgb<=30'b0000000000_0000000000_0000000000; 10'd209:if(char_line09[char_bit])vga_rgb<=30'b1111111111_0000000000_0000000000;else vga_rgb<=30'b0000000000_0000000000_0000000000;10'd210:if(char_line0a[char_bit])vga_rgb<=30'b1111111111_0000000000_0000000000;else vga_rgb<=30'b0000000000_0000000000_0000000000;10'd211:if(char_line0b[char_bit])vga_rgb<=30'b1111111111_0000000000_0000000000;else vga_rgb<=30'b0000000000_0000000000_0000000000;10'd212:if(char_line0c[char_bit])vga_rgb<=30'b1111111111_0000000000_0000000000;else vga_rgb<=30'b0000000000_0000000000_0000000000;10'd213:if(char_line0d[char_bit])vga_rgb<=30'b1111111111_0000000000_0000000000;else vga_rgb<=30'b0000000000_0000000000_0000000000;10'd214:if(char_line0e[char_bit])vga_rgb<=30'b1111111111_0000000000_0000000000;else vga_rgb<=30'b0000000000_0000000000_0000000000;10'd215:if(char_line0f[char_bit])vga_rgb<=30'b1111111111_0000000000_0000000000;else vga_rgb<=30'b0000000000_0000000000_0000000000;default:vga_rgb<=30'h0000000000; //默认颜色黑色endcase endelse vga_rgb<=30'h000000000; //否则黑色assign VGA_R=vga_rgb[23:16];assign VGA_G=vga_rgb[15:8];assign VGA_B=vga_rgb[7:0];
endmodule
2.实验效果
条纹显示:
字符显示:
四.参考文献
https://blog.csdn.net/lxr0106/article/details/139058075