|
- //器件:LCM2401282ZK
- //控制器:T6963C
- //日期:2009.06.15
- #include <MSP430x14x.h>
- #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個(gè)低電平信號(hào)實(shí)現(xiàn)復(fù)位,然后高電平進(jìn)入正常工作狀態(tài)
- #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口的狀態(tài)
- //==============================================================================
- // 定義的漢字字庫(kù)
- //==============================================================================
- const unsigned char hztab_bzj[175][32]={
- };// 這里自己定義字庫(kù)
- //==============================小延時(shí)==========================================
- void short_delay(unsigned int i)
- {
- unsigned int j;
- for(j=0;j<=i;j++);
- }
- //==============================延時(shí)ms級(jí)========================================
- 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();
- }
- }
- //==============================寫顯示數(shù)據(jù)子程序(左)==========================
- void wrdata(unsigned char dat0)
- {
- unsigned char cradd1;
- CD_1;
- CE_0;
- do
- {
- P4DIR=0xff; //P4口設(shè)置為輸出
- P4OUT=0xff;
- RD1_0;
- P4DIR=0x00; //P4口設(shè)置為輸入
- cradd1=P4IN;
- RD1_1;
- }
- while((cradd1&0x03)==0) ;
- P4DIR=0xff; //P4口設(shè)置為輸出
- 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口設(shè)置為輸出
- P4OUT=0xff;
- RD1_0;
- P4DIR=0x00; //P4口設(shè)置為輸入
- cradd1=P4IN;
- RD1_1;
- }
- while((cradd1&0x03)==0);
- P4DIR=0xff; //P4口設(shè)置為輸出
- 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);
- }
- //==============================/* 設(shè)置當(dāng)前地址*/===============================
- 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
- //==============================================================================
- //==============================讀液晶狀態(tài)字到STA===============================1
- //功能:用于往液晶寫數(shù)據(jù)或命令之前判斷忙閑
- void read_sta(void)
- {
- P4DIR=0x00; //P4數(shù)據(jù)輸入,由液晶到430,不需要電平的轉(zhuǎn)換,因?yàn)槭?.3V的液晶模塊
- CD_1; //命令方式
- RD1_0; //read
- STA=P4IN; //讀回狀態(tài)
- RD1_1; //cancel read
- P4DIR=0xff; //P4口設(shè)置為數(shù)據(jù)輸出模式,由430到液晶
- }
- //==============================判斷位S1,S0函數(shù)(讀寫指令和讀寫數(shù)據(jù)狀態(tài))=======2
- void ST1(void)
- {
- do
- {
- read_sta();
- }
- while((STA&0x03)!=0x03); //0000 0011---忙:1 閑:0
- }
- //==============================判斷位S2函數(shù)(數(shù)據(jù)自動(dòng)讀狀態(tài))==================3
- void ST2(void)
- {
- do
- {
- read_sta();
- }
- while((STA&0x04)!=0x04); //0000 0100---忙:1 閑:0
- }
- //==============================判斷位S3函數(shù)(數(shù)據(jù)自動(dòng)寫狀態(tài))==================4
- void ST3(void)
- {
- do
- {
- read_sta();
- }
- while((STA&0x08)!=0x08); //0000 1000---忙:1 閑:0
- }
- //==============================/*寫入數(shù)據(jù)字節(jié) */ ==============================5
- //one byte
- void write_data(unsigned char Byte )
- {
- ST1(); //讀完?duì)顟B(tài)后,設(shè)置為輸出模式---這里是正確的
- P4OUT = Byte;
- CD_0; //數(shù)據(jù)方式
- WR1_0; // write
- WR1_1;
- }
- //==============================/*自動(dòng)寫入數(shù)據(jù)字節(jié) */ ==========================6
- //不用判斷狀態(tài)標(biāo)志位
- void autowrite_data(unsigned char Byte )
- {
- P4OUT = Byte;
- CD_0; //數(shù)據(jù)方式
- WR1_0; // write
- WR1_1;
- }
- //==============================/*寫入命令字 */ ================================7
- void write_cmd(unsigned char cmd )
- {
- ST1();
- P4OUT = cmd ;
- CD_1; //命令方式
- WR1_0; // write
- WR1_1;
- }
- //==============================/*寫只有一個(gè)參數(shù)的命令 */ =====================8
- void write_onepara(unsigned char dat1,unsigned char command )
- {
- write_data(dat1);
- write_cmd(command);
- }
- //==============================/*寫含有二個(gè)參數(shù)的命令 */ =====================9
- void write_doublepara(unsigned char dat1,unsigned char dat2,unsigned char command )
- {
- write_data(dat1);
- write_data(dat2);
- write_cmd(command);
- }
- //==============================/*清顯示緩沖的函數(shù) */ ========================10
- void CLEAR_RAM( void )
- {
- int i;
- write_doublepara(0x00,0x00,0x24); /* 設(shè)置顯示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); /* 設(shè)置自動(dòng)寫方式 */
-
- //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存儲(chǔ)器 */
- {
- ST3(); /* 判狀態(tài)位S3 */
- write_data(0x00); /* 寫入數(shù)據(jù) */
- }
- write_cmd(0xb2); /* 設(shè)置自動(dòng)寫結(jié)束指令 */
-
- //b2H Auto reset
- // Auto reset should be performed after checking STA3=1(STA2=1).
-
- }
- //==============================/*對(duì)液晶屏的一些初始設(shè)置函數(shù) */ ==============11
- void LcdIni( void )
- {
- write_doublepara(0x00,0x00,0x40); /*設(shè)文本顯示區(qū)域首地址*/
-
- //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); /*設(shè)文本顯示區(qū)域?qū)挾?/
-
- //顯示區(qū)域?qū)挾葹?2.。需要說(shuō)明的是為了計(jì)算地址的方便,設(shè)置顯示區(qū)域的寬度為20H
- //41H Text area set D1:Columns(列) D2:00H
- //---------------------------寬度根據(jù)實(shí)際情況來(lái)改變吧�。。�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); /* 設(shè)置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); /*設(shè)圖形顯示區(qū)域首地址*/
-
- //42H Graphic home address set D1:Low address D2:High address
- write_doublepara(0x20,0x00,0x43); /*設(shè)圖形顯示區(qū)域?qū)挾?/
-
- //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); /*光標(biāo)形狀設(shè)置*/
- write_doublepara(0x00,0x00,0x21); /* 設(shè)置光標(biāo)位置*/
-
- /*
- 1010 0000 | 1 line cursor
- 1010 0001 | 2 line cursor
- …… | ……
- 10100111 | 8 lines cursor,選擇為最大的光標(biāo)
-
- 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); /* 設(shè)置光標(biāo) 位置*/
-
- //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); /*顯示方式設(shè)置邏輯"或"合成, 內(nèi)部字符發(fā)生器有效*/
-
- /* 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); /*顯示開關(guān)設(shè)置---開本和開圖形顯示*/
- 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;
- */
-
- /*初始化完成后,開文本和圖形顯示。此時(shí)調(diào)整對(duì)比度電壓,顯示屏上將因顯示存
- 儲(chǔ)器上電時(shí)的隨機(jī)數(shù)據(jù)而顯示出隨機(jī)的圖形和字符。以此可以驗(yàn)證接口電路和驅(qū)動(dòng)
- 程序的正確性。*/
- //CLEAR_RAM();
- }
- //==============================西文字符寫入函數(shù)================================
- //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);
- //設(shè)置顯示存儲(chǔ)器地址
- write_onepara(code_asc,0xc4);
- //裝入字符代碼,寫入數(shù)據(jù),地址不變
- }
- //==============================漢字寫入函數(shù)(正顯)============================
- //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; //計(jì)算顯示存儲(chǔ)器的地址
- //addr_hz=code_hz*32; //計(jì)算漢字字模地址(cctab的下標(biāo))由32改為16
- addr_hz=0;
- for(i_hz=0;i_hz<16;i_hz++) //計(jì)數(shù)值16
- {
- write_doublepara((unsigned char)(address),(unsigned char)(address>>8),0x24);
- //設(shè)置顯示存儲(chǔ)器地址
- write_onepara(hztab_bzj[code_hz][addr_hz],0xc0); //寫入漢字字模左部
- write_onepara(hztab_bzj[code_hz][16+addr_hz++],0xc0);//寫入漢字字模右部
- address+=32;//修改顯示存儲(chǔ)器地址,顯示下一列(共16列)
- }
- }
- //==============================漢字寫入函數(shù)(反顯)============================
- 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; //計(jì)算顯示存儲(chǔ)器的地址
- //addr_hz=code_hz*32; //計(jì)算漢字字模地址(cctab的下標(biāo))由32改為16
- addr_hz=0;
- for(i_hz=0;i_hz<16;i_hz++) //計(jì)數(shù)值16
- {
- write_doublepara((unsigned char)(address),(unsigned char)(address>>8),0x24);
- //設(shè)置顯示存儲(chǔ)器地址
- write_onepara((255-hztab_bzj[code_hz][addr_hz]),0xc0);//寫入漢字字模左部
- write_onepara((255-hztab_bzj[code_hz][16+addr_hz++]),0xc0);//寫入漢字字模右部
- address+=32;//修改顯示存儲(chǔ)器地址,顯示下一列(共16列)
- }
- }
- //================================顯示一個(gè)點(diǎn)函數(shù)================================
- //x:0-239; y:0-127(消除點(diǎn)) 128-255(顯示點(diǎn))
- 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;//計(jì)算顯示存儲(chǔ)器地址
- write_doublepara((unsigned char)(address),(unsigned char)(address>>8),0x24);
- //設(shè)置顯示存儲(chǔ)器地址
- x_pt=(~(x_pt%8))&0x07;
- y_pt=((y_pt&0x80)>>4)|0xf0;
- write_cmd(x_pt|y_pt);//寫入數(shù)據(jù)
- }
- //===================================顯示矩形框=================================
- //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; //顯示點(diǎn)
- 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
復(fù)制代碼 |
|