TI中文支持网
TI专业的中文技术问题搜集分享网站

MSP430模拟I2C速率问题。

系统时钟采用内部DCO 1MHZ, IO口模拟I2C读取数据,采用下面程序可以正确读取数据,但是我用逻辑分析仪看到SCL频率只有16KHZ,二球,简单的几个命令读取用时差不多就1S了,如图: 请问模拟I2C如何控制总线速率。 

#define MSP430CLOCKFREQHZ 1000000 // USER CONFIG: Existing Sys Freq
#define I2CDELAYUSEC 20 // USER CONFIG: GPIO change delay
#define GPIODELAYCYCLES ((MSP430CLOCKFREQHZ/1000000)*I2CDELAYUSEC)

#define I2CDELAY MSP430_SWI2CMST_delay() // Macro for GPIO change delay

void MSP430_SWI2CMST_delay(void)
{
__delay_cycles(GPIODELAYCYCLES); // Quick delay
}
void MSP430_SWI2CMST_start(void)
{
SDA_1; // SDA = 1
I2CDELAY; // Quick delay
SCL_1; // SCL = 1
I2CDELAY; // Quick delay
SDA_0; // SDA = 0
I2CDELAY; // Quick delay
SCL_0; // SCL = 0
I2CDELAY; // Quick delay}

void MSP430_SWI2CMST_stop(void)
{ SDA_0; // SDA = 0
I2CDELAY; // Quick delay
SCL_1; // SCL = 1
I2CDELAY; // Quick delay
SDA_1; // SDA = 1
I2CDELAY; // Quick delay}
//成功返回0,失败返回1
unsigned char MSP430_SWI2CMST_txByte(unsigned char data)
{
unsigned char bits, temp, ack;
SCL_0; // SCL = 0
temp = data; // Initialize temp variable
bits = 0x08; // Load I2C bit counter
while (bits != 0x00) // Loop until all bits are shifted
{
if (temp & BIT7) // Test data bit
SDA_1; // SDA = 1
else
SDA_0; // SDA = 0
I2CDELAY; // Quick delay SCL_1; // SCL = 1
if ((PxIN & SCL) == 0) // Wait for any SCL clock stretching
I2CDELAY; // Quick delay
temp = (temp << 1); // Shift bits 1 place to the left
SCL_0; // SCL = 0
bits = (bits – 1); // Loop until 8 bits are sent
}
I2CDELAY;
SDA_1; // SDA = 1
SCL_1; // SCL = 1
if ((PxIN & SCL) == 0) // Wait for any SCL clock stretching
I2CDELAY; // Quick delay
ack = (PxIN & SDA); // Read ACK state from Slave
SCL_0; // SCL = 0
if (ack) // Return ACK state to calling app
{
fail_flag++;
return (1);
} else
{
fail_flag=0;
return (0);
}
}

unsigned char MSP430_SWI2CMST_rxByte(char ack)
{
unsigned char bits, data = 0;

SDA_1; // SDA = 1
bits = 0x08; // Load I2C bit counter
while (bits > 0) // Loop until all bits are read
{
SCL_1; // SCL = 1
if ((PxIN & SCL) == 0) // Wait for any SCL clock stretching
I2CDELAY; // Quick delay
data = (data << 1); // Shift bits 1 place to the left
if (PxIN & SDA) // Check digital input
data = (data + 1); // If input is high, store a '1'
SCL_0; // SCL = 0
I2CDELAY; // Quick delay
bits = (bits – 1); // Decrement I2C bit counter
}
if (ack) // Need to send ACK to Slave?
SDA_0; // Yes, so pull SDA low
else
SDA_1; // No, so keep SDA high
SCL_1; // SCL = 1
I2CDELAY; // Equivalent to sending N(ACK)
SCL_0; // SCL = 0
SDA_1; // SDA = 1

return (data); // Return 8-bit data byte
}

灰小子:

你的I2CDELAY延时太长了,你改小一点。

还有你的dco配置的频率太低了,不知道你用的具体哪个型号,你可以先改成8MHz试试

赞(0)
未经允许不得转载:TI中文支持网 » MSP430模拟I2C速率问题。
分享到: 更多 (0)