请选择 进入手机版 | 继续访问电脑版

EDABOSS电子论坛

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 931|回复: 0

[转帖] I2C模块应用篇(查询法)

[复制链接]

106

主题

13

回帖

96

E币

助理工程师

Rank: 3Rank: 3

积分
225
发表于 2018-11-28 16:14:39 | 显示全部楼层 |阅读模式
F282xx/F283xx自身带有I2C模块,但受限于TI的官方例程中使用了fifo,其深度为4,在写EEPROM时,读写地址16位,占了2个字节,最多只留下2个字节存储数据,使用很不方便、实用性也极差。有不少网友在这一块碰壁,甚至逼的没办法使用IO口模拟I2C,放弃使用I2C模块。最近一直在测试28027的I2C模块有所获,可突破发送/接收长度限制(非fifo模式,当然这个长度也要和I2C器件匹)。本查介绍查询方式下的I2C模块使。
1、宏定义I2C模块操作参数4 G1 P; D1 L/ B- B
#define I2C_SLAVE_ADDR        0x50    //器件地址不含读写位。以24Cxx系列为例,器件地址应为:(0xA0>>1),即0x50
#define I2C_NUMBYTES          10        //发送/接收数据长度
#define I2C_EEPROM_HIGH_ADDR  0x00
#define I2C_EEPROM_LOW_ADDR   0x00

2、设置相应GPIO引脚用作I2C引脚。 修改F2802x_I2C.c文件中的void InitI2CGpio()函数。
3、初始化I2C模块。# \0 |9 F0 Q, T: A: z% r& k+ ~
void I2CA_Init(void)5 a' H$ {3 q- ~3 D4 p6 Z
{
   // Initialize I2C+ E5 }, U* ?1 _& I% m. U
   I2caRegs.I2CSAR = 0x0050;        // Slave address - EEPROM control code
- V) B+ s3 _7 Q3 ~* ^
   // I2CCLK = SYSCLK/(I2CPSC+1)$ m! m. X' A% Q: i% P
   #if (CPU_FRQ_40MHZ||CPU_FRQ_50MHZ)2 ~" ?. O# w. Y1 V& x, N
     I2caRegs.I2CPSC.all = 4;       // Prescaler - need 7-12 Mhz on module clk; Z/ g6 g' J& O, [1 i* `8 q
   #endif$ q/ P: @9 H2 W. }+ W4 T
. _; k( h- V, r& N+ ~: ]
   #if (CPU_FRQ_60MHZ)
     I2caRegs.I2CPSC.all = 6;       // Prescaler - need 7-12 Mhz on module clk# H$ Q! P4 n1 e
   #endif% h( Z: T1 i" T! D8 p" N; C
   I2caRegs.I2CCLKL = 10;           // NOTE: must be non zero
   I2caRegs.I2CCLKH = 5;            // NOTE: must be non zero/ G% p& F. d1 H$ F. |0 z
   I2caRegs.I2CIER.all = 0x00;      // Enable SCD & ARDY interrupts
