karelv
/
i2c_io_lpc4088
IO-based I2C functions with PCF8574 to proof my HW is correctly wired. Working on my LPC4088
Revision 0:b0247e85bf0e, committed 2014-12-07
- Comitter:
- karelv
- Date:
- Sun Dec 07 13:56:12 2014 +0000
- Commit message:
- IO-based I2C function, to show my HW is correctly wired
Changed in this revision
diff -r 000000000000 -r b0247e85bf0e main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Sun Dec 07 13:56:12 2014 +0000 @@ -0,0 +1,30 @@ +#include "mbed.h" +#include "my_i2c.h" + +DigitalOut myled(LED1); + +// Connections made: +// SDA to pin9 of LPC4088 QSB +// SCL to pin10 of LPC4088 QSB +// on I2C bus is PCF8574 connected at sub-address 0 (0x40). +// all supplied at 3.3V +// no external pullup (5K internal pullup used by my_i2c functions) +// +// note in the my_i2c functions a delay of 1us is added after each transition of the pin. +// so this means an absolute maximum frequency of 500kHz. + +int main() { + + while(1) { + myled = 1; + my_i2cStart(0x40); + my_i2cWrite (0x55); + my_i2cStop(); + wait(0.5); + myled = 0; + my_i2cStart(0x40); + my_i2cWrite (0xAA); + my_i2cStop(); + wait(0.5); + } +}
diff -r 000000000000 -r b0247e85bf0e mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Sun Dec 07 13:56:12 2014 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/4fc01daae5a5 \ No newline at end of file
diff -r 000000000000 -r b0247e85bf0e my_i2c.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/my_i2c.cpp Sun Dec 07 13:56:12 2014 +0000 @@ -0,0 +1,192 @@ +#include "mbed.h" +#include "my_i2c.h" + +DigitalInOut sda(p9); +DigitalInOut scl(p10); + + +#define SDA(state) if (state) { \ + sda.input (); \ + sda = 1; \ +} else { \ + sda = 0; \ + sda.output (); \ +} + +#define SCL(state) if (state) { \ + scl.input (); \ + scl = 1; \ +} else { \ + scl = 0; \ + scl.output (); \ +} + +#define IO_DELAY() wait_us (1) + + +void my_i2cWriteByte (unsigned char byte); +bool my_i2cGetAck (); +unsigned char my_i2cReadByte (); + + +bool +my_i2cStart (unsigned char slaveAddress) +{ + SDA(1); + SCL(1); + IO_DELAY(); + SDA(0); + IO_DELAY(); + + my_i2cWriteByte (slaveAddress); + + return my_i2cGetAck (); +} + + +bool +my_i2cStop () +{ + SDA(0); + SCL(0); + IO_DELAY(); + + SCL(1); + IO_DELAY(); + + SDA(1); + IO_DELAY(); + + return true; +} + + +bool +my_i2cRepeatedStart (unsigned char slaveAddress) +{ + SCL(0); + IO_DELAY(); + + SDA(1); + + IO_DELAY(); + + SCL(1); + + IO_DELAY(); + + SDA(0); + my_i2cWriteByte (slaveAddress); + + return my_i2cGetAck (); +} + + +bool +my_i2cWrite (unsigned char data) +{ + my_i2cWriteByte (data); + return my_i2cGetAck (); +} + + +unsigned char +my_i2cReadByte () +{ + unsigned char data = 0; + + for (int i = 0x80; i > 0x00; i >>= 1) + { + SCL(0); + SDA(1); + IO_DELAY(); + + SCL(1); + IO_DELAY(); + + if (sda) + { // turn bit to 1... + data |= i; + } + } + return data; +} + + +unsigned char +my_i2cReadAck () +{ + unsigned char data = my_i2cReadByte (); + + SCL(0); + SDA(0); + IO_DELAY(); + + SCL(1); + + // wait till scl is high + IO_DELAY(); + + return data; +} + + +unsigned char +my_i2cReadNak () +{ + unsigned char data = my_i2cReadByte (); + + SCL(0); + SDA(1); + IO_DELAY(); + + SCL(1); + // wait till scl is high + IO_DELAY(); + + return data; +} + + +unsigned char +my_i2cRead (unsigned char ack) +{ + if (ack) + { + return my_i2cReadAck (); + } + return my_i2cReadNak (); +} + + +void +my_i2cWriteByte (unsigned char byte) +{ + for (int i = 0x80; i > 0x00; i >>= 1) + { + SCL(0); + SDA((byte & i) ? true : false); + IO_DELAY(); + + SCL(1); + IO_DELAY(); + } +} + + +bool +my_i2cGetAck () +{ + SCL(0); + SDA(1); + IO_DELAY(); + + SCL(1); +// wait till clock = 1... + IO_DELAY(); + + bool ack = false; + ack = sda; + + return !ack; +}
diff -r 000000000000 -r b0247e85bf0e my_i2c.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/my_i2c.h Sun Dec 07 13:56:12 2014 +0000 @@ -0,0 +1,15 @@ +#ifndef __MY_I2C_H__ +#define __MY_I2C_H__ + +bool my_i2cStart (unsigned char slaveAddress); +bool my_i2cStop (); +bool my_i2cRepeatedStart (unsigned char slaveAddress); +bool my_i2cWrite (unsigned char data); +unsigned char my_i2cReadByte (); +unsigned char my_i2cReadAck (); +unsigned char my_i2cReadNak (); +unsigned char my_i2cRead (unsigned char ack); +void my_i2cWriteByte (unsigned char byte); +bool my_i2cGetAck (); + +#endif // __MY_I2C_H__