原创 1602驱动

2008-10-3 19:34 2365 5 5 分类: MCU/ 嵌入式
1None.gif//  ks0108.h
  2None.gif//  代码参考其他代码后编写,受益非浅
  3None.gif#ifndef __KS0108_h__
  4None.gif#define __KS0108_h__
  5None.gif
  6None.gif#define uchar unsigned char
  7None.gif#define uint  unsigned int
  8None.gif
  9None.gif#define __MAPLCD128X64__
 10None.gif
 11None.gif#ifdef __MAPLCD128X64__
 12None.gif#define selectChip_00()   lcd_csa=0,lcd_csb=0;
 13None.gif#define selectChip_01()   lcd_csa=0,lcd_csb=1;
 14None.gif#define selectChip_10()   lcd_csa=1,lcd_csb=0;
 15None.gif#define selectChip_11()   lcd_csa=1,lcd_csb=1;
 16None.gif#endif
 17None.gif
 18ExpandedBlockStart.gifContractedBlock.giftypedef enumdot.gif{
 19InBlock.gif    chipLeft  = 0,
 20InBlock.gif    chipRight = 1
 21ExpandedBlockEnd.gif}
bLcdChipSelect;
 22None.gif
 23ExpandedBlockStart.gifContractedBlock.giftypedef enum dot.gif{
 24InBlock.gif    LCD_COMMAND = 0,
 25InBlock.gif    LCD_DATA    = 1
 26ExpandedBlockEnd.gif}
bLcdSendType;
 27None.gif
 28ExpandedBlockStart.gifContractedBlock.giftypedef enum dot.gif{
 29InBlock.gif    LCD_NO_BUSY = 0,
 30InBlock.gif    LCD_BUSY    = 1
 31ExpandedBlockEnd.gif}
bLcdBusyFlag;
 32None.gif
 33ExpandedBlockStart.gifContractedBlock.giftypedef enum dot.gif{
 34InBlock.gif    Chinese     = 0,
 35InBlock.gif    OtherLetter = 1
 36ExpandedBlockEnd.gif}
bCharType;
 37None.gif
 38None.gif
 39None.gif#define LCD_ON  0x3f
 40None.gif#define LCD_OFF 0x3e
 41None.gif
 42None.gif// LCD_START_ROW | ROW (ROW = 0 ~ 63)
 43None.gif#define LCD_START_ROW 0xc0
 44None.gif#define MAX_ROW (64)
 45None.gif
 46None.gif// LCD_PAGE_SETUP | PAGE (PAGE = 0 ~ 7)
 47None.gif#define LCD_PAGE_SETUP 0xB8
 48None.gif#define MAX_PAGE (8)
 49None.gif
 50None.gif// Y_ADD_SETUP | add (add = 0 ~ 63)
 51None.gif#define Y_ADD_SETUP 0x40
 52None.gif#define MAX_COL (64)
 53None.gif
 54None.gifvoid clearLcd( void );
 55None.gifvoid lcdDispBmp(uchar *pBmp);
 56None.gifvoid lcdDispChinese( uchar pageStart,
 57None.gif                     uchar yStart,
 58None.gif                     uchar *pchar);
 59None.gif
 60None.gif#endif
 61None.gif
 62None.gif// ks0108.c
 63None.gif#define uchar unsigned char
 64None.gif#define uint  unsigned int
 65None.gif
 66None.gif//=========================================
 67None.gif#define lcdBus P2
 68None.gifsbit    lcd_csa = P0^0;
 69None.gifsbit    lcd_csb = P0^1;
 70None.gifsbit    lcd_rs  = P0^2;
 71None.gifsbit    lcd_rw  = P0^3;
 72None.gifsbit    lcd_e   = P0^4;
 73None.gif//=========================================
 74None.gif
 75None.gifstatic void waitBusy()
 76ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 77InBlock.gif    lcd_rs = 0;
 78InBlock.gif    lcd_rw = 1;
 79InBlock.gif    lcdBus = 0xff;
 80InBlock.gif    lcd_e = 1;
 81InBlock.gif    while( lcdBus&0xff );
 82InBlock.gif    lcd_e = 0;
 83ExpandedBlockEnd.gif}

 84None.gif
 85None.gifstatic void writeLcd( bLcdChipSelect chipSelect,
 86None.gif                      bLcdBusyFlag busy,
 87None.gif                      bLcdSendType style,
 88None.gif                      uchar input)
 89ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 90InBlock.gif    if!chipSelect )
 91InBlock.gif        selectChip_01()
 92InBlock.gif    else
 93InBlock.gif        selectChip_10()
 94InBlock.gif    if( busy )
 95InBlock.gif        waitBusy();
 96InBlock.gif    lcd_rs = style;
 97InBlock.gif    lcd_rw = 0;
 98InBlock.gif    lcdBus = input; _nop_();
 99InBlock.gif    lcd_e = 1; _nop_();