4 g  @. Q; q6 o" p
   I2caRegs.I2CMDR.all = 0x0020;    // Take I2C out of reset! R8 b5 ~; L+ P" b8 I
                                    // Stop I2C when suspended: f& d# C* n' O1 C: G

   I2caRegs.I2CFFTX.all = 0x0000;   // Enable FIFO mode and TXFIFO
   I2caRegs.I2CFFRX.all = 0x0000;   // Enable RXFIFO, clear RXFFINT,# a% A6 ^8 p0 ]4 @' _; u

   return;
}
4、执行I2C模块接收或者发送。
//I2C模块发送数据到I2C器件。
void  I2CA_SendData(void)
{
           Uint16 i;
           I2caRegs.I2CSAR = I2C_SLAVE_ADDR;                         //Set slave address0 K+ f4 c0 G2 s. N5 U8 Y
           I2caRegs.I2CCNT = I2C_NUMBYTES + 2;                         //Set count to 5 characters plus 2 address bytes
           I2caRegs.I2CDXR = I2C_EEPROM_HIGH_ADDR;                        //Send eeprom high address- C7 \" i5 i; x# f* D+ f/ Y/ p
           I2caRegs.I2CMDR.bit.TRX = 1;                                 //Set to Transmit mode
           I2caRegs.I2CMDR.bit.MST = 1;                                 //Set to Master mode
           I2caRegs.I2CMDR.bit.FREE = 1;                                //Run in FREE mode
           I2caRegs.I2CMDR.bit.STP = 1;                                 //Stop when internal counter becomes 0
           I2caRegs.I2CMDR.bit.STT = 1;                                 //Send the start bit, transmission will follow+ Z# D) I+ C! F$ o; G! v: ~/ w
           while(I2caRegs.I2CSTR.bit.XRDY == 0){};                 //Do nothing till data is shifted out; i; e2 N1 ~- i# n
           I2caRegs.I2CDXR = I2C_EEPROM_LOW_ADDR;                          //Send eeprom low address3 w9 D" u# }2 I! o6 Q
& r5 Q8 p0 \/ @, M9 P
           for(i = 0; i < I2C_NUMBYTES; i++){1 z- d& ?. C: f5 s
                   while(I2caRegs.I2CSTR.bit.XRDY == 0){};         //Do nothing till data is shifted out" r' R# R! F" z9 R! w3 Y3 a
                   I2caRegs.I2CDXR = TxdData;                                         //Send out the message9 t; v1 X: C1 N  o8 l! Q$ W
           }; G) {& U( K9 \! p  S8 F
}
//I2C模块从I2C器件接收数据。% m  j+ `' E' |* N( |% p3 b: [
void  I2CA_ReceiveData(void)
{2 M3 h' ~8 V5 J! _6 X
           Uint16 i;
           I2caRegs.I2CSAR = I2C_SLAVE_ADDR;                         //Set slave address0 y0 J7 W5 N# |
           I2caRegs.I2CCNT = 2;                                                 //Set count to 2 address bytes1 i6 K* I5 L  v8 S
           I2caRegs.I2CDXR = I2C_EEPROM_HIGH_ADDR;                        //Send eeprom high address7 D0 I/ O. D2 R3 U+ ~
           I2caRegs.I2CMDR.bit.TRX = 1;                                 //Set to Transmit mode
           I2caRegs.I2CMDR.bit.MST = 1;                                 //Set to Master mode" n. G. y2 I2 R# b
           I2caRegs.I2CMDR.bit.FREE = 1;                                //Run in FREE mode/ D  t: ^" w- _5 V0 r
           I2caRegs.I2CMDR.bit.STP = 0;                                 //Dont release the bus after Tx4 \! _* n5 p! ^
           I2caRegs.I2CMDR.bit.STT = 1;                                 //Send the start bit, transmission will follow, n; A1 j; O( Z
& o$ F+ H$ T% D# \
           while(I2caRegs.I2CSTR.bit.XRDY == 0){};                 //Do nothing till data is shifted out* z; A8 U( A$ l3 F
           I2caRegs.I2CDXR = I2C_EEPROM_LOW_ADDR;                         //Send eeprom low address
           I2caRegs.I2CCNT = I2C_NUMBYTES;                                //read 5 bytes from eeprom& Z" j, k! O3 r! {6 I3 i# V
           I2caRegs.I2CMDR.bit.TRX = 0;                                 //Set to Recieve mode. j) q) ~* m1 ~. W7 }
           I2caRegs.I2CMDR.bit.MST = 1;                                 //Set to Master mode
           I2caRegs.I2CMDR.bit.FREE = 1;                                //Run in FREE mode
           I2caRegs.I2CMDR.bit.STP = 1;                                 //Stop when internal counter becomes 0' S. [. x$ W2 n0 U) \( L
           I2caRegs.I2CMDR.bit.STT = 1; //Repeated start, Reception will follow7 b2 k/ T7 B5 p" U" B5 b
           for(i = 0; i < I2C_NUMBYTES; i++){
                   while(I2caRegs.I2CSTR.bit.RRDY == 0){};         //I2CDRR not ready to read?4 v0 y2 h* G* b, ?' q
                   RxdData = I2caRegs.I2CDRR;
           }+ M) H# Y2 O( }  B' Q
}

积分规则
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|EDABOSS电子论坛

GMT+8, 2024-3-29 07:02 , Processed in 0.038315 second(s), 20 queries .

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表