In the microcontroller application, I2C (I-Part C) bus is simple, two-way two-wire synchronous serial bus, which only requires two serial lines, pulse lines, can transmit information between bus and connection device, It does not have the following features: a. Each device connected to the bus can perform unique addressing, but also a simple primary zone, the master can be used as a transmitter or as a reception. It is a competition circuit and the arbitration circuit, which can receive data sent by multiple master devices, and these data will not produce confusion .c. Synchronous pulses can allow the device to communicate with different baud rates through the bus. Because only two lines, the connection is simple, convenient. For the MCS51 series, there is no I2C bus interface internally. In this case, the software simulation method can be used to write the operation of the I2C bus. Which pin is defined as a data cable SDA, which pin is used as a pulse line, see my program (written by C51, there are 8 functions to simulate I2C bus): Common.h File # Pragma la DB OE CD OT (SPEED, 6) // LA indicate LARGE compling Mode-All local and global variables are located in external-ram. // DB indicate DEBUG // SB indicate SYMBOL // OE indicate OBJECTEXTEND // CD indicate CODE // Generate ASM Code // OT indicate OPTIMIZATION # include
// Undefined bit-address, which will be defined in function: DEFI2Csbit SDA = 0xFF; // Undefined bit-address, which will be defined in function: DEFI2Csbit SHKL = 0xFF; // Undefined bit-address, which will be defined in function: DEFI2Csbit SHKH = 0xFF; // Undefined bit-address, which will be defined in function: DEFI2Csbit KH0 = 0xFF; // Undefined bit-address, which will be defined in function: DEFI2Csbit KH1 = 0xFF; // Undefined bit- address, which will be defined in function: DEFI2Csbit KH2 = 0xFF; // Undefined bit-address, which will be defined in function: DEFI2C extern void DEFI2C (char * name) // To define which pin is SDA and which pin is SCLextern void i2CInit () // Initialization For I2C Busextern void I2CStart () // Start I2C Busextern void I2CStop (); // Stop I2C Busextern bit I2CClock (); // return SDA while SCL is HIGHextern void I2CDelay (); // Delayextern Void i2cack (); // answer extern bit I2csend (byte i2cdata); // send data with i2c busExtern Byte i2creceive (); // receive data
Common.cvoid defi2c (char * name) {// Todo: Add your code: if you quothes me why the function defi2c? // Well, The Function Will be used To Define Which Pin IS SCL and which PIN is SDA, especially there are more than 1 I2C // equipment in your circuit.In this sitiuation, we write a function for defining I2C Bus named DEFI2C ....} void I2CStart () {SCL = HIGH; I2CDelay (); SDA = Low; I2cdelay (); SCL = Low; I2cdelay (); Void I2CStop () {SDA = Low; I2cdelay (); SCL = high; I2CDELAY (); SDA = high; I2cdelay (); SCL = Low; I2cdelay (); void i2cinit () {SCL = Low; I2cStop ();} bit I2cclock () // Return SDA while SCL ISHIGH; I2CDELAY (); SAMPLE = SDA; SCL = Low; I2cdelay (); return sample;} bit i2csend (byte i2cdata) {byte i; for (i = 0; i <8; i ) {sda = ((i2cdata) & 0x80) / 0x80 ); I2CDATA = I2cdata << 1; I2cClock ();} SDA = high; return (~ i2cclock ());} void i2cack () {sda = low; i2cclock (); sda = high;} void I2cdelay () {byte ll; for (ll = 0; ll <100; ll ) {;}} byte i2creceive () {byte i2cdata = 0; byte kk; for (kk = 0; kk <8; kk ) {I2CData * = 2; IF (i2cclock ()) {i2cdata ;}}} Return I2CData;} The above code is to simulate the I2C bus with the software. When operating a specific I2C device, it must be corresponding to the timing of the device. Soon, I have been engaged in microcontrollers, and the above procedure is written in Beijing last year. If there is any error, please ask the Hardware heroes, and the seniors give advice.