100InBlock.gif    lcd_e = 0; _nop_();
101ExpandedBlockEnd.gif}

102None.gif
103None.gifstatic void initLcd( void )
104ExpandedBlockStart.gifContractedBlock.gifdot.gif{
105InBlock.gif    writeLcd(chipLeft,LCD_BUSY,LCD_COMMAND,LCD_START_ROW);
106InBlock.gif    writeLcd(chipRight,LCD_BUSY,LCD_COMMAND,LCD_START_ROW);
107InBlock.gif
108InBlock.gif    writeLcd(chipLeft,LCD_BUSY,LCD_COMMAND,LCD_PAGE_SETUP);
109InBlock.gif    writeLcd(chipRight,LCD_BUSY,LCD_COMMAND,LCD_PAGE_SETUP);
110InBlock.gif
111InBlock.gif    writeLcd(chipLeft,LCD_BUSY,LCD_COMMAND,Y_ADD_SETUP);
112InBlock.gif    writeLcd(chipRight,LCD_BUSY,LCD_COMMAND,Y_ADD_SETUP);
113InBlock.gif
114InBlock.gif    writeLcd(chipLeft,LCD_BUSY,LCD_COMMAND,LCD_OFF);
115InBlock.gif    writeLcd(chipRight,LCD_BUSY,LCD_COMMAND,LCD_OFF);
116ExpandedBlockEnd.gif}

117None.gif
118None.gifvoid clearLcd( void )
119ExpandedBlockStart.gifContractedBlock.gifdot.gif{
120InBlock.gif    uchar i,j;
121InBlock.gif    initLcd();
122InBlock.gif    for( i=0 ; i<MAX_PAGE ; i++ )
123ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
124InBlock.gif        writeLcd(chipLeft,LCD_BUSY,LCD_COMMAND,LCD_PAGE_SETUP|i);
125InBlock.gif        writeLcd(chipRight,LCD_BUSY,LCD_COMMAND,LCD_PAGE_SETUP|i);
126InBlock.gif
127InBlock.gif        writeLcd(chipLeft,LCD_BUSY,LCD_COMMAND,Y_ADD_SETUP|0x00);//AC add one automic
128InBlock.gif        writeLcd(chipRight,LCD_BUSY,LCD_COMMAND,Y_ADD_SETUP|0x00);
129InBlock.gif        for( j=0 ; j<MAX_COL ; j++ )
130ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
131InBlock.gif            writeLcd(chipLeft,LCD_BUSY,LCD_DATA,0x00);
132InBlock.gif            writeLcd(chipRight,LCD_BUSY,LCD_DATA,0x00);
133ExpandedSubBlockEnd.gif        }

134ExpandedSubBlockEnd.gif    }

135InBlock.gif    writeLcd(chipLeft,LCD_BUSY,LCD_COMMAND,LCD_ON);
136InBlock.gif    writeLcd(chipRight,LCD_BUSY,LCD_COMMAND,LCD_ON);
137ExpandedBlockEnd.gif}

