示例#1
0
/*FUNCTION*-------------------------------------------------------------------
*
* Function Name    : max3353_WriteReg
* Returned Value   :
* Comments         : Write data to max3353 register
*    
*
*END*----------------------------------------------------------------------*/
bool max3353_WriteReg
(
    uint8_t i2c_channel, 
    uint8_t regAdd , 
    uint8_t regValue
)
{
    /* Send I2C slave address to bus*/
    i2c_Start(i2c_channel);
    i2c_WriteByte(i2c_channel,(MAX3353_SLAVE_ADDR <<1) | 0);
    i2c_Wait(i2c_channel);

    /* Send register address of MAX3353 */
    i2c_WriteByte(i2c_channel, regAdd);
    i2c_Wait(i2c_channel);
    
    /* Send data to MAX3353*/
    i2c_WriteByte(i2c_channel, regValue);
    i2c_Wait(i2c_channel);

    /* Send stop signal */
    i2c_Stop(i2c_channel);
    Pause();
    return TRUE;
}
示例#2
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;
}
/*!
 * 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;
}
示例#4
0
/*!
 *  @brief      读取I2C设备指定地址寄存器的数据
 *  @param      I2Cn_e        I2C模块(I2C0、I2C1)
 *  @param      SlaveID     从机地址(7位地址)
 *  @param      reg         从机寄存器地址
 *  @return                 读取的寄存器值
 *  @since      v5.0
 *  Sample usage:       uint8 value = i2c_read_reg(I2C0, 0x1D, 1);
 */
uint8 i2c_read_reg(I2Cn_e i2cn, uint8 SlaveID, uint8 reg)
{

    //先写入寄存器地址,再读取数据,因此此过程是 I2C 的复合格式,改变数据方向时需要重新启动
    uint8 result;

    ASSERT((SlaveID & 0x80) == 0);                      //断言,我们要求的7位地址的值仅仅是7bit,不是通信时要求的高7位
    //有些手册,给出的7位地址指的是8bit里的高7位
    //有些手册,给出的7位地址指的是7bit
    //请自行确认,可以尝试是否通信正常来确认

    i2c_Start(i2cn);                                    //发送启动信号

    i2c_write_byte(i2cn, ( SlaveID << 1 ) | MWSR);      //发送从机地址和写位

    i2c_write_byte(i2cn, reg);                          //发送从机里的寄存器地址

    i2c_RepeatedStart(i2cn);                            //复合格式,发送重新启动信号

    i2c_write_byte(i2cn, ( SlaveID << 1) | MRSW );      //发送从机地址和读位

    i2c_PutinRxMode(i2cn);                              //进入接收模式(不应答,只接收一个字节)
    result = I2C_D_REG(I2CN[i2cn]);                     //虚假读取一次,启动接收数据
    i2c_Wait(i2cn);                                     //等待接收完成

    i2c_Stop(i2cn);                                     //发送停止信号

    result = I2C_D_REG(I2CN[i2cn]);                     //读取数据

    Pause();                                            //必须延时一下,否则出错

    return result;
}
/*!
 * 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();
}
示例#6
0
/*!
 * Write a byte of Data to specified register on MMA7660
 * @param u8RegisterAddress is Register Address
 * @param u8Data is Data to write
 */
