- UID
- 824598
|
//lcd1602 drive program电子园51单片机学习网 ` L^)WWj^
//for 51 mcu电子园51单片机学习网e(?%k'b0[ \8f2h\i b
//designed by zhaoliang电子园51单片机学习网&i5zx%R3F l
//2005-6-14 20:16
-Y*A;Nw3f2?Bw E40425#i nclude <reg51.hJ>
Jw l0rQ2b;@fI40425/********************************************************************/
V2@(A+iLq E7uxBu40425//lcd part
wKB+m&Lb`q o40425#define LINE1 0
-Ag `?(_NK)VAj40425#define LINE2 1
[0v#{/el.f(gJ40425#define LINE1_HEAD 0x80
K]FM[d40425#define LINE2_HEAD 0xC0电子园51单片机学习网m'_ID3E[
#define LCD_DELAY_TIME 40
6R2la)K x40425#define DATA_MODE 0x38电子园51单片机学习网Hj'E@x@3|e0D_
#define OPEN_SCREEN 0x0C
%aB vn[xOi0V40425#define DISPLAY_ADDRESS 0x80
RCBh1m2h/T){40425#define CLEARSCREEN LCD_en_command(0x01)电子园51单片机学习网_["CT1c;s P
//common part
G:T4OKzi40425#define HIGH 1电子园51单片机学习网/Y i8MR(~ K9?
#define LOW 0电子园51单片机学习网5D c^'n~A
#define TRUE 1电子园51单片机学习网UlkW3Gr/Jg&j a
#define FALSE 0
{)\$g"t2iP"l Q40425#define ZERO 0
P5l3~O'g#w40425#define MSB 0x80电子园51单片机学习网n-u"qLvDAnbw.Iz!_
#define LSB 0x01电子园51单片机学习网9_3i*c }W{G?.d
jS o1| _-z40425/*******************************************************************/电子园51单片机学习网H!`,X oQ-_
//change this part at different board
;x3xV$\F]c4~`40425#define LCDIO P2
x fayd40425sbit LCD1602_RS=P0^7; //data command select 1 data 0 command pin 4电子园51单片机学习网3RkP}L
sbit LCD1602_RW=P0^6; //read write select 1 read 0 write pin 5电子园51单片机学习网Z(o9E0]+@2N
sbit LCD1602_EN=P0^5; //LCD enable signal pin 6
P-U0qh.xIz40425
"j,|H9t+Eam9dh40425/********************************************************************/
5SW]+fD@sl7U40425void LCD_delay(void);//lcd delay function电子园51单片机学习网 Ra t,C H@/c
void LCD_en_command(unsigned char command);//write command function
$Oz$[ Y*gxY40425void LCD_en_dat(unsigned char temp);//write data function
p(SX TI.rc6_V40425void LCD_set_xy( unsigned char x, unsigned char y );//set display address function
U$?(o [.a[ c ]k.T40425void LCD_write_char( unsigned x,unsigned char y,unsigned char dat);//write lcd a character function
~-D5H8xdSI3v40425void LCD_write_string(unsigned char X,unsigned char Y,unsigned char *s);//write lcd string function电子园51单片机学习网.hv%vX} _
void LCD_init(void);//lcd initize function电子园51单片机学习网 E|:U)mA4h3y
RK&A F/W#a$fc40425/********************************************************************/
0u p/D p2xT40425void delay_nms(unsigned int n);//delay function
eG'I N}O/y7g40425电子园51单片机学习网P}3Zd7{0C9Dd,R
/********************************************************************/电子园51单片机学习网0Dd!?fUr OK9X
void main(void)电子园51单片机学习网&[0mo Bg!z8vF
{
"A2NOv@k40425 LCD_init();电子园51单片机学习网HA9hLg*l8o&M@"I$F
while(TRUE ) 电子园51单片机学习网v.dC%qb-l?7oy
{
V(E8Q%I3sOI3G40425 CLEARSCREEN;电子园51单片机学习网S)v l2dQo@l Rz8e*~ f
电子园51单片机学习网4`"y e)_;sCqG
delay_nms(2);电子园51单片机学习网bC8]gSB
LCD_write_string(0,LINE1," LCD TEST ");
)vm']eB40425 LCD_write_string(0,LINE2," SUCCESSFUL ");
1^'i7Z1b?'s!Sb40425
6k3]Gt ?T0[L5A3Xs40425 delay_nms(200);
l g_6]8_7x40425
6s k%Rnjz40425 CLEARSCREEN;电子园51单片机学习网}t4] ]!Z4V:f.J
电子园51单片机学习网2Zy p5gv9@6b4`
delay_nms(2);
@%P1hVPI40425 LCD_write_string(0,LINE1," lcd test ");电子园51单片机学习网'{4B Q$@JZI]J
LCD_write_string(0,LINE2," successful ");
}z(x5ix8T`8_Vm40425
2D'@.~[*l-kX'}e3Hs40425 delay_nms(200); 电子园51单片机学习网*u%g^r p9A
}电子园51单片机学习网 Molamo"])p&@C
}电子园51单片机学习网a.zir~#rsyt
/********************************************************************/
lQJ1C[40425/******************** LCD PART *************************************/
!tt(~po_;]sai40425void LCD_delay(void) 电子园51单片机学习网s;te/U|@"V
{
dt3B5_,S40425 unsigned char i;电子园51单片机学习网 loGn{~ ] s
for(i=LCD_DELAY_TIME;i>ZERO;i--)//be sure lcd reset
H*A IHM0@.{8O$p40425 ;
@,o)iM)?iGX40425}
~h8Pa z(u40425/********************************************************************/ 电子园51单片机学习网;mo$c9Fk&G,z,|z)S
void LCD_en_command(unsigned char command)
zO*uGZG40425{
SeJb#r T'sUt i\40425 LCDIO=command;电子园51单片机学习网-v8T)ehyLY
LCD1602_RS=LOW;
:qS,\a2^"y|5JWg40425 LCD1602_RW=LOW;
/i.@0{ \Oddop40425 LCD1602_EN=LOW;电子园51单片机学习网Q)B UkK
LCD_delay();
.O)@Hw+F40425 LCD1602_EN=HIGH;
F;q9E*uo#J5h40425}
*X%\o3O N!qW:H40425/********************************************************************/电子园51单片机学习网f6n pi&s
void LCD_en_dat(unsigned char dat)电子园51单片机学习网$lt,{j%v
{电子园51单片机学习网(i`^jZiep?wGi @
LCDIO=dat;电子园51单片机学习网QZ*PL2ZU'D
LCD1602_RS=HIGH;电子园51单片机学习网A o8e{Dw#l
LCD1602_RW=LOW;
SvwN z;O$k40425 LCD1602_EN=LOW;电子园51单片机学习网 L2H/| PU
LCD_delay();
QKN c2XWe*Y40425 LCD1602_EN=HIGH;电子园51单片机学习网OztS@'HOYH
}电子园51单片机学习网~R:b;R!Z,q
/********************************************************************/电子园51单片机学习网f%e8}5I\V4hu2p
void LCD_set_xy( unsigned char x, unsigned char y )
2PW2vVHZ#e40425{
V&\q$dD9Qfq40425 unsigned char address;
&j&i\FH-XQ40425 if (y == LINE1)
]j%ydGfK+p:i;Z40425 address = LINE1_HEAD + x;
{!LOWQ _2ru!P40425 else电子园51单片机学习网D7i&UVS)v
address = LINE2_HEAD + x;电子园51单片机学习网L-J2~v}NQM
LCD_en_command(address);电子园51单片机学习网A eh|%\T5D
}
W(O9I-ks?et(A40425/********************************************************************/电子园51单片机学习网tY-n$X kv:I ^
void LCD_write_char( unsigned x,unsigned char y,unsigned char dat)
vpmwGi4t+\40425{电子园51单片机学习网W0J S]XCu
LCD_set_xy( x, y );电子园51单片机学习网(TDqqU5L x
LCD_en_dat(dat);电子园51单片机学习网(r&gs;O]#q
}电子园51单片机学习网j4ot!W4\%~
/********************************************************************/电子园51单片机学习网{ }KuZG-K(?
void LCD_write_string(unsigned char X,unsigned char Y,unsigned char *s)电子园51单片机学习网lg$Ib)n7t
{电子园51单片机学习网 F2o6i]E+l:y G
LCD_set_xy( X, Y ); //set address电子园51单片机学习网'aKjV*jatpO'V
while (*s) // write character电子园51单片机学习网 e#N J%{z2e`(?B
{电子园51单片机学习网 h/vPn d,{m
LCDIO=*s;电子园51单片机学习网a {8y ARhfk
LCD_en_dat(*s);
9CD!t$ASjh"lf40425 s ++;电子园51单片机学习网%]h]!tfw!S@+?/Xy
}
7UYwj(n ~3q40425}电子园51单片机学习网;oh|Q K`y5v
/********************************************************************/电子园51单片机学习网ZsZy R+?)zq
void LCD_init(void)电子园51单片机学习网e-x9N$?WhQ
{电子园51单片机学习网1Y3v [cC3s x&e
CLEARSCREEN;//clear screen
}0zyb#m DTt40425 LCD_en_command(DATA_MODE);//set 8 bit data transmission mode电子园51单片机学习网i$};R!U.Z R/W1f'mj5U
LCD_en_command(OPEN_SCREEN);//open display (enable lcd display)电子园51单片机学习网5g r3@i"L.A%IV"HVT
LCD_en_command(DISPLAY_ADDRESS);//set lcd first display address
W'G$P Tbod^q40425 CLEARSCREEN;//clear screen电子园51单片机学习网C1Z_"T q9Ep\V
}电子园51单片机学习网;~K k(gX f:z/T'K
/********************************************************************/
]'w(FjkF[40425/*********************** OTHER PART *********************************/
e;_;n1@| [oqOl{40425void delay_nms(unsigned int n)
lUF~&eY40425{电子园51单片机学习网@:g"m+ex'SP
unsigned int i="0",j=0;电子园51单片机学习网t]9~w _*j&f
for (i=n;i>0;i--)
^uot)l7Wu40425 for (j=0;j<1140;j++);
K1yxZ&~40425}
n7qh:f5z+kM40425/********************************************************************/
CEe QhS40425 |
|