138None.gif
139None.gifvoid lcdDispChinese( uchar pageStart,
140None.gif                     uchar yStart,
141None.gif                     uchar *pchar)
142ExpandedBlockStart.gifContractedBlock.gifdot.gif{
143InBlock.gif    uchar i,index=0;
144InBlock.gif    writeLcd(yStart/4,LCD_BUSY,LCD_COMMAND,LCD_PAGE_SETUP|pageStart);
145InBlock.gif    writeLcd(yStart/4,LCD_BUSY,LCD_COMMAND,Y_ADD_SETUP|((yStart&3)*16));
146InBlock.gif    for( i=0 ; i<16 ; i++ )
147InBlock.gif        writeLcd(yStart/4,LCD_BUSY,LCD_DATA,pchar);
148InBlock.gif    writeLcd(yStart/4,LCD_BUSY,LCD_COMMAND,LCD_PAGE_SETUP|pageStart+1);
149InBlock.gif    writeLcd(yStart/4,LCD_BUSY,LCD_COMMAND,Y_ADD_SETUP|((yStart&3)*16));
150InBlock.gif    for( i=0 ; i<16 ; i++ )
151InBlock.gif        writeLcd(yStart/4,LCD_BUSY,LCD_DATA,pchar[i+16]);
152ExpandedBlockEnd.gif}

153None.gif
154None.gif
155None.gifvoid lcdDispBmp(uchar *pBmp)
156ExpandedBlockStart.gifContractedBlock.gifdot.gif{
157InBlock.gif    uchar pageCnter,colCnter;
158InBlock.gif    uint index=0;
159InBlock.gif    for( pageCnter=0 ; pageCnter<MAX_PAGE ; pageCnter++ )
160ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
161InBlock.gif        writeLcd(chipLeft,LCD_BUSY,LCD_COMMAND,LCD_PAGE_SETUP|pageCnter);
162InBlock.gif        writeLcd(chipLeft,LCD_BUSY,LCD_COMMAND,Y_ADD_SETUP|0x00);
163InBlock.gif        for( colCnter=0 ; colCnter<MAX_COL ; colCnter++ )
164InBlock.gif            writeLcd(chipLeft,LCD_BUSY,LCD_DATA,pBmp[index++]);
165InBlock.gif        writeLcd(chipRight,LCD_BUSY,LCD_COMMAND,LCD_PAGE_SETUP|pageCnter);
166InBlock.gif        writeLcd(chipRight,LCD_BUSY,LCD_COMMAND,Y_ADD_SETUP|0x00);
167InBlock.gif        for( colCnter=0 ; colCnter<MAX_COL ; colCnter++ )
168InBlock.gif            writeLcd(chipRight,LCD_BUSY,LCD_DATA,pBmp[index++]);
169ExpandedSubBlockEnd.gif    }

