微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > 通过MSP430的IO口模拟I2C总线对AT24C25进行读写程序

通过MSP430的IO口模拟I2C总线对AT24C25进行读写程序

时间:11-27 来源:互联网 点击:

unsigned int EEPROM_PageWrite(unsigned int page,unsigned char* p,unsigned char n)
{
unsigned char Dev_addr;
unsigned char AddrLow;
unsigned char AddrHigh;
unsigned int tmp;
if((n > 64)|(page > 512)) //根据读写的设备而变更为适合的页数和每页字节数
{
return OutOfRang;
}
tmp = ((unsigned int)page) < 6; //得出页首地址
AddrLow = (unsigned char)tmp;
AddrHigh = (unsigned char)(tmp>>8);
Dev_addr = 0xa0 | (AddrHigh < 1);
i2c_start();
i2c_SendByte(Dev_addr);
if(i2c_check_ACK() == FALSE)
{
return AckError;
}
i2c_delay_ms(5);
i2c_SendByte(AddrHigh);
if(i2c_check_ACK() == FALSE)
{
return AckError;
}
i2c_SendByte(AddrLow);
if(i2c_check_ACK() == FALSE)
{
return AckError;
}
while(n--)
{
i2c_SendByte(*p++);
if(i2c_check_ACK() == FALSE)
{
return AckError;
}
}
i2c_stop();
i2c_delay_ms(10);
return 0;
}

unsigned char EEPROM_SequentialRead(unsigned int addr,unsigned int n,unsigned char* p)
{
unsigned char Dev_addr; //设备地址
unsigned char AddrLow;
unsigned char AddrHigh;
if(n > (32768 - addr)) //检查预写入地址是否有效
{
return OutOfAddr;
}
AddrLow = (unsigned char)addr;
AddrHigh = (unsigned char)(addr>>8);
Dev_addr = 0xa0|(AddrHigh<1);
i2c_start();
i2c_SendByte(Dev_addr);
if(i2c_check_ACK() == FALSE)
{
return AckError;
}
i2c_delay_ms(5);
i2c_SendByte(AddrHigh);
if(i2c_check_ACK() == FALSE)
{
return AckError;
}
i2c_SendByte(AddrLow);
if(i2c_check_ACK() == FALSE)
{
return AckError;
}
i2c_delay_ms(5);
i2c_start();
Dev_addr = 0xa1|(AddrHigh<1);
i2c_SendByte(Dev_addr);
if(i2c_check_ACK() == FALSE)
{
return AckError;
}
while(n--)
{
*p = i2c_RevByte();
p++;
if(n)
i2c_SendAck();
else
i2c_SendNoAck();
}
i2c_stop();
return 0;
}

main()
{
//unsigned char tt,tt1;
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
DCOCTL = 0X73;
BCSCTL1= 0X87; // 设置时钟频率4.00MHz
P5DIR |=0X03;
P5OUT |=0X03;
//EEPROM_ByteWrite(0x0,12);
//tt=EEPROM_RandomRead(0x0);
//tt1=tt;
EEPROM_PageWrite(2,dat,14);
EEPROM_SequentialRead(128,14,x);
}

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top