always @(posedge CLK_LCD or negedge RST_N) //只有在写数据操作时,RS信号才为高电平,其余为低电平
begin
if(!RST_N)
LCD_RS <= 1'b0;
else if(state == WRITERAM)
LCD_RS <= 1'b1;
else
LCD_RS <= 1'b0;
end
// State Machine
always @(posedge CLK_LCD or negedge RST_N)
begin
if(!RST_N)
begin
state <= IDLE;
LCD_D <= 8'bzzzzzzzz;
char_cnt <= 5'b0;
flag <= 1'b1;
end
else begin
case(state)
IDLE:
begin
state <= SETFUNCTION;
LCD_D <= 8'bzzzzzzzz;
end
SETFUNCTION:
begin
state <= SETFUNCTION2;
LCD_D <= 8'h30; // 8-bit 控制界面,基本指令集动作
end
SETFUNCTION2:
begin
state <= SWITCHMODE;
LCD_D <= 8'h30; // 清屏
end
SWITCHMODE:
begin
state <= CLEAR;
LCD_D <= 8'h0c; // 显示开关:开显示,光标和闪烁关闭
end
CLEAR:
begin
state <= SETMODE;
LCD_D <= 8'h01;
end
SETMODE:
begin
state <= SETDDRAM;
LCD_D <= 8'h06; // 输入方式设置: 数据读写后,地址自动加1,画面不动
end
SETDDRAM:
begin
state <= WRITERAM;
if(char_cnt == 0) //如果显示的是第一个字符,则设置第一行的首字符地址
begin
LCD_D <= 8'h80; //Line1
end
else //第二次设置时,是设置第二行的首字符地址
begin
LCD_D <= 8'h90; //Line2
end
end
WRITERAM:
begin
if(char_cnt <= 11)
begin
char_cnt <= char_cnt + 1'b1;
LCD_D <= data_disp;
if( char_cnt == 11 )
state <= SETDDRAM;
else
state <= WRITERAM;
end
else if( char_cnt >= 12 && char_cnt <= 25)
begin
LCD_D <= data_disp;
if(char_cnt == 25)
begin
state <= STOP;
char_cnt <= 5'b0;
flag <= 1'b0;
end
else
begin
state <= WRITERAM;
char_cnt <= char_cnt + 1'b1;
end
end
end
STOP: state <= STOP;
default: state <= IDLE;
endcase
end
end