刚刚接触FPGA,买了一块开发板,开始熟悉Quartus软件的使用,了解硬件的资源,将例程代码烧入芯片熟悉,自己编了个1602显示的程序,加深理解。
always @(posedge sys_clk_lcd or negedge sys_rst_n)
begin
if(!sys_rst_n)
begin
state <= Clear_Lcd; //复位:清屏并光标复位
rs <= 1'b0; //复位:RS=0时为写指令;
lcddate <= 8'b0; //复位:使DB8总线输出全0
LCD_EN_Sel <= 1'b1; //复位:开启夜晶使能信号
disp_count <= 5'b0;
end
else
case(state) //初始化LCD模块
Clear_Lcd:
begin
state <= Set_Disp_Mode;
lcddate <= 8'b00000001; //清屏并光标复位
end
Set_Disp_Mode:
begin
state <= Disp_On;
lcddate <= 8'b00111000; //设置显示模式:8位2行5x8点阵
end
Disp_On:
begin
state <= Shift_Down;
lcddate <= 8'b00001100; //显示器开、光标不显示、光标不允许闪烁
end
Shift_Down:
begin
state <= Write_Addr;
lcddate <= 8'b00000110; //文字不动,光标自动右移
end
Write_Addr:
begin
state <= Write_Data_First;
lcddate <= 8'b10000001; //写入第一行显示起始地址:第一行第二个位置
Data_First_Buf <= Data_First; //将第一行显示的数据赋给Data_First_Buf?
end
Write_Data_First: //写第一行数据
begin
if(disp_count == 15) //disp_count等于14时表示第一行数据已写完
begin
lcddate <= 8'b11000001; //送入写第二行的指令
rs <= 1'b0;
disp_count <= 5'b0;
Data_Second_Buf <= Data_Second;
state <= Write_Data_Second; //写完第一行进入写第二行状态
end
else
begin
lcddate <= Data_First_Buf[119:112];
Data_First_Buf <= (Data_First_Buf << 8);
rs <= 1'b1; //RS=1表示写数据
disp_count <= disp_count + 1'b1;
state <= Write_Data_First;
end
end
Write_Data_Second: //写第二行数据
begin
if(disp_count == 14)
begin
LCD_EN_Sel <= 1'b0;
rs <= 1'b0;
disp_count <= 5'b0;
state <= Idel; //写完进入空闲状态
end
else
begin
lcddate <= Data_Second_Buf[111:104];
Data_Second_Buf <= (Data_Second_Buf << 8);
rs <= 1'b1;
disp_count <= disp_count + 1'b1;
state <= Write_Data_Second;
end
end
Idel:
begin
state <= Idel; //在Idel状态循环
end
default: state <= Clear_Lcd; //若state为其他值,则将state置为Clear_Lcd
endcase
end
endmodule
主程序采用状态机形式编写,对1602进行初始化,将内容进行显示。
实验现象
nneverli_217963090 2014-9-11 23:37