【C2000 LaunchPad】I2C模块应用篇(查询法)
[复制链接]
本帖最后由 ltbytyn 于 2016-4-17 16:37 编辑
F282xx/F283xx自身带有I2C模块,但受限于TI的官方例程中使用了fifo,其深度为4,在写EEPROM时,读写地址16位,占了2个字节,最多只留下2个字节存储数据,使用很不方便、实用性也极差。有不少网友在这一块碰壁,甚至逼的没办法使用IO口模拟I2C,放弃使用I2C模块。最近一直在测试28027的I2C模块有所获,可突破发送/接收长度限制(非fifo模式,当然这个长度也要和I2C器件匹)。本查 介绍查 询方式下的I2C模块使。
1、宏定义I2C模块操作参数
#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模块。
void I2CA_Init(void)
{
// Initialize I2C
I2caRegs.I2CSAR = 0x0050; // Slave address - EEPROM control code
// I2CCLK = SYSCLK/(I2CPSC+1)
#if (CPU_FRQ_40MHZ||CPU_FRQ_50MHZ)
I2caRegs.I2CPSC.all = 4; // Prescaler - need 7-12 Mhz on module clk
#endif
#if (CPU_FRQ_60MHZ)
I2caRegs.I2CPSC.all = 6; // Prescaler - need 7-12 Mhz on module clk
#endif
I2caRegs.I2CCLKL = 10; // NOTE: must be non zero
I2caRegs.I2CCLKH = 5; // NOTE: must be non zero
I2caRegs.I2CIER.all = 0x00; // Enable SCD & ARDY interrupts
I2caRegs.I2CMDR.all = 0x0020; // Take I2C out of reset
// Stop I2C when suspended
I2caRegs.I2CFFTX.all = 0x0000; // Enable FIFO mode and TXFIFO
I2caRegs.I2CFFRX.all = 0x0000; // Enable RXFIFO, clear RXFFINT,
return;
}
4、执行I2C模块接收或者发送。
//I2C模块发送数据到I2C器件。
void I2CA_SendData(void)
{
Uint16 i;
I2caRegs.I2CSAR = I2C_SLAVE_ADDR; //Set slave address
I2caRegs.I2CCNT = I2C_NUMBYTES + 2; //Set count to 5 characters plus 2 address bytes
I2caRegs.I2CDXR = I2C_EEPROM_HIGH_ADDR; //Send eeprom high address
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
while(I2caRegs.I2CSTR.bit.XRDY == 0){}; //Do nothing till data is shifted out
I2caRegs.I2CDXR = I2C_EEPROM_LOW_ADDR; //Send eeprom low address
for(i = 0; i < I2C_NUMBYTES; i++){
while(I2caRegs.I2CSTR.bit.XRDY == 0){}; //Do nothing till data is shifted out
I2caRegs.I2CDXR = TxdData; //Send out the message
}
}
//I2C模块从I2C器件接收数据 。
void I2CA_ReceiveData(void)
{
Uint16 i;
I2caRegs.I2CSAR = I2C_SLAVE_ADDR; //Set slave address
I2caRegs.I2CCNT = 2; //Set count to 2 address bytes
I2caRegs.I2CDXR = I2C_EEPROM_HIGH_ADDR; //Send eeprom high address
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 = 0; //Dont release the bus after Tx
I2caRegs.I2CMDR.bit.STT = 1; //Send the start bit, transmission will follow
while(I2caRegs.I2CSTR.bit.XRDY == 0){}; //Do nothing till data is shifted out
I2caRegs.I2CDXR = I2C_EEPROM_LOW_ADDR; //Send eeprom low address
I2caRegs.I2CCNT = I2C_NUMBYTES; //read 5 bytes from eeprom
I2caRegs.I2CMDR.bit.TRX = 0; //Set to Recieve 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; //Repeated start, Reception will follow
for(i = 0; i < I2C_NUMBYTES; i++){
while(I2caRegs.I2CSTR.bit.RRDY == 0){}; //I2CDRR not ready to read?
RxdData = I2caRegs.I2CDRR;
}
}
测试程序见26楼。
赞赏
1
查看全部赞赏