void I2CWriteRegister(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();
}
示例#7
0
文件: i2c.c 项目: YoshimiSakuka/K10
/*************************************************************************
*                             野火嵌入式开发工作室
*
*  函数名称:I2C_WriteAddr
*  功能说明:写入一个字节数据到I2C设备指定寄存器地址
*  参数说明:I2Cn        模块号(I2C0、I2C1)
*            SlaveID     7位从机地址
*            Addr        从机的寄存器地址
*            Data        数据
*  函数返回:无
*  修改时间:2012-1-20
*  备    注:
*************************************************************************/
void I2C_WriteAddr(I2Cn i2cn, u8 SlaveID, u8 Addr, u8 Data)
{
    /* send data to slave */
    I2C_StartTransmission(i2cn, SlaveID, MWSR);    //启动传输
    i2c_Wait(i2cn);

    i2c_write_byte(i2cn, Addr);                    //写地址
    i2c_Wait(i2cn);

    i2c_write_byte(i2cn, Data);                    //写数据
    i2c_Wait(i2cn);

    i2c_Stop(i2cn);

    Pause();                                        //延时太短的话,可能写出错
}
示例#8
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;
}
示例#9
0
/*FUNCTION*-------------------------------------------------------------------
*
* Function Name    : max3353_ReadReg
* Returned Value   :
* Comments         : Read data from max3353 register
*    
*
*END*----------------------------------------------------------------------*/
bool max3353_ReadReg
(
    uint8_t     i2c_channel, 
    uint8_t     regAdd, 
    uint8_t*    p_regValue
)
{
    uint8_t result;
    uint32_t j;
    
    /* Send Slave Address */
    i2c_Start(i2c_channel);
    i2c_WriteByte(i2c_channel, (MAX3353_SLAVE_ADDR <<1) | 0);
    i2c_Wait(i2c_channel);
    
    /* Write Register Address */ 
    i2c_WriteByte(i2c_channel, regAdd);
    i2c_Wait(i2c_channel);

    /* Do a repeated start */
    i2c_RepeatedStart(i2c_channel);

    /* Send Slave Address */  
    i2c_WriteByte(i2c_channel, (MAX3353_SLAVE_ADDR << 1) | 0x01); 
    i2c_Wait(i2c_channel);

    /* Put in Rx Mode */
    i2c_SetRXMode(i2c_channel);

    /* Turn off ACK */
    i2c_GiveNACK(i2c_channel);

    /* Dummy read */
    result = i2c_ReadByte(i2c_channel);
    for (j=0; j<5000; j++){};  
    i2c_Wait(i2c_channel);
    
    /* Send stop signal */
    i2c_Stop(i2c_channel);

    result = i2c_ReadByte(i2c_channel);
    Pause();
    *p_regValue = result;
    return TRUE;
}
示例#10
0
// i2c_Read - Reads a byte from Slave device
uint8_t i2c_Read(uint8_t ack)
{
	// Read data from slave
	// ack should be 1 if there is going to be more data read
	// ack should be 0 if this is the last byte of data read
 	uint8_t i2cReadData;

 	i2c_Wait();
	RCEN=1;
 	i2c_Wait();
 	i2cReadData = SSPBUF;
 	i2c_Wait();
 	if ( ack ) ACKDT=0;			// Ack
	else       ACKDT=1;			// NAck
	ACKEN=1;   		            // send acknowledge sequence

	return( i2cReadData );
}
示例#11
0
// i2c_Address - Sends Slave Address and Read/Write mode
// mode is either I2C_WRITE or I2C_READ
void i2c_Address(uint8_t address, uint8_t mode)
{
	uint8_t l_address;

	l_address=address<<1;
	l_address+=mode;
 	i2c_Wait();
 	SSPBUF = l_address;
}
/*FUNCTION*-------------------------------------------------------------------
*
* Function Name    : I2C_WriteRegister
* Returned Value   : None
* Comments         : Write a byte of Data
*    
*
*END*----------------------------------------------------------------------*/
void I2C_WriteRegister(unsigned char u8I2CSlaveAddress,unsigned char u8RegisterAddress, unsigned char u8Data)
{
  /* send data to slave */
  IIC_StartTransmission(u8I2CSlaveAddress,MWSR);
  i2c_Wait();

  /* Send I2C address */
  I2C0_D = u8RegisterAddress;
  i2c_Wait();

  /* Send data */
  I2C0_D = u8Data;
  i2c_Wait();

  i2c_Stop();

  Pause();
}
示例#13
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();
	}
}
示例#14
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;
}
示例#15
0
文件: i2c.c 项目: YoshimiSakuka/K10
/*************************************************************************
*                             野火嵌入式开发工作室
*
*  函数名称:I2C_ReadAddr
*  功能说明:读取I2C设备指定地址寄存器的数据
*  参数说明:I2Cn        模块号(I2C0、I2C1)
*            SlaveID     7位从机地址
*            Addr        从机的寄存器地址
*  函数返回:从机寄存器的数据
*  修改时间:2012-1-20
*  备    注:
*************************************************************************/
u8 I2C_ReadAddr(I2Cn i2cn, u8 SlaveID, u8 Addr)
{
    u8 result;

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

    /* Write Register Address */
    i2c_write_byte(i2cn, Addr);
    i2c_Wait(i2cn);

    /* Do a repeated start */
    i2c_RepeatedStart(i2cn);

    /* Send Slave Address */
    i2c_write_byte(i2cn, ( SlaveID << 1) | MRSW );
    i2c_Wait(i2cn);

    /* Put in Rx Mode */
    i2c_PutinRxMode(i2cn);

    /* Turn off ACK since this is second to last byte being read*/
    i2c_DisableAck(i2cn); //不应答

    /* Dummy read 虚假读取*/
    result = I2C_D_REG(I2Cx[i2cn]);
    i2c_Wait(i2cn);

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

    /* Read byte */
    result = I2C_D_REG(I2Cx[i2cn]);

    return result;
}
/*FUNCTION*-------------------------------------------------------------------
*
* Function Name    : I2C_ReadRegister
* Returned Value   : None
* Comments         : Read a register 
*    
*
*END*----------------------------------------------------------------------*/
unsigned char I2C_ReadRegister(unsigned char u8I2CSlaveAddress,unsigned char u8RegisterAddress)
{
  unsigned char result;
  unsigned int j;

  /* Send Slave Address */
  IIC_StartTransmission(u8I2CSlaveAddress,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 = (u8I2CSlaveAddress << 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;
}
示例#17
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;
}
示例#18
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];
}
示例#19
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;
}
示例#20
0
// i2c_Start - Start I2C communication
void i2c_Start(void)
{
 	i2c_Wait();
	SSPCON2bits.SEN=1;
}
示例#21
0
// i2c_Restart - Re-Start I2C communication
void i2c_Restart(void){
 	i2c_Wait();
	SSPCON2bits.RSEN=1;
}
示例#22
0
// i2c_Stop - Stop I2C communication
void i2c_Stop(void)
{
 	i2c_Wait();
 	SSPCON2bits.PEN=1;
}
示例#23
0
// i2c_Write - Sends one byte of data
void i2c_Write(uint8_t data)
{
 	i2c_Wait();
 	SSPBUF = data;
}