Software analog I2C bus operation.

zhaozj2021-02-16  39

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 #include #include #include #pragma REGPARMS # pragma SAVE # define BOOL bit # define BYTE unsigned char #define UINT unsigned int # define ULONG unsigned long # define HIGH 1 # define LOW 0 # define TRUE 1 # define FALSE 0 # define MAXLONGS 2147483647 # define MINLONGS -2147483648 # define MAXINTS 32767 # define MININTS -32768 # define MAXLONGU 4 294967295 # define MAXINTU 65535 # define BLOCKLOW 0x0000 // The start-address in AT24C64 is 0x0000 # define BLOCKSIZE 0x20 // Each block is 32 bytes # define BLOCKNUM 0xC8 // At best 200, Only Save the newest 200 records. # Define BLOCKHIGH 0x18FF // The end-address in AT24C64 is 0x18FF # define OSC 24000000 // Osillcator Frequency # define BAUDRATE 9600 # define OTV 256-OSC / 12 / BAUDRATE / 32 // character O represents Obtain, character T represents Timer, character V represents Value // Important Announcement: I Use a word 0x1ffc, 0x1ffd to save the next block's address and 0x1ffe, 0x1fff to save the block's no.sbit gun = 0x85; // pin p05sbit SCL = 0xFF;

// 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.

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

New Post(0)