Ejemplo n.º 1
0
/* Read num bytes starting from regAddrStart. Each subsequent byte will be from the register above (newRegPtr = oldRegPtr + 1).
 * 
 * Input:	regAddrStart	The register address to start reading bytes from
 *			data			Location to store read data
 *			num				The number of registers to be read
 * Return:	0				Succeeded
 *			>0				Failed, see handle_error() for relavant error codes
 */
uint8_t twi_read_bytes(uint8_t regAddrStart, uint8_t *data, uint8_t num) {
	uint8_t i, error;
	
	/* Transmit start condition. */
	if((error = twi_start())) return error;
	
	/* Transmit slave address and write bit. */
	if((error = twi_sla_w(IMU_ADDR))) return error;
	
	/* Transmit to slave the register to be read. */
	if((error = twi_write(regAddrStart))) return error;
	
	/* Transmit repeated start condition. */
	if((error = twi_start_r())) return error;
	
	/* Transmit slave address and read bit. */
	if((error = twi_sla_r(IMU_ADDR))) return error;
	
	/* Read data from slave. */
	for(i = 0; i < num; i++) {
		if(i == num - 1) {
			/* Last byte, return NACK. */
			if((error = twi_read_nack(&data[i]))) return error;
		} else {
			if((error = twi_read_ack(&data[i]))) return error;
		}
	}
	
	/* Transmit stop condition. */
	twi_stop();
	
	return 0;
}
Ejemplo n.º 2
0
uint8_t twi_read_byte(void)
{
    //uint8_t databyte;
    twi_start();

    if (twi_get_status() != 0x08)
        return ERROR;

    //select devise and send A2 A1 A0 address bits
    twi_write(0xD0);
    if (twi_get_status() != 0x18)
        return ERROR;

    twi_write(0x01);
    if (twi_get_status() != 0x18)
        return ERROR;
	twi_read_ack();

    twi_write(0xD1);
    if (twi_get_status() != 0x18)
        return ERROR;

	twi_start();

    twi_read_nack();
    if (twi_get_status() != 0x58)
        return ERROR;
    twi_stop();

    return SUCCESS;
}
Ejemplo n.º 3
0
/**
 * Function   : twi::twi_read_bytes()
 * Purpose    : To receive a byte array.
 * Parameters : - data : The data array.
 *				- size : The size of the array.
 * Returns    : Nothing.
 * Notes      : None.
 **/
void twi_read_bytes(BYTE *data, BYTE size)
{
	for(BYTE i=0 ; i<size ; i++)
	{
		if(i < (size - 1)) data[i] = twi_read_ack();
		else			   data[i] = twi_read_nak();
	}
}