微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > PIC驱动JM240128液晶

PIC驱动JM240128液晶

时间:11-09 来源:互联网 点击:
这个驱动程序我耗了3天的时间,想想真够笨的!一开始一天半,没有一点收获,主要是液晶的硬件电路没有连好!却开始写程序,结果怎么调,液晶就是没有显示!以前的用过的液晶不需要接什么电位器来调节显示的亮度,这个液晶却需要,而我没有接!浪费了我这么长时间,真是郁闷!

头文件

#ifndef JM240_H
#define JM240_H
#include "main.h"

//define port
#define DB PORTD
#define DB_DIR TRISD
#define WR RA0
#define RD RA1
#define CE RA2
#define CD RA3
#define RST RA4
typedef struct JM240
{
uint graphic_add ;//only use graphic mode
uchar graphic_width ;//define graphic width
uint offset ;// offset address of currect page
uint cur_page ;//current page
uchar char_width ;//width of charactor
uchar chn_width ;//width of chn
} JM240 ;
//define the first address of seperative page

extern JM240 jm ;
enum state{} ;
//define basic function
void init_jm240() ;//init JM240 include initiating control port ,JM240 struct and iniatiating LCD
void send_byte(uchar data) ;//send data
void send_cmd(uchar cmd) ;//send a cmd
uchar ch_status() ;//get status word
void ch_busy(uchar flag) ;//check whether the current lcd is busy or not
void rst_lcd() ;//reset lcd
uchar rd_data() ;
//define high zone function
void wr_data(uchar dat) ;
void wr_cmd0(uchar cmd) ;//no para command
void wr_cmd1(uchar data,uchar cmd) ;//one para command
void wr_cmd2(uchar dat1,uchar dat2,uchar cmd) ;//two para command
void wr_cmd3(uint data,uchar cmd) ;//one para of int type command
void set_mode(uchar mode) ;
void enable_autowr(uchar flag) ;
void enable_autord(uchar flag) ;
void auto_wr(uchar dat) ;
uchar auto_rd() ;
//define higher zone function
void str_disp(const uchar *str,uchar x,uchar y) ;//display string through internal character library
void show_char(uchar data,uchar x,uchar y) ;
void show_chn(const uchar *chn,uchar x,uchar y) ;
void chns_disp(const uchar *chn,uchar x,uchar y,uchar count) ;
void show_img(uchar x,uchar y,uchar x1,uchar y1,const uchar *img) ;
void lcd_refresh(const uchar *img) ;
void clr_lcd(uchar x,uchar y,uchar x1,uchar y1) ;
void clr_ram() ;
void wr_img(uchar x,uchar y,uchar width,uchar height,const uchar *img) ;
void set_add(uint ad) ;
void add_xy(uchar x,uchar y) ;//move ram pointer
void add_add(uint x) ;
void set_graphic(uint add,uchar width) ;//set graphic par

//define draw
void draw_vline(uchar x,uchar y,uchar height) ;
void draw_hline(uchar x,uchar y,uchar width) ;
void draw_frame(uchar x,uchar y,uchar width,uchar height) ;
void draw_point(uchar x,uchar y) ;
void draw_page(uchar num) ;
#endif
子程序

#include "jm240.h"
#include "ascii0816.h"
const uint add[]={0x0000,0x0F00,0x1E00,0x2D00,0x3C00,0x4B00,0x5A00,0x6900,0x7800,0x9700,0xA600,0xB500,0xC400} ;
JM240 jm ;
void init_jm240()
{
//set the directio of port
DB_DIR=0 ;
ADCON1=0x06 ;
TRISA=TRISA&0xC0 ;
//init global variable
jm.graphic_add=0x0000 ;
jm.graphic_width=30 ;
jm.cur_page=0 ;
jm.offset=0x0000 ;
jm.char_width=8 ;
jm.chn_width=16 ;
//init
rst_lcd() ;
set_graphic(jm.graphic_add,jm.graphic_width) ;
wr_cmd0(0x80) ;// 或模式
wr_cmd0(0x98) ;//note here must start graphic display
clr_ram() ;
}
//restart lcd
void rst_lcd()
{
RST=0 ;
DelayUs(50) ;
RST=1 ;
DelayMs(50) ;
}
//get the currrent state of lcd
uchar ch_status()
{
uchar temp ;
CD=1 ;
WR=1 ;

CE=0 ;
DB_DIR=0x00 ;
DB=0xff ;
RD=0 ;
DB_DIR=0xFF ;
temp=DB ;
RD=1 ;
CE=1 ;
DB_DIR=0x00 ;
return temp ;
}
//check whether lcd is busy or not at present
void ch_busy(uchar flag)
{
uchar temp1,temp2 ;
if(!flag)//one write or read status flag
temp2=0x03 ;
else
{
if(flag==1)// auto write status flag check
temp2=0x08 ;
else//auto read state flag check
temp2=0x04 ;
}
do
{
temp1=ch_status() ;
temp1=temp1&temp2 ;
}while(temp1!=temp2) ;
}

