/************************************** 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; // I2C Clock for AT24C04 sbit SDA = P3^5; // I2C Data for AT24C04 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 This function writes 16 test data bytes from the TESTDATA array into the memory starting at address 0x00 to 0x0F. **************************************/ void AT24C04_WritePage() { BYTE i; AT24C04_Start(); // Send start condition AT24C04_SendByte(0xA0); // Send device address with write bit AT24C04_SendByte(0x00); // Send the memory address for(i = 0; i < 16; i++) { AT24C04_SendByte(TESTDATA[i]); // Send each byte of test data AT24C04_SendACK(0); // Send ACK after each byte except the last one } AT24C04_SendACK(1); // Send NAK for the last byte AT24C04_Stop(); // Send stop condition } /************************************** Delay 5 microseconds (for STC12C5A60S2 @ 12MHz) Note: This delay is calculated using 1T instruction cycles, which differs from traditional 12T MCUs. Adjust if necessary for different operating conditions. **************************************/ void Delay5us() { BYTE n = 4; while(n--) { _nop_(); _nop_(); } } /************************************** Delay 5 milliseconds (for STC12C5A60S2 @ 12MHz) Note: This delay is based on 1T instruction cycles. Adjust as needed for other environments. **************************************/ void Delay5ms() { WORD n = 2500; while(n--) { _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); } } /************************************** Send I2C start condition **************************************/ void AT24C04_Start() { SDA = 1; SCL = 1; Delay5us(); SDA = 0; Delay5us(); SCL = 0; } /************************************** Send I2C stop condition **************************************/ void AT24C04_Stop() { SDA = 0; SCL = 1; Delay5us(); SDA = 1; Delay5us(); } /************************************** Send acknowledgment (ACK) or not (NAK) Parameter: ack = 0 for ACK, 1 for NAK **************************************/ void AT24C04_SendACK(bit ack) { SDA = ack; SCL = 1; Delay5us(); SCL = 0; Delay5us(); } /************************************** Receive acknowledgment signal Returns: 1 for NAK, 0 for ACK **************************************/ bit AT24C04_RecvACK() { SCL = 1; Delay5us(); CY = SDA; SCL = 0; Delay5us(); return CY; } /************************************** Send a byte of data over I2C bus **************************************/ void AT24C04_SendByte(BYTE dat) { BYTE i; for(i = 0; i < 8; i++) { dat <<= 1; SDA = CY; SCL = 1; Delay5us(); SCL = 0; Delay5us(); } AT24C04_RecvACK(); } /************************************** Receive a byte of data from I2C bus **************************************/ BYTE AT24C04_RecvByte() { BYTE i; BYTE dat = 0; SDA = 1; for(i = 0; i < 8; i++) { dat <<= 1; SCL = 1; Delay5us(); dat |= SDA; SCL = 0; Delay5us(); } return dat; }

1.0nm-8mm Connector

YUEQING WEIMAI ELECTRONICS CO.,LTD , https://www.wmconnector.com