通過MSP430的IO口模擬I2C總線對(duì)AT24C25進(jìn)行讀寫程序
一、微處理器的兩個(gè)模擬I/O口在和SDA,SCL連接時(shí)必須使用上拉電阻。
本文引用地址:http://butianyuan.cn/article/201611/322430.htm一、I2C總線空閑的時(shí)候,兩條信號(hào)線應(yīng)該維持高電平。否則,上拉電阻上會(huì)有耗電。特別是在上電過程中,I/O線上電平也應(yīng)保持在高電平狀態(tài)。也就是說:當(dāng)Master的I2C使用的是I/O軟件模擬時(shí),一定要保證該兩個(gè)I/O上電默認(rèn)均為輸入(或高阻)或者輸出高電平,切不可默認(rèn)為輸出低電平。I/O默認(rèn)為輸入時(shí),可以通過外部上拉電阻將I2C信號(hào)線拉至高電平。
該程序通過調(diào)試,可以直接應(yīng)用,程序如下:
文 件 名 : AT24C256.S43.C
* 功能描述 : I/O模擬I2C時(shí)序讀寫AT24CXX(支持字節(jié)寫、頁寫、字節(jié)讀、順序讀)
* 作 者 : 夢回大唐
* 創(chuàng)建日期 : 2011-4-18
* 版 本 : Version1.0
*************************************************************************************************/
#include
#define SDA_IN
#define SDA_OUT
#define SDA_LOW
#define SDA_HIGH
#define SCL_IN
#define SCL_OUT
#define SCL_LOW
#define SCL_HIGH
#define
#define
#define
#define
#define
#define
unsigned char
unsigned char
void i2c_delay(unsigned char us);
void i2c_delay_ms(unsigned char ms);
void i2c_start();
void i2c_stop(void);
void i2c_SendAck(void);
void i2c_SendNoAck(void);
unsigned char i2c_check_ACK(void);
void i2c_SendByte(unsigned char data);
unsigned char i2c_RevByte(void);
unsigned char EEPROM_ByteWrite(unsigned int addr,unsigned char data);
unsigned char EEPROM_RandomRead(unsigned int addr);
unsigned char EEPROM_SequentialRead(unsigned int addr,unsigned int n,unsigned char* p);
unsigned int EEPROM_PageWrite(unsigned int page,unsigned char* p,unsigned char n);
void i2c_delay(unsigned char us)
{
unsigned char tmp;
while(us--)
{
for(tmp=0;tmp<4;tmp++)
{
_NOP();
}
}
}
void i2c_delay_ms(unsigned char ms)
{
}
void i2c_start(void)
{
SDA_OUT;
i2c_delay(20);
SDA_HIGH;
i2c_delay(3);
SCL_HIGH;
i2c_delay(2);
SDA_LOW;
i2c_delay(5);
SCL_LOW;
i2c_delay(10);
}
void i2c_stop(void)
{
SDA_OUT;
SDA_LOW;
i2c_delay(2);
SCL_HIGH;
i2c_delay(2);
SDA_LOW;
i2c_delay(2);
SDA_HIGH;
}
void i2c_SendAck(void)
{
SDA_OUT;
SDA_LOW;
i2c_delay(2);
SCL_LOW;
i2c_delay(2);
SCL_HIGH;
i2c_delay(2);
SCL_LOW;
SDA_HIGH;
}
void i2c_SendNoAck(void)
{
SDA_OUT;
SDA_HIGH;
i2c_delay(2);
SCL_LOW;
i2c_delay(2);
SCL_HIGH;
i2c_delay(2);
SCL_LOW;
}
unsigned char i2c_check_ACK(void)
{
unsigned char AckStatus;
SDA_IN;
SCL_HIGH;
i2c_delay(2);
if(P5IN & 0x01)
{
AckStatus = FALSE;
}
else
{
AckStatus = TURE;
}
SCL_LOW;
i2c_delay(2);
SDA_OUT;
return AckStatus;
}
void i2c_SendByte(unsigned char data)
{
unsigned char tmp;
SDA_OUT;
for(tmp=0;tmp<8;tmp++)
{
if(data & 0x80)
{
SDA_HIGH;
}
else
{
SDA_LOW;
}
i2c_delay(2);
SCL_HIGH;
i2c_delay(2);
SCL_LOW;
i2c_delay(2);
data <<= 1;
}
i2c_delay(15);
}
unsigned char i2c_RevByte(void)
{
SDA_OUT;
return DATA;
}
評(píng)論