|
EDA365欢迎您!
您需要 登录 才可以下载或查看,没有帐号?注册
x
F282xx/F283xx自身带有I2C模块,但受限于TI的官方例程中使用了fifo,其深度为4,在写EEPROM时,读写地址16位,占了2个字节,最多只留下2个字节存储数据,使用很不方便、实用性也极差。有不少网友在这一块碰壁,甚至逼的没办法使用IO口模拟I2C,放弃使用I2C模块。最近一直在测试28027的I2C模块有所获,可突破发送/接收长度限制(非fifo模式,当然这个长度也要和I2C器件匹)。本查介绍查询方式下的I2C模块使。5 c' D4 [9 C7 Y) |
1、宏定义I2C模块操作参数' N% d6 B, T; T' t- v' Z
#define I2C_SLAVE_ADDR 0x50 //器件地址不含读写位。以24Cxx系列为例,器件地址应为:(0xA0>>1),即0x50
0 S1 d3 a' O" |; ~, S" k#define I2C_NUMBYTES 10 //发送/接收数据长度* y5 I3 V- E7 p" z: J8 V. L
#define I2C_EEPROM_HIGH_ADDR 0x00
7 Z+ P) {8 p- Q" o; ]( X#define I2C_EEPROM_LOW_ADDR 0x00
% v3 b( a" o9 p; p% d6 w. [9 }7 ? N2 z
2、设置相应GPIO引脚用作I2C引脚。 修改F2802x_I2C.c文件中的void InitI2CGpio()函数。
V' G; }/ A1 d3、初始化I2C模块。/ V1 F" B' d3 r( r5 d, A' o9 }! I
void I2CA_Init(void). B8 b( U: G8 m+ V- B- x# H
{( p" m$ H' ~& ?* B% @; {, ?, L, X
// Initialize I2C
, q, ]1 w6 W7 \/ I, y2 w, I I2caRegs.I2CSAR = 0x0050; // Slave address - EEPROM control code
+ b5 |. y( r0 e+ Q8 \" e* Y; E8 C1 g' s6 J
// I2CCLK = SYSCLK/(I2CPSC+1)0 f( l' |2 [& Q
#if (CPU_FRQ_40MHZ||CPU_FRQ_50MHZ)
7 N: Z6 K% a- M2 B, G4 |+ [2 h I2caRegs.I2CPSC.all = 4; // Prescaler - need 7-12 Mhz on module clk. f1 y, Y2 M. Z: C. r
#endif; t- ?% Y6 R( Y8 X+ U1 X) f3 H$ Z
/ B/ r( c0 s' M1 J' f
#if (CPU_FRQ_60MHZ)% e# v6 t# G, h6 K' r
I2caRegs.I2CPSC.all = 6; // Prescaler - need 7-12 Mhz on module clk
$ w, y. O; v) ^$ i. n #endif2 O% h5 U# V w; b" ?1 m/ K
I2caRegs.I2CCLKL = 10; // NOTE: must be non zero
b- X1 {7 l% q. a/ T! P" X. P I2caRegs.I2CCLKH = 5; // NOTE: must be non zero
: D" F) @1 q- Z+ a, Y9 {' w( q; l I2caRegs.I2CIER.all = 0x00; // Enable SCD & ARDY interrupts2 V) I9 p6 ]! V
% w6 c. k5 P3 d9 ]4 C& y I2caRegs.I2CMDR.all = 0x0020; // Take I2C out of reset
3 X; h& u0 {8 x( C // Stop I2C when suspended
3 }1 ~* X, R |. e
: w! D( p$ O+ R5 y g0 \ I2caRegs.I2CFFTX.all = 0x0000; // Enable FIFO mode and TXFIFO. T, |3 F8 D! |
I2caRegs.I2CFFRX.all = 0x0000; // Enable RXFIFO, clear RXFFINT,
% y# f* ~/ T1 L! F/ `9 | C4 i5 W
3 N4 o9 w! a" q0 a) f; N0 Y# v return;$ P& y" t& Q- }
}5 I0 [( K% s/ N
4、执行I2C模块接收或者发送。
4 K! P1 p9 V1 F//I2C模块发送数据到I2C器件。
9 T) u1 ]' k, C7 {5 m/ L( }void I2CA_SendData(void)
2 _4 G3 m" {$ [, C! H8 B9 q{! f% ~# B/ G' A" j' g
Uint16 i;
" }/ f$ I h( Q& V8 j2 U. i1 D I2caRegs.I2CSAR = I2C_SLAVE_ADDR; //Set slave address
% W" v/ { e( [6 B6 z4 K* |1 D. } I2caRegs.I2CCNT = I2C_NUMBYTES + 2; //Set count to 5 characters plus 2 address bytes
2 P+ Y$ ~9 L9 e7 ~( j, g I2caRegs.I2CDXR = I2C_EEPROM_HIGH_ADDR; //Send eeprom high address7 `1 y# t1 x$ ]9 f' K- a- y/ F
I2caRegs.I2CMDR.bit.TRX = 1; //Set to Transmit mode, o. p7 B2 L8 A3 ~+ x' z7 C: ~6 S9 @
I2caRegs.I2CMDR.bit.MST = 1; //Set to Master mode- u, P( N3 p6 {5 G9 c% h4 R; i
I2caRegs.I2CMDR.bit.FREE = 1; //Run in FREE mode; @: a; m4 d) p7 M/ S) W
I2caRegs.I2CMDR.bit.STP = 1; //Stop when internal counter becomes 0
# m+ \6 P3 i- V: [( J' N% O I2caRegs.I2CMDR.bit.STT = 1; //Send the start bit, transmission will follow0 N: B- o8 P; q; ]1 H4 C+ ~
while(I2caRegs.I2CSTR.bit.XRDY == 0){}; //Do nothing till data is shifted out
! r) s3 w* v, y! q2 D9 |5 x I2caRegs.I2CDXR = I2C_EEPROM_LOW_ADDR; //Send eeprom low address
2 Z4 t1 l* D( e0 H* C' v* \$ C) G) E* T
for(i = 0; i < I2C_NUMBYTES; i++){
4 v' A1 o2 b) a2 Q$ C+ ~! V9 z! b/ Q while(I2caRegs.I2CSTR.bit.XRDY == 0){}; //Do nothing till data is shifted out2 o2 a1 O5 G3 f5 v) Y3 y3 a" X: o
I2caRegs.I2CDXR = TxdData; //Send out the message
: v! B! s- L1 |6 _7 m5 u" d }
$ M8 m5 D- C$ A5 Y9 y% b& P}2 b1 J/ g' g( W1 r/ s
//I2C模块从I2C器件接收数据。
+ V0 Z. C/ n% n2 X" L9 d" z5 D/ nvoid I2CA_ReceiveData(void)
2 d9 _ w9 B) ^ O# B4 L6 \" j{
9 y& [$ [. s0 Y- F Uint16 i;
' R5 k% d) ]5 w# S, r I2caRegs.I2CSAR = I2C_SLAVE_ADDR; //Set slave address
+ R* V; I5 \/ O3 U" K+ x I2caRegs.I2CCNT = 2; //Set count to 2 address bytes- l, i8 J0 ]! j$ T7 j8 X6 `
I2caRegs.I2CDXR = I2C_EEPROM_HIGH_ADDR; //Send eeprom high address3 z# `3 x5 U" P7 j2 s& q# p
I2caRegs.I2CMDR.bit.TRX = 1; //Set to Transmit mode+ [$ R0 [/ l2 I# Y
I2caRegs.I2CMDR.bit.MST = 1; //Set to Master mode
+ j. H2 I7 J& W! `& i1 Z0 B$ G I2caRegs.I2CMDR.bit.FREE = 1; //Run in FREE mode
0 w# l3 S J5 ?, g6 g- ]4 y I2caRegs.I2CMDR.bit.STP = 0; //Dont release the bus after Tx
' f: [4 I4 k( l2 ~3 `3 D! F I2caRegs.I2CMDR.bit.STT = 1; //Send the start bit, transmission will follow7 D8 \% B4 M5 W0 ^6 I0 S, ~
1 t3 a" ?3 E( u2 f9 Y! j4 M while(I2caRegs.I2CSTR.bit.XRDY == 0){}; //Do nothing till data is shifted out7 } t$ o' X/ ^9 e# [; [
I2caRegs.I2CDXR = I2C_EEPROM_LOW_ADDR; //Send eeprom low address/ w1 E2 g8 f0 ]7 ]4 [
I2caRegs.I2CCNT = I2C_NUMBYTES; //read 5 bytes from eeprom
3 y8 \/ u r. b5 o I2caRegs.I2CMDR.bit.TRX = 0; //Set to Recieve mode1 F7 C) q, W, v
I2caRegs.I2CMDR.bit.MST = 1; //Set to Master mode
, f( W5 E- s$ l; ^+ ?/ V2 U I2caRegs.I2CMDR.bit.FREE = 1; //Run in FREE mode
2 Q a6 ?; r- g2 j2 d I2caRegs.I2CMDR.bit.STP = 1; //Stop when internal counter becomes 0+ e* y1 k* e+ \+ x. \7 ~1 ~2 y' d
I2caRegs.I2CMDR.bit.STT = 1; //Repeated start, Reception will follow
^, e% y/ S9 y \: Y for(i = 0; i < I2C_NUMBYTES; i++){
& }$ P2 G( V; I) G1 q* \ while(I2caRegs.I2CSTR.bit.RRDY == 0){}; //I2CDRR not ready to read?
2 O) u8 h5 ?; K/ ~! N& }$ f8 o RxdData = I2caRegs.I2CDRR;7 G" U" z( o( L" c U5 v0 L
}) m! o. r) S& P" V% @8 `) c
} |
|