交一下1602课程的作业
这是实验现象:http://v.youku.com/v_show/id_XNzMwOTcyNDQw.html
实验中为了节省时间,将1S分频系数改小了,相当于视频中1602显示的一秒等于实际
一秒的千分之一。
/******************************************************************************
*Engineer: superdian
*Create Date: 2014/6/24
*Design Name: LCD1602Clock
*Module Name: LCD1602Clock
*Project Name: LCD1602Clock
*Target Devices: EP3CE5E144C8
*Tool versions:
*Description: LCD1602时钟
******************************************************************************/
module LCD1602(sys_clk,sys_rstn,lcd_rs,lcd_rw,lcd_en,lcd_data,shicounter);
//输入输出信号定义
input sys_clk;//系统时钟输入
input sys_rstn;//系统复位信号,低电平有效
output lcd_rs;//lcd的寄存器选择输出信号
output lcd_rw;//lcd的读、写操作选择输出信号
output lcd_en;//lcd使能信号
output [7:0] lcd_data;//lcd的数据总线(不进行读操作,故为输出)
output [4:0] shicounter;
/*******************************************************
寄存器定义
********************************************************/
//时间显示相关寄存器定义
reg [3:0] miao1;
reg [7:0] miao11;
reg [2:0] miao2;
reg [7:0] miao22;
reg [3:0] fen1;
reg [7:0] fen11;
reg [2:0] fen2;
reg [7:0] fen22;
reg [3:0] shi1;
reg [7:0] shi11;
reg [1:0] shi2;
reg [7:0] shi22;
reg [3:0] counter;
//1S分频相关寄存器定义
reg [24:0] delays_cnt;
reg clks_div;
//1602控制相关寄存器定义
reg lcd_rs;
reg clk_div;
reg [17:0] delay_cnt;
reg [7:0] lcd_data;
reg [4:0] char_cnt;
reg [7:0] data_disp;
reg [9:0] state;
parameter idle = 10'b000000000, //初始状态,下一个状态为CLEAR
clear = 10'b000000001, //清屏
set_function = 10'b000000010, //功能设置:8位数据接口/2行显示/5*8点阵字符
switch_mode = 10'b000000100, //显示开关控制:开显示,光标和闪烁关闭
set_mode = 10'b000001000, //输入方式设置:数据读写操作后,地址自动加一/画面不动
shift = 10'b000010000, //光标、画面位移设置:光标向左平移一个字符位(光标显示是关闭的,所以实际上设置是看不出效果的)
set_ddram1 = 10'b000100000, //设置DDRAM的地址:第一行起始为0x00(注意输出时DB7一定要为1)
set_ddram2 = 10'b001000000, //设置DDRAM的地址:第二行为0x40(注意输出时DB7一定要为1)
write_ram1 = 10'b010000000, //数据写入DDRAM相应的地址
write_ram2 = 10'b100000000; //数据写入DDRAM相应的地址
assign lcd_rw = 1'b0; //没有读操作,R/W信号始终为低电平
assign lcd_en = clk_div; //E信号出现高电平以及下降沿的时刻与LCD时钟相同
/*************************************************************
时钟分频50MS
*************************************************************/
always@(posedge sys_clk , negedge sys_rstn)
begin
if(!sys_rstn)
begin
delay_cnt<=18'd0;
clk_div<=1'b0;
end
else if(delay_cnt==18'd1000000)
begin
delay_cnt<=18'd0;
clk_div<=~clk_div;
end
else
begin
delay_cnt<=delay_cnt+1'b1;
clk_div<=clk_div;
end
end
/*************************************************************
时钟分频1S
*************************************************************/
always@(posedge sys_clk , negedge sys_rstn)
begin
if(!sys_rstn)
begin
delays_cnt<=25'd0;
clks_div<=1'b0;
end
else if(delays_cnt==25'd10000000)//1S分频系数
begin
delays_cnt<=25'd0;
clks_div<=~clks_div;
end
else
begin
delays_cnt<=delays_cnt+1'b1;
clks_div<=clks_div;
end
end
/**************************************************************
时间计数状态机
**************************************************************/
always@(posedge clks_div)
begin
if(counter==4'b1100)
begin
miao1<=4'h0;
miao2<=3'h0;
fen1<=4'h0;
shi1<=4'h0;
shi2<=2'h0;
counter<=4'h0;
end
else if(miao1==4'b1001)
begin
miao2<=miao2+1'd1;
miao1<=4'h0;
end
else if(miao2==3'b110)
begin
fen1<=fen1+1'd1;
miao2<=3'h0;
end
else if(fen1==4'b1001)
begin
fen2<=fen2+1'b1;
fen1<=4'h0;
end
else if(fen2==3'b110)
begin
shi1<=shi1+1;
fen2<=3'h0;
counter<=counter+1'b1;
end
else if(shi1==4'b1001)
begin
shi2<=shi2+1'b1;
shi1<=4'h0;
end
else
miao1=miao1+1'd1;
end
/************************************************************
1602状态机
************************************************************/
always@(posedge clk_div , negedge sys_rstn)
begin
if(!sys_rstn)
begin
state <= idle;
lcd_data <= 8'bzzzzzzzz;
char_cnt <= 5'd0;
end
else
begin
case(state)
idle: begin //初始状态
state <= clear;
lcd_data <= 8'bzzzzzzzz;
end
clear: begin //清屏
state <= set_function;
lcd_rs<=1'b0;
lcd_data <= 8'b00000001;
end
set_function: //功能设置(38H):8位数据接口/2行显示/5*8点阵字符
begin
state <= switch_mode;
lcd_rs<=1'b0;
lcd_data <= 8'b00111000;
end
switch_mode: //显示开关控制(0CH):开显示,光标和闪烁关闭
begin
state <= set_mode;
lcd_rs<=1'b0;
lcd_data <= 8'b00001100;
end
set_mode:begin //输入方式设置(06H):数据读写操作后,地址自动加一/画面不动
state <= shift;
lcd_rs<=1'b0;
lcd_data <= 8'b00000110;
end
shift: begin //光标、画面位移设置(10H):光标向左平移一个字符位(光标显示是关闭的,所以实际上设置是看不出效果的)
state <= set_ddram1;
lcd_rs<=1'b0;
lcd_data <= 8'b0001_0000;
end
set_ddram1: //设置DDRAM的地址:第一行起始为00H(注意输出时DB7一定要为1)
begin
state <= write_ram1;
lcd_rs<=1'b0;
lcd_data <= 8'b1000_0000;//Line1
end
set_ddram2: //设置DDRAM的地址:第二行为40H(注意输出时DB7一定要为1)
begin
state <= write_ram2;
lcd_rs<=1'b0;
lcd_data <= 8'b1100_0000;//Line2
end
write_ram1:
begin
if(char_cnt <=5'd14)
begin
char_cnt <= char_cnt + 1'b1;
lcd_rs<=1'b1;
lcd_data <= data_disp;
state <= write_ram1;
end
else
begin
state <= set_ddram2;
end
end
write_ram2:
begin
if(char_cnt <=5'd29)
begin
char_cnt <= char_cnt + 1'b1;
lcd_rs<=1'b1;
lcd_data <= data_disp;
state <= write_ram2;
end
else
begin
char_cnt <=5'd0;
state <= shift;
end
end
default: state <= idle;
endcase
end
end
//1602上下两行显示的字符
always @(char_cnt)
begin
case (char_cnt)
5'd0: data_disp = "E";
5'd1: data_disp = "E";
5'd2: data_disp = "P";
5'd3: data_disp = "W";
5'd4: data_disp = " ";
5'd5: data_disp = "S";
5'd6: data_disp = "U";
5'd7: data_disp = "P";
5'd8: data_disp = "E";
5'd9: data_disp = "R";
5'd10: data_disp = "D";
5'd11: data_disp = "I";
5'd12: data_disp = "A";
5'd13: data_disp = "N";
5'd14: data_disp = " ";
5'd15: data_disp = "C";
5'd16: data_disp = "l";
5'd17: data_disp = "o";
5'd18: data_disp = "c";
5'd19: data_disp = "k";
5'd20: data_disp = ":";
5'd21: data_disp = shi22;
5'd22: data_disp = shi11;
5'd23: data_disp = ":";
5'd24: data_disp = fen22;
5'd25: data_disp = fen11;
5'd26: data_disp = ":";
5'd27: data_disp = miao22;
5'd28: data_disp = miao11;
5'd29: data_disp = " ";
default : data_disp ="";
endcase
end
/***************************************************************
时间计数数值译码:时间计数状态机运行时,要将秒的个位十位显示出来,
必须用ASCII码表示这两位的数值。分钟,小时的显示也相同。
****************************************************************/
always @(miao1)
begin
case (miao1)
4'd0: miao11 = "0";
4'd1: miao11 = "1";
4'd2: miao11 = "2";
4'd3: miao11 = "3";
4'd4: miao11 = "4";
4'd5: miao11 = "5";
4'd6: miao11 = "6";
4'd7: miao11 = "7";
4'd8: miao11 = "8";
4'd9: miao11 = "9";
default : miao11 ="0";
endcase
end
always @(miao2)
begin
case (miao2)
3'd0: miao22 = "0";
3'd1: miao22 = "1";
3'd2: miao22 = "2";
3'd3: miao22 = "3";
3'd4: miao22 = "4";
3'd5: miao22 = "5";
default : miao22 ="0";
endcase
end
always @(fen1)
begin
case (fen1)
4'd0: fen11 = "0";
4'd1: fen11 = "1";
4'd2: fen11 = "2";
4'd3: fen11 = "3";
4'd4: fen11 = "4";
4'd5: fen11 = "5";
4'd6: fen11 = "6";
4'd7: fen11 = "7";
4'd8: fen11 = "8";
4'd9: fen11 = "9";
default : fen11 ="0";
endcase
end
always @(fen2)
begin
case (fen2)
3'd0: fen22 = "0";
3'd1: fen22 = "1";
3'd2: fen22 = "2";
3'd3: fen22 = "3";
3'd4: fen22 = "4";
3'd5: fen22 = "5";
default : fen22 ="0";
endcase
end
always @(shi1)
begin
case (shi1)
4'd0: shi11 = "0";
4'd1: shi11 = "1";
4'd2: shi11 = "2";
4'd3: shi11 = "3";
4'd4: shi11 = "4";
4'd5: shi11 = "5";
4'd6: shi11 = "6";
4'd7: shi11 = "7";
4'd8: shi11 = "8";
4'd9: shi11 = "9";
default : shi11 ="0";
endcase
end
always @(shi2)
begin
case (shi2)
2'd0: shi22 = "0";
2'd1: shi22 = "1";
default : shi22 ="0";
endcase
end
endmodule