Beispiel #1
0
/* Read a byte from I2C bus */
unsigned char i2c_read_byte(int nack)
{
  unsigned char byte = 0;
  unsigned bit;

  for (bit = 0; bit < 8; bit++)
    byte = (byte << 1) | i2c_read_bit();             
  i2c_write_bit(nack);

  return byte;
}
Beispiel #2
0
unsigned char i2c_read_with_response(enum i2c_ack ack)
{
    unsigned char byte=0;

    TRISCbits.TRISC2 = 1; // reading data

    for (size_t i=0; i < 8; ++i) {
        byte <<= 1;
        if (i2c_read_bit() != 0)
            byte |= 0x1;
    }

    TRISCbits.TRISC2 = 0; // write an ACK
    if (ack == I2C_ACK)
        i2c_write_bit(0); // send ack
    else
        i2c_write_bit(1);

    return byte;
}
Beispiel #3
0
//*----------------------------------------------------------------------------
//*  Name     : i2c_read_byte() 
//*  Brief    : Read a byte from I2C bus
//*  Argument : FALSE = No ACK, TRUE = ACK  
//*  Return   : Data read from I2C bus
//*----------------------------------------------------------------------------
int I2C_read_byte(int ACK) {
	uint8 buf = 0;
	int i;
	
	GPIOR_CFGIO_INPUT(I2C_PORT, I2C_SDA_PIN);
	for( i = 0; i < 8; i ++) {
		buf <<= 1;
		if(i2c_read_bit()) {
			buf |= 1;
		}
	}
	// ack or not
	GPIOR_CFGIO_OUTPUT(I2C_PORT, I2C_SDA_PIN);
	if(ACK) {
		i2c_write_bit(0);
	} else {
		i2c_write_bit(1);
	}
	return buf;	
}
Beispiel #4
0
// Read a byte from I2C bus
unsigned char i2c_read_byte(bool nack, bool send_stop) {
  unsigned char byte = 0;
  unsigned bit;
  for (bit = 0; bit < 8; bit++) {
    byte = (byte << 1) | i2c_read_bit();
  }
  i2c_write_bit(nack);
  if (send_stop) {
    i2c_stop_cond();
  }
  return byte;
}
Beispiel #5
0
static unsigned int i2c_write(unsigned char byte)
{
	unsigned int bit;
	unsigned int ack;

	for(bit = 0; bit < 8; bit++) {
		i2c_write_bit(byte & 0x80);
		byte <<= 1;
	}
	ack = !i2c_read_bit();
	return ack;
}
Beispiel #6
0
static unsigned char i2c_read(int ack)
{
  unsigned char byte = 0;
  unsigned int bit;

  for(bit = 0; bit < 8; bit++) {
    byte <<= 1;
    byte |= i2c_read_bit();
  }
  i2c_write_bit(!ack);
  return byte;
}
Beispiel #7
0
// Read a byte from I2C bus
unsigned char i2cReadByte(unsigned char nack, unsigned char send_stop) {
	unsigned char byte = 0;
	unsigned bit;
	for (bit = 0; bit < 8; bit++) {
		byte = (byte << 1) | i2c_read_bit();
	}
	i2c_write_bit(nack);
	if (send_stop) {
		i2c_stop();
	}
	return byte;
}
Beispiel #8
0
/*
  nack must be 0 if the data reading continues
  nack should be 1 after the last byte. send stop after this
*/
unsigned __attribute__ ((noinline)) i2c_read_byte(unsigned nack)
{
  unsigned i = 8;
  unsigned b = 0;
  do
  {
    b <<= 1;
    b |= i2c_read_bit();
    i--;
  } while ( i != 0 );
  i2c_write_bit(nack);
  return b;
}
Beispiel #9
0
/* Write a byte to I2C bus. Return 0 if ack by the slave */
int i2c_write_byte(unsigned char byte)
{
  unsigned bit;
  int nack;

  for (bit = 0; bit < 8; bit++) {
    i2c_write_bit((byte & 0x80) != 0);
    byte <<= 1;
  }
  nack = i2c_read_bit();

  return nack;
}
Beispiel #10
0
void i2c_write(unsigned char byte)
{

    TRISCbits.TRISC2 = 0; // writing data

    for (size_t i=0; i < 8; ++i) {
        i2c_write_bit((byte&0x80) == 0x80);
        byte <<= 1;
    }

    TRISCbits.TRISC2 = 1; // reading the nack
    i2c_read_bit();
}
Beispiel #11
0
static uint8_t i2c_write_byte(uint8_t b)
{
    i2c_write_bit(b & 128);
    i2c_write_bit(b & 64);
    i2c_write_bit(b & 32);
    i2c_write_bit(b & 16);
    i2c_write_bit(b & 8);
    i2c_write_bit(b & 4);
    i2c_write_bit(b & 2);
    i2c_write_bit(b & 1);

    /* read ack from client */
    /* 0: ack was given by client */
    /* 1: nothing happend during ack cycle */
    return i2c_read_bit();
}
Beispiel #12
0
uint8_t i2c_write_byte(uint8_t b)
{
  uint8_t i = 8;
  do
  {
    i2c_write_bit(b & 128);
    b <<= 1;
    i--;
  } while ( i != 0 );
  /* read ack from client */
  /* 0: ack was given by client */
  /* 1: nothing happend during ack cycle */  
  return i2c_read_bit();
}
Beispiel #13
0
unsigned __attribute__ ((noinline)) i2c_write_byte(unsigned b)
{
  unsigned i = 8;
  do
  {
    i2c_write_bit(b & 128);
    b <<= 1;
    i--;
  } while ( i != 0 );
  /* read ack from client */
  /* 0: ack was given by client */
  /* 1: nothing happend during ack cycle */  
  return i2c_read_bit();
}
Beispiel #14
0
// Write a byte to I2C bus. Return 0 if ack by the slave.
unsigned char i2cWriteByte(unsigned char send_start, unsigned char send_stop, unsigned char byte) {
	unsigned char bit;
	unsigned char nack;
	if (send_start) {
		i2c_start();
	}
	for (bit = 0; bit < 8; bit++) {
		i2c_write_bit((byte & 0x80) != 0);
		byte <<= 1;
	}
	nack = i2c_read_bit();
	if (send_stop) {
		i2c_stop();
	}
	return nack;
}
Beispiel #15
0
//*----------------------------------------------------------------------------
//*  Name     : i2c_write_byte()
//*  Brief    : Write a byte on I2C bus
//*  Argument : Data value which is 8bits
//*  Return   : ACK = 0, NACK = -1
//*----------------------------------------------------------------------------
int I2C_write_byte(uint8 c) {
	int i;
	
	GPIOR_CFGIO_OUTPUT(I2C_PORT, I2C_SDA_PIN);
	for( i = 0; i < 8; i++) {
		i2c_write_bit((c << i) & 0x80);
	}
	GPIOR_CFGIO_INPUT(I2C_PORT, I2C_SDA_PIN);
	// check ack
	if(i2c_read_bit()) {
		// no ack
		return -1;
	} else {
		// ack
		return 0;
	}
}
Beispiel #16
0
// Write a byte to I2C bus. Return 0 if ack by the slave.
bool i2c_write_byte(bool send_start,
                    bool send_stop,
                    unsigned char byte) {
  unsigned bit;
  bool nack;
  if (send_start) {
    i2c_start_cond();
  }
  for (bit = 0; bit < 8; bit++) {
    i2c_write_bit((byte & 0x80) != 0);
    byte <<= 1;
  }
  nack = i2c_read_bit();
  if (send_stop) {
    i2c_stop_cond();
  }
  return nack;
}