这篇基本只是贴出代码,不做详细的说明。如果想看详细的说明可以参考自恋狂vip版主的:2.VGA时序与模拟彩条实现/post.php?tid=650&fromuid=13
(出处: 电子资源)
但是上篇的VGA显示效果不是很好,这篇到时会上篇进行整合,做成资料。这篇的代码是经过精雕细琢的,追求的是时序的完美。
还有本篇代码分频是手工分频,方便大家移植程序。希望跟着本教程学习的同学,可以根据:
MiZ702学习笔记10——文本实例化IP的方法
(出处: 电子资源)
这篇文件,进行修改,将手工分配改成IP分配。算是给大家布置的作业。
程序结构如下:

外加一个头文件:
lcd_para.v
vga_demo.v 主要负责给VGA接口赋值,实现彩条
`timescale 1ns / 1psmy_vga.v 封装VGA接口://////////////////////////////////////////////////////////////////////////////////
// Company: 米联
// Engineer: 宋桓公
//
// Create Date: 2016/01/09 11:52:30
// Design Name:
// Module Name: vga_demo
// Project Name:
// Target Devices:
// Tool Versions:
// Description:
//
// Dependencies:
//
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
//
//////////////////////////////////////////////////////////////////////////////////
module vga_demo(
input CLK,
input RSTn,
//硬件接口
output lcd_hs, //lcd horizontal sync
output lcd_vs, //lcd vertical sync
//output lcd_blank, //lcd blank(L:blank)
output[3:0] lcd_red, //lcd red data
output[3:0] lcd_green, //lcd green data
output[3:0] lcd_blue //lcd blue data
);
`include "lcd_para.v"
reg rCLK_25M;
reg [2:0]i;
//简单粗暴的弄个4分频,从100M到25M
always@(posedge CLK or negedge RSTn)
if(!RSTn)
begin
rCLK_25M <= 1'b0;
i <= 3'd0;
end
else
case(i)
0,1,2,3:
begin
rCLK_25M <= 1'b1;
i <= i + 1'b1;
end
4,5,6:
begin
rCLK_25M <= 1'b0;
i <= i + 1'b1;
end
7:
begin
rCLK_25M <= 1'b0;
i <= 3'd0;
end
endcase
wire CLK_25M = rCLK_25M;
//-------------------------------------------------
reg [11:0]lcd_rgb; //RGB
wire [10:0]lcd_xpos; //lcd horizontal coordinate
wire [10:0]lcd_ypos; //lcd vertical coordinate
my_vga VGA
(
.CLK(CLK_25M),
.RSTn(RSTn),
//用户接口
.lcd_rgb(lcd_rgb), //RGB
.lcd_xpos(lcd_xpos), //lcd horizontal coordinate
.lcd_ypos(lcd_ypos), //lcd vertical coordinate
//硬件接口
.lcd_hs(lcd_hs), //lcd horizontal sync
.lcd_vs(lcd_vs), //lcd vertical sync
.lcd_blank(/*空*/), //lcd blank(L:blank)
.lcd_red(lcd_red), //lcd red data
.lcd_green(lcd_green), //lcd green data
.lcd_blue(lcd_blue) //lcd blue data
);
always@(posedge CLK or negedge RSTn)
begin
if(!RSTn)
lcd_rgb <= 12'h0;
else
begin
if (lcd_xpos >= 0 && lcd_xpos < (`H_DISP/8)*1)
lcd_rgb <= `RED;
else if(lcd_xpos >= (`H_DISP/8)*1 && lcd_xpos < (`H_DISP/8)*2)
lcd_rgb <= `GREEN;
else if(lcd_xpos >= (`H_DISP/8)*2 && lcd_xpos < (`H_DISP/8)*3)
lcd_rgb <= `BLUE;
else if(lcd_xpos >= (`H_DISP/8)*3 && lcd_xpos < (`H_DISP/8)*4)
lcd_rgb <= `WHITE;
else if(lcd_xpos >= (`H_DISP/8)*4 && lcd_xpos < (`H_DISP/8)*5)
lcd_rgb <= `BLACK;
else if(lcd_xpos >= (`H_DISP/8)*5 && lcd_xpos < (`H_DISP/8)*6)
lcd_rgb <= `YELLOW;
else if(lcd_xpos >= (`H_DISP/8)*6 && lcd_xpos < (`H_DISP/8)*7)
lcd_rgb <= `CYAN;
else
lcd_rgb <= `ROYAL;
end
end
endmodule
`timescale 1ns / 1pssync_module.v VGA驱动时序://////////////////////////////////////////////////////////////////////////////////
// Company: 米联
// Engineer: 宋桓公
//
// Create Date: 2016/01/09 10:08:16
// Design Name:
// Module Name: my_vga
// Project Name:
// Target Devices:
// Tool Versions:
// Description:
//
// Dependencies:
//
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
//
//////////////////////////////////////////////////////////////////////////////////
module my_vga(
input CLK,
input RSTn,
//用户接口
input [11:0]lcd_rgb, //RGB444
output [10:0]lcd_xpos, //lcd horizontal coordinate
output [10:0]lcd_ypos, //lcd vertical coordinate
//硬件接口
output lcd_hs, //lcd horizontal sync
output lcd_vs, //lcd vertical sync
output lcd_blank, //lcd blank(L:blank)
output[3:0] lcd_red, //lcd red data
output[3:0] lcd_green, //lcd green data
output[3:0] lcd_blue //lcd blue data
);
wire Ready_Sig;
sync_module sync
(
.CLK(CLK),
.RSTn(RSTn),
.Ready_Sig(Ready_Sig),
.lcd_xpos(lcd_xpos),
.lcd_ypos(lcd_ypos),
.lcd_vs(lcd_vs),
.lcd_hs(lcd_hs),
.lcd_blank(lcd_blank)
);
assign {lcd_red, lcd_green ,lcd_blue} = Ready_Sig ? lcd_rgb : 12'd0;
endmodule
`timescale 1ns / 1ps//////////////////////////////////////////////////////////////////////////////////
// Company: 米联
// Engineer: 宋桓公
//
// Create Date: 2016/01/09 11:15:59
// Design Name:
// Module Name: sync_module
// Project Name:
// Target Devices:
// Tool Versions:
// Description:
//
// Dependencies:
//
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
//
//////////////////////////////////////////////////////////////////////////////////
module sync_module(
input CLK,
input RSTn,
output [10:0]lcd_xpos,
output [10:0]lcd_ypos,
output Ready_Sig,
output lcd_hs,
output lcd_vs,
output lcd_blank
);
`include "lcd_para.v"
reg [10:0]Count_H;
reg [10:0]Count_V;
reg rH,rV;
always @(posedge CLK or negedge RSTn)
if(!RSTn)
begin
Count_H <= 11'd0;
Count_V <= 11'd0;
rH <= 1'b1;
rV <= 1'b1;
end
else
begin
if(Count_H == `H_TOTAL) rH <= 1'b0;
else if(Count_H == `H_SYNC) rH <= 1'b1;
else if(Count_H == 0) rH <= 1'b0;
if(Count_V == `V_TOTAL) rV <= 1'b0;
else if(Count_V == `V_SYNC) rV <= 1'b1;
else if(Count_V == 0) rV <= 1'b0;
if(Count_V == `V_TOTAL) Count_V <= 11'd1;
else if(Count_H == `H_TOTAL) Count_V <= Count_V + 1'b1;
if(Count_H == `H_TOTAL) Count_H <= 11'd1;
else Count_H <= Count_H + 1'b1;
end
wire h_sig = ( Count_H <= `H_SYNC - 11'd1) ? 1'b0 : 1'b1;
wire v_sig = ( Count_V <= `V_SYNC - 11'd1) ? 1'b0 : 1'b1;
//lcd_blank为高时,发出的颜色信号才有效!!
assign lcd_blank = h_sig & v_sig;
assign lcd_hs = rH;
assign lcd_vs = rV;
//--------------------------------------
reg V_Ready;
always @(posedge CLK or negedge RSTn)
if(!RSTn)
V_Ready <= 1'b0;
else if( (Count_V >= `V_Start) &&
(Count_V <= `V_END) )
V_Ready <= 1'b1;
else
V_Ready <= 1'b0;
reg H_Ready;
always @(posedge CLK or negedge RSTn)
if(!RSTn)
H_Ready <= 1'b0;
else if( (Count_H >= (`H_Start - 11'd1)) &&
(Count_H < (`H_END - 11'd1)) ) //后面一个判断不能要等于号
H_Ready <= 1'b1;
else
H_Ready <= 1'b0;
assign Ready_Sig = V_Ready & H_Ready;
//-------------------------------------------------------
reg [10:0]xpos;
always @(posedge CLK or negedge RSTn)
if(!RSTn)
begin
xpos <= 11'd0;
end
else if(Ready_Sig)
begin
xpos <= xpos + 1'b1;
end
else
begin
xpos <= 11'd0;
end
assign lcd_xpos = xpos;
reg [10:0]ypos;
always @(posedge CLK or negedge RSTn)
if(!RSTn)
begin
ypos <= 11'd0;
end
else if(xpos == `H_DISP)
begin
if(ypos == `V_DISP) ypos <= 11'd0;
else ypos <= ypos + 1'b1;
end
assign lcd_ypos = ypos;
endmodule
最后是lcd_para.v,起到头文件的作用:
//define colors RGB--4|4|4`define RED 12'h800 /*1111,0000,0000 */
`define GREEN 12'hF0 /*0000,1111,0000 */
`define BLUE 12'h00F /*0000,0000,1111 */
`define WHITE 12'hFFF /*1111,1111,1111 */
`define BLACK 12'h000 /*0000,0000,0000 */
`define YELLOW 12'hFF0 /*1111,1111,0000 */
`define CYAN 12'hF0F /*1111,0000,1111 */
`define ROYAL 12'h0FF /*0000,1111,1111 */
//---------------------------------
`define SYNC_POLARITY 1'b0
//------------------------------------
//vga parameter define
`define VGA_640_480_60FPS_25MHz
//`define VGA_800_600_72FPS_50MHz
//`define VGA_1024_768_60FPS_65MHz
//`define VGA_1280_1024_60FPS_105MHz
//`define VGA_800_600_60MHz
//---------------------------------
// 640 * 480
`ifdef VGA_640_480_60FPS_25MHz
`define H_FRONT 11'd16
`define H_SYNC 11'd96
`define H_BACK 11'd48
`define H_DISP 11'd640
`define H_TOTAL 11'd800
`define V_FRONT 11'd10
`define V_SYNC 11'd2
`define V_BACK 11'd33
`define V_DISP 11'd480
`define V_TOTAL 11'd525
`endif
//---------------------------------
// 800 * 600
`ifdef VGA_800_600_72FPS_50MHz
`define H_FRONT 11'd56
`define H_SYNC 11'd120
`define H_BACK 11'd64
`define H_DISP 11'd800
`define H_TOTAL 11'd1040
`define V_FRONT 11'd37
`define V_SYNC 11'd6
`define V_BACK 11'd23
`define V_DISP 11'd600
`define V_TOTAL 11'd666
`endif
//---------------------------------
// 1024 * 768
`ifdef VGA_1024_768_60FPS_65MHz
`define H_FRONT 11'd24
`define H_SYNC 11'd136
`define H_BACK 11'd160
`define H_DISP 11'd1024
`define H_TOTAL 11'd1344
`define V_FRONT 11'd3
`define V_SYNC 11'd6
`define V_BACK 11'd29
`define V_DISP 11'd768
`define V_TOTAL 11'd806
`endif
//---------------------------------
// 1280 * 1024
`ifdef VGA_1280_1024_60FPS_105MHz
`define H_FRONT 11'd48
`define H_SYNC 11'd112
`define H_BACK 11'd248
`define H_DISP 11'd1280
`define H_TOTAL 11'd1688
`define V_FRONT 11'd1
`define V_SYNC 11'd3
`define V_BACK 11'd38
`define V_DISP 11'd1024
`define V_TOTAL 11'd1066
`endif
// 800 * 600
`ifdef VGA_800_600_60MHz
`define H_SYNC 11'd128
`define H_BACK 11'd88
`define H_DISP 11'd800
`define H_FRONT 11'd40
`define H_TOTAL 11'd1056
`define V_SYNC 11'd4
`define V_BACK 11'd23
`define V_DISP 11'd600
`define V_FRONT 11'd1
`define V_TOTAL 11'd628
`endif
//--song
`define H_Start (`H_SYNC + `H_BACK)
`define H_END (`H_SYNC + `H_BACK + `H_DISP)
`define V_Start (`V_SYNC + `V_BACK)
`define V_END (`V_SYNC + `V_BACK + `V_DISP)
最后奉上,引脚约束文件:
set_property PACKAGE_PIN Y9 [get_ports {CLK}]
set_property IOSTANDARD LVCMOS33 [get_ports {CLK}]
set_property PACKAGE_PIN T18 [get_ports {RSTn}]
set_property IOSTANDARD LVCMOS18 [get_ports {RSTn}]
# HSync Horizontal sync AA19
set_property PACKAGE_PIN AA19 [get_ports {lcd_hs}]
set_property IOSTANDARD LVCMOS33 [get_ports {lcd_hs}]
# VSync Vertical sync Y19
set_property PACKAGE_PIN Y19 [get_ports {lcd_vs}]
set_property IOSTANDARD LVCMOS33 [get_ports {lcd_vs}]
# V20, U20, V19, V18
set_property PACKAGE_PIN V20 [get_ports {lcd_red[0]}]
set_property IOSTANDARD LVCMOS33 [get_ports {lcd_red[0]}]
set_property PACKAGE_PIN U20 [get_ports {lcd_red[1]}]
set_property IOSTANDARD LVCMOS33 [get_ports {lcd_red[1]}]
set_property PACKAGE_PIN V19 [get_ports {lcd_red[2]}]
set_property IOSTANDARD LVCMOS33 [get_ports {lcd_red[2]}]
set_property PACKAGE_PIN V18 [get_ports {lcd_red[3]}]
set_property IOSTANDARD LVCMOS33 [get_ports {lcd_red[3]}]
# AB22, AA22, AB21, AA21
set_property PACKAGE_PIN AB22 [get_ports {lcd_green[0]}]
set_property IOSTANDARD LVCMOS33 [get_ports {lcd_green[0]}]
set_property PACKAGE_PIN AA22 [get_ports {lcd_green[1]}]
set_property IOSTANDARD LVCMOS33 [get_ports {lcd_green[1]}]
set_property PACKAGE_PIN AB21 [get_ports {lcd_green[2]}]
set_property IOSTANDARD LVCMOS33 [get_ports {lcd_green[2]}]
set_property PACKAGE_PIN AA21 [get_ports {lcd_green[3]}]
set_property IOSTANDARD LVCMOS33 [get_ports {lcd_green[3]}]
# Y21, Y20, AB20, AB19
set_property PACKAGE_PIN Y21 [get_ports {lcd_blue[0]}]
set_property IOSTANDARD LVCMOS33 [get_ports {lcd_blue[0]}]
set_property PACKAGE_PIN Y20 [get_ports {lcd_blue[1]}]
set_property IOSTANDARD LVCMOS33 [get_ports {lcd_blue[1]}]
set_property PACKAGE_PIN AB20 [get_ports {lcd_blue[2]}]
set_property IOSTANDARD LVCMOS33 [get_ports {lcd_blue[2]}]
set_property PACKAGE_PIN AB19 [get_ports {lcd_blue[3]}]
set_property IOSTANDARD LVCMOS33 [get_ports {lcd_blue[3]}]
我们最终选择的是VGA_640_480_60FPS_25MHz。640_480决定是画面的大小,25MHz是我VGA扫描的频率。因为我VGA驱动模块的时钟输入是25M,所以这里只能选择VGA_640_480_60FPS_25MHz。640_480画面的大小其实是无所谓的,应为现在的VGA都有自动缩放的功能,所以都是满屏显示。