原创 VGA练习之图像的动态显示(后续)

2015-12-14 13:47 1557 13 13 分类: FPGA/CPLD

 

代码部分:

/*

分辨率:600*480

功能:每隔3s变化一次,a~e循环变换

a.全红

b.全白

c.2*2  4部分,分别是:红,蓝,绿,黄

d.3*3  9部分,分别是:红黄蓝绿黑紫白青粉

e.150*100矩形边框,边缘是20宽度的绿色框,其他为白色

*/

module VGA_test4(clk,rst_n,lcd_data,lcd_hs,lcd_vs);

 

input clk;   //25mhz

input rst_n;

output lcd_vs;//场同步信号

output lcd_hs;//行同步信号

output [7:0]lcd_data;

 

parameter LCD_HS_CNT = 799,

             LCD_VS_CNT = 524;

 

reg [9:0] lcd_hs_cnt;//行同步计数

always@(posedge clk or negedge rst_n)

    if(!rst_n)

        lcd_hs_cnt <= 10'd0;

    else if(lcd_hs_cnt==LCD_HS_CNT)

        lcd_hs_cnt <= 10'd0;

    else

        lcd_hs_cnt <= lcd_hs_cnt + 1'b1;

       

reg lcd_hs;    

always@(posedge clk or negedge rst_n)

    if(!rst_n)

        lcd_hs <= 1'b0;

    else if(lcd_hs_cnt<96)

        lcd_hs <= 1'b0;

    else

        lcd_hs <= 1'b1;

       

reg [9:0] lcd_vs_cnt;//场同步计数 

always@(posedge clk or negedge rst_n)

    if(!rst_n)

        lcd_vs_cnt <= 10'd0;

    else if(lcd_hs_cnt==LCD_HS_CNT && lcd_vs_cnt==LCD_VS_CNT)  

        lcd_vs_cnt <= 10'd0;

    else if(lcd_hs_cnt==LCD_HS_CNT)

        lcd_vs_cnt <= lcd_vs_cnt + 1'b1;

    else if(lcd_vs_cnt==LCD_VS_CNT)

        lcd_vs_cnt <= lcd_vs_cnt;

   

reg lcd_vs;        

always@(posedge clk or negedge rst_n)

    if(!rst_n)

        lcd_vs <= 1'b0;

    else if(lcd_vs_cnt<2)

        lcd_vs <= 1'b0;

    else

        lcd_vs <= 1'b1;

 

reg [24:0]time_1s;          //1s计数器

always@(posedge clk or negedge rst_n)

    if(!rst_n)

        time_1s <= 25'd0;

    else if(time_1s == 24_999_999)

        time_1s <= 25'd0;

    else

        time_1s <= time_1s + 1'b1;

       

reg [3:0]time_1s_cnt;       //计数1s的个数

always @(posedge clk or negedge rst_n)

    if(!rst_n)

        time_1s_cnt <= 4'd0;

    else if(time_1s_cnt==14)

        time_1s_cnt <= 4'd0;

    else if(time_1s==24_999_999)

        time_1s_cnt <= time_1s_cnt + 1'b1;

    else

        time_1s_cnt <= time_1s_cnt;    

       

reg [9:0]valid_area;    //画出矩形,长为:787-141=646,宽为:516-32=484

always@(*)begin

    valid_area = ((lcd_hs_cnt >=141) && (lcd_hs_cnt <= 787) && (lcd_vs_cnt >= 32) && (lcd_vs_cnt <= 516));

end

 

//c:2*2 4部分,将646*484分成4部分

reg [9:0]valid_area_c1; //画出矩形,长为:464-141=323,宽为:274-32=242

always@(*)begin

    valid_area_c1 = ((lcd_hs_cnt >=141) && (lcd_hs_cnt <= 464) && (lcd_vs_cnt >= 32) && (lcd_vs_cnt <= 274));

end

 

reg [9:0]valid_area_c2; //画出矩形,长为:787-464=323,宽为:274-32=242

always@(*)begin

    valid_area_c2 = ((lcd_hs_cnt >=464) && (lcd_hs_cnt <= 787) && (lcd_vs_cnt >= 32) && (lcd_vs_cnt <= 274));

end

 

reg [9:0]valid_area_c3; //画出矩形,长为:464-141=323,宽为:516-274=242

always@(*)begin

    valid_area_c3 = ((lcd_hs_cnt >=141) && (lcd_hs_cnt <= 464) && (lcd_vs_cnt >= 274) && (lcd_vs_cnt <= 516));

end

   

//d:3*3 9部分,将646*484分成9部分

reg [9:0]valid_area_d1; //画出矩形,长为:356-141=215,宽为:193-32=161

always@(*)begin

    valid_area_d1 = ((lcd_hs_cnt >=141) && (lcd_hs_cnt <= 356) && (lcd_vs_cnt >= 32) && (lcd_vs_cnt <= 193));

end

 

reg [9:0]valid_area_d2; //画出矩形,长为:571-356=215,宽为:354-193=161

always@(*)begin

    valid_area_d2 = ((lcd_hs_cnt >=356) && (lcd_hs_cnt <= 571) && (lcd_vs_cnt >= 32) && (lcd_vs_cnt <= 193));

