I2C bus is a 2-wire bus. Data line SDA, clock line SCL. Simple structure.

At24c04 is an EEPROM with I2C bus interface. The size is 512 * 8bit. The single chip microcomputer AT89S52 itself does not have I2C bus interface, so a program can be written to simulate I2C bus protocol with parallel port to read and write at24c04

Key points of multiple device communication (1. Level difference, such as communication between PC and single chip microcomputer in serial port communication. The serial port level of PC is + 12V ~ – 12V, and the single chip microcomputer is TTL level 0V ~ + 5V. Therefore, the level conversion chip should be used to change the level. 2. Communication protocol. (serial port communication protocol))The specific agreement content and data format can be found in the data.

Design of AT89S52 MCU parallel port analog I2C bus protocol reading and writing at24c04

The code is as follows:

#include

#define WriteDeviceAddress 0xa0

#define ReadDeviceAddress 0xa1

sbit SCL = P3^4;

sbit SDA = P3^5;

sbit DOG = P0^0;

sbit PP = P0^1;

sbit DOG1 = P0^7;

void DelayMs(unsigned int number)

{

unsigned char tmp;

for(;number!=0;number–,DOG1=!DOG1)

{

for(tmp=112;tmp!=0;tmp–)

{

}

}

}

void Start()

{

SDA = 1;

DelayMs(1);

SCL = 1;

DelayMs(1);

SDA = 0;

DelayMs(1);

SCL = 0;

DelayMs(1);

}

bit Write8bit(unsigned char input)

{

unsigned char tmp;

for(tmp =8;tmp!=0;tmp–)

{

SDA = (bit)(input&0x80);

DelayMs(1);

SCL = 1;

DelayMs(1);

SCL = 0;

DelayMs(1);

input = input 《《 1;

}

return 1;

}

bit TestAck()

{

bit ErrorBit;

SDA = 1;

DelayMs(1);

SCL = 1;

DelayMs(1);

ErrorBit = SDA;

DelayMs(1);

SCL = 0;

DelayMs(1);

return(ErrorBit);

}

void Stop()

{

SCL = 0;

DelayMs(1);

SDA = 0;

DelayMs(1);

SCL = 1;

DelayMs(1);

SDA = 1;

DelayMs(1);

}

void WriteI2C(unsigned char *Wdata, unsigned char RomAddress, unsigned char number)

{

Start();

Write8bit(WriteDeviceAddress);

TestAck();

Write8bit(RomAddress);

TestAck();

for(;number!=0;number–)

{

Write8bit(*Wdata);

TestAck();

Wdata++;

}

Stop();

DelayMs(1);

}

unsigned char Read8Bit()

{

unsigned char tmp,rbyte = 0;

for(tmp=8;tmp!=0;tmp–)

{

SCL = 1;

DelayMs(1);

rbyte = rbyte 《《 1;

DelayMs(1);

rbyte = rbyte|((unsigned char)(SDA));

SCL = 0;

DelayMs(1);

}

return(rbyte);

}

void Ack()

{

SDA = 0;

DelayMs(1);

SCL = 1;

DelayMs(1);

SCL = 0 ;

DelayMs(1);

SDA = 1;

DelayMs(1);

}

void NoAck()

{

SDA = 1;

DelayMs(1);

SCL = 1;

DelayMs(1);

SCL = 0 ;

DelayMs(1);

}

void ReadI2C(unsigned char* RamAddress,unsigned char RomAddress,unsigned char bytes)

{

Start();

Write8bit(WriteDeviceAddress);

TestAck();

Write8bit(RomAddress);

TestAck();

Start();

Write8bit(ReadDeviceAddress);

TestAck();

while(bytes != 1)

{

*RamAddress = Read8Bit();

Ack();

RamAddress++;

bytes–;

}

*RamAddress = Read8Bit();

NoAck();

Stop();

}

void main()

{

unsigned char writeByte[8] = {0xC0,0X34,0X12,0X22,0X11,0X01,0X00,0X00};

unsigned char readByte[8];

unsigned char *addw;

unsigned char *addr;

unsigned char i;

unsigned char ok = 0;

bit write = 1;

DOG = 1;

while(1)

{

if(write == 1)

{

addw = writeByte;

addr = readByte;

WriteI2C(addw,0x00,8);

ReadI2C(addr,0x00,8);

for(i=0;i《8;i++)

{

if(writeByte[i] == readByte[i])

{

ok++;

}

}

if(ok == 8)

{

DOG = 0; // Same p0.0 light

}

else

{

PP = 0; // Different P0.1 is on

}

write = 0;

}

}

}

Leave a Reply

Your email address will not be published. Required fields are marked *