没有很好的完善和没有记录,但希望这将有一些学习目的的地方。
我相信其他人会有更好的编写代码的方法。
请享用。
菲茨
`timescale 1ns / 1ps /////////////////////////////////////////////
///////////////////////////////////////公司://工程师:// //创造
日期:21:43:40 05/12/2008 //设计名称://模块名称:text_lcd //项目名称://目标设备://工具版本://说明:////依赖项://
//修订版://修订版0.01 - 文件创建//附加注释://////////////////////////////////
//////////////////////////////////////////////////
/ *******************
**********这是控制显示的部分********************************
**************************** /`include“text_chars.v”`定义CMD_STATE_INIT 6'h00`define CMD_STATE_MVBUF 6'h02`define
CMD_STATE_01 6'h04`define CMD_STATE_02 6'h06`define CMD_STATE_03 6'h08`define CMD_STATE_04 6'h0A`define CMD_STATE_05 6'h0C`define CMD_STATE_06 6'h0E`define CMD_STATE_07 6'h10`define CMD_STATE_08 6'h12`define CMD_STATE_09 6
'h14`define CMD_STATE_0A 6'h16`define CMD_STATE_0B 6'h18`define CMD_STATE_0C
6'h1A`define CMD_STATE_0D 6'h1C`define CMD_STATE_0E 6'h1E`define CMD_STATE_10 6'h20`define CMD_STATE_11 6'h22`define CMD_STATE_12 6'h24`define CMD_STATE_14 6'h26`define CMD_STATE_16 6'h28`define CMD_STATE_18 6'
h2A`define CMD_STATE_1A 6'h2C`define CMD_STATE_1B 6'h2E`define CMD_STATE_30 6'h30`define CMD_STATE_32 6'h32`define CMD_STATE_DELAY 6'b ????? 1module display_lcd(输入线rst,输入线clk,输入线[
255:0] msg,输出reg busy,输出reg lcd_e,输出reg lcd_rs,输出reg lcd_rw,输出reg [7:0] lcd_db);
//参数FUNC_SET = 1,DATA_INPUT = 2;
// reg lcd_e;
// reg [7:0] lcd_db;
// reg lcd_rs = 0;
// reg lcd_rw = 0;
// reg busy = 0;
reg [255:0] in_msg = 0;
reg [255:0] in_msg_stack = 0;
reg [19:0] counter = 0;
reg cnt_rst = 0;
reg [5:0] lcd_state = 0;
// reg [5:0] nxt_state = 0;
reg [7:0] col = 0;
reg [7:0] row = 0;
reg [7:0] str;
// wire [7:0] wstr;
wire count_flag;
// wire status_w;
// wire rst_w;
// fx_delay#(。WIDTH(20))delay0(// .clk_w_i(CLOCK),// .rst_w_i(rst_w),//。count_w_i(counter),//。status_w_o(status_w)//);
// //分配zz = in_msg_stack [251:248];
// // //指定count_flag =〜& counter;
总是@(posedge clk)开始if(rst == 1)lcd_state else if(〜& counter == 0)// casez({lcd_state [5:1],〜& counter})case(lcd_state)`CMD_STATE_INIT
:begin if(in_msg_stack!= msg)start in_msg in_msg_stack busy lcd_state counter str end end`CMD_STATE_MVBUF:begin if if(in_msg [255:248] == 8'b00000000)begin in_msg in_msg [7:0] end else begin lcd_state counter end
结束`CMD_STATE_01:开始lcd_db lcd_rs计数器lcd_state结束`CMD_STATE_02:开始lcd_e计数器lcd_state结束`CMD_STATE_03:开始lcd_e计数器lcd_state结束`CMD_STATE_04:开始lcd_db计数器lcd_state结束`CMD_STATE_05:开始lcd_e计数器lcd_state结束`CMD_STATE_06:开始lcd_e计数器lcd_state结束
`CMD_STATE_07:start lcd_db counter lcd_state end`CMD_STATE_08:begin lcd_e counter lcd_state end`CMD_STATE_09:begin lcd_e counter lcd_state end`CMD_STATE_0A:begin lcd_db counter lcd_state end`CMD_STATE_0B:begin lcd_e counter lcd_state end`CMD_STATE_0C:begin lcd_e counter lcd_state end`CMD_STATE_0D
:开始lcd_db co
unter lcd_state end`CMD_STATE_0E:begin lcd_e counter lcd_state end`CMD_STATE_10:begin lcd_e counter lcd_state end`CMD_STATE_11:begin lcd_db 以下为原文
This is my own code for the 16x2 LCD in Spartan 3A kit. Not well polished and not documented, but hope this will have some place for learning purposes. I'm sure others will have better way of writing the code.
Enjoy.
Fitz
`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company:
// Engineer:
//
// Create Date: 21:43:40 05/12/2008
// Design Name:
// Module Name: text_lcd
// Project Name:
// Target Devices:
// Tool versions:
// Description:
//
// Dependencies:
//
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
//
//////////////////////////////////////////////////////////////////////////////////
/*********************************************************
*
* This is part that will control the display
*
*
*
*********************************************************/
`include "text_chars.v"
`define CMD_STATE_INIT 6'h00
`define CMD_STATE_MVBUF 6'h02
`define CMD_STATE_01 6'h04
`define CMD_STATE_02 6'h06
`define CMD_STATE_03 6'h08
`define CMD_STATE_04 6'h0A
`define CMD_STATE_05 6'h0C
`define CMD_STATE_06 6'h0E
`define CMD_STATE_07 6'h10
`define CMD_STATE_08 6'h12
`define CMD_STATE_09 6'h14
`define CMD_STATE_0A 6'h16
`define CMD_STATE_0B 6'h18
`define CMD_STATE_0C 6'h1A
`define CMD_STATE_0D 6'h1C
`define CMD_STATE_0E 6'h1E
`define CMD_STATE_10 6'h20
`define CMD_STATE_11 6'h22
`define CMD_STATE_12 6'h24
`define CMD_STATE_14 6'h26
`define CMD_STATE_16 6'h28
`define CMD_STATE_18 6'h2A
`define CMD_STATE_1A 6'h2C
`define CMD_STATE_1B 6'h2E
`define CMD_STATE_30 6'h30
`define CMD_STATE_32 6'h32
`define CMD_STATE_DELAY 6'b?????1
module display_lcd (
input wire rst,
input wire clk,
input wire [255:0] msg,
output reg busy,
output reg lcd_e,
output reg lcd_rs,
output reg lcd_rw,
output reg [7:0] lcd_db
);
//parameter FUNC_SET = 1, DATA_INPUT = 2;
//reg lcd_e;
//reg [7:0] lcd_db;
//reg lcd_rs = 0;
//reg lcd_rw = 0;
//reg busy = 0;
reg [255:0] in_msg = 0;
reg [255:0] in_msg_stack = 0;
reg [19:0] counter = 0;
reg cnt_rst = 0;
reg [5:0] lcd_state = 0;
//reg [5:0] nxt_state = 0;
reg [7:0] col = 0;
reg [7:0] row = 0;
reg [7:0] str;
//wire [7:0] wstr;
wire count_flag;
//wire status_w;
//wire rst_w;
//fx_delay #(.WIDTH(20))delay0 (
// .clk_w_i(CLOCK),
// .rst_w_i(rst_w),
// .count_w_i(counter),
// .status_w_o(status_w)
//);
//
//assign zz = in_msg_stack[251:248];
//
//
//assign count_flag = ~&counter;
always @(posedge clk)
begin
if(rst == 1)
lcd_state <= `CMD_STATE_INIT;
else if (~&counter == 0)
//casez ({lcd_state[5:1],~&counter})
case (lcd_state)
`CMD_STATE_INIT:
begin
if(in_msg_stack != msg)
begin
in_msg <= msg;
in_msg_stack <= msg;
busy <= 1;
lcd_state <= `CMD_STATE_MVBUF;
counter <= 0;
str <= 0;
end
end
`CMD_STATE_MVBUF:
begin
if(in_msg[255:248] == 8'b00000000)
begin
in_msg <= in_msg << 8;
in_msg[7:0] <= 8'b00100000;
end
else
begin
lcd_state <= `CMD_STATE_01;
counter <= 0;
end
end
`CMD_STATE_01:
begin
lcd_db <= 8'h3F;
lcd_rs <= 0;
counter <= 2;
lcd_state <= `CMD_STATE_02;
end
`CMD_STATE_02:
begin
lcd_e <= 1;
counter <= 12;
lcd_state <= `CMD_STATE_03;
end
`CMD_STATE_03:
begin
lcd_e <= 0;
counter <= 2000;
lcd_state <= `CMD_STATE_04;
end
`CMD_STATE_04:
begin
lcd_db <= 8'h3B;
counter <= 2;
lcd_state <= `CMD_STATE_05;
end
`CMD_STATE_05:
begin
lcd_e <= 1;
counter <= 12;
lcd_state <= `CMD_STATE_06;
end
`CMD_STATE_06:
begin
lcd_e <= 0;
counter <= 2000;
lcd_state <= `CMD_STATE_07;
end
`CMD_STATE_07:
begin
lcd_db <= 8'hC;
counter <= 2;
lcd_state <= `CMD_STATE_08;
end
`CMD_STATE_08:
begin
lcd_e <= 1;
counter <= 12;
lcd_state <= `CMD_STATE_09;
end
`CMD_STATE_09:
begin
lcd_e <= 0;
counter <= 2000;
lcd_state <= `CMD_STATE_0A;
end
`CMD_STATE_0A:
begin
lcd_db <= 8'h1;
counter <= 2;
lcd_state <= `CMD_STATE_0B;
end
`CMD_STATE_0B:
begin
lcd_e <= 1;
counter <= 12;
lcd_state <= `CMD_STATE_0C;
end
`CMD_STATE_0C:
begin
lcd_e <= 0;
counter <= 100000;
lcd_state <= `CMD_STATE_0D;
end
`CMD_STATE_0D:
begin
lcd_db <= 8'b0000110;
counter <= 2;
lcd_state <= `CMD_STATE_0E;
end
`CMD_STATE_0E:
begin
lcd_e <= 1;
counter <= 12;
lcd_state <= `CMD_STATE_10;
end
`CMD_STATE_10:
begin
lcd_e <= 0;
counter <= 2000;
lcd_state <= `CMD_STATE_11;
end
`CMD_STATE_11:
begin
lcd_db <= `LCD_ADDR_00 | col | row ; // address
lcd_rs <= 0;
counter <= 2;
lcd_state <= `CMD_STATE_12;
end
`CMD_STATE_12:
begin
lcd_e <= 1;
counter <= 12;
lcd_state <= `CMD_STATE_14;
end
`CMD_STATE_14:
begin
lcd_e <= 0;
counter <= 2000;
lcd_state <= `CMD_STATE_16;
end
// After 40 microsecond
`CMD_STATE_16:
begin
//lcd_db = `LCD_TEXT_s; // data
lcd_db <= in_msg[255:248];
lcd_rs <= 1;
counter <= 12;
lcd_state <= `CMD_STATE_18;
end
`CMD_STATE_18:
begin
lcd_e <= 1;
counter <= 12;
lcd_state <= `CMD_STATE_1A;
end
`CMD_STATE_1A:
begin
lcd_e <= 0;
counter <= 0;
//counter <= 2000;
// go to next col, else go to next state
if (col == 8'h0F)
lcd_state <= `CMD_STATE_30;
else
lcd_state <= `CMD_STATE_32;
end
`CMD_STATE_30:
begin
counter <= 2000;
if (row == 8'b01000000) begin
lcd_state <= `CMD_STATE_1B;
end
else
begin
lcd_state <= `CMD_STATE_11;
col <= 0;
row <= 8'b01000000;
in_msg <= in_msg << 8;
end
end
//else
`CMD_STATE_32:
begin
counter <= 2000;
lcd_state <= `CMD_STATE_11;
col <= col + 1;
in_msg <= in_msg << 8;
end
`CMD_STATE_1B:
begin
busy <= 0;
lcd_state <= `CMD_STATE_INIT;
row <= 0;
col <= 0;
counter <= 0;
end
//`CMD_STATE_DELAY:
// begin
// counter <= counter - 1;
//
// end
default:
begin
busy <= 0;
lcd_state <= `CMD_STATE_INIT;
row <= 0;
col <= 0;
counter <= 0;
end
endcase
else
counter <= counter - 1;
end
endmodule