end

 

reg [9:0]valid_area_d3; //画出矩形,长为:787-571=216,宽为:516-354=162

always@(*)begin

    valid_area_d3 = ((lcd_hs_cnt >=571) && (lcd_hs_cnt <= 787) && (lcd_vs_cnt >= 32) && (lcd_vs_cnt <= 193));

end

 

reg [9:0]valid_area_d4; //画出矩形,长为:356-141=215,宽为:193-32=161

always@(*)begin

    valid_area_d4 = ((lcd_hs_cnt >=141) && (lcd_hs_cnt <= 356) && (lcd_vs_cnt >= 193) && (lcd_vs_cnt <= 354));

end

 

reg [9:0]valid_area_d5; //画出矩形,长为:571-356=215,宽为:354-193=161

always@(*)begin

    valid_area_d5 = ((lcd_hs_cnt >=356) && (lcd_hs_cnt <= 571) && (lcd_vs_cnt >= 193) && (lcd_vs_cnt <= 354));

end

 

reg [9:0]valid_area_d6; //画出矩形,长为:787-571=216,宽为:516-354=162

always@(*)begin

    valid_area_d6 = ((lcd_hs_cnt >=571) && (lcd_hs_cnt <= 787) && (lcd_vs_cnt >= 193) && (lcd_vs_cnt <= 354));

end

 

reg [9:0]valid_area_d7; //画出矩形,长为:356-141=215,宽为:193-32=161

always@(*)begin

    valid_area_d7 = ((lcd_hs_cnt >=141) && (lcd_hs_cnt <= 356) && (lcd_vs_cnt >= 354) && (lcd_vs_cnt <= 516));

end

 

reg [9:0]valid_area_d8; //画出矩形,长为:571-356=215,宽为:354-193=161

always@(*)begin

    valid_area_d8 = ((lcd_hs_cnt >=356) && (lcd_hs_cnt <= 571) && (lcd_vs_cnt >= 354) && (lcd_vs_cnt <= 516));

end

 

//e:150*100边框

reg [9:0]valid_area_e;

always@(*)begin

    valid_area_e = ((lcd_hs_cnt >=389) && (lcd_hs_cnt <= 539) && (lcd_vs_cnt >= 224) && (lcd_vs_cnt <= 324));

end

 

reg [9:0]valid_area_e1;

always@(*)begin

    valid_area_e1 = (((lcd_hs_cnt >= 389) && (lcd_hs_cnt <= 409)) |

                         ((lcd_hs_cnt >= 519) && (lcd_hs_cnt <= 539)) |

                          ((lcd_vs_cnt >= 224) && (lcd_vs_cnt <= 244)) |

                         ((lcd_vs_cnt >= 304) && (lcd_vs_cnt <= 324)));

end

 

reg [7:0]lcd_data;

always@(posedge clk or negedge rst_n)

    if(!rst_n)

        lcd_data <= 8'h0;

    else if(valid_area)begin

        if(time_1s_cnt>=0 && time_1s_cnt<=2)       //a

            lcd_data <= 8'b111_000_00;   //全红

        else if(time_1s_cnt>=3 && time_1s_cnt<=5)  //b

            lcd_data <= 8'b111_111_11;  //全白

        else if(time_1s_cnt>=6 && time_1s_cnt<=8)  //c

            if(valid_area_c1)

                lcd_data <=8'b111_000_00;  //红

            else if(valid_area_c2)  

                lcd_data <=8'b000_000_11;  //蓝

            else if(valid_area_c3)

                lcd_data <=8'b000_111_00;  //绿

            else   

                lcd_data <=8'b111_111_00;  //黄

        else if(time_1s_cnt>=9 && time_1s_cnt<=11)  //d

            if(valid_area_d1)

                lcd_data <=8'b111_000_00;//红

            else if(valid_area_d2)

                lcd_data <=8'b111_111_00;//黄

            else if(valid_area_d3)

                lcd_data <=8'b000_000_11;//蓝

            else if(valid_area_d4)

                lcd_data <=8'b000_111_00;//绿

            else if(valid_area_d5)

                lcd_data <=8'b000_000_00;//黑

            else if(valid_area_d6)

                lcd_data <=8'b111_000_11;//紫

            else if(valid_area_d7)

                lcd_data <=8'b111_111_11;//白

            else if(valid_area_d8)

                lcd_data <=8'b000_111_11;//青

            else

                lcd_data <=8'b000_111_10;//粉(不知)

        else if(time_1s_cnt>=12 && time_1s_cnt<=14)  //e

            if(valid_area_e)

                if(valid_area_e1)

                    lcd_data <=8'b000_111_00;//绿

                else

                    lcd_data <=8'b111_111_11;//白

            else

                lcd_data <=8'b111_111_11;//白         

    end

    else

        lcd_data <= 8'h0;               //black

 

endmodule

 

ps:行边沿从141开始,场边沿从32开始,使得图像在显示屏的中心显示。

 

在电脑显示屏上的显示结果如下:

222.jpg3333.jpg444.jpg5555.jpg

文章评论0条评论)

登录后参与讨论
我要评论
0
13
关闭 站长推荐上一条 /2 下一条