Exemplo n.º 1
0
BOOL I2Crecv(UINT8 address, int length, UINT8* buffer)
{
	BOOL success = TRUE;

    // Start the transfer for writing data to the LCD.
    if (StartTransfer(FALSE)) {
    	I2C_7_BIT_ADDRESS SlaveAddress;
    	I2C_FORMAT_7_BIT_ADDRESS(SlaveAddress, address, I2C_READ);
		if (TransmitOneByte(SlaveAddress.byte)) {
			const char* cp;
            // Call TransmitOneByte() for each non-null character in the string.
            // Stop with success set to false if TransmitOneByte() returns failure.
			int index = 0;
			while (index < length) {
				if (index == (length-1)) {
					success = ReceiveOneByte(buffer, FALSE);
					index++;
					buffer++;
				}
				else {
					success = ReceiveOneByte(buffer, TRUE);
					index++;
					buffer++;
				}
			}	
			if (success) {
				StopTransfer();
				}
			}
		else {
			success = FALSE;
    	}
	}
	else {
		success = FALSE;
	}	
	return success;
}
void i2c_read(I2C_MODULE i2c_id, uint8_t address, uint8_t *data, uint16_t len)
{
    uint16_t i;
    
    if (!StartTransfer(i2c_id, FALSE))
    {
        return;
    }
    
    if (!TransmitOneByte(i2c_id, (address | 0x01)))
    {
        return;
    }
    
    for (i = 0; i < len; i++)
    {
        if (i < len-1)  // send ACK
            data[i] = ReceiveOneByte(i2c_id, TRUE);
        else            // send NACK
            data[i] = ReceiveOneByte(i2c_id, FALSE);
    }
    
    StopTransfer(i2c_id);
}