170ExpandedBlockEnd.gif}

  1None.gif//ST7920.h
  2None.gif#define uchar unsigned char
  3None.gif#define uint  unsigned int
  4None.gif
  5None.gif#define lcdBus P2
  6None.gifsbit lcd_rs = P0^7;
  7None.gifsbit lcd_rw = P0^6;
  8None.gifsbit lcd_e  = P0^5;
  9None.gif
 10None.gifvoid checkBusy()
 11ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 12InBlock.gif    lcd_rs = 0;
 13InBlock.gif    lcd_rw = 1;
 14InBlock.gif    lcd_e = 1;
 15InBlock.gif    lcdBus = 0xff;
 16InBlock.gif    while( (lcdBus&0x80)==0x80 );
 17InBlock.gif    lcd_e = 0;
 18ExpandedBlockEnd.gif}

 19None.gif
 20None.gifvoid writeCommand(uchar com)
 21ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 22InBlock.gif    checkBusy();
 23InBlock.gif    lcd_rs = 0;
 24InBlock.gif    lcd_rw = 0;
 25InBlock.gif    lcdBus = com;
 26InBlock.gif    lcd_e = 1;
 27InBlock.gif    delayus( 10 );
 28InBlock.gif    lcd_e = 0;
 29InBlock.gif    delayus( 10 );
 30ExpandedBlockEnd.gif}

 31None.gif
 32None.gifvoid writeData(uchar dat)
 33ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 34InBlock.gif    checkBusy();
 35InBlock.gif    lcd_rs = 1;
 36InBlock.gif    lcd_rw = 0;
 37InBlock.gif    lcdBus = dat;
 38InBlock.gif    lcd_e = 1;
 39InBlock.gif    delayus( 10 );
 40InBlock.gif    lcd_e = 0;
 41InBlock.gif    delayus( 10 );
 42ExpandedBlockEnd.gif}

 43None.gif
 44None.gifvoid lcdReset()
 45ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 46InBlock.gif    delayms( 40 );
 47InBlock.gif    writeCommand( 0x30 ); //0b0011x0xx 8-bit interface and basic instruction
 48InBlock.gif    delayus( 130 );
 49InBlock.gif    writeCommand( 0x30 ); //0b0011x0xx
 50InBlock.gif    delayus( 50 );
 51InBlock.gif    writeCommand( 0x0c ); //0b00001bcd display on/off control
 52InBlock.gif    delayus( 130 );
 53InBlock.gif    writeCommand( 0x01 ); //0b00000001 display clear
 54InBlock.gif    delayms( 12 );
 55InBlock.gif    writeCommand( 0x06 ); //0b000001(I/D)s  entry mode set
 56ExpandedBlockEnd.gif}

 57None.gif
 58None.gifvoid writeCGRAM(uchar addr,uchar *array) // 0<=addr<=3
 59ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 60InBlock.gif    uchar i;
 61InBlock.gif    addr = addr<<4;
 62InBlock.gif    addr = addr|0x40;
 63InBlock.gif    writeCommand( 0x30 );
 64InBlock.gif    writeCommand( addr );
 65InBlock.gif    for( i=0 ; i<16 ; i++ )
 66ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 67InBlock.gif        writeData( array[i*2] );
 68InBlock.gif        writeData( array[i*2+1] );
 69ExpandedSubBlockEnd.gif    }

 70ExpandedBlockEnd.gif}

 71None.gif
 72None.gifvoid writeString(uchar line,uchar *str)
 73ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 74InBlock.gif    uchar i;
 75InBlock.gif    switch( line )
 76ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 77InBlock.gif        case 0:
 78InBlock.gif            writeCommand(0x80);
 79InBlock.gif            break;
 80InBlock.gif        case 1:
 81InBlock.gif            writeCommand(0x90);
 82InBlock.gif            break;
 83InBlock.gif        case 2:
 84InBlock.gif            writeCommand(0x88);
 85InBlock.gif            break;
 86InBlock.gif        case 3:
 87InBlock.gif            writeCommand(0x98);
 88ExpandedSubBlockEnd.gif    }

 89InBlock.gif    for( i=0 ; str ; i++ )
 90ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 91InBlock.gif        writeData(str);
 92InBlock.gif        delayus(100);
 93ExpandedSubBlockEnd.gif    }

 94ExpandedBlockEnd.gif}

 95None.gif
 96None.gifvoid writeBmp(uchar *img)
 97ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 98InBlock.gif    uchar x,y,i;
 99InBlock.gif    uint j = 0;
100InBlock.gif    for( i=0 ; i<9 ; i+=8 )
101InBlock.gif    for( y=0 ; y<32 ; y++ )
102InBlock.gif    for( x=0 ; x<8 ; x++ )
103ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
104InBlock.gif        writeCommand(0x36);
105InBlock.gif        writeCommand(y+0x80);
106InBlock.gif        writeCommand(x+0x80+i);
107InBlock.gif        writeCommand(0x30);
108InBlock.gif        writeData(img[j++]);
109InBlock.gif        writeData(img[j++]);
110ExpandedSubBlockEnd.gif    }

111ExpandedBlockEnd.gif}
代码部分参考其他人程序
LCD部分指令说明
显示模式:
        指令码:00111000
        功    能:设置16*2显示,5*7点阵,8位数据接口
显示开/关及光标设置
        指令码:00001DCB
        功    能:D=1,开显示,D=0,关显示
                        C="1",显示光标,C=0,不显示光标
                        B="1",光标闪烁,B=0,光标不闪烁
        指令码:000001NS
        功    能:N=1,当读或写一个字符后,地址指针加一,光标加一;N=0,当读或写一个字符后,地址指针减一,光标减一
                        S="1",当读或写一个字符后,N=1,整屏显示左移一,N=0,整屏右移;S=0,不移动
        数据指针设置:80H+地址码(0-27h,40-67h)

 1None.gif//   LCD1602.h
 2None.gif#define uchar unsigned char
 3None.gif#define uint unsigned int
 4None.gif
 5None.gifsbit rs=P0^7;
 6None.gifsbit rw=P0^6;
 7None.gifsbit e =P0^5;
 8None.gif#define dt P2
 9None.gif
