This thread has been locked.
If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.
I2C的通信代码参考这个帖子,先谢谢。
http://www.deyisupport.com/question_answer/microcontrollers/msp430/f/55/p/96783/251963.aspx#251963
写入,更改分辨率:
I2cByteWrite(TMP100_ADDR,0x01,1<<6);
以下为方法:
void I2cByteWrite(uchar device, uint addr, uchar bytedata)
{
Start_I2C();
delay_us(1);
SendByte(device);
delay_us(1);
SendByte(addr);
delay_us(1);
SendByte(bytedata);
delay_us(1);
Stop_I2C();
}
uchar ReceiveByte(uchar b)
{
uchar i;
uchar temp;
uchar Dat = 0;
SDA_DIR_IN;
for(i=0;i<8;i++)
{
SCL1;
delay_us(5);
Dat=Dat<<1;
delay_us(1);
temp=SDA_IN;
if(temp&0x10)
Dat|=0x01;
else
Dat|=0x00;
delay_us(1);
SCL0;
delay_us(5);
}
SDA_DIR_OUT;
if(b)
SDA1
else
SDA0;
delay_us(1);
SCL1;
delay_us(1);
SCL0;
delay_us(1);
SDA1;
delay_us(1);
return Dat;
}
uchar I2cByteRead(uchar device, uint addr)
{
uchar Dat=0;
Start_I2C();
SendByte(device);
SendByte(addr);
Start_I2C();
SendByte(0xd1);
Dat=ReceiveByte(1);
Stop_I2C();
return Dat;
}
void Start_I2C(void)
{
SDA_DIR_OUT;
SCL_DIR_OUT;
SDA1;
delay_us(5);
SCL1;
delay_us(5);
SDA0;
delay_us(5);
SCL0;
delay_us(2);
}
void Stop_I2C(void)
{
SDA_DIR_OUT;
SCL_DIR_OUT;
SDA0;
delay_us(5);
SCL1;
delay_us(5);
SDA1;
delay_us(1);
}
void SendByte(uchar c)
{
uchar bitCnt;
SDA_DIR_OUT;
SCL_DIR_OUT;
SCL0;
delay_us(10);
for(bitCnt=0;bitCnt<8;bitCnt++)
{
if(c&0x80)
SDA1
else
SDA0;
delay_us(1);
SCL0;
delay_us(1);
SCL1;
delay_us(5);
c=c<<1;
SCL0;
delay_us(5);
}
SDA1;
delay_us(1);
SCL0;
delay_us(1);
SCL1;
delay_us(1);
SCL0;
}