I2C serial bus learning summary

  

start signal (clock line is high, data line is changed from high to low): void AT24C04_Start(){SDA = 1; //pull high data line SCL = 1; Clock line Delay5us (); //delay SDA = 0; //generate a falling edge Delay5us (); //delay SCL = 0; //pull down the clock line } end signal: (clock line is high, the data line is Low to high) void AT24C04_Stop(){SDA = 0; //Pull low data line SCL = 1; //Pull high clock line Delay5us(); //Delay SDA = 1; //Generate rising edge Delay5us(); //delay}byte transfer: (8 bits per byte, one byte with a corresponding bit) Send data: 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 clock line Delay5us(); //delay SCL = 0; //Pull down the clock line Delay5us(); //delay}AT24C04_RecvACK();}Receive data: 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 the clock line Delay5u s(); //delay dat | = SDA; //read data SCL = 0; //pull down the clock line Delay5us (); //delay } return dat; } data response: each time the data transmission is successful, the receiving device sends a response signal, when the ninth The signal generation is that the device that generates the acknowledgment signal pulls SDA low. After receiving the start condition and the slave address, answer and select the read/write operation. For a write operation, each time a byte is received, 24c02 sends an acknowledgement signal. The read operation, after a byte is sent, releases the bus and waits for a reply signal. Receiving the response signal continues to transmit data, receives a non-acknowledgement signal, and waits for the reception end signal. Send the response signal when reading data 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} Receive response signal bit AT24C04_RecvACK(){SCL = 1; //High clock line Delay5us(); //Delay CY = SDA; //Read response signal SCL = 0 //Pull down the clock line Delay5us(); //Delay return CY;}Page write and read program: void AT24C04_WritePage(){BYTE i;AT24C04_Start(); //Start signal AT24C04_SendByte(0xa0); //Send device address + write signal AT24C04_SendByte (0x00); //Send memory location address for (i=0; i<16; i++){AT24C04_SendByte(TESTDATA[i]);}AT24C04_Stop(); //stop signal}

Copyright © Windows knowledge All Rights Reserved