Пример #1
0
/*!
 * Read a register from the MMA7660
 * @param u8RegisterAddress is Register Address
 * @return Data stored in Register
 */
unsigned char u8MMA7660ReadRegister(unsigned char u8RegisterAddress)
{
    unsigned char result;

    /* Send Slave Address */
    IIC_StartTransmission(SlaveID,MWSR);
    i2c_Wait();

    /* Write Register Address */
    I2C1_D = u8RegisterAddress;
    i2c_Wait();

    /* Do a repeated start */
    I2C1_C1 |= I2C_C1_RSTA_MASK;

    /* Send Slave Address */
    I2C1_D = (MMA7660_I2C_ADDRESS << 1) | 0x01; //read address
    i2c_Wait();

    /* Put in Rx Mode */
    I2C1_C1 &= (~I2C_C1_TX_MASK);

    /* Turn off ACK */
    I2C1_C1 |= I2C_C1_TXAK_MASK;

    /* Dummy read */
    result = I2C1_D ;
    i2c_Wait();

    /* Send stop */
    i2c_Stop();
    result = I2C1_D ;
    return result;
}
Пример #2
0
/*!
 * Read first three registers from the MMA7660
 * @param u8RegisterAddress is Register Address
 * @return Data stored in Register
 */
unsigned long u8MMA7660ReadThreeRegisters(unsigned char u8RegisterAddress)
{
  unsigned char result1 = 0, result2 = 0, result3 = 0;

  /* Send Slave Address */
  IIC_StartTransmission(SlaveID,MWSR);
  i2c_Wait();

  /* Write Register Address */
  I2C0_D = u8RegisterAddress;
  i2c_Wait();

  /* Do a repeated start */
  I2C0_C1 |= I2C_C1_RSTA_MASK;

  /* Send Slave Address */
  I2C0_D = (MMA7660_I2C_ADDRESS << 1) | 0x01; //read address
  i2c_Wait();

  /* Put in Rx Mode */
  I2C0_C1 &= (~I2C_C1_TX_MASK);

  /* Ensure TXAK bit is 0 */
  I2C0_C1 &= ~I2C_C1_TXAK_MASK;

  /* Dummy read */
  result1 = I2C0_D ;
  i2c_Wait();

  /* Read first byte */
  result1 = I2C0_D;
  i2c_Wait();

  /* Turn off ACK since this is second to last read*/
  I2C0_C1 |= I2C_C1_TXAK_MASK;

  /* Read second byte */
  result2 = I2C0_D;
  i2c_Wait();

  /* Send stop */
  i2c_Stop();

  /* Read third byte */
  result3 = I2C0_D;
  

  unsigned long totalresult = result1;
  totalresult <<= 8;
  totalresult |= result2;
  totalresult <<= 8;
  totalresult |= result3;
  
  // Provide a gap between this read and the next
  time_delay_ms(250);
  
  return totalresult;
}
Пример #3
0
/*!
 * Read first three registers from the MMA7660
 * @param u8RegisterAddress is Register Address
 * @return Data stored in Register
 */
unsigned char I2CReadMultiRegisters(unsigned char u8RegisterAddress, unsigned char bytes)
{
  unsigned char n=bytes;
  int i;

  /* Send Slave Address */
  IIC_StartTransmission(SlaveID,MWSR);
  i2c_Wait();

  /* Write Register Address */
  I2C0_D = u8RegisterAddress;
  i2c_Wait();

  /* Do a repeated start */
  I2C0_C1 |= I2C_C1_RSTA_MASK;

  /* Send Slave Address */
  I2C0_D = (ACCEL_I2C_ADDRESS << 1) | 0x01; //read address
  i2c_Wait();

  /* Put in Rx Mode */
  I2C0_C1 &= (~I2C_C1_TX_MASK);

  /* Ensure TXAK bit is 0 */
  I2C0_C1 &= ~I2C_C1_TXAK_MASK;

  /* Dummy read */
  result[0] = I2C0_D ;
  i2c_Wait();

  for(i=0;i<n-2;i++)
  {
    /* Read first byte */
    result[i] = I2C0_D;
    i2c_Wait();
  }
  /* Turn off ACK since this is second to last read*/
  I2C0_C1 |= I2C_C1_TXAK_MASK;

  /* Read second byte */
  result[i++] = I2C0_D;
  i2c_Wait();

  /* Send stop */
  i2c_Stop();

  /* Read third byte */
  result[i++] = I2C0_D;

//  printf("%3d    %3d     %3d\n",result[0],result[2],result[4]);
  return result[0];
}
Пример #4
0
/*!
 * Read a register from the MPR084
 * @param u8RegisterAddress is Register Address
 * @return Data stored in Register
 */
