C51 read and write AT24C04 source code and AT24C04 test program

First, C51 read and write AT24C04 source code

/*==============================================

/*;***********************************/

/*; Start 24C01 timing */

Void Start()

{

SCL=1;

SDA=1;

SDA=0;

SCL=0;

}

C51 read and write AT24C04 source code and AT24C04 test program

/*;************************************/

/*; Stop 24C01 timing */

Void Stop()

{

SDA=0;

SCL=1;

SDA=1;

}

/*;**************************************/

/*; Detect 24C01 response signal */

Bit ACK()

{

Bit c;

SDA=1;

SCL=1;

c=SDA;

SCL=0;

Return c;

}

/*;************************************/

/*; Sends an 8-bit data to 24C01*/

Void SendChar(unsigned char ch)

{

Unsigned char i;

i=8;

Do

{

SDA=(ch&0x80);

SCL=1;

SCL=0;

Ch "" = 1;

}while(--i!=0);

}

/*;**************************************/

/*; Receives an 8-bit data from 24C01*/

Unsigned char RecChar()

{

Unsigned char i,j;

i=8;

Do

{

SCL=1;

j=(j<<<<1||SDA;

SCL=0;

}while(--i!=0);

Return j;

}

//***;**************************************

/*;********************************/

/*; Write a byte to 24C01*/

Void WriteChar(unsigned int addr, unsigned char ch)

{

Unsigned char c;

c=((*((unsigned char *)&addr)) "1) & 0x02;

Start();

SendChar(0xa0|c);

ACK();

SendChar(addr);

ACK();

SendChar(ch);

ACK();

Stop();

// for(addr=4;addr!=0;addr--)

For(ch=0xff;ch!=0;ch--) ;

}

//***;**************************************

/*;********************************/

/*; Write multiple bytes to 24C01*/

Void WriteBuf(unsigned int addr, unsigned char idata *buf, unsigned char count)

{

Unsigned char c;

c=((*((unsigned char *)&addr)) "1) & 0x02;

Start();

SendChar(0xa0|c);

ACK();

SendChar(addr);

ACK();

Do

{

SendChar(*buf++);

ACK();

If(count!=1)

{if(((++addr)&0x7)==0)

{

Stop();

For(c=0xff;c!=0;c--) ;

c=((*((unsigned char *)&addr)) "1) & 0x02;

Start();

SendChar(0xa0|c);

ACK();

SendChar(addr);

ACK();

}

}

Else

{

Stop();

For(c=0xff;c!=0;c--) ;

}

}while(--count!=0);

}

/*;**********************************/

/*; Read one byte from 24C01*/

/*; Entry: R0 is the address to read out */

/*;Export: Content read in A*/

Unsigned char ReadChar(unsigned int addr)

{

Unsigned char ch;

Ch=((*((unsigned char *)&addr)) 《1) & 0x02;

Start();

SendChar(0xa0|ch);

ACK();

SendChar(addr);

ACK();

Start();

SendChar(0xa1|ch);

ACK();

Ch=RecChar();

Stop();

Return ch;

}

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

/* read at least 2 bytes */

Void ReadBuf(unsigned int addr, unsigned char idata *buf, unsigned char count)

{

Unsigned char ch;

Ch=((*((unsigned char *)&addr)) 《1) & 0x02;

Start();

SendChar(0xa0|ch);

ACK();

SendChar(addr);

ACK();

Start();

SendChar(0xa1|ch);

ACK();

Count--;

Do

{

*buf++=RecChar();

SDA=0;

SCL=1;

SCL=0;

SDA=1;

}while(--count!=0);

*buf=RecChar();

Stop();

}

Second, AT24C04 test procedure

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

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, 0x40, x55,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) data to AT24C04

Write 16 test data starting from TESTDATA as 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 address of storage unit

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

{

AT24C04_SendByte(TESTDATA[i]);

}

AT24C04_Stop(); // Stop signal

}

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

Reading one page (16 bytes) of data from the AT24C04

Read the data in the 00~0F address of the device in the BUF in the DATA area

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

Void AT24C04_ReadPage()

{

BYTE i;

AT24C04_Start(); //Start signal

AT24C04_SendByte(0xa0); //send device address + write signal

AT24C04_SendByte(0x00); //send address of storage unit

AT24C04_Start(); //Start signal

AT24C04_SendByte(0xa1); //send device address + read signal

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 environment, 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 5ms (STC12C5A60S2@12M)

Different working environment, 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; //high data line

SCL = 1; // pull up the clock line

Delay5us(); //Delay

SDA = 0; // produces 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; // generates a rising edge

Delay5us(); //Delay

}

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

Send reply signal

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

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

Void AT24C04_SendACK(bit ack)

{

SDA = ack; //Write reply signal

SCL = 1; // pull up the clock line

Delay5us(); //Delay

SCL = 0; // pull down the clock line

Delay5us(); //Delay

}

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

Receive 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 a 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; / / the highest position of the data out

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;

}

Solid-state capacitors / Motor starting capacitors

Solid - state capacitors are all called: solid - state Aluminum Electrolytic Capacitors.It with the ordinary capacitance (that is, the liquid aluminum electrolytic capacitors) the biggest difference is that use different dielectric material, liquid aluminum capacitor dielectric material as the electrolyte, and solid-state capacitor dielectric material is conductive polymer materials.Solid-state Capacitors / Motor Starting Capacitors

Solid-state capacitors / Motor starting capacitors,Solid-State Capacitors,Solid-State Small Size Capacitors,Solid-State Low Impedance Capacitors,Long Life Solid-State Capacitors

YANGZHOU POSITIONING TECH CO., LTD. , https://www.pst-thyristor.com

Posted on