Single chip simulation I2C bus and 24c02 (I2C EEPROM) read and write instance (source code)

zhaozj2021-02-16  50

/ * 51 Series single-chip in use, sometimes needs to simulate the I2C bus, * // * Here, one instance (read and write serial EEPROM chip AT2402) * // *********************** *********************************************************** ******** // * Name: AT24C02 memory read / write programs, used I2C bus, including relatively independent I2C bus read / write function * // * language: C51 single-chip programming language * // * Platform: Win98, Intel Celeron 433 Processor, Weifu Simulator, Simulation 8751 * // * Author: Stephenzhu JavaSdk@163.com * // * Date: May 21, May 22, May 29 * / / * Version: 1.1.1 * // * tahers: none * // ******************************************** *************************************** /

#include #include #include #define delay_time 60 / * experiment, don't be less than 50! Otherwise, it may cause timing chaos * / #define true 1 #define false 0

SBIT SCL = P1 ^ 7; / * Assumption by P1.7 and P1.6 control * / sbit sda = p1 ^ 6;

/ ********** Function definition function definition *********** /

Void delay (unsigned int T) / * delay function * / {while (t! = 0) t -;}

Void i2c_start (void) {/ * Start the function of the I2C bus, when the SCL is high, make SDA generate a negative hop * / SDA = 1; SCL = 1; delay (delay_time); sda = 0; delay_TIME) ; SCL = 0; delay (delay_time);

Void i2c_stop (void) {/ * terminates the I2C bus, when the SCL is high, the SDA generates a positive hop * / SDA = 0; SCL = 1; DELAY (DELAY_TIME); SDA = 1; DELAY (Delay_Time); SCL = 0; delay (delay_time);}

Void send_0 / * send ACK * / {/ * transmits 0, when SCL is high, the SDA signal is low * / sda = 0; SCL = 1; delay (delay_time); SCL = 0; delay_time );} Void send_1 (void) {/ * transmits 1, when SCL is high, the SDA signal is high * / sda = 1; SCL = 1; Delay_Time); SCL = 0; delay_time;}

Bit Check_acknowledge (void) {/ * Requirement of response signals for one byte after a byte check device * / SDA = 1; SCL = 1; DELAY (DELAY_TIME / 2); f0 = SDA; DELAY (Delay_Time / 2); SCL = 0 Delay (delay_time); if (f0 == 1) Return False; Return True;}

Void Writei2cByte (Char B) Reentrant {/ * writes a byte * / char i; for (i = 0; i <8; i ) IF ((B << i) & 0x80) IF ((B << i) & 0x80) Send_1 (); Else Send_0 ();

Char readi2cByte (void) Reentrant {/ * reads one byte from the I2C bus * / char b = 0, i; for (i = 0; i <8; i ) {sda = 1; / * Release bus * / SCL = 1; / * Accept data * / delay (10); f0 = sda; delay (10); SCL = 0; if (f0 == 1) {b = b << 1; b = B | 0x01;} else B = B << 1;} return b;}

/ ********** The following is a function of reading and writing 24c02 ********** / void write_one_byte (charid (char these) {bit AckTemp = 1; / * Write a byte to mem * / I2C_Start (); WriteI2CByte (0xa0); acktemp = Check_Acknowledge (); WriteI2CByte (addr); / * address * / acktemp = Check_Acknowledge (); WriteI2CByte (thedata); / * thedata * / acktemp = Check_Acknowledge (); I2C_STOP ();

void Write_A_Page (char * buffer, char addr) {bit acktemp = 1; bit wrtmp; int i; / * write a page to at24c02 * / I2C_Start (); WriteI2CByte (0xa0); acktemp = Check_Acknowledge (); WriteI2CByte (addr) ; / * address * / AckTemp = Check_acknowledge (); for (i = 0; i <7; i ) {Writei2cByte (buffer [i]); if (! check_acknowledge ()) {i2c_stop ();

}

I2C_STOP ();

}

char Read_One_Byte (char addr) {bit acktemp = 1; char mydata; / * read a byte from mem * / I2C_Start (); WriteI2CByte (0xa0); acktemp = Check_Acknowledge (); WriteI2CByte (addr); / * address * / acktemp = Check_acknowledge (); I2C_Start (); Writei2cByte (0xA1); AckTemp = Check_acknowledge ();

MyData = readi2cbyte (); AckTemp = Check_Acknowledge ();

Return mydata; i2c_stop ();

Void Read_n_bytes (Char * Buffer, Char N, Char Addr) {bit AckTemp = 1;

int i = 0; / * read 8 bytes from mem * / I2C_Start (); WriteI2CByte (0xa0); acktemp = Check_Acknowledge (); WriteI2CByte (addr); / * address * / acktemp = Check_Acknowledge (); I2C_Start (); WriteI2CByte (0xA1); AckTemp = Check_Acknowledge ();

For (i = 0; i

I2C_STOP ();

}

Void main () {INT i; char myrray [8]; char {= 0; i <8; i ) {MyArray [i] = i; myArray2 [i] = i 0x08;

}

Write_one_byte (0x20,0x28);

Write_a_page (MyArray, 0x10); Write_a_page (MyArray2,0x18);

mybyte = read_one_byte (0x20); read_n_bytes (rdarray, 16,0x10);

}

转载请注明原文地址:https://www.9cbs.com/read-28253.html

New Post(0)