|
EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
F282xx/F283xx自身带有I2C模块,但受限于TI的官方例程中使用了fifo,其深度为4,在写EEPROM时,读写地址16位,占了2个字节,最多只留下2个字节存储数据,使用很不方便、实用性也极差。有不少网友在这一块碰壁,甚至逼的没办法使用IO口模拟I2C,放弃使用I2C模块。最近一直在测试28027的I2C模块有所获,可突破发送/接收长度限制(非fifo模式,当然这个长度也要和I2C器件匹)。本查介绍查询方式下的I2C模块使。
: y0 p- I8 f* V& |1、宏定义I2C模块操作参数/ d$ ^# q2 O! |: A6 L1 ]
#define I2C_SLAVE_ADDR 0x50 //器件地址不含读写位。以24Cxx系列为例,器件地址应为:(0xA0>>1),即0x506 \. I) u" @2 K3 S, |! K
#define I2C_NUMBYTES 10 //发送/接收数据长度
: i4 @; C3 t" y, q8 O% i1 G#define I2C_EEPROM_HIGH_ADDR 0x00! c/ N8 ^. X0 ~- y U
#define I2C_EEPROM_LOW_ADDR 0x00 ?' |$ Z) B6 C5 v
0 P' D0 v8 h5 b5 Z$ G. L3 N2、设置相应GPIO引脚用作I2C引脚。 修改F2802x_I2C.c文件中的void InitI2CGpio()函数。
! i# x8 g) }5 _3、初始化I2C模块。3 v1 i! c& D/ @3 q' G
void I2CA_Init(void)& [! h# V' j+ a( B
{
3 w/ U: S, a0 r* H: f- A/ W/ w7 ] ^ // Initialize I2C# k2 \6 [; a( a
I2caRegs.I2CSAR = 0x0050; // Slave address - EEPROM control code) H* Q& m% T! p- e9 S. }, o, J
N( x. g3 v5 z; D- w$ S. B // I2CCLK = SYSCLK/(I2CPSC+1). R) E2 v! e& g7 w5 `: ]- F
#if (CPU_FRQ_40MHZ||CPU_FRQ_50MHZ)
# J+ X6 d! d$ e6 v* |* Q6 j# f I2caRegs.I2CPSC.all = 4; // Prescaler - need 7-12 Mhz on module clk
% r! ]) p. @- F1 V- G8 A #endif, q3 p( I- j3 y, q: N; \) ?# I
4 T6 Y" ^/ j0 Q4 k( S1 c. R9 K #if (CPU_FRQ_60MHZ)* Q* E' b6 j' y- A1 J5 a
I2caRegs.I2CPSC.all = 6; // Prescaler - need 7-12 Mhz on module clk
4 p$ E2 B" u. Z8 f- [ #endif
& Y, L2 k. h) @9 Z" G3 m I2caRegs.I2CCLKL = 10; // NOTE: must be non zero' m$ ~5 U# C# x4 l2 o* }& ?8 }
I2caRegs.I2CCLKH = 5; // NOTE: must be non zero8 P+ c, M2 z |4 [* g2 S0 |1 Y# Q
I2caRegs.I2CIER.all = 0x00; // Enable SCD & ARDY interrupts
' [0 [% ^$ \& e; b
6 E/ A9 G& \5 r/ g3 D% g2 j$ k$ \6 s I2caRegs.I2CMDR.all = 0x0020; // Take I2C out of reset
+ u# n& T \) E) A. S3 J, H1 p // Stop I2C when suspended/ V( m; D8 _0 I, g9 Q3 P
7 J5 i0 u# p% L
I2caRegs.I2CFFTX.all = 0x0000; // Enable FIFO mode and TXFIFO3 p c) p1 f' G2 ?: x2 q/ w9 O+ f
I2caRegs.I2CFFRX.all = 0x0000; // Enable RXFIFO, clear RXFFINT,
5 y( g. [. ?8 k: l4 y3 a8 q$ A; o6 G5 t) j
return;
) K8 r: J) |$ A6 a}
3 Q, l( N! n; L6 v' Y4、执行I2C模块接收或者发送。
% t# {9 G" e$ l7 m; R% P//I2C模块发送数据到I2C器件。
+ N$ v) P2 \, }# Gvoid I2CA_SendData(void), _/ c, F! k2 n% p" H' S
{
* J# i+ V* K8 R4 w% @( n# s& O Uint16 i;
+ C9 q/ j$ R" v6 a I2caRegs.I2CSAR = I2C_SLAVE_ADDR; //Set slave address
5 }7 \. i% L& A4 W5 }% r7 \. g I2caRegs.I2CCNT = I2C_NUMBYTES + 2; //Set count to 5 characters plus 2 address bytes
! a, v% W: P1 H4 d; R8 y' }6 ^ I2caRegs.I2CDXR = I2C_EEPROM_HIGH_ADDR; //Send eeprom high address
; l/ K- v; y! l P I2caRegs.I2CMDR.bit.TRX = 1; //Set to Transmit mode0 c+ V; ?* {- H# [& K% P T* P
I2caRegs.I2CMDR.bit.MST = 1; //Set to Master mode0 ]; j6 T5 y! U- X
I2caRegs.I2CMDR.bit.FREE = 1; //Run in FREE mode
$ s- c/ I6 o5 z# [9 [6 M I2caRegs.I2CMDR.bit.STP = 1; //Stop when internal counter becomes 0
+ R( ]. ]" L/ D2 O: | I2caRegs.I2CMDR.bit.STT = 1; //Send the start bit, transmission will follow! N( k1 _' h9 q! [4 J! x
while(I2caRegs.I2CSTR.bit.XRDY == 0){}; //Do nothing till data is shifted out
( [+ _) \- E6 `# @8 b; l I2caRegs.I2CDXR = I2C_EEPROM_LOW_ADDR; //Send eeprom low address
" P6 m% |- }% M" }! u% g
8 m1 E1 A( H% h! b# k/ Z- g for(i = 0; i < I2C_NUMBYTES; i++){3 w; |2 i. y) y4 `
while(I2caRegs.I2CSTR.bit.XRDY == 0){}; //Do nothing till data is shifted out
: f. g. K4 S" d; ^ I2caRegs.I2CDXR = TxdData; //Send out the message
. g: m. E/ t& \) O }/ s, M4 {; F, w4 z. m( }
}) K$ N1 n+ ?" g# ~8 h' T
//I2C模块从I2C器件接收数据。
( \7 j' G* o- {; a) s$ Q* Wvoid I2CA_ReceiveData(void)
) }' I g8 M6 p- P" t{
' p6 d& \/ k/ o2 `- u: i Uint16 i;8 F) ]$ l' C9 O
I2caRegs.I2CSAR = I2C_SLAVE_ADDR; //Set slave address! X, p C- n: G8 B* c; y& L9 i% E
I2caRegs.I2CCNT = 2; //Set count to 2 address bytes3 B6 Z) d7 Z$ _3 k5 i
I2caRegs.I2CDXR = I2C_EEPROM_HIGH_ADDR; //Send eeprom high address
9 S7 g$ \. g/ h+ f I2caRegs.I2CMDR.bit.TRX = 1; //Set to Transmit mode
9 g3 x- C2 B5 ^ Y/ r1 H I2caRegs.I2CMDR.bit.MST = 1; //Set to Master mode2 ?2 u3 ~/ X4 Z' \
I2caRegs.I2CMDR.bit.FREE = 1; //Run in FREE mode
, [' S2 d( b! r6 O I2caRegs.I2CMDR.bit.STP = 0; //Dont release the bus after Tx+ a) ~# I0 x: K8 I
I2caRegs.I2CMDR.bit.STT = 1; //Send the start bit, transmission will follow
! ?4 J w! L9 Q q) b6 E4 J/ e! H% a6 `7 s# p
while(I2caRegs.I2CSTR.bit.XRDY == 0){}; //Do nothing till data is shifted out" u2 u+ i: q6 f
I2caRegs.I2CDXR = I2C_EEPROM_LOW_ADDR; //Send eeprom low address4 Z7 {6 `$ } D: ?' e
I2caRegs.I2CCNT = I2C_NUMBYTES; //read 5 bytes from eeprom
6 L4 K4 p$ W; P9 `5 g I2caRegs.I2CMDR.bit.TRX = 0; //Set to Recieve mode
: J3 B0 U4 l0 ]* Y6 Y; s I2caRegs.I2CMDR.bit.MST = 1; //Set to Master mode6 n$ Q; V ^" A) E8 h
I2caRegs.I2CMDR.bit.FREE = 1; //Run in FREE mode
' K% E; w0 \0 u. F& l; u* B I2caRegs.I2CMDR.bit.STP = 1; //Stop when internal counter becomes 0
4 @+ C6 r' c8 L, z# E I2caRegs.I2CMDR.bit.STT = 1; //Repeated start, Reception will follow# j4 D6 F8 V5 C [
for(i = 0; i < I2C_NUMBYTES; i++){
8 ?+ U6 b, [+ A3 ?4 @1 _+ v0 u' z while(I2caRegs.I2CSTR.bit.RRDY == 0){}; //I2CDRR not ready to read?
3 W9 |6 N0 [: Q# C; ?8 ]3 c RxdData = I2caRegs.I2CDRR;
; \0 Z3 [$ r+ i }
; }, R: ^* i4 V+ \} |
|