//set graphic mode
void set_graphic(uint ad,uchar width)
{
jm.graphic_add=ad ;
jm.graphic_width=width ;
wr_cmd3(ad,0x42) ;
wr_cmd2(width,0x00,0x43) ;
}
//read a byte from lcd
uchar rd_data()
{
uchar temp ;
ch_busy(0) ;
CD=0 ;
DB=0 ;
CE=0 ;
DB_DIR=0x00 ;
DB=0xff ;
RD=0 ;
DB=0 ;
CE=0 ;
DB_DIR=0xFF ;
temp=DB ;
RD=1 ;
CD=1 ;
return temp ;
}
//send a byte to lcd ram
void send_byte(uchar dat)
{
RD=1 ;
DB_DIR=0x00 ;
CE=0 ;
CD=0 ;
WR=0 ;
DB=dat ;
//DelayUs(2) ;
CE=1 ;
CD=1 ;
WR=1 ;
}
//send a command word to lcd registor
void send_cmd(uchar cmd)
{
RD=1 ;
DB_DIR=0x00 ;
CE=0 ;
CD=1 ;
WR=0 ;
DB=cmd ;
//DelayUs(2) ;
CE=1 ;
CD=1 ;
WR=1 ;
}
//write a byte
void wr_data(uchar data)
{
ch_busy(0) ;
send_byte(data) ;
}
//write a command with no parameter
void wr_cmd0(uchar cmd)
{
ch_busy(0) ;
send_cmd(cmd) ;
}
//write a command with one parametre
void wr_cmd1(uchar para,uchar cmd)
{
ch_busy(0) ;
send_byte(para) ;
ch_busy(0) ;
send_cmd(cmd) ;
}
//write a cammand with two parameters
void wr_cmd2(uchar para1,uchar para2,uchar cmd)
{
ch_busy(0) ;
send_byte(para1) ;
ch_busy(0) ;
send_byte(para2) ;
ch_busy(0) ;
send_cmd(cmd) ;
}
//write a command with a uint parameter
void wr_cmd3(uint para,uchar cmd)
{
uchar temp,temp1 ;
temp=(uchar )para ;
temp1=(uchar)(para>>8) ;
wr_cmd2(temp,temp1,cmd) ;
}
//autowrite
void auto_wr(uchar dat)
{
ch_busy(1) ;
send_byte(dat) ;
}
//autoread
uchar auto_rd()
{
uchar temp ;
ch_busy(2) ;
CD=0 ;
DB=0 ;
CE=0 ;
DB_DIR=0x00 ;
DB=0xff ;
RD=0 ;
DB=0 ;
CE=0 ;
DB_DIR=0xFF ;
temp=DB ;
RD=1 ;
CD=1 ;
return temp ;
}
//enable auto write or not
void enable_autowr(uchar flag)
{
if(flag)
wr_cmd0(0xB0) ;
else
wr_cmd0(0xB2) ;
}
//enable autoread or not
void enable_autord(uchar flag)
{
if(flag)
wr_cmd0(0xB1) ;
else
wr_cmd0(0xB3) ;
}
//define higher function
//current display ram address add add
void add_add(uint ad)
{
uint temp ;
jm.offset+=ad ;
temp=jm.cur_page+jm.offset ;
wr_cmd3(temp,0x24) ;
}
//set addres s pointer
// had better not use this function
void set_add(uint ad)
{
jm.cur_page=ad ;
wr_cmd3(ad,0x24) ;
}
//move address pointer to y row and x column
void add_xy(uchar x,uchar y)
{
uint temp ;
temp=(uint)x+((uint)y)*jm.graphic_width ;
jm.offset=temp ;
temp=temp+jm.cur_page ;
wr_cmd3(temp,0x24) ;
}

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top