char MOD_IO2::WriteSingle( char data ) { char i; // Send the 8 bits for(i = 0; i<8; i++) { WaitMicrosecond(1000); if(data&RW_MASK) SDA_IN(); else SDA_OUT(); data <<= 1; WaitMicrosecond(1000); SCL_IN(); WaitMicrosecond(1000); SCL_OUT(); } // Read the ACK WaitMicrosecond(1000); SDA_IN(); WaitMicrosecond(1000); SCL_IN(); WaitMicrosecond(1000); i = digitalRead(pinSDA); SCL_OUT(); WaitMicrosecond(1000); return i; }
char MOD_IO2::ReadSingle( char ack ) { char data = 0, i; SDA_IN(); for(i = 0; i < 8; i++) { WaitMicrosecond(1000); SCL_IN(); while(digitalRead(pinSCL)==0); WaitMicrosecond(1000); data |= digitalRead(pinSDA); if( i!=7 ) data<<=1; WaitMicrosecond(1000); SCL_OUT(); WaitMicrosecond(1000); } // send the ACK/NACK WaitMicrosecond(1000); if(ack) SDA_IN(); else SDA_OUT(); WaitMicrosecond(1000); SCL_IN(); WaitMicrosecond(1000); SCL_OUT(); WaitMicrosecond(1000); return data; }
byte MP3Player::i2c_rx(byte ack) { byte x, d=0; SDA_H(); for(x=0; x<8; x++) { d <<= 1; do { SCL_H(); } while(SCL_IN()==0); // wait for any SCL clock stretching i2c_dly(); if(SDA_IN()) d |= 1; SCL_L(); } if(ack) { SDA_L(); } else { SDA_H(); } SCL_H() i2c_dly(); // send (N)ACK bit SCL_L(); SDA_H(); return d; }
void MOD_IO2::Stop() { SDA_OUT(); WaitMicrosecond(1000); SCL_IN(); WaitMicrosecond(1000); SDA_IN(); WaitMicrosecond(1000); }
unsigned char read_SCL(void) // Set SCL as input and return current level of line, 0 or 1 { SCL_IN(); return !!SCL(); }
void i2cInit(void) { SDA_IN(); SCL_IN(); }