此程序控制LCD12864液晶屏,IC为KS0108或兼容型号
图形文件获取方法:
在字模提取V21软件中 ,导入一幅128*64黑白图像.
* 参数设置:
* 参数设置->其它选项,选择纵向取模,勾上字节倒序,保留逗号,
* 取模方式为C51。
将生成的数组通过keilc等C编译软件,在编译软件中新建一工程,写入源程序如下:
unsigned char code tab[]=
{
//图像数据
}
编译此工程将得到hex文件.在QII中使用lpm_rom宏功能模块中调用此hex文件.
*
*******************************************************************************/
module newlcd(clock,rst_n,rs,rw,en,data,lcd_cs)
// I/O口声明
input clock //系统时钟
input rst_n //复位信号
output[1:0] lcd_cs //
outputrs //1:数据模式;0:指令模式
outputrw //1:读 *** 作;0:写 *** 作
outputen //使能信号,写 *** 作时在下降沿将数并答亩据举肢送出;读 *** 作时保持高电平
output[7:0] data//绝森LCD数据总线
// I/O寄存器
reg rs
reg en
reg[1:0] lcd_cs
reg[7:0] data
//内部寄存器
reg[3:0] state //状态机
reg[3:0] next_state
reg[20:0] div_cnt //分频计数器
reg[9:0] cnt //写 *** 作计数器
reg cnt_rst //写 *** 作计数器复位信号
wire[7:0] showdata //要显示的数据
reg[1:0] cs_r
reg [2:0] page_addr
reg [5:0] row_addr
//内部网线
wire clk_div//分频时钟
wire clk_divs
wire page_done //写一行数据完成标志位
wire frame_done //写一屏数据完成标志位
wire left_done
//状态机参数
parameter idle =4'b0000,
setbase_1=4'b0001,
setbase_2=4'b0011,
setmode_1=4'b0010,
setmode_2=4'b0110,
SETpage_addr_1=4'b0111,
SETpage_addr_2=4'b0101,
SETrow_addr_1 =4'b1101,
SETrow_addr_2 =4'b1111,
write_right_1 =4'b1110,
write_right_2 =4'b1010,
write_nextpage_1 =4'b1011,
write_nextpage_2 =4'b1001,
wr_data_1 =4'b0100,
wr_data_2 =4'b1100
// set_1=4'b1000
//******************************代码开始*********************************
assign rw = 1'b0 //对LCD始终为写 *** 作
//时钟分频
always@(posedge clock or negedge rst_n)
begin
if(!rst_n)
div_cnt <= 0
else
div_cnt <= div_cnt+1'b1
end
assign clk_div = (div_cnt[15:0] == 20'h7fff)
//状态机转向
always@(posedge clock or negedge rst_n)
begin
if(! rst_n)
state <= idle
else if(clk_div)
state <= next_state
end
//************************状态机逻辑*********************************
always@(state or page_done or left_done or frame_done or cnt or showdata or page_addr or row_addr or cs_r)
begin
rs <= 1'b0
en <= 1'b0
lcd_cs <= cs_r
cnt_rst <= 1'b0
data <= 8'h0
case(state)
idle:
begin
next_state <= setbase_1
cnt_rst <= 1'b1
end
//**************************初始化LCD********************************
setbase_1: //基本指令 *** 作
begin
lcd_cs <= 2'b11
next_state <= setbase_2
data <= 8'hc0
en <= 1'b1
end
setbase_2:
begin
lcd_cs <= 2'b11
next_state <= setmode_1
data <= 8'hc0
end
//******************************************************************
setmode_1:
begin
lcd_cs <= 2'b11
next_state <= setmode_2
data <= 8'h3f
en <=1'b1
end
setmode_2:
begin
next_state <= SETpage_addr_1
data <= 8'h3f
end
//******************************************************************
SETpage_addr_1: //设置页地址
begin
next_state <= SETpage_addr_2
data <=
en <= 1'b1
end
SETpage_addr_2:
begin
next_state <= SETrow_addr_1
data <=
end
SETrow_addr_1: //设置列地址
begin
next_state <= SETrow_addr_2
data <=
en <= 1'b1
end
SETrow_addr_2:
begin
next_state <= wr_data_1
data <=
end
//******************************************************************
/*
write_right_1: //写完左半屏64个,换为右半屏显示
begin
next_state <=write_right_2
row_addr <= 0
end
write_right_2:
begin
next_state <= SETpage_addr_1
end
//******************************************************************
write_nextpage_1: //写完全一行128个
begin
next_state <=write_nextpage_2
row_addr <= 0
end
write_nextpage_2:
begin
next_state <= SETpage_addr_1
end
*/
//******************************************************************
wr_data_1: //写数据到图形显示区
begin
next_state <= wr_data_2
rs <= 1'b1
en <= 1'b1
data <= showdata
end
wr_data_2:
begin
rs <= 1'b1
data <= showdata
if(left_done) //写完左半屏数据64个
begin
if(page_done) //写完一页数据128个
begin
if(frame_done) //写完一屏数据(8页)
next_state <= idle
else
// next_state <= write_nextpage_1
next_state <= SETpage_addr_1
end
else
// next_state <= write_right_1
next_state <= SETpage_addr_1
end
else
next_state <= wr_data_1
end
default: next_state <= idle
endcase
end
//********************************************************************
always@(posedge clock)
begin
if(clk_div)
begin
if(cnt_rst)
begin
cnt <= 0
end
else if(state == wr_data_2)
begin
cnt <= cnt+1'b1
end
end
end
//****************************************************
always@(posedge clock or negedge rst_n)
if(!rst_n)
begin
cs_r <= 2'b01
page_addr <= 0
end
else
if(clk_div &&(state == wr_data_2))
if(page_done)//
begin
cs_r <= 2'b01
page_addr <= page_addr + 1'b1//一页写完时写下一页
end
else
if(left_done)
begin
cs_r <= 2'b10
end
//*********************************************************************
//********************************************************************
assign left_done = (cnt[5:0] == 6'd63) //写完左半屏数据64个
assign page_done = (cnt[6:0] == 7'd127) //写完一页数据128个
assign frame_done = (cnt[9:4] == 7'h3f) //写完一屏数据
//***********************************************************************
//*******************************************************************
//调用ROM(图片数据)
rom rom(.address(cnt+'d8),.clock(clock),.q(showdata))
endmodule
开发板例程 自己看吧
我可以帮助你,你先设置我最佳答案后,我百度Hii教你。
module LCD_Driver(clk,rst,lcd_rs,lcd_en,lcd_rw,lcd_data,f0,f1,f2,d0,d1,d2,sel)input clk,rst//时钟、复位
input [3:0] f0 //频率、占空比、档位输入
input [3:0] f1
input [3:0] f2
input [3:0] d0
input [3:0] d1
input [3:0] d2
input [3:0] sel
output lcd_rs,lcd_en,lcd_rw//指令/数据控制、弊碧绝片选、租姿读写控制、背光控制
output [7:0] lcd_data//数据线
reg lcd_rs//指令/数据控制
reg [7:0] lcd_data//数据线
reg [7:0] current_state//当前状态
reg [1:0] state_counter//状态计数
reg en_temp//使能标志
reg [15:0] clk_counter//时钟计数
reg clk_en//时钟使能慧友
//reg [7:0] ps_datain_temp
assign lcd_rw=1'b0//一直为写状态
/********************状态编码******************************/
parameter set0=8'b0000_0000,
set1=8'b0000_0001,
set2=8'b0000_0011,
set3=8'b0000_0100,
set4=8'b0000_0101,
set5=8'b0000_0110,
data1=8'b0000_1000,
data2=8'b0000_1001,
data3=8'b0000_1010,
data4=8'b0000_1011,
data5=8'b0000_1100,
data6=8'b0000_1101,
data7=8'b0000_1110,
data8=8'b0000_1111,
data9=8'b0001_0000,
data10=8'b0001_0001,
data11=8'b0001_0010,
data12=8'b0001_0011,
data13=8'b0001_0100,
data14=8'b0001_0101,
data15=8'b0001_0110,
data16=8'b0001_0111,
data17=8'b0001_1000,
data18=8'b0001_1001,
data19=8'b0001_1010,
data20=8'b0001_1011,
data21=8'b0001_1100,
data22=8'b0001_1101,
data23=8'b0001_1110,
data24=8'b0001_1111,
data25=8'b0010_0001,
data26=8'b0010_0010,
data27=8'b0010_0011,
data28=8'b0010_0100,
data29=8'b0010_0101,
data30=8'b0010_0110,
data31=8'b0010_0111,
data32=8'b0010_1000,
stop=8'b1111_1111
/**************状态转换时钟***********************/
always @(posedge clk)
begin
if(clk_counter==16'h6000)
begin
clk_counter<=16'h0
clk_en<=~clk_en
end
else
clk_counter<=clk_counter+1'b1
end
/**************状态转换**************************///
always @(posedge clk_en or negedge rst)
begin
if(!rst)
begin
current_state<=set0
end
else
begin
case (current_state)
/*********************************************************************************/
set0:begin lcd_rs<=1'b0lcd_data<=8'h38current_state<=set1end//显示模式设置
set1:begin lcd_rs<=1'b0lcd_data<=8'h0ccurrent_state<=set2end//显示开及光标设置
set2:begin lcd_rs<=1'b0lcd_data<=8'h06current_state<=set3end//显示光标移动设置
set3:begin lcd_rs<=1'b0lcd_data<=8'h01current_state<=set4end//显示清屏
set4:begin lcd_rs<=1'b0lcd_data<=8'h80current_state<=data1end//设置第一行地址
/***********************************************************************************/
data1:begin lcd_rs<=1'b1lcd_data<=" "current_state<=data2end//显示第1个字符
data2:begin lcd_rs<=1'b1lcd_data<="F"current_state<=data3end//显示第2个字符
data3:begin lcd_rs<=1'b1lcd_data<=" "current_state<=data4end//显示第3个字符
data4:begin lcd_rs<=1'b1lcd_data<="="current_state<=data5end//显示第4个字符
data5:begin lcd_rs<=1'b1lcd_data<=" "current_state<=data6end//显示第5个字符
data6:begin lcd_rs<=1'b1lcd_data<=f0+"0"current_state<=data7end//显示第6个字符
data7:begin lcd_rs<=1'b1lcd_data<=f1+"0"current_state<=data8end//显示第7个字符
data8:begin lcd_rs<=1'b1lcd_data<=f2+"0"current_state<=data9end//显示第8个字符
data9:begin lcd_rs<=1'b1lcd_data<="x"current_state<=data10end//显示第1个字符
data10:begin lcd_rs<=1'b1if(sel[3])lcd_data<="1"else lcd_data<="0"current_state<=data11end//显示第2个字符
data11:begin lcd_rs<=1'b1if(sel[2])lcd_data<="1"else lcd_data<="0"current_state<=data12end//显示第3个字符
data12:begin lcd_rs<=1'b1if(sel[1])lcd_data<="1"else lcd_data<="0"current_state<=data13end//显示第4个字符
data13:begin lcd_rs<=1'b1if(sel[0])lcd_data<="1"else lcd_data<="0"current_state<=data14end//显示第5个字符
data14:begin lcd_rs<=1'b1lcd_data<=" "current_state<=data15end//显示第6个字符
data15:begin lcd_rs<=1'b1lcd_data<="H"current_state<=data16end//显示第7个字符
data16:begin lcd_rs<=1'b1lcd_data<="Z"current_state<=set5end//显示第8个字符
set5:begin lcd_rs<=1'b0lcd_data<=8'hc0current_state<=data17end//设置第2行地址
data17:begin lcd_rs<=1'b1lcd_data<=" "current_state<=data18end//显示第1个字符
data18:begin lcd_rs<=1'b1lcd_data<="D"current_state<=data19end//显示第2个字符
data19:begin lcd_rs<=1'b1lcd_data<=" "current_state<=data20end//显示第3个字符
data20:begin lcd_rs<=1'b1lcd_data<="="current_state<=data21end//显示第4个字符
data21:begin lcd_rs<=1'b1lcd_data<=" "current_state<=data22end//显示第5个字符
data22:begin lcd_rs<=1'b1lcd_data<=d0+"0"current_state<=data23end//显示第6个字符
data23:begin lcd_rs<=1'b1lcd_data<=d1+"0"current_state<=data24end//显示第7个字符
data24:begin lcd_rs<=1'b1lcd_data<=d2+"0"current_state<=data25end//显示第8个字符
data25:begin lcd_rs<=1'b1lcd_data<=" "current_state<=data26end//显示第二个字符
data26:begin lcd_rs<=1'b1lcd_data<="%"current_state<=data27end//显示第四个字符
data27:begin lcd_rs<=1'b1lcd_data<=" "current_state<=data28end//显示第五个字符
data28:begin lcd_rs<=1'b1lcd_data<=" "current_state<=data29end//显示第六个字符
data29:begin lcd_rs<=1'b1lcd_data<=" "current_state<=data30end//显示第七个字符
data30:begin lcd_rs<=1'b1lcd_data<=" "current_state<=data31end//显示第八个字符
data31:begin lcd_rs<=1'b1lcd_data<=" "current_state<=data32end//显示第九个字符
data32:begin lcd_rs<=1'b1lcd_data<=" "current_state<=stopend//显示第九个字符
/*********************************************************************************/
stop:begin //控制指令与数据写入的次数
lcd_rs<=1'b0
lcd_data<=8'b0000_0000
if(state_counter!=2'b10)
begin
en_temp<=1'b0
current_state<=set4
state_counter<=state_counter+1'b1
end
else
begin
current_state<=set4
en_temp<=1'b0//最后数据写入完成后将lcd_en线拉高
end
end
default: current_state<=set0
endcase
end
end
assign lcd_en=clk_en|en_temp//lcd_en为‘1’有效
endmodule
写的PWM,如果实时显示就要状态机一直循环,并且给模块定义一个数据输入变量
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)