|
现在我把代码贴一下。代码我觉得应该没什么问题。是参考了几个类似的例子写出来的。现在问题是写不进去。I2CReceiveACK函数中SDA_IN一直为1 ,也就是SDA线P3.1一直为高,所以一直停在那里。
//使用MSP430F2618 完成该功能,P3.2 作为SCL,P3.1 作为SDA EEPROM型号为24LC64
#define SDA_1 P3OUT |= BIT1 //SDA = 1
#define SDA_0 P3OUT &= ~BIT1 //SDA = 0
#define SCL_1 P3OUT |= BIT2 //SCL = 1
#define SCL_0 P3OUT &= ~BIT2 //SCL = 0
#define DIR_IN P3DIR &= ~BIT1 //I/O 口为输入
#define DIR_OUT P3DIR |= BIT1 //SDA 输出数据
#define SDA_IN ((P3IN >> 1) & 0x01)
void dly_usec(int time)
{
for(int j=0;j
}
void i2c_start(void)
{
DIR_OUT ;
SDA_1 ; dly_usec(5) ;
SCL_1 ; dly_usec(5) ;
SDA_0 ; dly_usec(5) ;
SCL_0 ; dly_usec(9) ;
}
//.............................................................................
void i2c_stop(void)
{
DIR_OUT ;
SDA_0 ; dly_usec(5);
SCL_1 ; dly_usec(6) ;
SDA_1 ;
}
void I2CReceiveACK(void)
{
SCL_1;
dly_usec(5);
DIR_IN;
while(SDA_IN);
DIR_OUT;
SCL_0;
dly_usec(5);
}
void I2CAcknowledge(void)
{
DIR_OUT;
SCL_0;
SDA_0;
dly_usec(5) ;
SCL_1;
dly_usec(5) ;
SCL_0;
}
//....先写高位
void i2c_wr(unsigned char wrbyte)
{
unsigned char bit_count = 0 ;
DIR_OUT ;
while(bit_count<8)
{
SCL_0;
dly_usec(9) ;
if((wrbyte&0x80)==0x80)
SDA_1;
else
SDA_0;
dly_usec(5) ;
SCL_1;
dly_usec(4) ;
wrbyte=wrbyte<<1;
bit_count++ ;dly_usec(10) ;
} // increment bit counter(repeat for 8b)
SCL_0;
SDA_1;
dly_usec(5) ;
}
//....先读高位
unsigned char i2c_rd(void) // read an 8b streaming
{
unsigned char bit_count = 0 ,temp, eep_buf;
DIR_OUT;
SCL_0;
dly_usec(5) ;
SDA_1;
DIR_IN ;
while(bit_count<8)
{
SCL_1;
dly_usec(9) ;
temp=SDA_IN;
dly_usec(9) ;
eep_buf=((eep_buf<<1)|temp);
SCL_0;
dly_usec(9) ;
bit_count++ ;
} // increment bit counter(repeat for 8b)
return eep_buf ;
}
void Byte_Write(unsigned int eep_adr,unsigned char lofsstr,unsigned char *source)
{
unsigned char k = 0 ;
i2c_start(); // START command
i2c_wr(DEV_ADR_WR); // write SLAVE ADDRESS + wr_bit
//I2CReceiveACK();
i2c_wr(eep_adr>>8); // write high address
//I2CReceiveACK();
i2c_wr(eep_adr&0xff) ; // write low address
//I2CReceiveACK();
while(k
{
i2c_wr(*source) ; // stream data in the memory
I2CReceiveACK();
source++ ; // increment pointer
k++ ; // increment counter
} // do until end
i2c_stop() ; // STOP command
dly_usec(10) ; // final write cycle time
}
//............................................................................
void Byte_Read(unsigned int eep_adr,unsigned char lofdstr,unsigned char *dest)
{
unsigned char k = 0 ; // auxiliary counter
i2c_start() ; // START command
i2c_wr(DEV_ADR_WR) ; // write slave address + write_bit
// I2CReceiveACK();
i2c_wr(eep_adr>>8) ; // write the high_byte of the adr
// I2CReceiveACK();
i2c_wr(eep_adr&0xff) ; // write the low_byte of the adr
// I2CReceiveACK();
i2c_start() ; // REPEATED START condition
i2c_wr(DEV_ADR_RD) ; // change the direction of the trsf
// I2CReceiveACK();
if(lofdstr==1) ; // if single byte string
// {;} // do nothing , the byte will be read
// & the NACK will be sent at the end
else
{
while(k 1
// do for the first (lofdstr-1) bytes
{
*dest = i2c_rd() ; // read byte
I2CAcknowledge();
dest++ ; k++ ;
}
} // increment pointer & counter
*dest = i2c_rd() ; // read the last byte
I2CAcknowledge();
//nack_mcu() ; // send the final NACK (for the last byte)
i2c_stop() ;
}
main()
{
while(1)
{
unsigned char mybyte;
unsigned char dst_str[9];
mybyte=0x02;
Byte_Write(0x00,1,&mybyte);
Byte_Read(0x00,1,dst_str);
}
} |
|