1// ks0108.h
2// 代码参考其他代码后编写,受益非浅
3#ifndef __KS0108_h__
4#define __KS0108_h__
5
6#define uchar unsigned char
7#define uint unsigned int
8
9#define __MAPLCD128X64__
10
11#ifdef __MAPLCD128X64__
12#define selectChip_00() lcd_csa=0,lcd_csb=0;
13#define selectChip_01() lcd_csa=0,lcd_csb=1;
14#define selectChip_10() lcd_csa=1,lcd_csb=0;
15#define selectChip_11() lcd_csa=1,lcd_csb=1;
16#endif
17
18typedef enum{
19 chipLeft = 0,
20 chipRight = 1
21}bLcdChipSelect;
22
23typedef enum {
24 LCD_COMMAND = 0,
25 LCD_DATA = 1
26}bLcdSendType;
27
28typedef enum {
29 LCD_NO_BUSY = 0,
30 LCD_BUSY = 1
31}bLcdBusyFlag;
32
33typedef enum {
34 Chinese = 0,
35 OtherLetter = 1
36}bCharType;
37
38
39#define LCD_ON 0x3f
40#define LCD_OFF 0x3e
41
42// LCD_START_ROW | ROW (ROW = 0 ~ 63)
43#define LCD_START_ROW 0xc0
44#define MAX_ROW (64)
45
46// LCD_PAGE_SETUP | PAGE (PAGE = 0 ~ 7)
47#define LCD_PAGE_SETUP 0xB8
48#define MAX_PAGE (8)
49
50// Y_ADD_SETUP | add (add = 0 ~ 63)
51#define Y_ADD_SETUP 0x40
52#define MAX_COL (64)
53
54void clearLcd( void );
55void lcdDispBmp(uchar *pBmp);
56void lcdDispChinese( uchar pageStart,
57 uchar yStart,
58 uchar *pchar);
59
60#endif
61
62// ks0108.c
63#define uchar unsigned char
64#define uint unsigned int
65
66//=========================================
67#define lcdBus P2
68sbit lcd_csa = P0^0;
69sbit lcd_csb = P0^1;
70sbit lcd_rs = P0^2;
71sbit lcd_rw = P0^3;
72sbit lcd_e = P0^4;
73//=========================================
74
75static void waitBusy()
76{
77 lcd_rs = 0;
78 lcd_rw = 1;
79 lcdBus = 0xff;
80 lcd_e = 1;
81 while( lcdBus&0xff );
82 lcd_e = 0;
83}
84
85static void writeLcd( bLcdChipSelect chipSelect,
86 bLcdBusyFlag busy,
87 bLcdSendType style,
88 uchar input)
89{
90 if( !chipSelect )
91 selectChip_01()
92 else
93 selectChip_10()
94 if( busy )
95 waitBusy();
96 lcd_rs = style;
97 lcd_rw = 0;
98 lcdBus = input; _nop_();
99 lcd_e = 1; _nop_();
100 lcd_e = 0; _nop_();
101}
102
103static void initLcd( void )
104{
105 writeLcd(chipLeft,LCD_BUSY,LCD_COMMAND,LCD_START_ROW);
106 writeLcd(chipRight,LCD_BUSY,LCD_COMMAND,LCD_START_ROW);
107
108 writeLcd(chipLeft,LCD_BUSY,LCD_COMMAND,LCD_PAGE_SETUP);
109 writeLcd(chipRight,LCD_BUSY,LCD_COMMAND,LCD_PAGE_SETUP);
110
111 writeLcd(chipLeft,LCD_BUSY,LCD_COMMAND,Y_ADD_SETUP);
112 writeLcd(chipRight,LCD_BUSY,LCD_COMMAND,Y_ADD_SETUP);
113
114 writeLcd(chipLeft,LCD_BUSY,LCD_COMMAND,LCD_OFF);
115 writeLcd(chipRight,LCD_BUSY,LCD_COMMAND,LCD_OFF);
116}
117
118void clearLcd( void )
119{
120 uchar i,j;
121 initLcd();
122 for( i=0 ; i<MAX_PAGE ; i++ )
123 {
124 writeLcd(chipLeft,LCD_BUSY,LCD_COMMAND,LCD_PAGE_SETUP|i);
125 writeLcd(chipRight,LCD_BUSY,LCD_COMMAND,LCD_PAGE_SETUP|i);
126
127 writeLcd(chipLeft,LCD_BUSY,LCD_COMMAND,Y_ADD_SETUP|0x00);//AC add one automic
128 writeLcd(chipRight,LCD_BUSY,LCD_COMMAND,Y_ADD_SETUP|0x00);
129 for( j=0 ; j<MAX_COL ; j++ )
130 {
131 writeLcd(chipLeft,LCD_BUSY,LCD_DATA,0x00);
132 writeLcd(chipRight,LCD_BUSY,LCD_DATA,0x00);
133 }
134 }
135 writeLcd(chipLeft,LCD_BUSY,LCD_COMMAND,LCD_ON);
136 writeLcd(chipRight,LCD_BUSY,LCD_COMMAND,LCD_ON);
137}
138
139void lcdDispChinese( uchar pageStart,
140 uchar yStart,
141 uchar *pchar)
142{
143 uchar i,index=0;
144 writeLcd(yStart/4,LCD_BUSY,LCD_COMMAND,LCD_PAGE_SETUP|pageStart);
145 writeLcd(yStart/4,LCD_BUSY,LCD_COMMAND,Y_ADD_SETUP|((yStart&3)*16));
146 for( i=0 ; i<16 ; i++ )
147 writeLcd(yStart/4,LCD_BUSY,LCD_DATA,pchar);
148 writeLcd(yStart/4,LCD_BUSY,LCD_COMMAND,LCD_PAGE_SETUP|pageStart+1);
149 writeLcd(yStart/4,LCD_BUSY,LCD_COMMAND,Y_ADD_SETUP|((yStart&3)*16));
150 for( i=0 ; i<16 ; i++ )
151 writeLcd(yStart/4,LCD_BUSY,LCD_DATA,pchar[i+16]);
152}
153
154
155void lcdDispBmp(uchar *pBmp)
156{
157 uchar pageCnter,colCnter;
158 uint index=0;
159 for( pageCnter=0 ; pageCnter<MAX_PAGE ; pageCnter++ )
160 {
161 writeLcd(chipLeft,LCD_BUSY,LCD_COMMAND,LCD_PAGE_SETUP|pageCnter);
162 writeLcd(chipLeft,LCD_BUSY,LCD_COMMAND,Y_ADD_SETUP|0x00);
163 for( colCnter=0 ; colCnter<MAX_COL ; colCnter++ )
164 writeLcd(chipLeft,LCD_BUSY,LCD_DATA,pBmp[index++]);
165 writeLcd(chipRight,LCD_BUSY,LCD_COMMAND,LCD_PAGE_SETUP|pageCnter);
166 writeLcd(chipRight,LCD_BUSY,LCD_COMMAND,Y_ADD_SETUP|0x00);
167 for( colCnter=0 ; colCnter<MAX_COL ; colCnter++ )
168 writeLcd(chipRight,LCD_BUSY,LCD_DATA,pBmp[index++]);
169 }
170} 1//ST7920.h
2#define uchar unsigned char
3#define uint unsigned int
4
5#define lcdBus P2
6sbit lcd_rs = P0^7;
7sbit lcd_rw = P0^6;
8sbit lcd_e = P0^5;
9
10void checkBusy()
11{
12 lcd_rs = 0;
13 lcd_rw = 1;
14 lcd_e = 1;
15 lcdBus = 0xff;
16 while( (lcdBus&0x80)==0x80 );
17 lcd_e = 0;
18}
19
20void writeCommand(uchar com)
21{
22 checkBusy();
23 lcd_rs = 0;
24 lcd_rw = 0;
25 lcdBus = com;
26 lcd_e = 1;
27 delayus( 10 );
28 lcd_e = 0;
29 delayus( 10 );
30}
31
32void writeData(uchar dat)
33{
34 checkBusy();
35 lcd_rs = 1;
36 lcd_rw = 0;
37 lcdBus = dat;
38 lcd_e = 1;
39 delayus( 10 );
40 lcd_e = 0;
41 delayus( 10 );
42}
43
44void lcdReset()
45{
46 delayms( 40 );
47 writeCommand( 0x30 ); //0b0011x0xx 8-bit interface and basic instruction
48 delayus( 130 );
49 writeCommand( 0x30 ); //0b0011x0xx
50 delayus( 50 );
51 writeCommand( 0x0c ); //0b00001bcd display on/off control
52 delayus( 130 );
53 writeCommand( 0x01 ); //0b00000001 display clear
54 delayms( 12 );
55 writeCommand( 0x06 ); //0b000001(I/D)s entry mode set
56}
57
58void writeCGRAM(uchar addr,uchar *array) // 0<=addr<=3
59{
60 uchar i;
61 addr = addr<<4;
62 addr = addr|0x40;
63 writeCommand( 0x30 );
64 writeCommand( addr );
65 for( i=0 ; i<16 ; i++ )
66 {
67 writeData( array[i*2] );
68 writeData( array[i*2+1] );
69 }
70}
71
72void writeString(uchar line,uchar *str)
73{
74 uchar i;
75 switch( line )
76 {
77 case 0:
78 writeCommand(0x80);
79 break;
80 case 1:
81 writeCommand(0x90);
82 break;
83 case 2:
84 writeCommand(0x88);
85 break;
86 case 3:
87 writeCommand(0x98);
88 }
89 for( i=0 ; str ; i++ )
90 {
91 writeData(str);
92 delayus(100);
93 }
94}
95
96void writeBmp(uchar *img)
97{
98 uchar x,y,i;
99 uint j = 0;
100 for( i=0 ; i<9 ; i+=8 )
101 for( y=0 ; y<32 ; y++ )
102 for( x=0 ; x<8 ; x++ )
103 {
104 writeCommand(0x36);
105 writeCommand(y+0x80);
106 writeCommand(x+0x80+i);
107 writeCommand(0x30);
108 writeData(img[j++]);
109 writeData(img[j++]);
110 }
111} 代码部分参考其他人程序
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)
1// LCD1602.h
2#define uchar unsigned char
3#define uint unsigned int
4
5sbit rs=P0^7;
6sbit rw=P0^6;
7sbit e =P0^5;
8#define dt P2
9
10static void delayus(uchar n)
11{
12 n = n>>1;
13 while( --n );
14}
15
16static void delayms(uchar n)
17{
18 while( n-- )
19 {
20 delayus( 250 );
21 delayus( 250 );
22 delayus( 250 );
23 delayus( 250 );
24 }
25}
26
27static readBusy()
28{
29 rs = 0;
30 rw = 1;
31 dt = 0xff;
32 e = 1; _nop_();
33 while( dt&0x80 );
34 e = 0; _nop_();
35}
36
37static void writeCommand(uchar com)
38{
39 readBusy();
40 rs = 0;
41 rw = 0;
42 dt = com;
43 e = 0; _nop_();
44 e = 1; _nop_();
45 e = 0;
46}
47
48static void writeData(uchar dat)
49{
50 readBusy();
51 rs = 1;
52 rw = 0;
53 dt = dat;
54 e = 0; _nop_();
55 e = 1; _nop_();
56 e = 0;
57}
58
59void showChar(uchar pos,uchar c)
60{
61 if( pos>=0x10 )
62 pos = pos+0xb0;//second line
63 else
64 pos = pos+0x80;//first line
65 writeCommand(pos);
66 writeData(c);
67}
68
69void showString(uchar line,char *ptr)
70{
71 uchar i;
72 if( line )
73 writeCommand(0xc0);
74 else
75 writeCommand(0x80);
76 for( i=0 ; ptr ; i++ )
77 writeData(ptr);
78}
79
80void initLcd()
81{
82 delayms(15);
83 writeCommand(0x38);
84 delayms(5);
85 writeCommand(0x38);
86 delayms(5);
87 writeCommand(0x38);
88 delayms(5);
89 writeCommand(0x38); //显示模式设置
90 writeCommand(0x08); //显示关闭
91 writeCommand(0x01); //显示清屏
92 writeCommand(0x06); //显示光标移动设置
93 delayms(5);
94 writeCommand(0x0c); //显示光标及开关设置
95}
文章评论(0条评论)
登录后参与讨论