twi_functions.c
uint8_t readReg(uint8_t address, uint8_t register_location)
{
	uint8_t result = 0;
	
	twi_start();
	TWDR = (address<<1) + TW_WRITE;
	TWCR = (1<<TWINT)|(1<<TWEN);
	while(!(TWCR & (1<<TWINT)));
	if (TW_STATUS != TW_MT_SLA_ACK)
		return 0;
		
	TWDR = register_location;
	TWCR = (1<<TWINT)|(1<<TWEN);
	while(!(TWCR & (1<<TWINT)));
	if (TW_STATUS != TW_MT_DATA_ACK)
		return 0;
		
	twi_restart();
	TWDR = (address<<1) + TW_READ;
	TWCR = (1<<TWINT)|(1<<TWEN);
	while(!(TWCR & (1<<TWINT)));
	if (TW_STATUS != TW_MR_SLA_ACK)
		return 0;
	
	result = readNACK();
	twi_stop();
	return result;	
}
/* Read a register from a device */
Read a register from a device
Read a register from a device
uint8_t readReg(uint8_t address, uint8_t register_location)
{
	uint8_t result = 0;
	twi_start();
	TWDR = (address<<1) + TW_WRITE;
	TWCR = (1<<TWINT)|(1<<TWEN);
	while(!(TWCR & (1<<TWINT)));
	if (TW_STATUS != TW_MT_SLA_ACK)
		return 0;
	TWDR = register_location;
	TWCR = (1<<TWINT)|(1<<TWEN);
	while(!(TWCR & (1<<TWINT)));
	if (TW_STATUS != TW_MT_DATA_ACK)
		return 0;
	twi_restart();
	TWDR = (address<<1) + TW_READ;
	TWCR = (1<<TWINT)|(1<<TWEN);
	while(!(TWCR & (1<<TWINT)));
	if (TW_STATUS != TW_MR_SLA_ACK)
		return 0;
	result = readNACK();
	twi_stop();
	return result;	
}
uint8_t readReg ( uint8_t address , uint8_t register_location ) { uint8_t result = 0 ; twi_start ( ) ; TWDR = ( address << 1 ) + TW_WRITE ; TWCR = ( 1 << TWINT ) | ( 1 << TWEN ) ; while ( ! ( TWCR & ( 1 << TWINT ) ) ) ; if ( TW_STATUS != TW_MT_SLA_ACK ) return 0 ; TWDR = register_location ; TWCR = ( 1 << TWINT ) | ( 1 << TWEN ) ; while ( ! ( TWCR & ( 1 << TWINT ) ) ) ; if ( TW_STATUS != TW_MT_DATA_ACK ) return 0 ; twi_restart ( ) ; TWDR = ( address << 1 ) + TW_READ ; TWCR = ( 1 << TWINT ) | ( 1 << TWEN ) ; while ( ! ( TWCR & ( 1 << TWINT ) ) ) ; if ( TW_STATUS != TW_MR_SLA_ACK ) return 0 ; result = readNACK ( ) ; twi_stop ( ) ; return result ; }
Read a register from a device
Read a register from a device