unsigned char i2cMultiReadRegister(uint8_t Address, uint8_t u8RegisterAddress, uint8_t *DataBlock, uint8_t ReadSize)
{
	uint8_t result = 0;
	unsigned int j;

	/* Send Slave Address */
	IIC_StartTransmission(Address,SlaveID,MWSR);
	i2c_Wait();

	/* Write Register Address */
	I2C0_D = u8RegisterAddress;
	i2c_Wait();

	/* Do a repeated start */
	I2C0_C1 |= I2C_C1_RSTA_MASK;

	/* Send Slave Address */
	I2C0_D = (Address << 1) | 0x01; //read address
	i2c_Wait();

	/* Put in Rx Mode */
	I2C0_C1 &= (~I2C_C1_TX_MASK);

	I2C0_C1 &= (~I2C_C1_TXAK_MASK);
	/* Turn off ACK */
//	I2C0_C1 |= I2C_C1_TXAK_MASK;

	/* Dummy read */
	result = I2C0_D ;
	for (j=0; j<5000; j++){};
	i2c_Wait();

	result = 0;
	while(ReadSize--)
	{
		DataBlock[result++] = I2C0_D;
		i2c_Wait();

		if(ReadSize == 1)
		{
			/* Turn off ACK */
			I2C0_C1 |= I2C_C1_TXAK_MASK;
		}
	}
	/* Send stop */
	i2c_Stop();
	DataBlock[result] = I2C0_D ;
	Pause();
	return result;
}
Пример #5
0
/*!
 * Read first three registers from the MMA7660
 * @param u8RegisterAddress is Register Address
 * @return Data stored in Register
 */
unsigned char u8MMA7660ReadThreeRegisters(unsigned char u8RegisterAddress)
{
  unsigned char result1, result2, result3;

  /* Send Slave Address */
  IIC_StartTransmission(SlaveID,MWSR);
  i2c_Wait();

  /* Write Register Address */
  I2C0_D = u8RegisterAddress;
  i2c_Wait();

  /* Do a repeated start */
  I2C0_C1 |= I2C_C1_RSTA_MASK;

  /* Send Slave Address */
  I2C0_D = (MMA7660_I2C_ADDRESS << 1) | 0x01; //read address
  i2c_Wait();

  /* Put in Rx Mode */
  I2C0_C1 &= (~I2C_C1_TX_MASK);

  /* Ensure TXAK bit is 0 */
  I2C0_C1 &= ~I2C_C1_TXAK_MASK;

  /* Dummy read */
  result1 = I2C0_D ;
  i2c_Wait();

  /* Read first byte */
  result1 = I2C0_D;
  i2c_Wait();

  /* Turn off ACK since this is second to last read*/
  I2C0_C1 |= I2C_C1_TXAK_MASK;

  /* Read second byte */
  result2 = I2C0_D;
  i2c_Wait();

  /* Send stop */
  i2c_Stop();

  /* Read third byte */
  result3 = I2C0_D;

  printf("%3d    %3d     %3d\n",result1,result2,result3);
  return result1;
}
Пример #6
0
/*!
 * Write a byte of Data to specified register on MPR084
 * @param u8RegisterAddress is Register Address
 * @param u8Data is Data to write
 */
void I2C_WriteRegister(unsigned char u8I2CSlaveAddress,unsigned char u8RegisterAddress, unsigned char u8Data)
{
  /* send data to slave */
  IIC_StartTransmission(u8I2CSlaveAddress,MWSR);
  i2c0_Wait();

  I2C0_D = u8RegisterAddress;
  i2c0_Wait();

  I2C0_D = u8Data;
  i2c0_Wait();

  i2c0_Stop();

  Pause();
}
Пример #7
0
/*!
 * Write a byte of Data to specified register on MMA7660
 * @param u8RegisterAddress is Register Address
 * @param u8Data is Data to write
 */
void MMA7660WriteRegister(unsigned char u8RegisterAddress, unsigned char u8Data)
{
  /* send data to slave */
  IIC_StartTransmission(SlaveID,MWSR);
  i2c_Wait();

  I2C0_D = u8RegisterAddress;
  i2c_Wait();

  I2C0_D = u8Data;
  i2c_Wait();

  i2c_Stop();

  Pause();
}
Пример #8
0
/*!
 * Write a byte of Data to specified register on I2C0 bus
 * @param Address I2C bus address
 * @param u8RegisterAddress is Register Address
 * @param u8Data is Data to write
 */
void i2cWriteRegister(uint8_t Address, uint8_t u8RegisterAddress, uint8_t u8Data)
{
	/* send data to slave */
	IIC_StartTransmission(Address,SlaveID,MWSR);
	i2c_Wait();

	I2C0_D = u8RegisterAddress;
	i2c_Wait();

	I2C0_D = u8Data;
	i2c_Wait();

	i2c_Stop();

	Pause();
}
Пример #9
0
/*!
 * Read a register from the MMA7660
 * @param u8RegisterAddress is Register Address
 * @return Data stored in Register
 */
