reg[17:0] count;
reg clk_buf;
always@(posedge clk or negedge rst)
if(!rst)
begin
clk_buf<=0;
count<=0;
end
else if(count<=125000)
begin
clk_buf<=0;
count<=count+1;
end
else if(count>250000-1)
count<=0;
else
begin
clk_buf<=1;
count<=count+1;
end
assign lcd_en=clk_buf;
initial
begin
clock_second_l<=8'h30;
clock_second_h<=8'h30;
clock_minute_l<=8'h30;
clock_minute_h<=8'h30;
clock_hour_l<=8'h30;
clock_hour_h<=8'h30;
counter<=0;
end
always@(posedge clk or negedge rst)
begin
if(rst==0)
begin
clock_second_l<=8'h30;
clock_second_h<=8'h30;
clock_minute_l<=8'h30;
clock_minute_h<=8'h30;
clock_hour_l<=8'h30;
clock_hour_h<=8'h30;
counter<=0;
end
else if(counter==50_000_000)
begin
clock_second_l<=clock_second_l+1'b1;
counter<=0;
end
else if(clock_second_l==8'h3A)
begin
clock_second_h<=clock_second_h+1'b1;
clock_second_l<=8'h30;
counter<=counter+1'b1;
end
else if(clock_second_h==8'h36)
begin
clock_minute_l<=clock_minute_l+1'b1;
clock_second_h<=8'h30;
counter<=counter+1'b1;
end
else if(clock_minute_l==8'h3A)
begin
clock_minute_h<=clock_minute_h+1'b1;
clock_minute_l<=8'h30;
counter<=counter+1'b1;
end
else if(clock_minute_h==8'h36)
begin
clock_hour_l<=clock_hour_l+1'b1;
clock_minute_h<=8'h30;
counter<=counter+1'b1;
end
else if(clock_hour_l==8'h3A)
begin
clock_hour_h<=clock_hour_h+1'b1;
clock_hour_l<=8'h30;
counter<=counter+1'b1;
end
else if(clock_hour_h==8'h32)
begin
if(clock_hour_l==8'h34)
begin
clock_second_l<=8'h30;
clock_second_h<=8'h30;
clock_minute_l<=8'h30;
clock_minute_h<=8'h30;
clock_hour_l<=8'h30;
clock_hour_h<=8'h30;
counter<=0;
end
end
else counter<=counter+1'b1;
end
always@(posedge clk_buf or negedge rst)
if(rst==0)
begin
state<=IDLE;
lcd_data<=8'b0000_0000;
lcd_rs<=0;
lcd_rw<=0;
address<=0;
end
else
begin
case(state)
IDLE:
begin
lcd_rs<=0;
lcd_rw<=0;
lcd_data<=8'b0000_0000;
state<=CLEAR;
end
CLEAR:
begin
lcd_rs<=0;
lcd_rw<=0;
lcd_data<=8'b0000_0001;
state<=SET_FUNCTION;
end
SET_FUNCTION:
begin
lcd_rs<=0;
lcd_rw<=0;
lcd_data<=8'b0011_1000;//0011_1000
state<=SWITCH_MODE;
end
SWITCH_MODE:
begin
lcd_rs<=0;
lcd_rw<=0;
lcd_data<=8'b0000_1111;
state<=SET_MODE;
end
SET_MODE:
begin
lcd_rs<=0;
lcd_rw<=0;
lcd_data<=8'b0000_0110;
state<=SHIFT;
end
SHIFT:
begin
lcd_rs<=0;
lcd_rw<=0;
lcd_data<=8'b0001_0100;
state<=SET_DDRAM;
end
SET_DDRAM:
begin
lcd_rs<=0;
lcd_rw<=0;
lcd_data<=8'b1000_0000;
address<=0;
state<=WRITE_RAM;
end
WRITE_RAM:
begin
if(address<=15)
begin
lcd_rs<=1;
lcd_rw<=0;
lcd_data<=data[address];
address<=address+1'b1;
// state<=WRITE_RAM;
end
else
begin
address<=0;
state<=WRITE_RAM;
end
end
STOP:
begin
state<=STOP;
address<=0;
lcd_rw<=1;
end
default: state<=CLEAR;
endcase
end
endmodule