今天首先了解到了昨天不太清楚的一些名词的含义以及使用方法,也明白了分辨率,帧率的含义
其次学习了在vga上显示256种颜色代码
代码思路上和上一篇文章挺像的,只是加之在有效区之上划分出来了256个小格子,并在各个小格子上利用不同的rgb值赋予不同的颜色,有区别的是,上一篇文章中的rgb三个输出端口都是一位的,而这次分别是三位,三位,二位。
令我不太理解的一点是,上一篇文章的制作是以800*600的分辨率制作的,而这次的分辨率是640*480,各个数据都会有所变化,而现在就我的电脑而言,有许多种的分辨率选择,所以接下来我要着手尝试一下上述功能,附代码:
module vga_2(
input clk_25m,
input rst_n,
output hsync,
output vsync,
output [2:0]vga_r,// 8位rgb来配置256色
output [2:0]vga_g,
output [1:0]vga_b
);
reg [9:0] x_cnt;
reg [9:0] y_cnt;
always@(posedge clk_25m or negedge rst_n)
begin
if(!rst_n)
x_cnt <= 10'd0;
else if(x_cnt == 10'd799)
x_cnt <= 10'd0;
else
x_cnt <= x_cnt + 1'b1;
end
always@(posedge clk_25m or negedge rst_n)
begin
if(!rst_n)
y_cnt <= 10'd0;
else if(y_cnt == 10'd524)
y_cnt <= 10'd0;
else if(x_cnt == 10'd799)
y_cnt <= y_cnt + 1'b1;
else
y_cnt <= y_cnt;
end
reg hsync_r,vsync_r;
always@(posedge clk_25m or negedge rst_n)
begin
if(!rst_n)
hsync_r <= 1'b1;
else if(x_cnt == 10'd0)
hsync_r <= 1'b0;
else if(x_cnt == 10'd96)
hsync_r <= 1'b1;
else
hsync_r <= hsync_r;
end
always@(posedge clk_25m or negedge rst_n)
begin
if(!rst_n)
vsync_r <= 1'b1;
else if(y_cnt == 10'd0)
vsync_r <= 1'b0;
else if(y_cnt == 10'd2)
vsync_r <= 1'b1;
else
vsync_r <= vsync_r;
end
assign hsync = hsync_r;
assign vsync = vsync_r;
reg valid_yr;
always@(posedge clk_25m or negedge rst_n)
begin
if(!rst_n)
valid_yr <= 1'b0;
else if(y_cnt == 10'd32)
valid_yr <= 1'b1;
else if(y_cnt == 10'd512)
valid_yr <= 1'b0;
else
valid_yr <= valid_yr;
end
wire valid_y = valid_yr;
reg valid_r;
always@(posedge clk_25m or negedge rst_n)
begin
if(!rst_n)
valid_r <= 1'b0;
else if( (x_cnt == 10'd141) && valid_y )
valid_r <= 1'b1;
else if( (x_cnt == 10'd781) && valid_y )
valid_r <= 1'b0;
else
valid_r <= valid_r;
end
wire valid = valid_r;
wire [9:0] x_dis; // 横坐标显示有效区域相对坐标值0-639
wire [9:0] y_dis; // 数坐标显示有效区域相对坐标值0-479
assign x_dis = x_cnt - 10'd142;
assign y_dis = y_cnt - 10'd33;
reg [7:0] vga_rgb; // vga色彩显示寄存器
always@(posedge clk_25m )
begin
if(!valid)
vga_rgb <= 8'd0;
else
begin
case(x_dis)
10'd0:
begin
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
vga_rgb <= 8'd240;
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:
vga_rgb <= vga_rgb + 1'b1;
default: ;
endcase
end
end
assign vga_r = vga_rgb[7:5];
assign vga_g = vga_rgb[4:2];
assign vga_b = vga_rgb[1:0];
endmodule