|
发表于 2008-8-1 16:09:00
|
显示全部楼层
就是啊,首先你要看懂I2C协议,在就是根据某个芯片,看一下控制程序就大概可以了!下面就是24C02的读写程序:
$ c/ y! u8 e4 D1 I
1 g; u4 a% q3 t; x) p
0 b5 n8 h8 v7 N0 H6 K% U #define uchar unsigned char7 @. k0 G2 f2 f8 y
#define uint unsigned int1 h$ e" F& A) y+ C2 [" Z
#include <reg52.h># l8 z2 ]2 W1 h+ Z) l
#include <stdio.h>
% O! ?0 m1 L: M# w #include <absacc.h>
* d+ ]4 U$ V h) E1 b. ? sbit scl=P3^5; //24c02 SCL
. ~* V5 k$ l1 G sbit sda=P3^4; //24c02 SDA
4 G1 D. b$ V! }! k1 b sbit DOG=P1^7; //狗
0 c, C2 \* a5 _, c uchar x24c02_read(uchar address); //从24c02的地址address中读取一个字节数据
B5 h* f X% p void x24c02_write(uchar address,uchar info); : S5 ], p4 |8 n; g, L
//向24c02的address地址中写入一字节数据info
6 P: \ k5 l: b3 _ void x24c02_init(); //24c02初始化子程序
; o) B. B9 P) F void delay1(uchar x);
/ Z, o* R* M2 E2 k7 [* k$ t. Z# P: O0 W void flash();9 X \6 h) T( x6 N" P. u7 l
void x24c01_init();3 z+ ?: g5 n; u9 P8 y5 T: I: v
void start();
, s3 w( a% }+ i3 x: B' z void stop();
4 f) L3 W! ~7 N6 L void writex(uchar j);
8 g( o9 k$ t3 v. }. u uchar readx();) |- C2 q b% w; D0 ~
void clock();9 D9 f! Q6 ^" ^
void delay1(uchar x)
* H% q; m1 V- h {
7 n* w$ _9 o. _& j4 Z0 ?4 C uint i;
) d0 j- _; t7 Q- i H+ C1 c for(i=0;i<x;i++);
. [* l3 j$ r5 _$ H8 P/ L) Q DOG=~DOG;# E* C% `9 z! `
}7 `3 t3 w9 w. @# ?
void flash()" q! g+ e3 t4 y5 C% e
{% R' y( ^( M8 M9 g
uchar u;
9 e% C. x+ H0 @, t DOG=~DOG;4 x. v0 `. m& H9 W1 V/ J( B
}( l: C; S: o4 v2 Y/ l' _
void x24c02_init()
& c6 }% E/ v0 Q& M+ v {' P8 e ]" @/ H8 S) [8 }4 X+ _
scl=1; flash(); sda=1; flash();- m+ |: z* V, S1 @
}
' W5 e- c& |& o+ _) w void start()( d- J' f. I; x. I5 f
{# ^ E4 m2 z. o, V5 |
sda=1; flash(); scl=1; flash(); sda=0; flash(); scl=0; flash();
2 j; k+ S1 O3 G }
6 u6 u3 w& `/ N; H" e6 D void stop()5 ], d7 H! k2 G, [
{& h- L- [# C5 ?: v7 ~5 K' Y/ k
sda=0; flash(); scl=1; flash(); sda=1; flash();; ^( f; S3 |5 O9 A w
}! w2 X/ w/ d7 F2 W1 L M
void writex(uchar j)3 L1 A& ^# F, D
{# L& @- e' M5 L0 c0 V2 \
uchar i,temp;5 e6 o. d+ P( _
temp=j;
/ @7 ~# m# A' p) z' e- N) _ for (i=0;i<8;i++){
( O. h! l9 ^3 z: M l temp=temp<<1; scl=0; flash(); sda=CY; flash(); scl=1;
V, R; }; }- m. j/ F, u: E8 F flash();5 H0 C( i' K- b) d8 W2 {9 _2 f
}
. O9 ^% E* D* \ scl=0; flash(); sda=1; flash();! d3 D S6 X1 j6 m$ {/ R. }
}5 |# a& e4 V9 t# Y
uchar readx()
, |' ]9 b" T9 X8 i! x {
" s; |2 |8 c" b. T5 O uchar i,j,k=0;
+ o/ E& H" J& Z1 m( T* [) u4 H scl=0; flash(); sda=1;% G6 N2 X% o; d' b1 ~ Q
for (i=0;i<8;i++){
4 t2 X, ~6 X( [ K1 J% a' p8 O% } flash(); scl=1; flash();
' K" j7 v8 b3 r: N" }$ m if (sda==1) j=1;1 l( _5 d# e0 N( U( x# x* B$ v0 i
else j=0;0 A6 ?" ?) i o3 E" m* A) S
k=(k<<1)|j; scl=0;+ _- Q4 I2 \$ |- u6 O. C1 g4 ~
}
2 L' ]. v" }8 e3 n0 T flash(); return(k);1 d; ~& {; D$ N3 \2 V8 N
}% V& b& q( W1 N2 o9 F- ^2 f
void clock()! s5 S7 g% Y7 [5 c3 _2 ~
{
* @ {) S' ?( R uchar i=0;5 j5 S8 b$ b4 ^8 u
scl=1; flash();
5 w0 v: F2 a" |# T while ((sda==1)&&(i<255))i++;
1 |# P: O5 N8 M0 X. w scl=0; flash();5 l' c) ?, O" {0 K' g8 z
}( j! x4 G% ^. w1 l& U0 ?" z8 ^
uchar x24c02_read(uchar address): i% s* n/ k2 @
{& r8 w& m; ^ D3 D" n
uchar i;6 E7 e1 n) R$ L; ]& d4 R
start(); writex(0xa0);& Z5 `. A, E1 {" O* D7 ^
clock(); writex(address);% Z$ w! q; l' M) R
clock(); start();# M9 n2 L5 C/ F6 z8 X
writex(0xa1); clock();( C9 m! A& Z6 {# Y
i=readx(); stop();! F" D, Z, `) F; y# I; C
delay1(10);
7 l; O" Z, R* u8 ] Q; y return(i);( u5 P- @$ l% ]- \' P
}. _8 g, o# K3 G+ `, w+ w& H
void x24c02_write(uchar address,uchar info)' ^& N, _8 H0 `8 D. t2 N
{2 i5 n8 E) W! u0 p
EA=0;9 d) ?: \9 x* }7 I" D- ?# i9 p
start(); writex(0xa0);% ?! b0 y2 I ]3 q7 V" |
clock(); writex(address);: y* R( A5 B: R7 c; B0 O
clock(); writex(info);4 I% E" ]( w, V k. Z* Q" {- T) s
clock(); stop();
6 x* p5 e+ b, X5 m7 X; a7 A EA=1;6 V* X7 R- c2 A" A6 g( T! v- S
delay1(50);/ Q- K7 \" R# }4 U+ h+ ]$ W- G
} |
|