The program of the AT24C04 is controlled by the single-chip STC12C5A60S2 (C language)

/**************************************

Main chip : STC12C5A60S2 (1T)
Operating frequency: 12.000MHz
**************************************/

#include "REG51.H"
#include "INTRINS.H"

Typedef unsigned char BYTE;
Typedef unsigned short WORD;

Sbit SCL = P3^4; // AT24C04 clock
Sbit SDA = P3^5; //AT24C04 data

BYTE BUF[16]; //Data buffer

BYTE code TESTDATA[] =
{
0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF
};

Void Delay5us();
Void Delay5ms();
Void AT24C04_Start();
Void AT24C04_Stop();
Void AT24C04_SenDACK(bit ack);
Bit AT24C04_RecvACK();
Void AT24C04_SendByte(BYTE dat);
BYTE AT24C04_RecvByte();
Void AT24C04_ReadPage();
Void AT24C04_WritePage();

Void main()
{
AT24C04_WritePage();
Delay5ms();
AT24C04_ReadPage();

While (1);

}

/**************************************
Write 1 page (16 bytes) of data to AT24C04
Write 16 test data starting from TESTDATA as the 00~0F address of the device.
**************************************/
Void AT24C04_WritePage()
{
BYTE i;

AT24C04_Start(); //Start signal
AT24C04_SendByte(0xa0); //Send device address + write signal
AT24C04_SendByte (0x00); / / send storage unit address

For (i=0; i"16; i++)

{

BUF[i] = AT24C04_RecvByte();

If (i == 15)

{

AT24C04_SendACK(1); //The last data needs to be NAK

}

Else

{

AT24C04_SendACK (0); / / response ACK

}

}

AT24C04_Stop(); //stop signal

}

/**************************************

Delay 5 microseconds (STC12C5A60S2@12M)

Different working environments, you need to adjust this function

This delay function is calculated using a 1T instruction cycle, which is different from the traditional 12T MCU.

**************************************/

Void Delay5us()

{

BYTE n = 4;

While (n--)

{

_nop_();

_nop_();

}

}

/**************************************

Delay 5 milliseconds (STC12C5A60S2@12M)

Different working environments, you need to adjust this function

This delay function is calculated using a 1T instruction cycle, which is different from the traditional 12T MCU.

**************************************/

Void Delay5ms()

{

WORD n = 2500;

While (n--)

{

_nop_();

_nop_();

_nop_();

_nop_();

_nop_();

}

}

/**************************************

Start signal

**************************************/

Void AT24C04_Start()

{

SDA = 1; // Pull the data line high

SCL = 1; // pull up the clock line

Delay5us(); //delay

SDA = 0; //generate a falling edge

Delay5us(); //delay

SCL = 0; // pull down the clock line

}

/**************************************

Stop signal

**************************************/

Void AT24C04_Stop()

{

SDA = 0; // pull down the data line

SCL = 1; // pull up the clock line

Delay5us(); //delay

SDA = 1; //generate a rising edge

Delay5us(); //delay

}

/**************************************

Send a reply signal

Entry parameters: ack (0: ACK 1: NAK)

**************************************/

Void AT24C04_SendACK(bit ack)

{

SDA = ack; //Write response signal

SCL = 1; // pull up the clock line

Delay5us(); //delay

SCL = 0; // pull down the clock line

Delay5us(); //delay

}

/**************************************

Receiving response signal

**************************************/

Bit AT24C04_RecvACK()

{

SCL = 1; // pull up the clock line

Delay5us(); //delay

CY = SDA; //Read response signal

SCL = 0; // pull down the clock line

Delay5us(); //delay

Return CY;

}

/**************************************

Send one byte of data to the IIC bus

**************************************/

Void AT24C04_SendByte(BYTE dat)

{

BYTE i;

For (i=0; i<8; i++) //8-bit counter

{

Dat <<= 1; //remove the highest bit of data

SDA = CY; //Send data port

SCL = 1; // pull up the clock line

Delay5us(); //delay

SCL = 0; // pull down the clock line

Delay5us(); //delay

}

AT24C04_RecvACK();

}

/**************************************

Receive one byte of data from the IIC bus

**************************************/

BYTE AT24C04_RecvByte()

{

BYTE i;

BYTE dat = 0;

SDA = 1; //Enable internal pull-up, ready to read data

For (i=0; i<8; i++) //8-bit counter

{

Dat <<= 1;

SCL = 1; // pull up the clock line

Delay5us(); //delay

Dat |= SDA; //read data

SCL = 0; // pull down the clock line

Delay5us(); //delay

}

Return dat;

}

SENSOR KIT

Sensor Kit,Sensors Modules Kits,Sensor Test Kits,Sensor Electronic Kit

Cixi Zhongyi Electronics Factory , https://www.zybreadboard.com