S8 I2C1_READ_String(U8 DevAdd, U8 RegAdd, U8 *RegValue, U8 len) { int DataWait = len*100; char ComRes,DevAddr; DevAddr = (DevAdd<<1)|M_I2C_Write; StartI2C1(); // Send the start bit IdleI2C1(); // Wait to complete while(I2C1STATbits.ACKSTAT); //wait for acknowledgement ComRes=MasterWriteI2C1(DevAddr); // Adress with write mode IdleI2C1(); // Wait to complete while(I2C1STATbits.ACKSTAT); //wait for acknowledgement ComRes=MasterWriteI2C1(RegAdd); // Write the chip ID register loaction for read IdleI2C1(); // Send the start bit while(I2C1STATbits.ACKSTAT); //wait for acknowledgement RestartI2C1(); // Send the start bit IdleI2C1(); // Wait to complete while(I2C1STATbits.ACKSTAT); //wait for acknowledgement DevAddr = (DevAdd<<1)|M_I2C_Read; MasterWriteI2C1(DevAddr); // Adress with write mode IdleI2C1(); // Wait to complete while(I2C1STATbits.ACKSTAT); //wait for acknowledgement MastergetsI2C1(len, RegValue, DataWait); // Read the Register value IdleI2C1(); // Wait to complete NotAckI2C1(); // Not Acknowledge I2C IdleI2C1(); // Wait to complete StopI2C1(); // Stop I2C communication IdleI2C1(); // Wait to complete; return ComRes; }
uint8_t I2C_ReadFromReg(uint8_t I2CAddress, uint8_t deviceRegister) { uint8_t data = 0; StartI2C1(); while(I2CCONbits.SEN); MasterWriteI2C1(I2CAddress << 1); while(I2CSTATbits.TBF); // 8 clock cycles while(!IFS1bits.MI2C1IF); // Wait for 9th clock cycle IFS1bits.MI2C1IF = 0; // Clear interrupt flag MasterWriteI2C1(deviceRegister); while(I2CSTATbits.TBF); // 8 clock cycles while(!IFS1bits.MI2C1IF); // Wait for 9th clock cycle IFS1bits.MI2C1IF = 0; // Clear interrupt flag RestartI2C1(); // Second start. while(I2CCONbits.RSEN); MasterWriteI2C1((I2CAddress << 1 )+ 1); while(I2CSTATbits.TBF); // 8 clock cycles while(!IFS1bits.MI2C1IF); // Wait for 9th clock cycle IFS1bits.MI2C1IF = 0; // Clear interrupt flag data = MasterReadI2C1(); NotAckI2C1(); // Read stop sequence. while(I2C1CONbits.ACKEN == 1); StopI2C1(); while(I2CCONbits.PEN); IdleI2C1(); return(data); }
void portableCommonNackI2C(I2cBusConnection* i2cBusConnection) { I2cBus* i2cBus = i2cBusConnection->i2cBus; if (i2cBus == NULL) { NotAckI2C1(); } else { I2C_MODULE i2cModule = getI2C_MODULE(i2cBus->port); I2CAcknowledgeByte(i2cModule, false); } }
void I2C_ReadFromReg_Burst(uint8_t address, uint8_t deviceRegister, uint8_t* data, uint8_t burstNum) { // Assert the start condition StartI2C1(); while (I2C1CONbits.SEN); // Send the 7-bit device address MasterWriteI2C1(address << 1); while (I2C1STATbits.TBF); // 8 clock cycles while (!IFS1bits.MI2C1IF); // Wait for 9th clock cycle IFS1bits.MI2C1IF = 0; // Clear interrupt flag // Send the register address MasterWriteI2C1(deviceRegister); while (I2C1STATbits.TBF); // 8 clock cycles while (!IFS1bits.MI2C1IF); // Wait for 9th clock cycle IFS1bits.MI2C1IF = 0; // Clear interrupt flag // Start a new I2C transaction RestartI2C1(); while (I2C1CONbits.RSEN); // Send the second address MasterWriteI2C1((address << 1) + 1); while (I2C1STATbits.TBF); // 8 clock cycles while (!IFS1bits.MI2C1IF); // Wait for 9th clock cycle IFS1bits.MI2C1IF = 0; // Clear interrupt flag // Read the data data[0] = MasterReadI2C1(); if (burstNum > 1) { uint8_t i; for (i = 1; i < burstNum; i++) { AckI2C1(); while (I2C1CONbits.ACKEN == 1); data[i] = MasterReadI2C1(); } } // No need to ack reception of this data NotAckI2C1(); while (I2C1CONbits.ACKEN == 1); // Stop the I2C transaction StopI2C1(); while (I2C1CONbits.PEN); // Go idle on the bus IdleI2C1(); }
unsigned char I2CReadByte(unsigned char add,unsigned char subadd) { unsigned char _temp=0; StartI2C1(); while(WriteI2C1(add<<1)!=0); while(WriteI2C1(subadd)!=0); RestartI2C1(); while(WriteI2C1(1+(add<<1))!=0); _temp=ReadI2C1(); NotAckI2C1(); StopI2C1(); return _temp; }
int8_t I2C1dev_readBytes(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8_t *data) { count_accel = 0; // S IdleI2C1(); StartI2C1(); // Device write address IdleI2C1(); WriteI2C1(devAddr << 1 | 0x00); // Register address IdleI2C1(); WriteI2C1(regAddr); // R IdleI2C1(); RestartI2C1(); // Device read address IdleI2C1(); WriteI2C1(devAddr << 1 | 0x01); for (count_accel = 0; count_accel < length; count_accel++) { // Data byte IdleI2C1(); data[count_accel] = ReadI2C1(); if (count_accel == length - 1) { // NACK IdleI2C1(); NotAckI2C1(); } else { // ACK IdleI2C1(); AckI2C1(); } } // P IdleI2C1(); StopI2C1(); return count_accel; }
uint8_t mti_tcn75a_get_temperature(int32_t *result) { uint16_t tmp; uint8_t rc; uint8_t tmp_lsb = 4; uint8_t tmp_msb = 2; /* start */ StartI2C1(); /* send slave address (write) */ rc = WriteI2C1(0b10010000); if (rc != 0x00) { rc = CH_ERROR_I2C_SLAVE_ADDRESS; goto out; } /* set ambient temperature pointer */ rc = WriteI2C1(0b00000000); /* reset the bus */ RestartI2C1(); /* send slave address (read) */ WriteI2C1(0b10010001); /* read the temperature */ tmp_msb = ReadI2C1(); AckI2C1(); tmp_lsb = ReadI2C1(); NotAckI2C1(); /* stop */ StopI2C1(); /* format result */ *result = (((int32_t) tmp_msb) << 16) + (((int32_t) tmp_lsb) << 8); out: return rc; }
unsigned char Read23X08_17(unsigned char reg, unsigned char gAddrPins) { unsigned char num; StartI2C1(); // Wait till start sequence is completed while (I2C1CONbits.SEN); MasterWriteI2C1(gControlByte | WrtCmd | gAddrPins); WaitForACK(); MasterWriteI2C1(reg); WaitForACK(); RestartI2C1(); IdleI2C1(); MasterWriteI2C1(gControlByte | RdCmd | gAddrPins); WaitForACK(); num = MasterReadI2C1(); NotAckI2C1(); StopI2C1(); IdleI2C1(); return (num); }
void i2cSendNACK(unsigned char channel){ if (channel == 1) { NotAckI2C1(); while(I2C1CONbits.ACKEN); } else { NotAckI2C2(); while(I2C2CONbits.ACKEN); } }
signed int EERandomRead1( unsigned char control, unsigned char address ) { IdleI2C1(); // ensure module is idle StartI2C1(); // initiate START condition while ( SSP1CON2bits.SEN ); // wait until start condition is over if ( PIR2bits.BCL1IF ) // test for bus collision { return ( -1 ); // return with Bus Collision error } else { if ( WriteI2C1( control ) ) // write 1 byte { StopI2C1(); return ( -3 ); // return with write collision error } //IdleI2C1(); // ensure module is idle if ( !SSP1CON2bits.ACKSTAT ) // test for ACK condition, if received { if ( WriteI2C1( address ) ) // WRITE word address for EEPROM { StopI2C1(); return ( -3 ); // return with write collision error } //IdleI2C1(); // ensure module is idle if ( !SSP1CON2bits.ACKSTAT ) // test for ACK condition, if received { RestartI2C1(); // generate I2C1 bus restart condition while ( SSP1CON2bits.RSEN );// wait until re-start condition is over if ( PIR2bits.BCL1IF ) // test for bus collision { return ( -1 ); // return with Bus Collision error } if ( WriteI2C1( control+1 ))// write 1 byte - R/W bit should be 1 { StopI2C1(); return ( -3 ); // return with write collision error } //IdleI2C1(); // ensure module is idle if ( !SSP1CON2bits.ACKSTAT )// test for ACK condition, if received { SSP1CON2bits.RCEN = 1; // enable master for 1 byte reception while ( SSP1CON2bits.RCEN ); // check that receive sequence is over NotAckI2C1(); // send ACK condition while ( SSP1CON2bits.ACKEN ); // wait until ACK sequence is over StopI2C1(); // send STOP condition while ( SSP1CON2bits.PEN ); // wait until stop condition is over if ( PIR2bits.BCL1IF ) // test for bus collision { return ( -1 ); // return with Bus Collision error } } else { StopI2C1(); return ( -2 ); // return with Not Ack error } } else { StopI2C1(); return ( -2 ); // return with Not Ack error } } else { StopI2C1(); return ( -2 ); // return with Not Ack error } } return ( (unsigned int) SSP1BUF ); // return with data }