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; }
/*whenever reach here, the SCL is low, so enable change SDA*/ unsigned char i2c_rx(char ack) { char x, d=0; SDA = 1; //release SDA to slave for(x=0; x<8; x++) { d <<= 1; do { SCL = 1; //master pull up clock(slave might hold clk for clock sync) } while(scl_in()==0); //wait for any SCL clock stretching (clock sync) /*slave data ready and relase clock*/ i2c_dly(); if(sda_in()) d |= 1; /*input the bit*/ SCL = 0; //done this bit( slave now prepare next bit, and hold clock for clock sync) } //bit 9, master might sending ACK bit if(ack) SDA = 0; else SDA = 1; SCL = 1; //master ACK bit ready, relase clock /*here need wait clock to sync, i.e, slave is reading the bit*/ while(scl_in()==0); /*by hyl*/ i2c_dly();//send (N)ACK bit SCL = 0; // SDA = 1; return d; }
void i2c_stop(void) { SDA = 0; // i2c stop bit sequence i2c_dly(); SCL = 1; i2c_dly(); SDA = 1; i2c_dly(); }
void MP3Player::i2c_stop(void) { SDA_L(); // i2c stop bit sequence i2c_dly(); SCL_H(); i2c_dly(); SDA_H(); i2c_dly(); }
void i2c_start(void) { /*only the start stop allow change SDA while the SCL keep high*/ SDA = 1; // i2c start bit sequence i2c_dly(); SCL = 1; // Clock is high, SDA from high to low i2c_dly(); SDA = 0; i2c_dly(); SCL = 0; //master pull down clock,and prepare SDA to slave (Address) i2c_dly(); }
byte MP3Player::i2c_tx(byte d) { byte x; byte b; for(x=8; x; x--) { if(d&0x80) { SDA_H(); } else { SDA_L(); } SCL_H(); d <<= 1; SCL_L(); } SDA_H(); SCL_H(); i2c_dly(); b = SDA_IN(); // possible ACK bit SCL_L(); return b; }
/*clock from low to high might need deal with clock sync*/ unsigned char i2c_tx(unsigned char d) { char x; static bit nack; //prepare data for(x=8; x; x--) { if(d&0x80) SDA = 1; else SDA = 0; i2c_dly(); SCL = 1; //data ready, relase clock d <<= 1; i2c_dly(); //dont need?, by hyl i2c_dly(); //dont need?, by hyl SCL = 0; //master is prepareing next bit i2c_dly(); //dont need?, by hyl } //reive the ACK SDA = 1; //relase contorl the SDA pin ready for input ? i2c_dly(); //wait the ack SCL = 1; //the bit 9 clock event, might a ACK i2c_dly(); nack = sda_in(); // possible ACK bit if(nack) ; //should end transimition and abort with stop.... SCL = 0; /*end the tx*/ return b; }