victory for prepared
分类: 嵌入式
2013-09-14 23:18:13
/* VO-VCC 对比度调节端接VCC +5V */6 K! m6 y9 ^, I6 Y, } #include #include sbit CS =P2^2; //RS 这个引脚直接接VCC +5V1 ]. p" ^- f( q3 C5 ` sbit SID=P2^3; //RW3 N' T9 g' k) @8 J3 o& w3 L sbit SCLK=P2^4; //E ( u, a; L+ d" X1 ` 3 j9 B' T. ^5 I void delay(unsigned int j)* j: a/ l) d4 k: M9 C6 x { unsigned char i;; ^$ X8 T+ l- P& M7 f6 x do{ for(i=0;i <100;i++);. P. J( `0 t k" \; a& U4 r( X }while(j--); } * G" h% z8 ~( q7 b0 W void send_command(unsigned char command_data) //发送命令0 o/ t, Z& P' e0 Q" R0 T4 e" @ { 3 Q6 h/ \7 Z- V/ j, P unsigned char i; unsigned char i_data,temp_data1,temp_data2; 3 A" n4 O9 M+ {3 C( Z- } i_data=0xf8; //操作命令,可以查看资料 delay(10); - p: Y. Y4 S6 a CS=1; 1 O8 L# Q* {5 C) K2 d+ e SCLK=0; 0 y( r9 r/ y$ K9 g' Q* s, [ for(i=0;i <8;i++) . J* J9 U1 e' A7 k; P+ A$ s { SID=(bit)(i_data&0x80); . b/ e8 Z) P; c' e/ c9 @) x SCLK=0; SCLK=1; i_data=i_data <<1; . Z/ \, U" o* i! m- B } i_data=command_data; i_data&=0xf0; 6 B# ^' {6 W+ B0 |" L, V6 V for(i=0;i <8;i++) { SID=(bit)(i_data&0x80); SCLK=0; SCLK=1; i_data=i_data <<1; } i_data=command_data; ( D7 s8 j2 U4 i/ ^- A temp_data1=i_data&0xf0; , n, O- q; B8 u5 M temp_data2=i_data&0x0f; temp_data1 >>=4; . `# \ n9 }9 d" \) | temp_data2 <<=4; 9 G @8 M2 K! {3 e5 m- F' ~* j i_data=temp_data1|temp_data2; 8 i" |% P0 D2 f3 U) P5 ^ i_data&=0xf0; for(i=0;i <8;i++) { SID=(bit)(i_data&0x80); SCLK=0; SCLK=1; - s8 z7 g+ l+ `- I% l i_data=i_data <<1; : p) H& ~5 x2 S6 Z D } ) K5 w& I/ B$ J7 a8 m5 g8 U, q CS=0; 9 | ^: M- T+ f! N' i# X0 t } ' ~- `, u* Q& E' D ( X4 h3 a" A. G$ w6 Z; @2 I void send_data(unsigned char command_data) //发送数据 { unsigned char i; . T1 z9 ]0 w/ M$ V) d' y unsigned char i_data,temp_data1,temp_data2;9 r/ L- _6 g/ y1 t5 G i_data=0xfa; //操作命令,可以查看资料- z: V, |( _9 n8 T2 M0 u3 {& O. K1 x delay(10); 6 U* I+ y) o6 g5 |1 B& U. P5 \ CS=1; for(i=0;i <8;i++) 3 w7 Z* Z$ A( w; |% D" _ { SID=(bit)(i_data&0x80); SCLK=0; % k- C5 ~) m$ c& H2 i SCLK=1; i_data=i_data <<1; } 2 O$ f# W4 J1 g1 d4 a i_data=command_data; i_data&=0xf0; for(i=0;i <8;i++) V5 G0 g9 F& a- V$ x! ~( {. l4 u { + ?. L# R0 q+ D$ m" N8 ^; t SID=(bit)(i_data&0x80); ( v' i. [( S6 Y8 ? SCLK=0; SCLK=1; i_data=i_data <<1; } i_data=command_data; ' N2 K) C2 F, m! x7 R# F temp_data1=i_data&0xf0; temp_data2=i_data&0x0f; temp_data1>>=4; ( q9 l8 u2 e- [9 g+ _1 c- j temp_data2 <<=4; 5 P% S- f$ E0 x' j3 p i_data=temp_data1|temp_data2; $ G z n- Z# z i_data&=0xf0; for(i=0;i <8;i++) { 5 K7 L6 U- X$ M2 P SID=(bit)(i_data&0x80); 4 z- S) B. o3 i% c( M/ O SCLK=0; 9 b( X0 d" i T' i* Q2 s( z SCLK=1; - \9 `. b( f: }% ^; U& S i_data=i_data <<1; 6 U/ Y+ S1 J+ |% p } CS=0; 2 c9 k D. V0 ]4 A& R } K1 ]/ C) \! v' | void InitLCD() //液晶初始化 { j, b2 g: b( Q( s( z$ d1 x ) g, ~" h' E e# G* _ send_command(0x30); //功能设置:一次送8位数据,基本指令集 send_command(0x06); //点设定:显示字符/光标从左到右移位,DDRAM地址加1 send_command(0x0c); //显示设定:开显示,显示光标,当前显示位反白闪动 E6 l, U4 P* }8 e" k& m; B send_command(0x04); //显示设定:开显示,显示光标,当前显示位反白闪动 8 A6 h4 x; t7 o9 x' a- G send_command(0x01); //清DDRAM 8 V4 z5 c' S6 b6 `3 L# W% \( m send_command(0x02); //DDRAM地址归位 send_command(0x80); //把显示地址设为0X80,即为第一行的首位 }, w1 ?$ b2 f! c& |) y- C /* flagnew 是刷新标志位,1则刷新,0不刷新, x,y为起始座标 x(0 <=x <=3),y(0 <=y <=7),x为行座标,y为列座标; how为要显示汉字的个数; + }9 H; a, a' D str是要显示汉字的地址 */# Y: m' c+ x, s3 Q6 y9 U N ) I+ y9 J, Q. ~ void DispHanzi(unsigned char x,unsigned char y,unsigned char 3 K o3 X# E7 ~$ O; i: Q how,unsigned char *stri): O \; }7 F# \# \ { unsigned char hi=0; //汉字显示- W5 b" Z% j5 T$ e# s if(x==0) send_command(0x80+y); //: K: a5 V& g0 p @6 y else if(x==1) send_command(0x90+y); else if(x==2) send_command(0x88+y);, K1 ^* P# [' m# s; t else if(x==3) send_command(0x98+y);, K ?1 [: I. |2 |$ o for(hi=0;hi send_data(*(stri+hi*2)); send_data(*(stri+hi*2+1)); } } void DispZimu(unsigned char x,unsigned char y,unsigned char 9 z2 v8 Y) k% y6 {. ]6 ?' k$ [ how,unsigned char *stri)//字母数字都可以显示 { unsigned char hi=0; //字母显示* r. v- F; b0 ]! ` if(x==0) send_command(0x80+y); else if(x==1) send_command(0x90+y);) c1 b- H: _$ x9 Z9 \1 f7 t else if(x==2) send_command(0x88+y); else if(x==3) send_command(0x98+y); , y: c0 O M$ W5 T! @2 U( R. A for(hi=0;hi { send_data(*(stri+hi));" S2 w- S: y5 A) @+ i } }9 c) L$ A+ t5 D$ i% j 6 f/ k- C; q# X I! y, P, J main() { 7 @3 ]; b, y W- N; h InitLCD();) l- f* g( S; Z8 W7 Z DispHanzi(0,5,3,"开发板");) s T4 J/ |+ F* f( \ DispZimu(0,1,8,"DOFLY 51");9 f! l/ g; R: X- Y# H; ]. i DispHanzi(1,0,8,"上海朗译电子科技"); DispZimu(2,0,16,""); DispHanzi(3,0,8,"字库液晶测试程序");" P- c+ v6 |. G7 T while(1); & {+ [: T+ I: Q2 R, N0 g/ a" t } |