Changeset - f48b9f22aa7a
[Not reviewed]
default
0 1 0
kripperger@CL-SEC241-09.cedarville.edu - 12 years ago 2013-02-01 14:53:36
kripperger@CL-SEC241-09.cedarville.edu
ver
1 file changed with 1 insertions and 0 deletions:
0 comments (0 inline, 0 general)
slave/slave/lib/i2c.c
Show inline comments
 
@@ -147,115 +147,116 @@ void i2c_stop(void)
 
 
/*************************************************************************
 
  Send one byte to I2C device
 
  
 
  Input:    byte to be transfered
 
  Return:   0 write successful 
 
            1 write failed
 
*************************************************************************/
 
unsigned char i2c_writeX( unsigned char data )
 
{	
 
    uint8_t   twst;
 
    
 
	// send data to the previously addressed device
 
	TWDR = data;
 
	TWCR = (1<<TWINT) | (1<<TWEN);
 
 
	// wait until transmission completed
 
	while(!(TWCR & (1<<TWINT)));
 
 
	// check value of TWI Status Register. Mask prescaler bits
 
	twst = TW_STATUS & 0xF8;
 
	if( twst != TW_MT_DATA_ACK) return 1;
 
	return 0;
 
 
}/* i2c_write */
 
 
 
 
/*************************************************************************
 
 Read one byte from the I2C device, request more data from device 
 
 
 
 Return:  byte read from I2C device
 
*************************************************************************/
 
unsigned char i2c_readAck(void)
 
{
 
	TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWEA);
 
	while(!(TWCR & (1<<TWINT)));    
 
 
    return TWDR;
 
 
}/* i2c_readAck */
 
 
 
 
/*************************************************************************
 
 Read one byte from the I2C device, read is followed by a stop condition 
 
 
 
 Return:  byte read from I2C device
 
*************************************************************************/
 
unsigned char i2c_readNak(void)
 
{
 
	TWCR = (1<<TWINT) | (1<<TWEN);
 
	while(!(TWCR & (1<<TWINT)));
 
	
 
    return TWDR;
 
 
}/* i2c_readNak */
 
 
 
 
/*************************************************************************
 
 Write one byte to the I2C device, read is followed by a stop condition 
 
 
 
 Return:  void
 
*************************************************************************/
 
void i2c_write(unsigned char addr, unsigned char reg, unsigned char data)
 
{
 
	i2c_start_wait(addr+I2C_WRITE);     // set device address and write mode
 
	i2c_writeX(reg);                     // write register address
 
	i2c_writeX(data);                    // write value data to register
 
	i2c_stop();                         // set stop condition = release bus
 
 
}/* i2c_write */
 
 
 
 
/*************************************************************************
 
 Read one byte from the I2C device, read is followed by a stop condition 
 
 
 
 Return:  byte read from I2C device
 
*************************************************************************/
 
uint8_t i2c_read(unsigned char addr, unsigned char reg)
 
{
 
	uint8_t   data;
 
	
 
	i2c_start_wait(addr+I2C_WRITE);		// set device address and write mode
 
	i2c_writeX(reg);						// write register address
 
	
 
	i2c_rep_start(addr+I2C_READ);   // set device address and read mode
 
	data = i2c_readNak();               // read one byte
 
	i2c_stop();
 
	
 
    return data;
 
 
}/* i2c_read */
 
 
 
///////////added for humidity
 
uint16_t i2c_read16(unsigned char addr)
 
{
 
	uint16_t   data;
 
	uint8_t   dataL;
 
	
 
	i2c_start_wait(addr+I2C_WRITE);		// set device address and write mode
 
	
 
	i2c_rep_start(addr+I2C_READ);   // set device address and read mode
 
	data = i2c_readAck();               // read one byte
 
	dataL = i2c_readNak();
 
	i2c_stop();
 
	
 
	data = data << 8;
 
	data = data | dataL;
 
	
 
	return data;
 
 
}/* i2c_read16 */
 
\ No newline at end of file
0 comments (0 inline, 0 general)