Using General IO Port Simulation I2C Bus

xiaoxiao2021-03-06  27

1. Basic knowledge of the I2C bus. The I2C bus is a synchronous serial bus protocol proposed by Philips. 2. Use the general IO port to simulate the C51 code of the I2C bus. 3, an example of reading and writing 24C series serial E2PROM. // 24C01-24C16 Read and write driver, sbit A0 = ACC ^ 0; // Defines the ACC bit, using the ACC operating speed upstorthoscopic sbit A1 = ACC ^ 1; sbit A2 = ACC ^ 2; Sbit A3 = ACC ^ 3 SBIT A4 = ACC ^ 4; SBIT A5 = ACC ^ 5; SBIT A6 = ACC ^ 6; SBIT A7 = ACC ^ 7;

Void S24 (void) {_nop _ (); SCL = 0; SDA = 1; SCL = 1; _nop _ (); SDA = 0; _nop _ (); SCL = 0;} VOID S240 (void) {_nop _ (); SCL0 = 0; SDA0 = 1; SCL0 = 1; _nop _ (); SDA0 = 0; _nop _ (); SCL0 = 0;} VOID P24 (Void) {SDA = 0; SCL = 1; _nop _ (); sda = 1; } VOID P240 (Void) {SDA0 = 0; SCL0 = 1; _nop _ (); SDA0 = 1;} Unsigned char RD24 (Void) {SDA = 1; SCL = 1; A7 = SDA; SCL = 0; SCL = 1 A6 = SDA; SCL = 0; SCL = 1; A5 = SDA; SCL = 0; SCL = 1; A4 = SDA; SCL = 0; SCL = 1; A3 = SDA; SCL = 0; SCL = 1; A2 = SDA; SCL = 0; SCL = 1; A1 = SDA; SCL = 0; SCL = 1; A0 = SDA; SCL = 0; SDA = 1; SCL = 1; SCL = 0; RETURN (ACC);}

Void WD24 (unsigned char DD) {ACC = DD; SDA = A7; SCL = 1; SCL = 0; SDA = A6; SCL = 1; SCL = 0; SDA = A5; SCL = 1; SCL = 0; SDA = A4; SCL = 1; SCL = 0; SDA = A3; SCL = 1; SCL = 0; SDA = A2; SCL = 1; SCL = 0; SDA = A1; SCL = 1; SCL = 0; SDA = A0; SCL = 1; SCL = 0; SDA = 1; SCL = 1;}

Unsigned char read (unsigned int address) {unsigned char dd; DD = ((address & 0x7ff) / 256) << 1; S24 (); WD24 (0xA0 | DD); SCL = 0; WD24; SCL = 0; S24 (); WD24 (0xA1 | DD); SCL = 0; DD = RD24 (); P24 (); Return (DD);}

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

New Post(0)