- //器件:LCM2401282ZK
- //控制器:T6963C
- //日期:2009.06.15
- #include
- #include "LCM2401282ZK.h"
- //============================控制线定义========================================
- #define WR1 BIT3 // WR1 = P5.3
- #define RD1 BIT2 // RD1 = P5.2
- #define CE BIT1 // CE = P5.1
- #define CD BIT6 // CD = P3.6
- #define FS BIT0 // FS = P5.0
- #define RST BIT7 // RST = P3.7 正常情况下,5个低电平信号实现复位,然后高电平进入正常工作状态
- #define WR1_0 P5OUT &= ~WR1
- #define WR1_1 P5OUT |= WR1
- #define RD1_0 P5OUT &= ~RD1
- #define RD1_1 P5OUT |= RD1
- #define CE_0 P5OUT &= ~CE
- #define CE_1 P5OUT |= CE
- #define CD_0 P3OUT &= ~CD
- #define CD_1 P3OUT |= CD
- #define FS_0 P5OUT &= ~FS
- #define FS_1 P5OUT |= FS
- #define RST_0 P3OUT &= ~RST
- #define RST_1 P3OUT |= RST // 控制线输出高、低电平定义
- unsigned char cradd1,cradd2,dat2,dat0,com,ctrlcode;
- unsigned char urow,ucol,h;
- unsigned char STA; //用于存放p4口的状态
- //==============================================================================
- // 定义的汉字字库
- //==============================================================================
- const unsigned char hztab_bzj[175][32]={
- };// 这里自己定义字库
- //==============================小延时==========================================
- void short_delay(unsigned int i)
- {
- unsigned int j;
- for(j=0;j<=i;j++);
- }
- //==============================延时ms级========================================
- void delayms(unsigned char tickms)
- {
- unsigned int count,i;
- _NOP();
- _NOP();
- _NOP();
- for(i=tickms;i>0;i--)
- { for(count=0;count<4000;count++) {;}
- _NOP();
- }
- }
- //==============================写显示数据子程序(左)==========================
- void wrdata(unsigned char dat0)
- {
- unsigned char cradd1;
- CD_1;
- CE_0;
- do
- {
- P4DIR=0xff; //P4口设置为输出
- P4OUT=0xff;
- RD1_0;
- P4DIR=0x00; //P4口设置为输入
- cradd1=P4IN;
- RD1_1;
- }
- while((cradd1&0x03)==0) ;
- P4DIR=0xff; //P4口设置为输出
- P4OUT=0xff;
- CD_0;
- P4OUT=dat0;
- WR1_0;
- WR1_1;
- }
- //==============================/* 写指令代码子程序(左)*/=====================
- void wrctrl0(unsigned char com)
- {
- unsigned char cradd1;
- CD_1;
- CE_0;
- do
- {
- P4DIR=0xff; //P4口设置为输出
- P4OUT=0xff;
- RD1_0;
- P4DIR=0x00; //P4口设置为输入
- cradd1=P4IN;
- RD1_1;
- }
- while((cradd1&0x03)==0);
- P4DIR=0xff; //P4口设置为输出
- P4OUT=0xff;
- CD_1;
- P4OUT=com;
- WR1_0;
- WR1_1;
- }
- void wrctrl(unsigned char dat1,unsigned char dat2,unsigned char ctrlcode)
- {
- wrdata(dat1);
- _NOP();
- _NOP();
- _NOP();
- wrdata(dat2);
- _NOP();
- _NOP();
- _NOP();
- wrctrl0(ctrlcode);
- }
- //==============================清屏============================================
- void clrc(void)
- { unsigned char i,j;
- wrctrl(0x00,0x00,0x24);
- wrctrl0(0xb0);
- for(i=0;i<64;i++)
- {
- for(j=0;j<30;j++)
- {
- wrdata(0x10);
- }
- }
- }
- //==============================清寄存器========================================
- void clrg(void)
- { unsigned char i,j;
- wrctrl(0x00,0x08,0x42);
- wrctrl(0x1e,0x00,0x43);
- wrctrl(0x00,0x08,0x24);
- wrctrl0(0xb0);
- for(i=0;i<128;i++)
- {
- for(j=0;j<30;j++)
- {
- wrdata(0x00);
- }
- }
- wrctrl0(0xb2);
- }
- //==============================================================================
- void allon(void)
- { unsigned char i,j;
- wrctrl(0x00,0x08,0x42);
- wrctrl(0x1e,0x00,0x43);
- wrctrl(0x00,0x08,0x24);
- wrctrl0(0xb0);
- for(i=0;i<64;i++)
- {
- for(j=0;j<30;j++)
- {
- wrdata(0xff);
- }
- }
- wrctrl0(0xb2);
- }
- //==============================================================================
- void stripe(void)
- { unsigned char i,j;
- wrctrl(0x00,0x08,0x42);
- wrctrl(0x1e,0x00,0x43);
- wrctrl(0x00,0x08,0x24);
- wrctrl0(0xb0);
- for(i=0;i<64;i++)
- {
- for(j=0;j<30;j++)
- {
- wrdata(0xaa);
- }
- }
- wrctrl0(0xb2);
- }
- //==============================================================================
- void stripe1(void)
- { unsigned char i,j;
- wrctrl(0x00,0x08,0x42);
- wrctrl(0x1e,0x00,0x43);
- wrctrl(0x00,0x08,0x24);
- wrctrl0(0xb0);
- for(i=0;i<64;i++)
- {
- for(j=0;j<30;j++)
- {
- wrdata(0x55);
- }
- }
- wrctrl0(0xb2);
- }
- //==============================/* 设置当前地址*/===============================
- void fnSetPos(unsigned char urow, unsigned char ucol)
- {
- unsigned int iPos;
- iPos = urow * 30 + ucol;
- wrctrl(iPos & 0xFF,iPos / 256+0x08,0x24);
- }
- //==============================/*写汉字子程序*/================================
- void wrhz_lcd(unsigned char h,unsigned char urow,unsigned char ucol)
- { unsigned char dat10;
- unsigned int i,j,k;
- wrctrl(0x00,0x08,0x42);
- wrctrl(0x1e,0x00,0x43);
- wrctrl(0x00,0x08,0x24);
- fnSetPos(urow, ucol);
- k=0;
- for(i=0;i<16;i++)
- { wrctrl0(0xb0);
- for(j=0;j<2;j++)
- {
- dat10= hztab_bzj[h][k];
- wrdata(dat10);
- k++;
- }
- wrctrl0(0xb2);
- fnSetPos(urow+(i+1), ucol);
- }
- }
- //2009.07.06 evening
- //==============================================================================
- //==============================读液晶状态字到STA===============================1
- //功能:用于往液晶写数据或命令之前判断忙闲
- void read_sta(void)
- {
- P4DIR=0x00; //P4数据输入,由液晶到430,不需要电平的转换,因为是3.3V的液晶模块
- CD_1; //命令方式
- RD1_0; //read
- STA=P4IN; //读回状态
- RD1_1; //cancel read
- P4DIR=0xff; //P4口设置为数据输出模式,由430到液晶
- }
- //==============================判断位S1,S0函数(读写指令和读写数据状态)=======2
- void ST1(void)
- {
- do
- {
- read_sta();
- }
- while((STA&0x03)!=0x03); //0000 0011---忙:1 闲:0
- }
- //==============================判断位S2函数(数据自动读状态)==================3
- void ST2(void)
- {
- do
- {
- read_sta();
- }
- while((STA&0x04)!=0x04); //0000 0100---忙:1 闲:0
- }
- //==============================判断位S3函数(数据自动写状态)==================4
- void ST3(void)
- {
- do
- {
- read_sta();
- }
- while((STA&0x08)!=0x08); //0000 1000---忙:1 闲:0
- }
- //==============================/*写入数据字节 */ ==============================5
- //one byte
- void write_data(unsigned char Byte )
- {
- ST1(); //读完状态后,设置为输出模式---这里是正确的
- P4OUT = Byte;
- CD_0; //数据方式
- WR1_0; // write
- WR1_1;
- }
- //==============================/*自动写入数据字节 */ ==========================6
- //不用判断状态标志位
- void autowrite_data(unsigned char Byte )
- {
- P4OUT = Byte;
- CD_0; //数据方式
- WR1_0; // write
- WR1_1;
- }
- //==============================/*写入命令字 */ ================================7
- void write_cmd(unsigned char cmd )
- {
- ST1();
- P4OUT = cmd ;
- CD_1; //命令方式
- WR1_0; // write
- WR1_1;
- }
- //==============================/*写只有一个参数的命令 */ =====================8
- void write_onepara(unsigned char dat1,unsigned char command )
- {
- write_data(dat1);
- write_cmd(command);
- }
- //==============================/*写含有二个参数的命令 */ =====================9
- void write_doublepara(unsigned char dat1,unsigned char dat2,unsigned char command )
- {
- write_data(dat1);
- write_data(dat2);
- write_cmd(command);
- }
- //==============================/*清显示缓冲的函数 */ ========================10
- void CLEAR_RAM( void )
- {
- int i;
- write_doublepara(0x00,0x00,0x24); /* 设置显示RAM 首地址 */
- //24H Address Pointer Set D1:Low ADRS D2:High ADRS
- //the address pointer se command is used to indicate the start address for writing(or reading) to external RAM.
- write_cmd(0xb0); /* 设置自动写方式 */
- //b0H Data auto write set
- //Note:Status check for auto mode(STA2,STA3)should be checked between each data.
- for(i=0;i<8192;i++) /* 清8K=8192存储器 */
- {
- ST3(); /* 判状态位S3 */
- write_data(0x00); /* 写入数据 */
- }
- write_cmd(0xb2); /* 设置自动写结束指令 */
- //b2H Auto reset
- // Auto reset should be performed after checking STA3=1(STA2=1).
- }
- //==============================/*对液晶屏的一些初始设置函数 */ ==============11
- void LcdIni( void )
- {
- write_doublepara(0x00,0x00,0x40); /*设文本显示区域首地址*/
- //40H Text home address set D1:Low address D2:High address
- //The starting address of external display RAM for Text display is defined by this command.
- //The text home address shows the left end and most upper position.
- write_doublepara(0x20,0x00,0x41); /*设文本显示区域宽度*/
- //显示区域宽度为32.。需要说明的是为了计算地址的方便,设置显示区域的宽度为20H
- //41H Text area set D1:Columns(列) D2:00H
- //---------------------------宽度根据实际情况来改变吧!!!2009.07.06
- /*
- ------------------------------------------------------------------------
- TH | | TH+CL
- ------------------------------------------------------------------------
- TH+TA | | TH+TA+CL
- ------------------------------------------------------------------------
- (TH+TA)+TA | | TH+2TA+CL
- ------------------------------------------------------------------------
- TH:Text home address
- TA:Text area number(columns)
- CL:Columns are fixed by hardware.(pin-programmable)
- */
- //write_doublepara(0x03,0x00,0x22); /* 设置CGRAM偏置地址 */
- //22H Function:Offset Register Set D1:Data D2:00H
- //The offset register is used to determine external character generator RAM area.
- //The upper 5bit(ad15-ad11)are determined by offset register.
- write_doublepara(0x00,0x08,0x42); /*设图形显示区域首地址*/
- //42H Graphic home address set D1:Low address D2:High address
- write_doublepara(0x20,0x00,0x43); /*设图形显示区域宽度*/
- //0x1e-ox20
- //43H Graphic area set D1:Columns D2:00H
- /*
- ------------------------------------------------------------------------
- GH | | GH+CL
- ------------------------------------------------------------------------
- GH+GA | | GH+GA+CL
- ------------------------------------------------------------------------
- (GH+GA)+GA | | GH+2GA+CL
- ------------------------------------------------------------------------
- GH:Graphic home address
- GA:Graphic area number(columns)
- CL:Columns are fixed by hardware.(pin-programmable)
- */
- write_cmd(0xa0); /*光标形状设置*/
- write_doublepara(0x00,0x00,0x21); /* 设置光标位置*/
- /*
- 1010 0000 | 1 line cursor
- 1010 0001 | 2 line cursor
- …… | ……
- 10100111 | 8 lines cursor,选择为最大的光标
- when cursor display is ON,this command selects the cursor pattern form 1
- line to 8 lines.the cursor address is defined by cursor pointer set command.
- */
- //write_doublepara(0x08,0x08,0x21); /* 设置光标 位置*/
- //21H Cursor Pointer Set X ADRS Y ADRS
- //X ADRS----=00H~4FH (Lower 7bits are valid)
- //Y ADRS----=00H~1FH (Lower 5bits are valid)
- //The cursor position is moved only by this command.
- write_cmd(0x80); /*显示方式设置逻辑"或"合成, 内部字符发生器有效*/
- /* Mode Set
- ------------------------------------------------------------------------
- Code | Function
- 1000x000 | "OR" Mode
- 1000x001 | "EXOR" Mode
- 1000x011 | "AND" Mode
- 1000x100 | "TEXT ATTRIBUTE" Mode
- 10000xxx | Internal Charater Generator Mode
- 10001xxx | External ……
- ------------------------------------------------------------------------
- When internal character generator mode is selected,character code 00
- -7FH are selected from built-in character generator ROM.The character code
- 80H-FFH are automatically selected external character generator RAM.
- */
- //write_cmd(0x9c); /*显示开关设置---开本和开图形显示*/
- write_cmd(0x9f);
- /* Display Set
- ------------------------------------------------------------------------
- Code | Function
- 10010000 | Display off
- 1001xx10 | Cursor on,blink off
- 1001xx11 | Cursor on,blink on
- 100101xx | Text on,graphic off
- 100110xx | Text off,graphic on
- 100111xx | Text on,graphic on
- ------------------------------------------------------------------------
- 1 0 0 1 d3 d2 d1 d0
- |d0:Cursor blink on:1,off:0
- |d1:Cursor display on:1,off:0
- |d2:Text display on:1,off:0
- |d3:Graphic display on:1,off:0
- Note:It is necessary to turn on "Text display" and "Graphic display" in
- following case.
- 1)Combination of text/graphic display;
- 2)Attribute function;
- */
- /*初始化完成后,开文本和图形显示。此时调整对比度电压,显示屏上将因显示存
- 储器上电时的随机数据而显示出随机的图形和字符。以此可以验证接口电路和驱动
- 程序的正确性。*/
- //CLEAR_RAM();
- }
- //==============================西文字符写入函数================================
- //x_asx:0~29; y_asc:0~15
- void write_asc(unsigned char x_asc,unsigned char y_asc,unsigned char code_asc)
- {
- unsigned int address;
- address=y_asc*32+x_asc;
- write_doublepara((unsigned char)(address),(unsigned char)(address>>8),0x24);
- //设置显示存储器地址
- write_onepara(code_asc,0xc4);
- //装入字符代码,写入数据,地址不变
- }
- //==============================汉字写入函数(正显)============================
- //x_hz:0-29; y_hz:0-127
- void write_hz1(unsigned char x_hz,unsigned char y_hz,unsigned char code_hz)
- {
- unsigned char i_hz;
- unsigned int address,addr_hz;
- address=y_hz*32+x_hz+0x0800; //计算显示存储器的地址
- //addr_hz=code_hz*32; //计算汉字字模地址(cctab的下标)由32改为16
- addr_hz=0;
- for(i_hz=0;i_hz<16;i_hz++) //计数值16
- {
- write_doublepara((unsigned char)(address),(unsigned char)(address>>8),0x24);
- //设置显示存储器地址
- write_onepara(hztab_bzj[code_hz][addr_hz],0xc0); //写入汉字字模左部
- write_onepara(hztab_bzj[code_hz][16+addr_hz++],0xc0);//写入汉字字模右部
- address+=32;//修改显示存储器地址,显示下一列(共16列)
- }
- }
- //==============================汉字写入函数(反显)============================
- void write_hz2(unsigned char x_hz,unsigned char y_hz,unsigned char code_hz)
- {
- unsigned char i_hz;
- unsigned int address,addr_hz;
- address=y_hz*32+x_hz+0x0800; //计算显示存储器的地址
- //addr_hz=code_hz*32; //计算汉字字模地址(cctab的下标)由32改为16
- addr_hz=0;
- for(i_hz=0;i_hz<16;i_hz++) //计数值16
- {
- write_doublepara((unsigned char)(address),(unsigned char)(address>>8),0x24);
- //设置显示存储器地址
- write_onepara((255-hztab_bzj[code_hz][addr_hz]),0xc0);//写入汉字字模左部
- write_onepara((255-hztab_bzj[code_hz][16+addr_hz++]),0xc0);//写入汉字字模右部
- address+=32;//修改显示存储器地址,显示下一列(共16列)
- }
- }
- //================================显示一个点函数================================
- //x:0-239; y:0-127(消除点) 128-255(显示点)
- void write_point(unsigned char x,unsigned char y)
- {
- unsigned char x_pt,y_pt;
- unsigned int address;
- x_pt=x;
- y_pt=y;
- address=(y_pt&0x7f)*32+x_pt/8+0x0800;//计算显示存储器地址
- write_doublepara((unsigned char)(address),(unsigned char)(address>>8),0x24);
- //设置显示存储器地址
- x_pt=(~(x_pt%8))&0x07;
- y_pt=((y_pt&0x80)>>4)|0xf0;
- write_cmd(x_pt|y_pt);//写入数据
- }
- //===================================显示矩形框=================================
- //x:0-239,y:0-127
- void rectangle(unsigned char xstar,unsigned char xend,unsigned char ystar,unsigned char yend)
- {
- unsigned char i;
- ystar+=128;
- yend+=128; //显示点
- for(i=xstar;i<=xend;i++)
- {
- write_point(i,ystar);
- write_point(i,yend);
- }
- for(i=ystar;i<=yend;i++)
- {
- write_point(xstar,i);
- write_point(xend,i);
- }
- }
- //========================================擦除矩形框============================
- void clr_rectangle(unsigned char xstar,unsigned char xend,unsigned char ystar,unsigned char yend)
- {
- unsigned char i;
- for(i=xstar;i<=xend;i++)
- {
- write_point(i,ystar);
- write_point(i,yend);
- }
- for(i=ystar;i<=yend;i++)
- {
- write_point(xstar,i);
- write_point(xend,i);
- }
- }
- //==============================================================================1400
|