Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of SoftI2C by
Diff: SoftI2C.cpp
- Revision:
- 2:b174177b2bf2
- Parent:
- 1:05473196d133
--- a/SoftI2C.cpp Mon Nov 28 19:55:30 2016 +0000 +++ b/SoftI2C.cpp Wed Sep 13 22:36:30 2017 +0000 @@ -59,11 +59,11 @@ int SoftI2C::read(int ack) { int retval = 0; - _scl.output(); // Shift the bits out, msb first for (int i = 7; i>=0; i--) { //SCL low + _scl.output(); _scl.write(0); _sda.input(); wait_us(delay_us); @@ -73,11 +73,13 @@ wait_us(delay_us); //SCL high again - _scl.write(1); + _scl.input(); + while (!_scl.read()); wait_us(delay_us << 1); //wait two delays } // Last cycle to set the ACK + _scl.output(); _scl.write(0); if ( ack ) { _sda.output(); @@ -87,19 +89,18 @@ } wait_us(delay_us << 1); - _scl.write(1); + _scl.input(); + while (!_scl.read()); wait_us(delay_us << 1); - return retval; } int SoftI2C::write(int data) { - _scl.output(); - // Shift the bits out, msb first for (int i = 7; i>=0; i--) { //SCL low + _scl.output(); _scl.write(0); wait_us(delay_us); @@ -113,18 +114,21 @@ wait_us(delay_us); //SCL high again - _scl.write(1); + _scl.input(); + while (!_scl.read()); wait_us(delay_us << 1); //wait two delays } // Last cycle to get the ACK + _scl.output(); _scl.write(0); wait_us(delay_us); _sda.input(); wait_us(delay_us); - _scl.write(1); + _scl.input(); + while (!_scl.read()); wait_us(delay_us); int retval = ~_sda.read(); //Read the ack wait_us(delay_us); @@ -139,7 +143,8 @@ _scl.output(); _scl.write(0); wait_us(delay_us << 1); - _scl.write(1); + _scl.input(); + while (!_scl.read()); wait_us(delay_us << 1); } // Pull SDA low @@ -157,6 +162,7 @@ _sda.write(0); wait_us(delay_us); _scl.input(); + while (!_scl.read()); wait_us(delay_us); _sda.input(); wait_us(delay_us);