首页 | 新闻 | 新品 | 文库 | 方案 | 视频 | 下载 | 商城 | 开发板 | 数据中心 | 座谈新版 | 培训 | 工具 | 博客 | 论坛 | 百科 | GEC | 活动 | 主题月 | 电子展
返回列表 回复 发帖

AT24C02 C语言驱动程序—LCD1602 显示

AT24C02 C语言驱动程序—LCD1602 显示

//AT24C02 EEPROM drive program电子园51单片机学习网7PyG1u(dK
//for 51 mcu with lcd1602 as display电子园51单片机学习网|a4[r6uJ9[
//designed by zhaoliang电子园51单片机学习网c @5h:gI C(^Z,D!D
//2005-6-14 21:02
2c)o~b/jrB10444
#c}.U^ n$Ga4\l10444#i nclude " reg51.h "电子园51单片机学习网0fUe%Nr%Kv w3bv:g
#i nclude " intrins.h "
t(o!mY `yC'S%WO10444/********************************************************************/
u8u#S&o5bG'K#qEg10444//lcd part电子园51单片机学习网np#j4R1G
#define  LINE1     0
fH IBi&d&g,c10444#define  LINE2     1
G9Ag3O'VA?9}sq10444#define  LINE1_HEAD    0x80
!W'SCMm a10444#define  LINE2_HEAD    0xC0电子园51单片机学习网"?Q.ri}j:lDp6o
#define  LCD_DELAY_TIME   40电子园51单片机学习网 ^1K)p9{9`7a;hG j;u
#define  DATA_MODE    0x38电子园51单片机学习网4~?t[9N4B0a
#define  OPEN_SCREEN    0x0C
kb,e,\&O^O10444#define  DISPLAY_ADDRESS   0x80电子园51单片机学习网Q_(xXf/h7v%M7|h
#define  CLEARSCREEN    LCD_en_command(0x01)电子园51单片机学习网s5t5XH,Z2LK
//common part电子园51单片机学习网Z]eE&CP$c1Vb
#define  HIGH   1
J0C^I$v*U10444#define  LOW    0
#C5K0F4Q%lN't J7x10444#define  TRUE    1
iVm kI10444#define  ZERO    0电子园51单片机学习网Bp^/Js ~
//at24c02 part电子园51单片机学习网(xgO(m?0j
#define  WRITE24C02    0xA0
1o8Cpn3V%JTw10444#define     READ24C02    0xA1
$Q{#K4p~Q10444//I2C part
yByt1W|}$H10444#define  ACK    0电子园51单片机学习网o+Kv)uGX o1AA
#define     NO_ACK    1
v&Bdc%{2O:^3^!f10444#define  MSB    0x80
FD2e}"H/qgHU10444/********************************************************************/电子园51单片机学习网r-]P?y#N'f
//change this part at different board
%u^fXW'? w10444#define  LCDIO     P2
&~-]b'kB10444sbit LCD1602_RS=P0^7;   //data command select  1 data  0 command  pin 4
;xA-cn3P+Q10444sbit LCD1602_RW=P0^6;   //read write select   1 read   0 write     pin 5
.X7`%U9I+Qr10444sbit LCD1602_EN=P0^5;   //LCD enable signal             pin 6
qMT&TV ?10444
hlb {|C10444sbit SDA="2"^3;      //AT24C02 serial data    pin 5电子园51单片机学习网4p:iaM/zC
sbit SCLK="2"^2;     //AT24C02 serial clock    pin 6电子园51单片机学习网?%Tj8c|(|
/********************************************************************/
p{+y {m5]Uakdw#W10444void LCD_delay(void);//lcd delay function
&f,D]yHV4b10444void LCD_en_command(unsigned char command);//write command function电子园51单片机学习网,Ix7FpPE3I o&R
void LCD_en_dat(unsigned char temp);//write data function电子园51单片机学习网V@:x.H d
void LCD_set_xy( unsigned char x, unsigned char y );//set display address function
2]1N#Y Bq*K1X&k]Z7\10444void LCD_write_char( unsigned x,unsigned char y,unsigned char dat);//write lcd a character function电子园51单片机学习网kF-|p(P,^2|o
void LCD_write_string(unsigned char X,unsigned char Y,unsigned char *s);//write lcd string function电子园51单片机学习网Mo h,B} DvNsy.t
void LCD_init(void);//lcd initize function
mdz+}5h+n HO$\10444/********************************************************************/
_ AyJ+[3Pk10444void I2C_delay(void);//I2C delay function
T0Z2Pu X(w,@/F~[%\10444void I2C_start(void);//I2C start function电子园51单片机学习网H+Z'Nm5N5mE2d
void I2C_stop(void);//I2C stop function电子园51单片机学习网/f!| _ N9M!q
void I2C_send_ack(bit k);//I2C send responsion function
*V ['w'jM%D"IG10444void I2C_write_byte(unsigned char dat);//I2C bus write byte function电子园51单片机学习网w4~b$Vo3K
unsigned char I2C_read_byte(void);//I2C bus read byte function电子园51单片机学习网8q*c2d;U [X p
/********************************************************************/电子园51单片机学习网A/op/n"e
void AT24C02_write(unsigned char address,unsigned char dat);//write 24c02 information function
:~O/l+C$[Lz10444unsigned char AT24C02_read(unsigned char address);//read 24c02 information function
GR1?o X)lr10444/********************************************************************/
%FOq;E%Q)MY'rQ b10444void Mcu_init(void);//system initize funcition电子园51单片机学习网%p P%cVn]0eo3v9S
void delay_nms(unsigned int n);//delay functinon
;{Q8Nobe&C10444unsigned char count[2];
/{~]5hA3V n*mR)h10444/********************************************************************/
2eG^ ^xB10444void main()
(G2H \Z$u kn&`P10444{
o#n8vLkX RO10444 unsigned char temp;
&{*F]-ffqT @7b I1t10444 Mcu_init();电子园51单片机学习网3S+jQ1F H0ecN
while(1)   电子园51单片机学习网Nl0^ y!R
{
e x;c9N-|3Z8Y7?@10444  AT24C02_write(0x00,count[1]);电子园51单片机学习网D@vP&Bo-S]+AI
  temp="AT24C02"_read(0x00);
un] u @rF1C3\10444  LCD_write_string(0x00,LINE1,"AT24C02 TEST");电子园51单片机学习网lwTS/y k+Hj
     LCD_write_char(0x0e,LINE1,(temp/10)|0x30);电子园51单片机学习网 CAO`b5x2pV
     LCD_write_char(0x0f,LINE1,(temp%10)|0x30);电子园51单片机学习网e$l)GF1M)E?"T
     LCD_write_string(0x00,LINE2,"TIMER0 JISHU");电子园51单片机学习网I0e0Ij'];Q2w9R
     LCD_write_char(0x0e,1,(count[1]/10)|0x30);
a[ Fq o.e10444     LCD_write_char(0x0f,1,(count[1]%10)|0x30);电子园51单片机学习网!e(u0I d!p AK.tK;v fB
     AT24C02_write(0x00,count[1]);电子园51单片机学习网$UZ7Ge&M p(sc
}
T [ Mi w10444}
v f-Dp'M8G` B+nR10444/***********************************************************************/
9L Y[2gg+E,_4A0N10444void timer0(void) interrupt 1 using 1电子园51单片机学习网Q9r e3aFY
{
-DL-Wbw*Lv10444 TH0=-(12000/256);
(e1I)MR,W10444    TL0=-(12000%256);电子园51单片机学习网d1F0?6{%E{B&c/G8^V"?"q
count[0]=count[0]+1;电子园51单片机学习网4[~0snTM
if(count[0]==100)
g:s-@2M ^"M10444    {
/f5Yx V R10444  count[0]=0;电子园51单片机学习网Hu$GJc/M8_f8^,b
  count[1]=count[1]+1;电子园51单片机学习网k z0wCT.ae
  if(count[1]==99)电子园51单片机学习网3u)z4E{s R5Z-F
   count[1]=0;电子园51单片机学习网'B^+~&y!@u
}
$`r q\Up WN~10444}电子园51单片机学习网k.}'Em~!?
/***********************************************************************/
Pwh9B e10444void Mcu_init(void)
.{"zy l'U!KrSl10444{电子园51单片机学习网7h"U(HO~c,x
TMOD=0x11;电子园51单片机学习网$_/nj1O"x'r
TH0=-(12000/256);
%^$^Tn9ioX10444 TL0=-(12000%256);
y ^U1bY}b10444 EA=HIGH;电子园51单片机学习网isk9| `2ZKj
ET0=HIGH;
#O*W+p-_S10444 TR0=HIGH;
5XC M#f _y/x&0d#Z10444 LCD_init();电子园51单片机学习网'P-KCEX}\+P N0j
}电子园51单片机学习网%W5T2a.W x
/***********************************************************************/
5L+A-T/d6u-}mI10444/******************************** I2C PART **************************/电子园51单片机学习网gF_EK;sEf
void I2C_delay(void)电子园51单片机学习网^"|7s9RsbuZok
{
{?'i F&m10444  _nop_();_nop_();_nop_();_nop_();
#\@_y/c(`10444}
,T U-I|/bY#j}7]1G1D10444/***********************************************************************/
w{ o.J"Z O%Z0g10444void I2C_start(void)
b9s7J6C7gk0q[10444{
'P!JB"Bv1ZkP6pe10444  SDA="HIGH";
}4gn'wI9?1?Q-l10444  _nop_();
y:Rh3J,Trz k*H t10444  SCLK="HIGH";电子园51单片机学习网$[8RAN,C%V]
  _nop_();电子园51单片机学习网 QfK k$WXE?
  SDA="LOW";
pV`;d/P.v DX C3Ej10444  _nop_();
fyL`m`H;b10444  SCLK="LOW";
*JK'_ {N&w10444  _nop_();电子园51单片机学习网0C*yW'}X$U
}电子园51单片机学习网5h7}:['Cxt4?]
/***********************************************************************/电子园51单片机学习网7xQwEH f6M
void I2C_stop(void)
H*[8Sv hRd2d5FJVY{10444{电子园51单片机学习网IA2O(x9T5R `8@
  SDA="LOW";电子园51单片机学习网F9uj5A rGvp8Y
  _nop_();电子园51单片机学习网ZrcI%_}(w,}
  SCLK="HIGH";电子园51单片机学习网5CN#O"j+F;p5V
   _nop_();电子园51单片机学习网0Msc5~"Uu
  SDA="HIGH";电子园51单片机学习网~${1a0V2x0u"Y(o/p.G
  _nop_();电子园51单片机学习网%l-p{ n$^*C};K2Hk
  SCLK="LOW";
cM+JoUp^(X10444  _nop_();
%D8Y3H|Q W10444}电子园51单片机学习网Y&Y&\V~o NG6J
/***********************************************************************/
cS{(g%z10444void I2C_send_ack(bit k)
w%e h2F2Xk10444{
-n zjG&~.v10444  SDA="k";
7F(|_? q|1w.\10444  I2C_delay();
_ U;p N;\-j10444  SCLK="HIGH";
6\O3A c}%L J d10444  I2C_delay();
&Y\/W_!h [ iqN10444  SCLK="LOW";
z#[nY^10444}
t0}.Z]S9oH5Q10444/***********************************************************************/电子园51单片机学习网9mC#be] Q]
void I2C_write_byte(unsigned char dat)电子园51单片机学习网cEXQ D dL
{
8B K+@0t-X5C'G3N10444  unsigned char i;
3nmZ_-I.Ts lWj10444   for (i=8;i>0;i--)
%X ]fcP w10444    {电子园51单片机学习网$r;LMF6W!zHK{
     SCLK="LOW";
$|L'n l6R+[v'U10444     I2C_delay();
E\/pSu[qO u10444     SDA=(bit)(dat&MSB);
+a@7h+qu4l nSe?10444     dat<<=1;
'r GK BtW9h0x10444     I2C_delay();
&}yU%lU@-cl2r10444     SCLK="HIGH";
$?.E,`(X(Mj$h10444     I2C_delay();
S'e8h*F9W10444    }电子园51单片机学习网)po*v W n F)| J
    SCLK="LOW";  电子园51单片机学习网*wALo2E3P7v
}电子园51单片机学习网U7P'e3G&Ab
/***********************************************************************/电子园51单片机学习网_8Dn0j%m
unsigned char I2C_read_byte(void)电子园51单片机学习网 C:TSMG6J2Ee8~3E [
{电子园51单片机学习网%s)d-P&u K'aceE{!p
  unsigned char i,dat;电子园51单片机学习网q|5]` f6wP {"V
   for (i=0;i<8;i++)
:M!} lq4C4N@10444    {电子园51单片机学习网Mv$BD?
     SCLK="LOW";电子园51单片机学习网/w/X.w9_s!_Hn
     I2C_delay();电子园51单片机学习网d&`G[/y_-~7\d0ETYK
     SDA="HIGH";
Y4W0Du/U10444     I2C_delay();电子园51单片机学习网+K UPR'^IjHJ
     SCLK="HIGH";
*V,~1i6GM10444     dat<<=1;电子园51单片机学习网/`^V-hkA2uVZ
        I2C_delay();
LEnf4N SZ"d10444     if(SDA)
6n UxA^[10444        dat++;
)P"z7mz$eV4l5o10444    }
%Yu\ dI.]o'dY"j10444    SCLK="LOW"; 电子园51单片机学习网s$r|Ho,[S
   
8O3V3d0zU$v10444   return (dat);
$y5f4~ gmlU10444}电子园51单片机学习网R-a4l8en"C w VT7|
/***********************************************************************/电子园51单片机学习网Vf8s RQ/`O4j
/************************ 24C02 PART **********************************/电子园51单片机学习网8r,VU!R1Nd&QJ
void AT24C02_write(unsigned char address,unsigned char dat)
F{;g0tH10444{
7zGth OD2t*m$H.Q10444 unsigned char temp;电子园51单片机学习网z5`3~b.cm
temp=dat/10;电子园51单片机学习网:Ew$_+L&C pxA0}
temp<<=4;
R+gx"J1H^e10444 temp=dat%10+temp;
3|0Z"F$I5D;Xs-J,x10444
~C(ze%WpR){10444 I2C_start();电子园51单片机学习网5Wd1H9v]g
I2C_write_byte(WRITE24C02);    电子园51单片机学习网+K+{R9Y|2T'BW.X'C"W
I2C_send_ack(ACK);电子园51单片机学习网1a`Jy-U,g
I2C_write_byte(address);  电子园51单片机学习网5zVX+Yin1Ya
I2C_send_ack(ACK);   
7POq\]$q*W3\U t+n10444 I2C_write_byte(temp);   
3p6}%[K S+d10444 I2C_send_ack(NO_ACK);
"C Hsar)g L3C(PR10444 I2C_stop();电子园51单片机学习网9n[Z'P-\ e5x
}电子园51单片机学习网1@9U|9g i*l&n
/***********************************************************************/
PYz'p(sv d k o10444unsigned char AT24C02_read(unsigned char address)
U#w8Z:p @[10444{电子园51单片机学习网Kq ^w1j.ki#G`
unsigned char temp,dat;
%t f F/FOkf6PF m10444 I2C_start();
6p;M}N'qM%ltKJXH10444 I2C_write_byte(WRITE24C02);电子园51单片机学习网oY}:~1]'w
I2C_send_ack(ACK);
s9GV]$VAu}:U10444 I2C_write_byte(address);电子园51单片机学习网4D.Q Ch&b*Tb;p
I2C_send_ack(NO_ACK);
y:w~i pH1rE10444 I2C_stop();
i&{7M W2O_m}?\10444 电子园51单片机学习网"L:gudI"B1M
I2C_start();
j-w[!yXc z_ [10444 I2C_write_byte(READ24C02);
W vX\.FU O;]10444 I2C_send_ack(ACK);
4G?5? yd*z5M10444 dat=I2C_read_byte();电子园51单片机学习网@K3wB?$xH
I2C_send_ack(NO_ACK);电子园51单片机学习网~?3^5yi {I
I2C_stop();      
d&k\`_6ii0m10444
#C[2p7W6F9V^g6T7l10444 temp=dat/16;电子园51单片机学习网7@*J:K7d s2] q
dat=dat%16;
X.o7G q4X&k,m,Y]_10444 dat=dat+temp*10;电子园51单片机学习网+s9L)v7ACa7Rm1w

nH s ?z!e10444  return (dat);
`&W.e%l Z10444}
$C6lQ8~1k10444/******************** LCD PART *************************************/电子园51单片机学习网Is-| |t\ KlO
void LCD_delay(void)  
#g9D;Da)`M"Hk+q ~%Y10444{
Q4BJ M CZ10444 unsigned char i;
@*i(x_U7n10444 for(i=LCD_DELAY_TIME;i>ZERO;i--)//be sure lcd reset
&ZOs v5T'}%h^ TS10444   ;电子园51单片机学习网)Y[ {`#}
}电子园51单片机学习网lZC0A+U2f(O*@*C&n
/********************************************************************/
$?v2Q6xO-m10444void LCD_en_command(unsigned char command)电子园51单片机学习网%^$_;e1VD
{
(}Pb j$w(X10444 LCDIO=command;
3Z0Q0Z_jFa'hz10444 LCD1602_RS=LOW;  
K*jZf5G0s,] P10444 LCD1602_RW=LOW;
B+a:e GB u10444 LCD1602_EN=LOW;电子园51单片机学习网l L5E8T#N
LCD_delay();
?&^`)xlu(wc7}10444 LCD1602_EN=HIGH;
:x(I$Q!S-rw/gNt R10444}电子园51单片机学习网"E"~+d(h nQ
/********************************************************************/
U6PF:s,C'o10444void LCD_en_dat(unsigned char dat)电子园51单片机学习网k$U)k5Y cW(D&n5M
{
4c'h]!G{-QB10444 LCDIO=dat;电子园51单片机学习网SPW1aI VDm
LCD1602_RS=HIGH;
i7e iTZI b10444 LCD1602_RW=LOW;
!B PR-h"b10444 LCD1602_EN=LOW;
~N"n7q7IWs10444 LCD_delay();
B|;yB.OG*K10444 LCD1602_EN=HIGH;电子园51单片机学习网0m.oN'JM
}电子园51单片机学习网wxg'{7{\9i
/********************************************************************/电子园51单片机学习网7~SY.q)\ FF\:U#K
void LCD_set_xy( unsigned char x, unsigned char y )电子园51单片机学习网(U7i H$E8A YG"@;l
{电子园51单片机学习网7Ev;Hfk9vH
unsigned char address;电子园51单片机学习网 SzZ&W}V
if (y == LINE1)电子园51单片机学习网U5]fo,s i Z0h
  address = LINE1_HEAD + x;
\"fi6{'nxs~d10444 else电子园51单片机学习网7OAn"x@lm
     address = LINE2_HEAD + x;电子园51单片机学习网-M1]Iu h }:Go
LCD_en_command(address);
.`ns-xs Uuh@10444}
3n2G$S0_$nZ0^'n10444/********************************************************************/电子园51单片机学习网%tX6r y;a KC)N,e
void LCD_write_char( unsigned x,unsigned char y,unsigned char dat)
[+U3M*^A10444{
%x8}Q.K'`z8K%l10444 LCD_set_xy( x, y );电子园51单片机学习网GbYxw9Q^!?-T
LCD_en_dat(dat);电子园51单片机学习网Tn:df/H!~j B
}
m(~$Ls S2G6V"xw&}10444/********************************************************************/
0Pi I4Y H10444void LCD_write_string(unsigned char X,unsigned char Y,unsigned char *s)电子园51单片机学习网WA"W"Lp!~B'^3|
{电子园51单片机学习网PX?O)Uc2s)drE
    LCD_set_xy( X, Y ); //set address
4[rnw2]3}4T10444    while (*s)  // write character
*ti~T`vU:F)u10444    {
a*kh~'z#L G6L/{,b F%Y10444     LCDIO=*s;
"/l4v]~^ z10444        LCD_en_dat(*s);  
S,\C0Z M.\10444 s ++;
3Y]bb b8H4O s| x)N10444    }
J L P6M?,Gr t Krj0At10444}电子园51单片机学习网`-l k2F ~)S F:B&z W
/********************************************************************/
#C\i~1hR-P2{'q@10444void LCD_init(void)电子园51单片机学习网y fMYxrX
{
R [/S5p8WG,O0L10444 CLEARSCREEN;//clear screen
p h(Hb MZV2x/O10444 LCD_en_command(DATA_MODE);//set 8 bit data transmission mode
&M^3~J(C2`10444 LCD_en_command(OPEN_SCREEN);//open display (enable lcd display)电子园51单片机学习网_&I%C1^q4]
LCD_en_command(DISPLAY_ADDRESS);//set lcd first display address电子园51单片机学习网Vs$fnVv1I,ek2~)b
CLEARSCREEN;//clear screen
N#ZxrrA9M10444}电子园51单片机学习网m:O8a#k6V\
/********************************************************************/
oo5E&LrC(n\EJ8O10444/******************* OTHER PART ***********************************/
:KfG @U p4VM4~xq1j10444void delay_nms(unsigned int n)      
lmRYG/h k$A10444{电子园51单片机学习网P^ U ef9LLs
    unsigned int i="0",j=0;
f"n0Gay10444    for (i=n;i>0;i--)
t(t3d)h bQ Q.K7^I(|10444     for (j=0;j<1140;j++)
%bA)k QKv10444      ; 电子园51单片机学习网[vUhR
}
[\](XLs10444
返回列表