新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > 通過MSP430的IO口模擬I2C總線對(duì)AT24C25進(jìn)行讀寫程序

通過MSP430的IO口模擬I2C總線對(duì)AT24C25進(jìn)行讀寫程序

作者: 時(shí)間:2016-11-27 來源:網(wǎng)絡(luò) 收藏
再通過微處理器I/O口模擬I2C總線對(duì)AT24C進(jìn)行讀寫之前應(yīng)注意一下兩個(gè)問題:

一、微處理器的兩個(gè)模擬I/O口在和SDA,SCL連接時(shí)必須使用上拉電阻。

本文引用地址:http://m.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é)讀、順序讀)
* 作 者 : 夢(mèng)回大唐

* 創(chuàng)建日期 : 2011-4-18

* 版 本 : Version1.0
*************************************************************************************************/

#include
#define SDA_INP5DIR &=~BIT0// P5.0 IN
#define SDA_OUTP5DIR |=BIT0// P5.0 OUT

#define SDA_LOWP5OUT &=~BIT0// sda=0
#define SDA_HIGHP5OUT |=BIT0// sda=1

#define SCL_INP5DIR &=~BIT1// P5.1 IN
#define SCL_OUTP5DIR |=BIT1// P5.1 OUT

#define SCL_LOWP5OUT &=~BIT1
#define SCL_HIGHP5OUT |=BIT1
#defineW_EEPROM_LENGH14


#defineTURE1
#defineFALSE0
#defineAckError0x55
#defineOutOfRang0xaa
#defineOutOfAddr0xbb

unsigned chardat[W_EEPROM_LENGH]={1,2,3,4,5,6,7,8,9,10,11,12,13,14};
unsigned charx[W_EEPROM_LENGH];

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)
{
unsigned int tmp;
while(ms--)
{
for(tmp=0;tmp<5000;tmp++)
{
_NOP();
}
}

}

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)
{
unsigned char tmp;
unsigned char DATA=0;
SDA_IN;
SCL_LOW;
i2c_delay(2);
for(tmp=0;tmp<8;tmp++)
{
SCL_HIGH;
i2c_delay(2);
DATA <<= 1;
if(P5IN & 0x01)
{
DATA |= 0x01;
}
else
{
DATA &= 0xfe;
}
SCL_LOW;
}
SDA_OUT;
return DATA;
}


上一頁 1 2 下一頁

評(píng)論


技術(shù)專區(qū)

關(guān)閉