|
EDA365欢迎您!
您需要 登录 才可以下载或查看,没有帐号?注册
x
F282xx/F283xx自身带有I2C模块,但受限于TI的官方例程中使用了fifo,其深度为4,在写EEPROM时,读写地址16位,占了2个字节,最多只留下2个字节存储数据,使用很不方便、实用性也极差。有不少网友在这一块碰壁,甚至逼的没办法使用IO口模拟I2C,放弃使用I2C模块。最近一直在测试28027的I2C模块有所获,可突破发送/接收长度限制(非fifo模式,当然这个长度也要和I2C器件匹)。本查介绍查询方式下的I2C模块使。' G6 [( C7 w, C# Y- K; m1 N% K8 E+ y
1、宏定义I2C模块操作参数5 u i# b& k( B9 _, Z: B
#define I2C_SLAVE_ADDR 0x50 //器件地址不含读写位。以24Cxx系列为例,器件地址应为:(0xA0>>1),即0x50 L' j4 f8 G; ~( E. j7 Q6 y8 R
#define I2C_NUMBYTES 10 //发送/接收数据长度3 M: S% B8 ^$ e f4 q
#define I2C_EEPROM_HIGH_ADDR 0x00" b9 x5 \5 m4 p9 l! _7 [8 a( B: i5 {& N
#define I2C_EEPROM_LOW_ADDR 0x000 I1 ^5 X) t) H
5 Q4 G% `" M5 u% c2、设置相应GPIO引脚用作I2C引脚。 修改F2802x_I2C.c文件中的void InitI2CGpio()函数。
; v# d2 A; M. i% J; C7 E3、初始化I2C模块。" y; v$ N5 V" p3 I, z
void I2CA_Init(void)
$ M5 K8 c2 ]' h3 `{1 ]4 G7 S( M* j; p8 b( d) f
// Initialize I2C
5 V7 Y$ r W4 b/ O0 {# Q0 ], b I2caRegs.I2CSAR = 0x0050; // Slave address - EEPROM control code1 W1 b6 [. B7 x. p% W
: i1 W. O; y4 t7 J6 S // I2CCLK = SYSCLK/(I2CPSC+1)
; I1 Z1 v+ ]4 j- y$ I" V8 ^ #if (CPU_FRQ_40MHZ||CPU_FRQ_50MHZ)
1 v$ W, D* p r7 K% } I2caRegs.I2CPSC.all = 4; // Prescaler - need 7-12 Mhz on module clk$ d" }: H) ~% I' {& Q" b
#endif# c/ d, x- a; N5 `2 W3 a' c& p
5 ]: b, {3 g) E8 \) t" |2 s( ? #if (CPU_FRQ_60MHZ)
- V9 a$ f3 Y% A0 |$ y0 d I2caRegs.I2CPSC.all = 6; // Prescaler - need 7-12 Mhz on module clk
7 H! \: q1 L% z. ~ #endif
4 {7 r% O- l+ \6 h8 z6 U: ^ I2caRegs.I2CCLKL = 10; // NOTE: must be non zero- E, ~+ A9 D* C
I2caRegs.I2CCLKH = 5; // NOTE: must be non zero9 E ?! X5 w7 L* o w9 C
I2caRegs.I2CIER.all = 0x00; // Enable SCD & ARDY interrupts
! F5 _+ R u( n$ W5 B! ~6 }! ]1 ~3 D
I2caRegs.I2CMDR.all = 0x0020; // Take I2C out of reset
+ v; G* C# F Z" ~/ |: E // Stop I2C when suspended
7 J( ?* H2 j, l$ b
2 {9 R$ q9 c; w# f @" n# s I2caRegs.I2CFFTX.all = 0x0000; // Enable FIFO mode and TXFIFO2 |9 q0 z7 a# o' `( _
I2caRegs.I2CFFRX.all = 0x0000; // Enable RXFIFO, clear RXFFINT,
5 F- W! {/ k2 X* l
. n& B0 t$ S/ L return;
3 \, d- `% Y2 f* Q% k3 i: |}# B2 _1 H, c% w
4、执行I2C模块接收或者发送。 D$ W* y* b6 R3 I- n
//I2C模块发送数据到I2C器件。
; |: B( d8 T) p5 ~* Avoid I2CA_SendData(void)
; T3 [/ C0 G1 F; H) N8 f{/ m: Y* k, X3 ]
Uint16 i;2 F4 W ^2 {* B7 \
I2caRegs.I2CSAR = I2C_SLAVE_ADDR; //Set slave address- I5 P) J. x/ B7 s
I2caRegs.I2CCNT = I2C_NUMBYTES + 2; //Set count to 5 characters plus 2 address bytes$ e% ]& N! H9 e5 k8 |
I2caRegs.I2CDXR = I2C_EEPROM_HIGH_ADDR; //Send eeprom high address
3 ?$ u/ u+ h3 z; j I2caRegs.I2CMDR.bit.TRX = 1; //Set to Transmit mode! N" w) Z3 I! x) {) d! N/ d
I2caRegs.I2CMDR.bit.MST = 1; //Set to Master mode) @7 M: L0 T9 e7 f
I2caRegs.I2CMDR.bit.FREE = 1; //Run in FREE mode* H& r3 W( p% j& Y7 q# X2 e" @$ T
I2caRegs.I2CMDR.bit.STP = 1; //Stop when internal counter becomes 0
4 }; J7 z. c) z+ q n I2caRegs.I2CMDR.bit.STT = 1; //Send the start bit, transmission will follow
, k& W1 \4 W! n5 ~7 e$ i8 p while(I2caRegs.I2CSTR.bit.XRDY == 0){}; //Do nothing till data is shifted out
+ R3 [* V1 d' T* R+ B; _7 E" m; G I2caRegs.I2CDXR = I2C_EEPROM_LOW_ADDR; //Send eeprom low address. ^$ k# _* ?$ }# c. m; Z1 r
) m% `- i7 c. n q" x: | for(i = 0; i < I2C_NUMBYTES; i++){" ]& \; _& v+ K
while(I2caRegs.I2CSTR.bit.XRDY == 0){}; //Do nothing till data is shifted out
9 d! ~" i/ [. f# _ I2caRegs.I2CDXR = TxdData; //Send out the message8 \% K) A! t0 Q5 n) q: J
}
* B5 i% o4 k' ]+ a+ }# [}$ G# I- Z6 g; `9 ]* p, {
//I2C模块从I2C器件接收数据。* Y9 e7 _ p" F: Q
void I2CA_ReceiveData(void)
! i" |' X6 Q: v l/ L( F{7 _( a2 ]1 e# H9 x) G+ d1 h% M
Uint16 i;
: g6 p. F8 n" U4 K$ ]8 l I2caRegs.I2CSAR = I2C_SLAVE_ADDR; //Set slave address
( _" d+ g# J, \+ R* u/ _! \) } I2caRegs.I2CCNT = 2; //Set count to 2 address bytes: i' J" V7 X0 u# p3 D& D6 a. l- Q
I2caRegs.I2CDXR = I2C_EEPROM_HIGH_ADDR; //Send eeprom high address2 S% O- ?8 W6 m. x
I2caRegs.I2CMDR.bit.TRX = 1; //Set to Transmit mode! i" m% x% h6 G. s4 b! q9 ]
I2caRegs.I2CMDR.bit.MST = 1; //Set to Master mode
; @1 l% s* u* o6 h& v+ w% d I2caRegs.I2CMDR.bit.FREE = 1; //Run in FREE mode9 }1 @& F. c( v% a1 {
I2caRegs.I2CMDR.bit.STP = 0; //Dont release the bus after Tx$ k) ^% _( r$ Y# p. d% s/ g
I2caRegs.I2CMDR.bit.STT = 1; //Send the start bit, transmission will follow
' L4 O3 `! v+ }; Q4 b% T
& a9 Q9 h+ @" W; t# u while(I2caRegs.I2CSTR.bit.XRDY == 0){}; //Do nothing till data is shifted out
7 `8 R3 b4 n0 n( x: Q% o1 w$ L# } I2caRegs.I2CDXR = I2C_EEPROM_LOW_ADDR; //Send eeprom low address3 j, z1 n2 s9 j8 V6 s- O; u; o) {
I2caRegs.I2CCNT = I2C_NUMBYTES; //read 5 bytes from eeprom6 B! b: q; U9 u9 U
I2caRegs.I2CMDR.bit.TRX = 0; //Set to Recieve mode
& n+ }* R9 w( W! H I2caRegs.I2CMDR.bit.MST = 1; //Set to Master mode: i% z& i, i, d g( _
I2caRegs.I2CMDR.bit.FREE = 1; //Run in FREE mode
! b4 p# x1 d+ v9 o I2caRegs.I2CMDR.bit.STP = 1; //Stop when internal counter becomes 0+ z+ _2 P! z* m% l
I2caRegs.I2CMDR.bit.STT = 1; //Repeated start, Reception will follow7 o; O* o- a: i3 I- R
for(i = 0; i < I2C_NUMBYTES; i++){ x: Y" U, I) V J M0 b
while(I2caRegs.I2CSTR.bit.RRDY == 0){}; //I2CDRR not ready to read?& T, _9 y$ W1 ]; R" n# }0 M; L
RxdData = I2caRegs.I2CDRR;9 \+ c! c3 K' n! |& ?3 U! J1 w
}: a3 \& E+ r+ G
} |
|