module VGA(
clk,rst_n, //系统控制
hsync,vsync,
vga_r,vga_g,vga_b //VGA控制
);
input clk; //50M
input rst_n; //低电平复位
//FPGA与VGA接口信号
output hsync; //行同步信号
output vsync; //场同步信号
output[2:0] vga_r;
output[2:0] vga_g;
output[1:0] vga_b;
//坐标计数
reg[10:0] x_cnt; //行坐标
reg[9:0] y_cnt; //列坐标
always @(posedge clk or negedge rst_n)
if(!rst_n) x_cnt <= 10'd0;
else if(x_cnt == 10'd1039) x_cnt <= 10'd0;
else x_cnt <= x_cnt+1'b1;
always @(posedge clk or negedge rst_n)
if(!rst_n) y_cnt <= 10'd0;
else if(y_cnt == 10'd665) y_cnt <= 10'd0;
else if(x_cnt == 10'd1039) y_cnt <= y_cnt+1'b1;
//VGA场同步,行同步信号
reg hsync_r,vsync_r; //同步信号
always @(posedge clk or negedge rst_n)
if(!rst_n) hsync_r <= 1'b1;
else if(x_cnt == 10'd0) hsync_r <= 1'b0; //产生hsync信号
else if(x_cnt == 10'd120) hsync_r <= 1'b1;
always @(posedge clk or negedge rst_n)
if(!rst_n) vsync_r <= 1'b1;
else if(y_cnt == 10'd0) vsync_r <= 1'b0; //产生vsync信号
else if(y_cnt == 10'd6) vsync_r <= 1'b1;
assign hsync =hsync_r;
assign vsync = vsync_r;
//有效显示标志位产生
reg valid_yr; //行显示有效信号
always @(posedge clk or negedge rst_n)
if(!rst_n) valid_yr <= 1'b0;
else if(y_cnt == 10'd32) valid_yr <= 1'b1;
else if(y_cnt == 10'd632) valid_yr <= 1'b0;
wire valid_y=valid_yr;
reg valid_r;
always @(posedge clk or negedge rst_n)
if(!rst_n) valid_r <= 1'b0;
else if((x_cnt == 10'd187) && valid_y) valid_r <= 1'b1;
else if((x_cnt == 10'd987) && valid_y) valid_r <= 1'b0;
wire valid=valid_r;
wire[9:0] x_dis; //横坐标显示有效区域0-639
wire[9:0] y_dis; //纵坐标显示有效区域0-479
assign x_dis = x_cnt - 10'd187;
assign y_dis = y_cnt - 10'd33;
//VGA {MOD}彩信号产生
/*
RGB = 000 黑 {MOD} RGB = 100 红 {MOD}
= 001 蓝 {MOD} = 101 紫 {MOD}
= 010 绿 {MOD} = 110 黄 {MOD}
= 011 青 {MOD} = 111 白 {MOD}
*/
/*
一共640*480个像素点,需要显示256种颜 {MOD};
那么每个颜 {MOD}显示区域为40*30,就正好。
*/
reg[7:0] vga_rgb; //VGA {MOD}彩显示寄存器
always @(posedge clk)
if(!valid) vga_rgb <= 8'd0;
else begin
case(x_dis)
10'd0: begin //当x坐标画到0时,让显示 {MOD}彩数据根据当前的y坐标值重新
if(y_dis >= 10'd0 && y_dis < 10'd30) vga_rgb <= 8'd0;
else if(y_dis >= 10'd30 && y_dis < 10'd60) vga_rgb <= 8'd16;
else if(y_dis >= 10'd60 && y_dis < 10'd90) vga_rgb <= 8'd32;
else if(y_dis >= 10'd90 && y_dis < 10'd120) vga_rgb <= 8'd48;
else if(y_dis >= 10'd120 && y_dis < 10'd150) vga_rgb <= 8'd64;
else if(y_dis >= 10'd150 && y_dis < 10'd180) vga_rgb <= 8'd80;
else if(y_dis >= 10'd180 && y_dis < 10'd210) vga_rgb <= 8'd96;
else if(y_dis >= 10'd210 && y_dis < 10'd240) vga_rgb <= 8'd112;
else if(y_dis >= 10'd240 && y_dis < 10'd270) vga_rgb <= 8'd128;
else if(y_dis >= 10'd270 && y_dis < 10'd300) vga_rgb <= 8'd144;
else if(y_dis >= 10'd300 && y_dis < 10'd330) vga_rgb <= 8'd160;
else if(y_dis >= 10'd330 && y_dis < 10'd360) vga_rgb <= 8'd176;
else if(y_dis >= 10'd360 && y_dis < 10'd390) vga_rgb <= 8'd192;
else if(y_dis >= 10'd390 && y_dis < 10'd420) vga_rgb <= 8'd208;
else if(y_dis >= 10'd420 && y_dis < 10'd450) vga_rgb <= 8'd224;
else if(y_dis >= 10'd450 && y_dis < 10'd480) vga_rgb <= 8'd240;
else if(y_dis >= 10'd480 && y_dis < 10'd510) vga_rgb <= 8'd256;
else if(y_dis >= 10'd510 && y_dis < 10'd540) vga_rgb <= 8'd272;
else if(y_dis >= 10'd540 && y_dis < 10'd570) vga_rgb <= 8'd288;
else vga_rgb <= 8'd304;
end
10'd40,10'd80,10'd120,10'd160,10'd200,10'd240,10'd280,10'd320,10'd360,10'd400,10'd440,10'd480,
10'd520,10'd560,10'd600,10'd640,10'd680,10'd720,10'd760,10'd800: vga_rgb <= vga_rgb + 1'b1;
default:;
endcase
end
assign vga_r = vga_rgb[7:5];
assign vga_g = vga_rgb[4:2];
assign vga_b = vga_rgb[1:0];
endmodule
已上是我根据特权同学640*480的VGA历程改的,下进去程序,插上VGA
线,显示屏提示节电模式。怎么搞啊?哪里有错,谢谢!
此帖出自
小平头技术问答
没有预期的小方块。
程序如下:
module VGA(
clk,rst_n, //系统控制
hsync,vsync,
vga_r,vga_g,vga_b //VGA控制
);
input clk; //50M
input rst_n; //低电平复位
//FPGA与VGA接口信号
output hsync; //行同步信号
output vsync; //场同步信号
output[2:0] vga_r;
output[2:0] vga_g;
output[1:0] vga_b;
//坐标计数
reg[10:0] x_cnt; //行坐标
reg[9:0] y_cnt; //列坐标
always @(posedge clk or negedge rst_n)
if(!rst_n) x_cnt <= 11'd0;
else if(x_cnt == 11'd1039) x_cnt <= 11'd0;
else x_cnt <= x_cnt+1'b1;
always @(posedge clk or negedge rst_n)
if(!rst_n) y_cnt <= 10'd0;
else if(y_cnt == 10'd665) y_cnt <= 10'd0;
else if(x_cnt == 11'd1039) y_cnt <= y_cnt+1'b1;
//VGA场同步,行同步信号
reg hsync_r,vsync_r; //同步信号
always @(posedge clk or negedge rst_n)
if(!rst_n) hsync_r <= 1'b1;
else if(x_cnt == 11'd0) hsync_r <= 1'b0; //产生hsync信号
else if(x_cnt == 11'd120) hsync_r <= 1'b1;
always @(posedge clk or negedge rst_n)
if(!rst_n) vsync_r <= 1'b1;
else if(y_cnt == 10'd0) vsync_r <= 1'b0; //产生vsync信号
else if(y_cnt == 10'd6) vsync_r <= 1'b1;
assign hsync =hsync_r;
assign vsync = vsync_r;
//有效显示标志位产生
reg valid_yr; //行显示有效信号
always @(posedge clk or negedge rst_n)
if(!rst_n) valid_yr <= 1'b0;
else if(y_cnt == 10'd32) valid_yr <= 1'b1;
else if(y_cnt == 10'd632) valid_yr <= 1'b0;
wire valid_y=valid_yr;
reg valid_r;
always @(posedge clk or negedge rst_n)
if(!rst_n) valid_r <= 1'b0;
else if((x_cnt == 11'd187) && valid_y) valid_r <= 1'b1;
else if((x_cnt == 11'd987) && valid_y) valid_r <= 1'b0;
wire valid=valid_r;
wire[10:0] x_dis; //横坐标显示有效区域0-639
wire[9:0] y_dis; //纵坐标显示有效区域0-479
assign x_dis = x_cnt - 11'd187;
assign y_dis = y_cnt - 10'd33;
//VGA {MOD}彩信号产生
/*
RGB = 000 黑 {MOD} RGB = 100 红 {MOD}
= 001 蓝 {MOD} = 101 紫 {MOD}
= 010 绿 {MOD} = 110 黄 {MOD}
= 011 青 {MOD} = 111 白 {MOD}
*/
/*
一共640*480个像素点,需要显示256种颜 {MOD};
那么每个颜 {MOD}显示区域为40*30,就正好。
*/
reg[7:0] vga_rgb; //VGA {MOD}彩显示寄存器
always @(posedge clk)
if(!valid) vga_rgb <= 8'd0;
else begin
case(x_dis)
11'd0: begin //当x坐标画到0时,让显示 {MOD}彩数据根据当前的y坐标值重新
if(y_dis >= 10'd0 && y_dis < 10'd30) vga_rgb <= 8'd0;
else if(y_dis >= 10'd30 && y_dis < 10'd60) vga_rgb <= 8'd16;
else if(y_dis >= 10'd60 && y_dis < 10'd90) vga_rgb <= 8'd32;
else if(y_dis >= 10'd90 && y_dis < 10'd120) vga_rgb <= 8'd48;
else if(y_dis >= 10'd120 && y_dis < 10'd150) vga_rgb <= 8'd64;
else if(y_dis >= 10'd150 && y_dis < 10'd180) vga_rgb <= 8'd80;
else if(y_dis >= 10'd180 && y_dis < 10'd210) vga_rgb <= 8'd96;
else if(y_dis >= 10'd210 && y_dis < 10'd240) vga_rgb <= 8'd112;
else if(y_dis >= 10'd240 && y_dis < 10'd270) vga_rgb <= 8'd128;
else if(y_dis >= 10'd270 && y_dis < 10'd300) vga_rgb <= 8'd144;
else if(y_dis >= 10'd300 && y_dis < 10'd330) vga_rgb <= 8'd160;
else if(y_dis >= 10'd330 && y_dis < 10'd360) vga_rgb <= 8'd176;
else if(y_dis >= 10'd360 && y_dis < 10'd390) vga_rgb <= 8'd192;
else if(y_dis >= 10'd390 && y_dis < 10'd420) vga_rgb <= 8'd208;
else if(y_dis >= 10'd420 && y_dis < 10'd450) vga_rgb <= 8'd224;
else if(y_dis >= 10'd450 && y_dis < 10'd480) vga_rgb <= 8'd240;
else if(y_dis >= 10'd480 && y_dis < 10'd510) vga_rgb <= 8'd240;
else if(y_dis >= 10'd510 && y_dis < 10'd540) vga_rgb <= 8'd240;
else if(y_dis >= 10'd540 && y_dis < 10'd570) vga_rgb <= 8'd240;
else vga_rgb <= 8'd240;
end
11'd40,11'd80,11'd120,11'd160,11'd200,11'd240,11'd280,11'd320,11'd360,11'd400,11'd440,11'd480,
11'd520,11'd560,11'd600,11'd640,11'd680,11'd720,11'd760: vga_rgb <= vga_rgb + 1'b1;
default:;
endcase
end
assign vga_r = vga_rgb[7:5];
assign vga_g = vga_rgb[4:2];
assign vga_b = vga_rgb[1:0];
endmodule
一周热门 更多>