微波EDA网,见证研发工程师的成长!
首页 > 研发问答 > 无线和射频 > TI蓝牙设计交流 > cc2541的iic功能与一般io之间切换

cc2541的iic功能与一般io之间切换

时间:10-02 整理:3721RD 点击:

我现在有1个项目,需要在读取iic数据时,把iic功能开启,读取数据完毕后,把iic关闭且sda和scl都切换到高阻无输入上拉,试了几次都没有实现,那位大侠提供一下这样的处理代码,谢谢!

切换的时候寄存器 PxSEL重新配置了吗?

切换的时候,没有置,需要置吗?谢谢!

#define I2C_ENS1 BV(6)
#define I2C_STA BV(5) //00100000
#define I2C_STO BV(4)
#define I2C_SI BV(3)
#define I2C_AA BV(2)
#define I2C_MST_RD_BIT BV(0) // Master RD/WRn bit to be OR'ed with Slave address.

#define I2C_CLOCK_MASK 0x83

#define I2C_PXIFG P2IFG
#define I2C_IF P2IF
#define I2C_IE BV(1)

#define I2C_WRAPPER_DISABLE() st( I2CWC = 0x0C; )
#define I2C_CLOCK_RATE(x) st( I2CCFG &= ~I2C_CLOCK_MASK; \
I2CCFG |= x; )

#define I2C_CLR_NACK() st( I2CCFG |= I2C_AA; I2CCFG &= ~I2C_SI; )
#define I2C_SET_NACK() st( I2CCFG &= ~I2C_AA; )
#define I2C_SET_ACK() st( I2CCFG |= I2C_AA; )

// Master to Ack to every byte read from the Slave; Slave to Ack Address & Data from the Master.
#define I2C_ENABLE() st( I2CCFG |= (I2C_ENS1 | I2C_AA); )

unsigned int smb_cycle_out=55600;
#define I2C_STRT() st ( \
/* if((I2CIO&0x03)!=0x03) break; */ \
smb_cycle_out=15600; \
I2CCFG &= ~I2C_SI; \
I2CCFG |= I2C_STA; \
while ((I2CCFG & I2C_SI) == 0) \
{ \
NOP();smb_cycle_out--; \
if( smb_cycle_out == 0 ) break; \
} \
I2CCFG &= ~I2C_STA; \
asm("NOP"); \
)
// Must set STO before clearing SI.
#define I2C_STOP() st ( \
smb_cycle_out=15600; \
I2CCFG |= I2C_STO; \
I2CCFG &= ~I2C_SI; \
while ((I2CCFG & I2C_STO) != 0) \
{ \
NOP();smb_cycle_out--; \
if( smb_cycle_out == 0 ) break; \
} \
)

// Stop clock-stretching and then read when it arrives.
#define I2C_READ(_X_) st ( \
smb_cycle_out=15600; \
I2CCFG &= ~I2C_SI; \
while ((I2CCFG & I2C_SI) == 0) \
{ \
NOP();smb_cycle_out--; \
if( smb_cycle_out == 0 ) break; \
} \
(_X_) = I2CDATA; \
)

// First write new data and then stop clock-stretching.
#define I2C_WRITE(_X_) st ( \
smb_cycle_out=15600; \
I2CDATA = (_X_); \
I2CCFG &= ~I2C_SI; \
while ((I2CCFG & I2C_SI) == 0) \
{ \
NOP();smb_cycle_out--; \
if( smb_cycle_out == 0 ) break; \
} \
)

/****************************************************************************************************
******************************************************************************************************
******************************************************************************************************
*****************************************************************************************************/
static uint8 i2cAddr; // Target Slave address pre-shifted up by one leaving RD/WRn LSB as zero.
uint8 pec_enable,smb_pec=0;

void delayms(uint16 ms)
{
uint16 i,j;
for(i=ms;i>0;i--)
{
for(j=587;j>0;j--)
{
}
}
}

//pack 地址为0x16(0x0b<<1)

void HalI2CInit(uint8 address)
{
P1SEL &= ~(1<<1);
P1DIR |= (1<<1);
P1_1 = 1;
delayms(1);
I2CWC=0;
I2CCFG=0;

i2cAddr=address << 1;
I2C_WRAPPER_DISABLE();
I2CADDR = 0; // no multi master support at this time
I2C_CLOCK_RATE(i2cClock_33KHZ);
I2C_ENABLE();
pec_enable=1;
P1DIR|=(1<<5); //P1_5输出状态
P1_5=0;
delayms(3);

}
//pack 地址为0x16(0x0b<<1)

void close_smbus(void)
{
delayms(1);

I2CCFG&=~(1<<6); //sda and scl 到高阻输入
I2CWC=0X80;
P1SEL &= ~(1<<1);
P1DIR &= ~(1<<1);
}

能帮我看看吗?谢谢!

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top