10None.gifstatic void delayus(uchar n)
11ExpandedBlockStart.gifContractedBlock.gifdot.gif{
12InBlock.gif    n = n>>1;
13InBlock.gif    while--n );
14ExpandedBlockEnd.gif}

15None.gif
16None.gifstatic void delayms(uchar n)
17ExpandedBlockStart.gifContractedBlock.gifdot.gif{
18InBlock.gif    while( n-- )
19ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
20InBlock.gif        delayus( 250 );
21InBlock.gif        delayus( 250 );
22InBlock.gif        delayus( 250 );
23InBlock.gif        delayus( 250 );
24ExpandedSubBlockEnd.gif    }

25ExpandedBlockEnd.gif}

26None.gif
27None.gifstatic readBusy()
28ExpandedBlockStart.gifContractedBlock.gifdot.gif{
29InBlock.gif    rs = 0;
30InBlock.gif    rw = 1;
31InBlock.gif    dt = 0xff;
32InBlock.gif    e = 1; _nop_();
33InBlock.gif    while( dt&0x80 );
34InBlock.gif    e = 0; _nop_();
35ExpandedBlockEnd.gif}

36None.gif
37None.gifstatic void writeCommand(uchar com)
38ExpandedBlockStart.gifContractedBlock.gifdot.gif{
39InBlock.gif    readBusy();
40InBlock.gif    rs = 0;
41InBlock.gif    rw = 0;
42InBlock.gif    dt = com;
43InBlock.gif    e = 0; _nop_();
44InBlock.gif    e = 1; _nop_();
45InBlock.gif    e = 0;
46ExpandedBlockEnd.gif}

47None.gif
48None.gifstatic void writeData(uchar dat)
49ExpandedBlockStart.gifContractedBlock.gifdot.gif{
50InBlock.gif    readBusy();
51InBlock.gif    rs = 1;
52InBlock.gif    rw = 0;
53InBlock.gif    dt = dat;
54InBlock.gif    e = 0; _nop_();
55InBlock.gif    e = 1; _nop_();
56InBlock.gif    e = 0;
57ExpandedBlockEnd.gif}

58None.gif
59None.gifvoid showChar(uchar pos,uchar c)
60ExpandedBlockStart.gifContractedBlock.gifdot.gif{
61InBlock.gif    if( pos>=0x10 )
62InBlock.gif        pos = pos+0xb0;//second line
63InBlock.gif    else
64InBlock.gif        pos = pos+0x80;//first line
65InBlock.gif    writeCommand(pos);
66InBlock.gif    writeData(c);
67ExpandedBlockEnd.gif}

68None.gif
69None.gifvoid showString(uchar line,char *ptr)
70ExpandedBlockStart.gifContractedBlock.gifdot.gif{
71InBlock.gif    uchar i;
72InBlock.gif    if( line )
73InBlock.gif        writeCommand(0xc0);
74InBlock.gif    else
75InBlock.gif        writeCommand(0x80);
76InBlock.gif    for( i=0 ; ptr ; i++ )
77InBlock.gif        writeData(ptr);
78ExpandedBlockEnd.gif}

79None.gif
80None.gifvoid initLcd()
81ExpandedBlockStart.gifContractedBlock.gifdot.gif{
82InBlock.gif    delayms(15);
83InBlock.gif    writeCommand(0x38);
84InBlock.gif    delayms(5);
85InBlock.gif    writeCommand(0x38);
86InBlock.gif    delayms(5);
87InBlock.gif    writeCommand(0x38);
88InBlock.gif    delayms(5);
89InBlock.gif    writeCommand(0x38);  //显示模式设置
90InBlock.gif    writeCommand(0x08);  //显示关闭
91InBlock.gif    writeCommand(0x01);  //显示清屏
92InBlock.gif    writeCommand(0x06);  //显示光标移动设置
93InBlock.gif    delayms(5);
94InBlock.gif    writeCommand(0x0c);  //显示光标及开关设置
95ExpandedBlockEnd.gif}
PARTNER CONTENT

文章评论0条评论)

登录后参与讨论
EE直播间
更多
我要评论
0
5
关闭 站长推荐上一条 /3 下一条