2014-3-15 FPGA学习 1602

来源:互联网 发布:微商晒钱视频制作软件 编辑:程序博客网 时间:2024/05/19 02:18

昨天写了6个小时18B20的时序代码,结果,还是不行,一会重新换个方式写。

今天参照着样例写了1602的代码,其中状态转移的方式用处很大。另外,1602的时钟远慢于FPGA的时钟。


module led( clk, //50MHz
rst_n, //复位
rs,rw,en,
char, //外部传入ascii
data //1602输入管脚
);


input clk;
input rst_n;
input[7:0] char;


output rs;
output rw;
output en;
output[7:0] data;


reg clk_div; //分频
reg rs;
reg[7:0] data;
reg[3:0] state;
reg[17:0] cnt; //分频计数
reg[3:0] c_cnt;//每行数量显示计数


wire[7:0] char;


parameter idle = 4'd0,//初始状态
clear = 4'd1, //清屏
set_function = 4'd2,//设置
switch_mode = 4'd3,
set_mode = 4'd4,
shift = 4'd5,
set_ram1 = 4'd6,//写入指令:第一行
set_ram2 = 4'd7,//写入指令:  第二行
write_ram1 = 4'd8,//向第一行写入
write_ram2 = 4'd9;//向第二行写入

assign rw = 1'b0; //不读取状态
assign en = clk_div; //在时钟高电平使能


//================分频===================
always@(posedge clk or negedge rst_n)
begin
if(!rst_n)
begin
cnt <= 18'd0;
clk_div <= 1'b0;
end
else if(cnt == 8'd249999)
begin
cnt <= 18'd0;
clk_div <= ~clk_div;
end
else
begin
cnt <= cnt + 1;
clk_div <= clk_div;
end
end
//==============状态转换======================
always@(posedge clk_div or negedge rst_n)
begin
if(!rst_n)
begin
state <= idle;
data <= 8'bzzzzzzzz;
end
else
begin
case(state)
idle:
begin
state <= clear;
c_cnt <= 4'd0;
data <= 8'bzzzzzzzz;
end

clear:
begin
state <= set_function;
rs <= 1'b0;
data <= 8'd1;
end

set_function:
begin
state <= switch_mode;
rs <= 1'b0;
data <= 8'b00111000;
end

switch_mode:
begin
state <= set_mode;
rs <= 1'b0;
data <= 8'b00001100;
end
set_mode:
begin
state <= shift;
rs <= 1'b0;
data <= 8'b00000110;
end
shift:
begin
state <= set_ram1;
rs <= 1'b0;
data <= 8'b00010000;
end
set_ram1:
begin
state <=write_ram1;
rs <= 1'b0;
data <= 8'h80;
end
set_ram2:
begin
state <= write_ram2;
rs <= 1'b0;
data <= 8'hA0;
end
write_ram1:
begin
if(c_cnt<=15)
begin
state <= write_ram1;
rs <= 1'b1;
data <= char;
end
else
begin
state <= write_ram2;
c_cnt <= 4'd0;
end
end
write_ram2:
begin
if(c_cnt<=15)
begin
state <= write_ram2;
rs <= 1'b1;
data <= char;
end
else
begin
state <= shift;
c_cnt <= 4'd0;
end
end
default : state <= idle;
endcase
end
end


endmodule

0 0
原创粉丝点击