|
发表于 2008-8-1 16:09:00
|
显示全部楼层
就是啊,首先你要看懂I2C协议,在就是根据某个芯片,看一下控制程序就大概可以了!下面就是24C02的读写程序:
/ L" F* |" B! z9 G" ]2 [2 a3 ~5 {& K% I( ^
1 R# \+ q0 }& ?# L# T9 ^2 k% L
#define uchar unsigned char
9 _' ?0 U" I; U; t$ L' C #define uint unsigned int
P: ]3 J7 ^/ M7 n #include <reg52.h>6 v; w" x- v4 K5 G; m
#include <stdio.h>) I7 Q& W! \* N& C& a, _
#include <absacc.h>
! l8 H6 W1 v' I" Q0 | sbit scl=P3^5; //24c02 SCL
( N; h( g2 b1 h0 P) H/ \ sbit sda=P3^4; //24c02 SDA- b+ }" ^; T) P7 `& k% |3 b9 A
sbit DOG=P1^7; //狗
1 a, c0 H* w% c+ s; u0 E uchar x24c02_read(uchar address); //从24c02的地址address中读取一个字节数据3 V: {% [- U1 l) } V) j. b
void x24c02_write(uchar address,uchar info);
5 c0 m* h# K2 a3 J5 B# g5 B //向24c02的address地址中写入一字节数据info4 {- {4 r% D8 w
void x24c02_init(); //24c02初始化子程序
, z+ C1 A4 x/ ^! x9 n5 z void delay1(uchar x);& Y+ \9 m7 R. G0 M( {$ [8 {: b0 X5 x
void flash();: n7 S c' \1 }% [9 J. c
void x24c01_init();
* T- r4 K1 C8 X E, ? void start();6 w# w, f9 P! p8 Q
void stop();
3 K& F& D t2 \4 j8 v# \ void writex(uchar j);
' f, `# \% U9 s8 L# Y uchar readx();
& s+ c. g0 d r' \ void clock();0 P H: V' x, _7 C+ G1 ?: E5 L
void delay1(uchar x)" H6 P) ]6 N8 T" J
{/ v! K; W8 o4 \. Q) S) A: u( Y
uint i;
' O: o( |' J5 L( t for(i=0;i<x;i++);8 N5 G9 _0 v+ m$ Z; N3 I4 C
DOG=~DOG;
5 g# G! M) [6 K% X+ S# ? }
9 q3 ?8 V* [$ H' m void flash()
" Q9 ~9 o. P! u4 k% L) U+ w0 | {
0 N) X9 H: K8 ^+ [% p4 v0 @9 B uchar u;
+ e! F+ ]) x8 ?/ Z DOG=~DOG;
$ ]# ^/ O% O. @) n9 {) ? }6 a' M( I w: X2 l2 O
void x24c02_init()9 @7 M, \8 {' f" d6 x
{
! a9 W. T/ v& D, ?+ K3 M/ |4 H! Q scl=1; flash(); sda=1; flash();
1 T* @- C. n! _3 o4 E. P8 Y& n }
^+ V _* j2 y+ Q& Y' L void start()
9 n2 \& ]2 K- W/ G {. b9 Y0 l5 j; x3 i
sda=1; flash(); scl=1; flash(); sda=0; flash(); scl=0; flash();
4 \6 X# u" Y" a4 m }0 k3 y5 N" L7 g# f$ h N, w+ _ D; O
void stop()! a2 c0 Q( W" G
{2 s) r) L6 L# U& V
sda=0; flash(); scl=1; flash(); sda=1; flash();$ X1 b: o2 g6 R, h
}
{1 c0 ^. R% n2 U* ? void writex(uchar j)& a. K1 D$ k- p7 S3 P
{5 c* ?# {. n- j$ q
uchar i,temp;
~% O0 b6 V. q- y/ y/ \5 F: [ temp=j;
6 I! ?( {+ o1 M! q, \* S. V% E6 [ for (i=0;i<8;i++){
- y$ J5 p% H# ^- t5 v! g9 ^ temp=temp<<1; scl=0; flash(); sda=CY; flash(); scl=1; ( I! S1 X2 L' \
flash();8 e4 D$ f8 G" `! f G! y# t
}
' `6 Q6 W2 x) Y2 J- C scl=0; flash(); sda=1; flash();
# ?$ y* J9 [, [* B }
/ T- ~7 E/ a- u/ {( L/ _, { uchar readx()
. u3 w; w4 r. t& O0 Y k" M6 A {4 e$ {; v0 T7 x5 ^
uchar i,j,k=0;% ~; e+ v# \* E7 R
scl=0; flash(); sda=1; ]% J( p! W5 t0 x1 Q
for (i=0;i<8;i++){
7 {0 X9 F9 J3 c; n3 G- `! J flash(); scl=1; flash();
: k( g) D, w5 W3 H" g if (sda==1) j=1;
- C2 l; {7 U8 w# F; } else j=0;
1 S8 d! m" l$ J! I k=(k<<1)|j; scl=0;
, P I. J9 C6 z) v3 p$ \- A }8 U/ t% y4 J1 P3 m$ W+ A
flash(); return(k);
1 G2 a+ |4 z. ^% Y% G* u }* p3 G; s+ f5 n2 Y7 O8 i+ O
void clock()
}/ N: A0 j! l {+ V1 M! \$ U" |2 u/ G0 v, n* U
uchar i=0;! C; S, N4 W) }
scl=1; flash();
/ U# {" j$ }* A# P' H6 K7 ^8 b B while ((sda==1)&&(i<255))i++;9 i) j V; C, a4 F- `# w
scl=0; flash();7 k; a, {( ]9 B9 r+ @/ W5 t2 \' C
}
# h" n$ y" R5 c uchar x24c02_read(uchar address)* f9 b L- M7 a# y3 v8 V8 e- j
{# k5 |$ l1 E0 o, Y Z
uchar i;
7 w# q y* T& g8 o start(); writex(0xa0);
7 G( C3 d) }$ A2 s+ Z( x1 Y clock(); writex(address);7 C$ C5 o Y" K8 S9 C- t, f& m/ J; k5 o
clock(); start();. C3 F: d( C4 i. b, _/ {+ N( P
writex(0xa1); clock();
. I f) c$ F" g" R i=readx(); stop();; b' }+ D U: n
delay1(10); h. I5 }2 j. }, y3 Q
return(i); K; Q+ C' J/ O% O6 U W# ^0 T$ s- O' b* j
}
: c8 ]5 i1 ^2 ~, j3 X6 O3 k void x24c02_write(uchar address,uchar info)
5 c' u: u$ k9 w {
# m5 l/ C n9 [' A/ L+ H: ] EA=0;. V7 L. E# B# p5 S' f5 w
start(); writex(0xa0);6 U1 @0 H' b% v. O1 ]
clock(); writex(address);& u) |9 E3 t& N8 c) n2 |
clock(); writex(info);' @, {6 S( {. z ^
clock(); stop();
, ~$ l1 \3 ]' B$ C3 n( x9 | EA=1;
& F. x; e& i- X4 o delay1(50);
, [$ a, W; U' f ~ } |
|