First release of a Software I2C implementation, Tested and confirmed to work with the BMP085 Barometric Pressure Sensor
Fork of SoftwareI2C by
Diff: SoftwareI2C.h
- Revision:
- 2:8670e78c4b63
- Parent:
- 0:6f6cfcdfe3d8
- Child:
- 3:adc1a829ed60
--- a/SoftwareI2C.h Sun Apr 01 19:27:03 2012 +0000 +++ b/SoftwareI2C.h Sun Apr 01 22:52:47 2012 +0000 @@ -33,6 +33,10 @@ void setDeviceAddress(uint8_t address){ _device_address = address; } + + void setFrequency(uint32_t frequency){ + _frequency_delay = 1000000 / frequency; + } inline void initialise() { _scl.output(); @@ -40,7 +44,7 @@ _sda = 1; _scl = 0; - wait_us(10); + wait_us(_frequency_delay); for ( int n = 0; n <= 3; ++n ) { stop(); @@ -50,33 +54,33 @@ private: inline void start() { _sda.output(); - wait_us(1); + wait_us(_frequency_delay); _scl = 1; _sda = 1; - wait_us(1); + wait_us(_frequency_delay); _sda = 0; - wait_us(1); + wait_us(_frequency_delay); _scl = 0; - wait_us(1); + wait_us(_frequency_delay); } inline void stop() { _sda.output(); - wait_us(1); + wait_us(_frequency_delay); _sda = 0; - wait_us(1); + wait_us(_frequency_delay); _scl = 1; - wait_us(1); + wait_us(_frequency_delay); _sda = 1; } inline void putByte(uint8_t byte) { _sda.output(); for ( int n = 8; n > 0; --n) { - wait_us(1); + wait_us(_frequency_delay); _sda = byte & (1 << (n-1)); _scl = 1; - wait_us(1); + wait_us(_frequency_delay); _scl = 0; } _sda = 1; @@ -88,15 +92,15 @@ _sda.input(); //release the data line _sda.mode(OpenDrain); - wait_us(1); + wait_us(_frequency_delay); for ( int n = 8; n > 0; --n ) { _scl=1; //set clock high - wait_us(1); + wait_us(_frequency_delay); byte |= _sda << (n-1); //read the bit - wait_us(1); + wait_us(_frequency_delay); _scl=0; //set clock low - wait_us(1); + wait_us(_frequency_delay); } _sda.output(); //take data line back @@ -106,10 +110,10 @@ inline void giveAck() { _sda.output(); - wait_us(1); + wait_us(_frequency_delay); _sda = 0; _scl = 1; - wait_us(1); + wait_us(_frequency_delay); _scl = 0; _sda = 1; @@ -121,12 +125,12 @@ _scl = 1; _sda.input(); _sda.mode(OpenDrain); - wait_us(1); + wait_us(_frequency_delay); _scl = 0; if(_sda != 0){return false;} - wait_us(1); + wait_us(_frequency_delay); return true; } @@ -134,6 +138,7 @@ DigitalInOut _scl; uint8_t _device_address; + uint32_t _frequency_delay; }; #endif \ No newline at end of file