Example #1
0
int8_t 	SOFTI2C::read_byte(uint8_t slave_address,uint8_t reg_address,uint8_t* data,uint16_t num_to_read)
{
	int8_t ret = 0;
	int i = 0;
	
		start();

    if (send_7bits_address(slave_address) == -1)
        ret = -1;


    if (send_byte(reg_address) == -1)
        ret = -2;

		start();
    if (send_7bits_address(slave_address + 1) == -1)
        ret = -3;
		
		while(num_to_read)
		{
			*data++ = receive_byte();
			num_to_read--;
			i++;
			if(num_to_read == 0)
			{
				send_no_ack();
				stop();
				ret = 0;
			}
			else
				send_ack();
		}

	return ret;
}
Example #2
0
int8_t I2C::read_byte(uint8_t slaveAddress,uint8_t regAddress,uint8_t* data,uint16_t numToRead)
{
	uint8_t i = 0;
	start();
	send_7bits_address(slaveAddress);
	I2C_Cmd(_I2Cx,ENABLE);
	send_byte(regAddress);
	start();
	send_7bits_address(slaveAddress + 1);
	
	while(numToRead)
	{
		if(numToRead == 1)
		{
			send_no_ack();
			stop();
		}
	  receive_byte(data);
		data++;
		numToRead--;
		i++;
	}
	send_ack();

	return i;
}
Example #3
0
int8_t I2C::read_byte(uint8_t slaveAddress,uint8_t regAddress,uint8_t* data)
{
	start();
	send_7bits_address(slaveAddress);
	I2C_Cmd(_I2Cx,ENABLE);
	send_byte(regAddress);
	start();
	send_7bits_address(slaveAddress + 1);
	send_no_ack();
	stop();
	receive_byte(data);
	send_ack();
	return 0;
}
Example #4
0
int8_t I2C::write_byte(uint8_t slaveAddress,uint8_t regAddress,uint8_t data)
{
	uint16_t err = 0;

	start();
	send_7bits_address(slaveAddress);
	send_byte(regAddress);
	send_byte(data);
	stop();

	return err;
	
}
Example #5
0
int8_t I2C::write_byte(uint8_t slaveAddress,uint8_t regAddress,uint8_t* data,uint16_t numToRead)
{
	uint16_t err = 0;

	start();
	send_7bits_address(slaveAddress);
	send_byte(regAddress);
	while(numToRead--)
	{
	  send_byte(*data);
		data++;
	}
	stop();

	return err;
	
}
Example #6
0
int8_t I2c::write_byte(uint8_t slave_address, uint8_t reg_address, uint8_t *data, uint16_t num_to_write)
{
    uint16_t err = 0;

    start();
    send_7bits_address(slave_address);
    send_byte(reg_address);
    while(num_to_write--)
    {
        send_byte(*data);
        data++;
    }
    stop();

    return err;

}
Example #7
0
int8_t I2C::wait_dev_busy(uint8_t slaveAddress)
{
	int8_t ret;
	uint8_t i = 0;
	do
	{
		start();
		ret = send_7bits_address(slaveAddress);
		send_ack();
		send_byte(slaveAddress);
		stop();
		if(i++==100)
		{
			return -1;
		}
	}while(ret != 0);//如果返回值不是0,继续等待
	return 0;
}
Example #8
0
int8_t SOFTI2C::write_byte(uint8_t slave_address,uint8_t reg_address,uint8_t data)
{
	int8_t ret = 0;
		start();
	
    if (send_7bits_address(slave_address) == -1)
        ret = -1;
		
    if (send_byte(reg_address) == -1)
        ret = -2;

    if (send_byte(data) == -1)
        ret = -3;
		
		stop();

    delay_us(10);      
    return ret;
}
Example #9
0
int8_t SOFTI2C::write_byte(uint8_t slave_address,uint8_t reg_address,uint8_t* data,uint16_t num_to_write)
{
	int8_t ret = 0;
		start();

    if (send_7bits_address(slave_address) == -1)
        ret = -1;
		
    if (send_byte(reg_address) == -1)
        ret = -2;

		while(num_to_write--)
		{
			send_byte(*data++);
			if (wait_ack() == -1)
					ret = -3;
		}
		
		stop();

    delay_us(10);      
    return ret;
}
Example #10
0
int8_t 	SOFTI2C::read_byte(uint8_t slave_address,uint8_t reg_address,uint8_t* data)
{

	int8_t ret = 0;
		start();

    if (send_7bits_address(slave_address) == -1)
        ret = -1;


    if (send_byte(reg_address) == -1)
        ret = -2;

		start();

    if (send_byte(slave_address + 1) == -1)
        ret = -3;

		*data = receive_byte();
		send_no_ack();
		stop();

	return ret;
}