unsigned char u8MMA7660ReadRegister(unsigned char u8RegisterAddress)
{
  signed char result = 0;
  int tries_remaining = 5;
  
//  while ((result & 0x40) && tries_remaining--)
//  {
    /* Send Slave Address */
    IIC_StartTransmission(SlaveID,MWSR);
    i2c_Wait();
  
    /* Write Register Address */
    I2C0_D = u8RegisterAddress;
    i2c_Wait();
  
    /* Do a repeated start */
    I2C0_C1 |= I2C_C1_RSTA_MASK;
  
    /* Send Slave Address */
    I2C0_D = (MMA7660_I2C_ADDRESS << 1) | 0x01; //read address
    i2c_Wait();
  
    /* Put in Rx Mode */
    I2C0_C1 &= (~I2C_C1_TX_MASK);
  
    /* Turn off ACK since this is second to last byte being read*/
    I2C0_C1 |= I2C_C1_TXAK_MASK;
  
    /* Dummy read */
    result = I2C0_D ;
    i2c_Wait();
  
    /* Send stop since about to read last byte */
    i2c_Stop();
  
    /* Read byte */
    result = I2C0_D ;
 // }
  
  if (result & 0x40)
  {
    puts("Returning alerted (invalid) result");
  }

  time_delay_ms(200);
  return result;
}
Пример #10
0
/*!
 * Write a byte of Data to specified register on I2C0 bus
 * @param Address I2C bus address
 * @param PtrData pointer to where the data is located
 * @param Size number of bytes to write
 */
void i2cWriteData(uint8_t Address, uint8_t *PtrData,uint8_t Size)
{
	uint8_t i;
	if(Size)
	{
		/* send data to slave */
		IIC_StartTransmission(Address,SlaveID,MWSR);
		i2c_Wait();

		for(i=0; i<Size; i++)
		{
			I2C0_D = PtrData[i];
			i2c_Wait();
		}
		i2c_Stop();

		Pause();
	}
}
Пример #11
0
/*!
 * Read a register from the MPR084
 * @param u8RegisterAddress is Register Address
 * @return Data stored in Register
 */
unsigned char I2C_ReadRegister(unsigned char u8I2CSlaveAddress,unsigned char u8RegisterAddress)
{
  unsigned char result;
  unsigned int j;

  /* Send Slave Address */
  IIC_StartTransmission(u8I2CSlaveAddress,MWSR);
  i2c0_Wait();

  /* Write Register Address */
  I2C0_D = u8RegisterAddress;
  i2c0_Wait();

  /* Do a repeated start */
  I2C0_C1 |= I2C_C1_RSTA_MASK;

  /* Send Slave Address */
  I2C0_D = (u8I2CSlaveAddress << 1) | 0x01; //read address
  i2c0_Wait();

  /* Put in Rx Mode */
  I2C0_C1 &= (~I2C_C1_TX_MASK);

  /* Turn off ACK */
  I2C0_C1 |= I2C_C1_TXAK_MASK;

  /* Dummy read */
  result = I2C0_D ;
  for (j=0; j<5000; j++){}; //not sure why need delay
  i2c0_Wait();

  /* Send stop */
  i2c0_Stop();
  result = I2C0_D ;
  Pause();
  return result;
}
Пример #12
0
/*!
 * Read a register from the MMA7660
 * @param u8RegisterAddress is Register Address
 * @return Data stored in Register
 */
unsigned char I2CReadRegister(unsigned char u8RegisterAddress)
{
  unsigned char result;

  /* Send Slave Address */
  IIC_StartTransmission(SlaveID,MWSR);
  i2c_Wait();

  /* Write Register Address */
  I2C0_D = u8RegisterAddress;
  i2c_Wait();

  /* Do a repeated start */
  I2C0_C1 |= I2C_C1_RSTA_MASK;

  /* Send Slave Address */
  I2C0_D = (ACCEL_I2C_ADDRESS << 1) | 0x01; //read address
  i2c_Wait();

  /* Put in Rx Mode */
  I2C0_C1 &= (~I2C_C1_TX_MASK);

  /* Turn off ACK since this is second to last byte being read*/
  I2C0_C1 |= I2C_C1_TXAK_MASK;

  /* Dummy read */
  result = I2C0_D ;
  i2c_Wait();

  /* Send stop since about to read last byte */
  i2c_Stop();

  /* Read byte */
  result = I2C0_D ;

  return result;
}
Пример #13
0
/*!
 * Read a register from the MPR084
 * @param u8RegisterAddress is Register Address
 * @return Data stored in Register
 */
unsigned char i2cReadRegister(uint8_t Address, uint8_t u8RegisterAddress)
{
	uint8_t result;
	unsigned int j;

	/* Send Slave Address */
	IIC_StartTransmission(Address,SlaveID,MWSR);
	i2c_Wait();

	/* Write Register Address */
	I2C0_D = u8RegisterAddress;
	i2c_Wait();

	/* Do a repeated start */
	I2C0_C1 |= I2C_C1_RSTA_MASK;

	/* Send Slave Address */
	I2C0_D = (Address << 1) | 0x01; //read address
	i2c_Wait();

	/* Put in Rx Mode */
	I2C0_C1 &= (~I2C_C1_TX_MASK);

	/* Turn off ACK */
	I2C0_C1 |= I2C_C1_TXAK_MASK;

	/* Dummy read */
	result = I2C0_D ;
	for (j=0; j<5000; j++){};
	i2c_Wait();

	/* Send stop */
	i2c_Stop();
	result = I2C0_D ;
	Pause();
	return result;
}