找回密码
 注册
关于网站域名变更的通知
查看: 718|回复: 1
打印 上一主题 下一主题

I2C模块应用篇(查询法)

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2016-6-28 15:05 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

EDA365欢迎您登录!

您需要 登录 才可以下载或查看,没有帐号?注册

x
F282xx/F283xx自身带有I2C模块,但受限于TI的官方例程中使用了fifo,其深度为4,在写EEPROM时,读写地址16位,占了2个字节,最多只留下2个字节存储数据,使用很不方便、实用性也极差。有不少网友在这一块碰壁,甚至逼的没办法使用IO口模拟I2C,放弃使用I2C模块。最近一直在测试28027的I2C模块有所获,可突破发送/接收长度限制(非fifo模式,当然这个长度也要和I2C器件匹)。本查介绍查询方式下的I2C模块使。
3 y4 m' k0 E6 M2 P1、宏定义I2C模块操作参数8 S$ U* a( P+ t
#define I2C_SLAVE_ADDR        0x50    //器件地址不含读写位。以24Cxx系列为例,器件地址应为:(0xA0>>1),即0x50
# R! y8 ]- _, P. N9 o2 k; b#define I2C_NUMBYTES          10        //发送/接收数据长度
2 F* P; z- N7 V+ k" e#define I2C_EEPROM_HIGH_ADDR  0x00+ O% v* O! J# W* d8 L; ]
#define I2C_EEPROM_LOW_ADDR   0x00
/ _/ ~7 ^  O  ~7 n3 e. Q' x: W9 S
2、设置相应GPIO引脚用作I2C引脚。 修改F2802x_I2C.c文件中的void InitI2CGpio()函数。
, ^, z. D/ ^( n- N7 Q3、初始化I2C模块。
( }( R) T3 Z/ C3 dvoid I2CA_Init(void)
2 Z- x6 @( f: B! l6 w{
/ H: u; l; E  \7 N6 z, @   // Initialize I2C. |% I  K3 c4 J
   I2caRegs.I2CSAR = 0x0050;        // Slave address - EEPROM control code# V4 F6 _  Q( r+ k) j" q6 N

% k& B+ S/ b! S$ @2 g$ Q4 }   // I2CCLK = SYSCLK/(I2CPSC+1), z" _* ~" ^! Y: }# W# a- H
   #if (CPU_FRQ_40MHZ||CPU_FRQ_50MHZ)
: K4 W2 l  ?" o! ~% |' Z' H3 f     I2caRegs.I2CPSC.all = 4;       // Prescaler - need 7-12 Mhz on module clk3 m  Q4 I$ P3 W' W. H: |
   #endif
2 U9 \9 f# R* h- ?3 f- }; ^8 f
   #if (CPU_FRQ_60MHZ)
( E; {) V5 Y: I1 L$ V' u3 S( G4 O     I2caRegs.I2CPSC.all = 6;       // Prescaler - need 7-12 Mhz on module clk- `4 q) w/ @# M! W1 P2 S
   #endif
  z% t# V8 q, S+ r   I2caRegs.I2CCLKL = 10;           // NOTE: must be non zero  x5 `2 t$ |: q  |
   I2caRegs.I2CCLKH = 5;            // NOTE: must be non zero
# @! H' D; l# \. [" H$ E   I2caRegs.I2CIER.all = 0x00;      // Enable SCD & ARDY interrupts
5 i! `% O# P2 u$ L) x- c' ^5 |1 w7 P6 }) I  J+ @! ]
   I2caRegs.I2CMDR.all = 0x0020;    // Take I2C out of reset! O0 k5 Q. _/ y5 U* A3 ?' ^
                                    // Stop I2C when suspended+ |  ^6 p8 C7 t% S4 M$ U: {7 M5 r

- w* a" e* [, q: I! z2 z   I2caRegs.I2CFFTX.all = 0x0000;   // Enable FIFO mode and TXFIFO9 S8 y& H6 @# c/ |
   I2caRegs.I2CFFRX.all = 0x0000;   // Enable RXFIFO, clear RXFFINT,7 }8 ]6 J: a4 {: v8 L

% R& {: F+ ]2 V/ \& M* J% _  P   return;* |# m9 \2 P7 ^; o, M# u
}" u) t4 B+ l: J* a
4、执行I2C模块接收或者发送。2 n/ }/ Q# n. U& p6 }$ B3 A; H
//I2C模块发送数据到I2C器件。
" D  r$ `& a: P5 @, H/ avoid  I2CA_SendData(void)$ U" i* Q7 N5 l  t& t" u
{
" I6 V0 T6 @* w8 D           Uint16 i;
3 b2 w9 q- H* C  j" `. ^# i2 e           I2caRegs.I2CSAR = I2C_SLAVE_ADDR;                         //Set slave address
# N2 i  s8 r! P) _           I2caRegs.I2CCNT = I2C_NUMBYTES + 2;                         //Set count to 5 characters plus 2 address bytes
1 b+ k* g9 P3 ~- q( q           I2caRegs.I2CDXR = I2C_EEPROM_HIGH_ADDR;                        //Send eeprom high address5 B1 V* _% G, M# S9 ?# I2 L2 U" r
           I2caRegs.I2CMDR.bit.TRX = 1;                                 //Set to Transmit mode0 B, u+ A( D3 ?( y+ o
           I2caRegs.I2CMDR.bit.MST = 1;                                 //Set to Master mode
% s5 W" @. S. Q  h) I% W0 F1 H           I2caRegs.I2CMDR.bit.FREE = 1;                                //Run in FREE mode
7 S9 z' O/ _! B/ B           I2caRegs.I2CMDR.bit.STP = 1;                                 //Stop when internal counter becomes 0
$ f2 l% _1 g' t: n2 k           I2caRegs.I2CMDR.bit.STT = 1;                                 //Send the start bit, transmission will follow
9 S* ]3 y; b, P$ r2 b8 E( w# n           while(I2caRegs.I2CSTR.bit.XRDY == 0){};                 //Do nothing till data is shifted out
9 s2 p" Y- n/ @( z. Q. _0 H5 ]: ?           I2caRegs.I2CDXR = I2C_EEPROM_LOW_ADDR;                          //Send eeprom low address
& h7 Z# Q% O( C1 [2 `- W( R
( P0 ]! x7 [$ h           for(i = 0; i < I2C_NUMBYTES; i++){
, c2 B/ @. j/ y  g2 g: H/ V                   while(I2caRegs.I2CSTR.bit.XRDY == 0){};         //Do nothing till data is shifted out7 R) c! ^) ~1 o+ e4 V5 K! E5 g7 |
                   I2caRegs.I2CDXR = TxdData;                                         //Send out the message) M' p" w7 }0 r& L  r0 j5 |3 m  ]
           }2 [; b/ \+ n5 V" {2 v$ P! P
}
! o4 h; O  J7 K: h( k7 d5 }//I2C模块从I2C器件接收数据。9 _$ |6 }$ k9 R0 B4 w
void  I2CA_ReceiveData(void)
+ m3 _8 ?; Z0 |- w# {) ?; _{0 G$ B% r. c$ @; y  t
           Uint16 i;
+ Y& }/ E2 b$ f# B$ v           I2caRegs.I2CSAR = I2C_SLAVE_ADDR;                         //Set slave address
; J) i% A4 g' \# W0 D! W7 y           I2caRegs.I2CCNT = 2;                                                 //Set count to 2 address bytes+ q/ D+ x8 q$ C/ r: r9 Q
           I2caRegs.I2CDXR = I2C_EEPROM_HIGH_ADDR;                        //Send eeprom high address
* u0 j1 ^: F2 s& l, [3 q$ q2 ]           I2caRegs.I2CMDR.bit.TRX = 1;                                 //Set to Transmit mode+ S: l9 ]( l9 ]0 r" c
           I2caRegs.I2CMDR.bit.MST = 1;                                 //Set to Master mode
+ w5 b8 G9 T" W. l+ Q           I2caRegs.I2CMDR.bit.FREE = 1;                                //Run in FREE mode
& f9 v  J: W9 U2 b           I2caRegs.I2CMDR.bit.STP = 0;                                 //Dont release the bus after Tx
8 x' o3 l! M# H( R% \) c           I2caRegs.I2CMDR.bit.STT = 1;                                 //Send the start bit, transmission will follow3 X( M' X+ T0 @9 _& K- N
6 {' K7 p( j7 n, j; Q5 Y
           while(I2caRegs.I2CSTR.bit.XRDY == 0){};                 //Do nothing till data is shifted out5 c% o* Q! F& q/ Q: b
           I2caRegs.I2CDXR = I2C_EEPROM_LOW_ADDR;                         //Send eeprom low address
" N( Q7 l: T  Y4 A, z           I2caRegs.I2CCNT = I2C_NUMBYTES;                                //read 5 bytes from eeprom
: o) c: P: u( ^; b& W; P           I2caRegs.I2CMDR.bit.TRX = 0;                                 //Set to Recieve mode
, K( l; M) c4 R           I2caRegs.I2CMDR.bit.MST = 1;                                 //Set to Master mode
- V, ]& C) i0 i& W/ v/ U8 p* l1 v           I2caRegs.I2CMDR.bit.FREE = 1;                                //Run in FREE mode
6 Z9 a1 b# I# L6 c& B           I2caRegs.I2CMDR.bit.STP = 1;                                 //Stop when internal counter becomes 0
5 c: a4 D% y7 A: {           I2caRegs.I2CMDR.bit.STT = 1; //Repeated start, Reception will follow2 [# ]9 T, z! ~/ z
           for(i = 0; i < I2C_NUMBYTES; i++){' j. g2 P3 s1 Z; d: w( h1 w/ T( ^
                   while(I2caRegs.I2CSTR.bit.RRDY == 0){};         //I2CDRR not ready to read?: M* p; }7 ^  M* W
                   RxdData = I2caRegs.I2CDRR;
* D) T$ E5 a5 B/ j# R. {. O, q           }
0 q1 R5 x( \( W& h}

该用户从未签到

2#
发表于 2016-6-29 10:11 | 只看该作者
学习中,谢谢分享
) |+ v3 K# j  l3 c
您需要登录后才可以回帖 登录 | 注册

本版积分规则

关闭

推荐内容上一条 /1 下一条

EDA365公众号

关于我们|手机版|EDA365电子论坛网 ( 粤ICP备18020198号-1 )

GMT+8, 2025-7-18 10:08 , Processed in 0.109375 second(s), 23 queries , Gzip On.

深圳市墨知创新科技有限公司

地址:深圳市南山区科技生态园2栋A座805 电话:19926409050

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