|
EDA365欢迎您!
您需要 登录 才可以下载或查看,没有帐号?注册
x
F282xx/F283xx自身带有I2C模块,但受限于TI的官方例程中使用了fifo,其深度为4,在写EEPROM时,读写地址16位,占了2个字节,最多只留下2个字节存储数据,使用很不方便、实用性也极差。有不少网友在这一块碰壁,甚至逼的没办法使用IO口模拟I2C,放弃使用I2C模块。最近一直在测试28027的I2C模块有所获,可突破发送/接收长度限制(非fifo模式,当然这个长度也要和I2C器件匹)。本查介绍查询方式下的I2C模块使。 Y: H- E) q& \' f5 X
1、宏定义I2C模块操作参数 p. K) E: F* K$ L/ _7 K6 e7 `1 A
#define I2C_SLAVE_ADDR 0x50 //器件地址不含读写位。以24Cxx系列为例,器件地址应为:(0xA0>>1),即0x50
7 T' C. a1 F- o. P N5 P6 u#define I2C_NUMBYTES 10 //发送/接收数据长度) | S" U" m/ j( C3 [3 E5 T u
#define I2C_EEPROM_HIGH_ADDR 0x00
8 G9 \$ A3 w9 n! g7 @#define I2C_EEPROM_LOW_ADDR 0x00; R. Q- m: T u
, j4 D! A @# [' ]' z4 H. q2、设置相应GPIO引脚用作I2C引脚。 修改F2802x_I2C.c文件中的void InitI2CGpio()函数。
$ r- p# O. Y4 X2 f* j3、初始化I2C模块。4 _1 `6 O! n1 ]4 [$ p
void I2CA_Init(void)
- R" n3 m$ ~5 F% e{0 P; R7 |" r, i0 T4 @
// Initialize I2C
+ \ x( E: [7 @3 | I2caRegs.I2CSAR = 0x0050; // Slave address - EEPROM control code
6 e) o( E4 A1 }" S& E3 c7 p
0 _* w2 |' n1 c1 ~ // I2CCLK = SYSCLK/(I2CPSC+1)
1 q" S% t% R b0 I) j4 O) \! S- f #if (CPU_FRQ_40MHZ||CPU_FRQ_50MHZ)2 C& f" l5 w h: O" b, q+ n' O
I2caRegs.I2CPSC.all = 4; // Prescaler - need 7-12 Mhz on module clk9 C+ _3 P% c, c6 M) i* [/ d
#endif
6 m4 E \+ {6 f
, e% l0 V: t4 r+ B1 s/ N& U ` #if (CPU_FRQ_60MHZ): O/ z5 k8 a! `, }1 W% f
I2caRegs.I2CPSC.all = 6; // Prescaler - need 7-12 Mhz on module clk8 B8 Q3 }9 t) O, u* g- d
#endif
! W0 z. `3 d. t* C I2caRegs.I2CCLKL = 10; // NOTE: must be non zero( |7 p4 {: O* i( v! s
I2caRegs.I2CCLKH = 5; // NOTE: must be non zero
! o4 l. h1 g4 z- n0 S I2caRegs.I2CIER.all = 0x00; // Enable SCD & ARDY interrupts
& L; V5 O3 J- @) R* Z6 E
+ A$ E4 i$ b. J) A: Q6 h2 [- c I2caRegs.I2CMDR.all = 0x0020; // Take I2C out of reset" ]( A8 ?/ ` `. I0 {2 a, Z6 Y
// Stop I2C when suspended
( f7 w. K; i7 ?, n+ g; @3 B
& s0 Z% u/ X6 z0 n& Z, w I2caRegs.I2CFFTX.all = 0x0000; // Enable FIFO mode and TXFIFO% y% |4 D( x# Q$ Z
I2caRegs.I2CFFRX.all = 0x0000; // Enable RXFIFO, clear RXFFINT,3 C9 I4 ~& h( `$ M* s
: u- K! `) N8 L p6 i& @% b4 o! t
return;8 ?2 R0 E. M+ f
}3 B: T, g; j: U7 m# s9 s+ w0 Z1 ~( B9 U
4、执行I2C模块接收或者发送。, n( N% L5 J$ M0 @
//I2C模块发送数据到I2C器件。
; C; D7 A7 K; Q3 \void I2CA_SendData(void)
& j8 Q. s1 [" H# B2 T. b& E{
) a2 @, r6 O! F% m# L! A! K5 Q Uint16 i;
1 f+ G" u) w/ R) i" X. s( E I2caRegs.I2CSAR = I2C_SLAVE_ADDR; //Set slave address
7 ?1 G0 O/ b. c. q% ] I2caRegs.I2CCNT = I2C_NUMBYTES + 2; //Set count to 5 characters plus 2 address bytes
3 S+ @. _/ L& t s$ o0 c7 J I2caRegs.I2CDXR = I2C_EEPROM_HIGH_ADDR; //Send eeprom high address) n/ X- V7 }% e% ~' Z$ i
I2caRegs.I2CMDR.bit.TRX = 1; //Set to Transmit mode8 q% i# Y9 _/ T& Q$ h0 n* O, ^' R
I2caRegs.I2CMDR.bit.MST = 1; //Set to Master mode4 `, C: Q* p; L8 v4 l# X
I2caRegs.I2CMDR.bit.FREE = 1; //Run in FREE mode& X+ i$ x! ]9 g# H. n
I2caRegs.I2CMDR.bit.STP = 1; //Stop when internal counter becomes 0
6 J" Z' J& T2 a. c5 u# U( |$ j* g I2caRegs.I2CMDR.bit.STT = 1; //Send the start bit, transmission will follow1 p. h: |; X2 ~ R1 Y; D( H* S
while(I2caRegs.I2CSTR.bit.XRDY == 0){}; //Do nothing till data is shifted out
$ x3 h6 p, y' J3 D! Q! N9 E I2caRegs.I2CDXR = I2C_EEPROM_LOW_ADDR; //Send eeprom low address' ~! ?6 Q, _. j
8 n5 R; U" B' L6 J6 s! y
for(i = 0; i < I2C_NUMBYTES; i++){$ E. @% [! v) W2 {% T
while(I2caRegs.I2CSTR.bit.XRDY == 0){}; //Do nothing till data is shifted out8 \( `+ X! N; T$ j
I2caRegs.I2CDXR = TxdData; //Send out the message
! R# }- a4 f+ R( V9 i* T1 }/ p1 S }! v9 d& P2 d3 w+ O: [# m
}$ J+ s8 V' v, Z" s X: O
//I2C模块从I2C器件接收数据。4 O. j, ]8 G7 O8 X) V6 W [
void I2CA_ReceiveData(void)
" h1 p6 F# X7 o, T3 w7 W{
4 l# C) R4 r% w Uint16 i;
. ?! O, d' l7 b* \1 {& k$ C5 t I2caRegs.I2CSAR = I2C_SLAVE_ADDR; //Set slave address
: _3 N/ |7 ^9 s L2 n$ v I2caRegs.I2CCNT = 2; //Set count to 2 address bytes0 h+ r7 D+ X9 Q1 ^
I2caRegs.I2CDXR = I2C_EEPROM_HIGH_ADDR; //Send eeprom high address
; E( B9 Z3 ~, B4 }- ~ I2caRegs.I2CMDR.bit.TRX = 1; //Set to Transmit mode" r( }0 ?) z+ b6 J0 ~
I2caRegs.I2CMDR.bit.MST = 1; //Set to Master mode. c2 X1 I4 {8 D4 k& W' f; n" f
I2caRegs.I2CMDR.bit.FREE = 1; //Run in FREE mode
" `! _( F& |# c) g8 D I2caRegs.I2CMDR.bit.STP = 0; //Dont release the bus after Tx* X N1 t( }' c* y: d
I2caRegs.I2CMDR.bit.STT = 1; //Send the start bit, transmission will follow2 U* Y/ \7 @: z+ u2 W* M0 z
- Z' Z7 W& B9 S+ l/ v* z
while(I2caRegs.I2CSTR.bit.XRDY == 0){}; //Do nothing till data is shifted out
9 ~" G0 _+ o& v% t I2caRegs.I2CDXR = I2C_EEPROM_LOW_ADDR; //Send eeprom low address9 P6 P4 _0 v8 }
I2caRegs.I2CCNT = I2C_NUMBYTES; //read 5 bytes from eeprom0 H0 \9 ], p4 Z1 Y3 N' |# X5 l& O
I2caRegs.I2CMDR.bit.TRX = 0; //Set to Recieve mode- b9 `1 [ o! a9 K- Y" m
I2caRegs.I2CMDR.bit.MST = 1; //Set to Master mode# j6 G# R/ T+ h0 l
I2caRegs.I2CMDR.bit.FREE = 1; //Run in FREE mode
% u7 t' j9 B2 k- A* x4 n/ Y I2caRegs.I2CMDR.bit.STP = 1; //Stop when internal counter becomes 0
V+ w& \2 z( {) G: A/ |. s I2caRegs.I2CMDR.bit.STT = 1; //Repeated start, Reception will follow
. a0 b0 ] z& h* C, a1 { for(i = 0; i < I2C_NUMBYTES; i++){' H9 C: j& ^0 e( @
while(I2caRegs.I2CSTR.bit.RRDY == 0){}; //I2CDRR not ready to read?
! ]. z1 k. L( y9 i4 D! t4 `* A, o RxdData = I2caRegs.I2CDRR;, Y' L* W! @1 b7 t* X, V2 ?9 U1 Z
}9 t8 f1 G7 d! D: g& d l( Y' E s
} |
|