Beispiel #1
0
uint8_t i2c_WriteBufferReadByte(uint8_t nAddress, uint8_t *pByteIn, uint8_t nLen, uint8_t *pByteOut)
{
    uint8_t stat = 0;

    do {

        stat = i2c_start( nAddress, TW_WRITE );
        while ( nLen && !stat) {

            stat = i2c_write( *pByteIn );
            nLen--;
            pByteIn++;
        }

        if ( stat )
            break;

        stat = i2c_rep_start( nAddress, TW_READ );
        if ( stat )
            break;

        stat = i2c_read( pByteOut,0 );

    } while(0);

    i2c_stop();

    return stat;
}
Beispiel #2
0
uint8_t i2c_WriteByteReadBuffer(uint8_t nAddress, uint8_t nByteIn, uint8_t *pByteOut, uint8_t nBufSize, uint8_t *pLen)
{
    uint8_t stat = 0;

    *pLen = 0;

    do {
        stat = i2c_start( nAddress, TW_WRITE );
        if ( stat ) 
            break;

        stat = i2c_write( nByteIn );
        if ( stat ) 
            break;

        stat = i2c_rep_start( nAddress, TW_READ );

        while (!stat && nBufSize) {
        
            stat = i2c_read( pByteOut, nBufSize != 1 );

            if ( !stat || stat == TW_MR_DATA_NACK) {
                nBufSize--;
                pByteOut++;
                (*pLen)++;
                stat = 0;
            }
        }
    } while(0);

    i2c_stop();

    return stat;
}
Beispiel #3
0
static int read(uint8_t *ptr, uint8_t offset, uint8_t len) {
	int ret;

	// START condition
	ret = i2c_start(DS1307_ADDR + I2C_WRITE);
	if (ret) {
		i2c_stop();
		return -1;
	}

	// Send the pointer address
	ret = i2c_write(offset);
	if (ret) {
		i2c_stop();
		return -1;
	}

	// REPEATED START condition
	ret = i2c_rep_start(DS1307_ADDR + I2C_READ);
	if (ret) {
		i2c_stop();
		return -1;
	}

	// Read all the bytes
	while (len--) {
		*ptr++ = i2c_read(len > 0);
	}

	// STOP condition
	i2c_stop();

	return 0;
}
void collect_data_mpu(void) {
	i2c_start(MPU_6050_WRITE_ADDRESS);
	i2c_write(MPU_6050_REGISTER_ACCEL_XOUT_H);
	i2c_rep_start(MPU_6050_READ_ADDRESS);
	i2c_read_ack(&accelXH);
	i2c_read_ack(&accelXL);
	i2c_read_ack(&accelYH);
	i2c_read_ack(&accelYL);
	i2c_read_ack(&accelZH);
	i2c_read_ack(&accelZL);
	i2c_read_ack(&temperatureH);
	i2c_read_ack(&temperatureL);
	i2c_read_ack(&gyroXH);
	i2c_read_ack(&gyroXL);
	i2c_read_ack(&gyroYH);
	i2c_read_ack(&gyroYL);
	i2c_read_ack(&gyroZH);
	i2c_read_nack(&gyroZL);
	i2c_stop();
	accelX = (accelXH << 8) | accelXL;
	accelY = (accelYH << 8) | accelYL;
	accelZ = (accelZH << 8) | accelZL;
	temperature = (temperatureH << 8) | temperatureL;
	gyroX = (gyroXH << 8) | gyroXL;
	gyroY = (gyroYH << 8) | gyroYL;
	gyroZ = (gyroZH << 8) | gyroZL;
}
Beispiel #5
0
uint8_t i2c_TransactByte(uint8_t nAddress, uint8_t nByteIn, uint8_t * pByteOut)
{
    uint8_t stat = 0;

    do {

        stat = i2c_start( nAddress, TW_WRITE );
        if ( stat )
            break;

        stat = i2c_write( nByteIn );
        if ( stat ) 
            break;

        stat = i2c_rep_start( nAddress, TW_READ );
        if ( stat )
            break;

        stat = i2c_read( pByteOut,0 );

    } while (0);

    i2c_stop();

    return stat;
}
Beispiel #6
0
uint8_t ds3231_get_time(struct DS3231_Time *p)
{
    uint8_t ack = 0;
    ack |= i2c_start(DS3231_ADDRESS | I2C_WRITE);
    ack |= i2c_write(DS3231_SECONDS);
    ack |= i2c_rep_start(DS3231_ADDRESS | I2C_READ);
    if (ack != 0)
    {
        i2c_stop();
        return ack;
    }
    p->seconds_bcd      = i2c_readAck();
    p->minutes_bcd      = i2c_readAck();
    p->hours_bcd        = i2c_readAck();
    p->day_of_week      = i2c_readAck();
    p->day_of_month_bcd = i2c_readAck();
    p->month_bcd        = i2c_readAck();
    p->year_bcd         = i2c_readNak() + DS3231_YEAR_ZERO;
    i2c_stop();

    if ((p->month_bcd & DS3231_CENTURY) != 0)
    {
        p->year_bcd |= 0x0100;
        p->month_bcd &= ~DS3231_CENTURY;
    }

    p->ampm = 0;
    if ((p->hours_bcd & DS3231_12_24) != 0)
    {
        p->ampm = ((p->hours_bcd & DS3231_AM_PM) >> 5) + 1;
        p->hours_bcd &= 0x1F;
    }
Beispiel #7
0
uint8_t getIR(void)
{
    uint16_t tempdata = 0;
    uint8_t returnvar = 0;

    for(uint8_t i = 0; i < NUMBER_OF_MLX; i++)
    {
        if((i2c_start(pgm_read_byte(&mlx90614_i2c_addresses[i])) == 0) &&
           (i2c_write(I2C_REG_MLX90614) == 0) &&
           (i2c_rep_start(pgm_read_byte(&mlx90614_i2c_addresses[i]) + I2C_READ) == 0))
        {
            for(uint8_t i = 0; i<2; i++)
                i2cbuf[i] = i2c_readAck();
            i2cbuf[2] = i2c_readNak();

            if(i2c_stop() != 0)
                returnvar |= (1<<i);
            else
                returnvar &= ~(1<<i);

            // This masks off the error bit of the high byte, then moves it left 8 bits and adds the low byte.
            tempdata = (((i2cbuf[1] & 0x007F) << 8) + i2cbuf[0]);
            tempdata = ((tempdata * 2)-1);
            mlx90614[i].is = (tempdata - 27315);
        }
        else returnvar |= (1<<i);
    }

    return returnvar;
}
int main (void)
{
	uint16_t heading;
	int16_t pitch, roll;
	uint16_t headingIntegerPart, headingFractionPart;
	int16_t pitchIntegerPart, pitchFractionPart;
	int16_t rollIntegerPart, rollFractionPart;

	// initialize the hardware stuff we use
	InitTimer ();
	InitUART ();
	i2c_init ();

	fdevopen (UART0_PutCharStdio, UART0_GetCharStdio);

	// set the LED port for output
	LED_DDR |= LED_MASK;

	printf ("\nHoneywell HMC6343 I2C Compass Test\n\n");

	// Flash the LED for 1/2 a second...
	turnOnLED ();
	ms_spin (500);
	turnOffLED ();

	while (1)	// outer loop is once every 250 ms (more or less)
	{
		// send the HEADING command to the compass
		i2c_start_wait (COMPASS_ADDRESS_WRITE);
		i2c_write (COMPASS_HEADING_COMMAND);

		// now read the response
		i2c_rep_start (COMPASS_ADDRESS_READ);
		heading = (i2c_readAck () * 256) + i2c_readAck ();
		pitch = (i2c_readAck () * 256) + i2c_readAck ();
		roll = (i2c_readAck () * 256) + i2c_readNak ();
		i2c_stop ();

		headingIntegerPart = heading / 10;
		headingFractionPart = heading - (headingIntegerPart * 10);
		pitchIntegerPart = pitch / 10;
		pitchFractionPart = pitch - (pitchIntegerPart * 10);
		if (pitchFractionPart < 0)
			pitchFractionPart *= -1;
		rollIntegerPart = roll / 10;
		rollFractionPart = roll - (rollIntegerPart * 10);
		if (rollFractionPart < 0)
			rollFractionPart *= -1;

		printf ("Heading: %3d.%1d   Pitch: %3d.%1d   Roll: %3d.%1d\n", headingIntegerPart, headingFractionPart, pitchIntegerPart, pitchFractionPart, rollIntegerPart, rollFractionPart);

		turnOnLED ();
		ms_spin (100);
		turnOffLED ();
		ms_spin (150);
	}

	// we'll never get here...
	return 0;
}
Beispiel #9
0
/*
	get temperature (16 bit) from thermometer and stores in array
	input:	none
	output:	none
*/
void therm_getTemp16bit(void)
{
	// send read temperature command
	if(i2c_start(THERM_ADDR+I2C_WRITE))
	{
		// start condition failed
		i2c_stop();
		error(1);		
	}		
	else
	{
		// start condition ok, device accessible
		i2c_write(THERM_READ_TEMP);
	}

	//send repeated start to begin reading temperature
	if(i2c_rep_start(THERM_ADDR+I2C_READ))
	{
		// start condition failed
		i2c_stop();
		error(1);		
	}
	else
	{
		// start condition ok, device accessible
		// read MSB
		temp_reading[TEMP_MSB] = i2c_readAck();

		// read LSB
		temp_reading[TEMP_LSB] = i2c_readNak();
		i2c_stop();
	}
}
Beispiel #10
0
//Read 8bit data from register with 8bit address
void Gyroscope::cmr_read(byte address) {
  i2c_start_wait( (ADDR << 1) | I2C_WRITE ); // Start / Device address and Write (0)
  i2c_write(address); // 8bit register address 
  i2c_rep_start( (ADDR << 1) | I2C_READ ); // Repeated Start / Device address and Read (1)
  res = i2c_readNak(); // Read the result to res
  i2c_stop(); // End condition
}
void _rtc_get_date_dev(Date* tm, uint8_t year_offset, uint8_t addr)
{
   uint8_t c;
   i2c_start_wait(addr+RTC_DEV_WRITE_ADDRESS);  // set device address and write mode
   i2c_write(0x02);                            // write memory address = 0x02
   i2c_rep_start(addr+RTC_DEV_READ_ADDRESS);    // set device address and read mode
   
   c = i2c_readAck();                     // 0x02
   tm->seconds = rtc_bcd_to_binary(c);
   
   c = i2c_readAck();                     // 0x03
   tm->minutes = rtc_bcd_to_binary(c);
	  
   // Throw away AMPM information
   i2c_readAck();                         // 0x04
   
   c = i2c_readAck();                     // 0x05
   tm->day = rtc_bcd_to_binary(c & 0x3F);
   tm->year =  ((rtc_bcd_to_binary(c) >> 6) & 0x3) + 1970 + year_offset;
         
   c = i2c_readNak();                     // 0x06: don't acknowledge the last byte
   
   // Throw away day of week information
   //tm->dayOfWeek = ((c >> 5) & 0x7);
   tm->month = (c & 0x1F);
      
   i2c_stop();					          // stop i2c bus
}
// Lese MD49 Daten (Encodervalues, Mode, Acceleration, etc..)
// von Slave_Drivecontrol und speichere sie in Array MD49data
void readMD49data(void){
	uint8_t i;
	// Daten von I2C- Slave lesen im Master-Receiver-Mode
	if(!(i2c_start(SLAVE_ADRESSE+I2C_WRITE))) 			// Slave bereit zum lesen?
	{
		i2c_write(15); 									// Buffer Startadresse zum Auslesen
		i2c_rep_start(SLAVE_ADRESSE+I2C_READ); 			// Lesen beginnen
			for (i=0;i<18;i++)							// Empfangene Daten in Array schreiben
				{
					if ((i) < 17)
					{
						MD49data[i]=i2c_readAck();		// Bytes lesen...
					}
					else
					{
						MD49data[i]=i2c_readNak();		// letztes Byte lesen...
					}
				}
		i2c_stop();										// Zugriff beenden
	}
	else
	{
	//		/* Fehlerbehandlung... */
	}
}
Beispiel #13
0
// Free returned memory!
uint8_t *memGetBytes(uint32_t address, uint8_t length) {
	// We could use the High-Speed Mode of the FM24V10 here, but we don't, right now...
	uint8_t addA, addB, memAddress = MEMTWIADDRESS, i, *ret;
	if (address >= 65536) {
		// Address needs more than 16 bits, we have to set the PAGE bit in i2c address
		memAddress |= 2;
	}
	addA = (address & 0xFF00) >> 8;
	addB = address & 0xFF;
	ret = (uint8_t *)malloc(length); // Allocate memory for values read
	if (ret == NULL) {
		serialWriteString(getString(24));
		return NULL;
	}

	if (i2c_start(memAddress | I2C_WRITE) == 0) {
		i2c_write(addA);
		i2c_write(addB);
		i2c_rep_start(memAddress | I2C_READ);
		for (i = 0; i < (length - 1); i++) {
			ret[i] = i2c_readAck();
		}
		ret[length - 1] = i2c_readNak();
		i2c_stop();
		return ret;
	} else {
		return NULL;
	}
}
Beispiel #14
0
uint8_t gps_read_ram(uint8_t address, uint8_t length, unsigned char *data)
{
  uint8_t rc;
  uint8_t pos;

  /*
  rc = i2c_start(0x60, I2C_WRITE);
  if(rc) return 1;

  rc = i2c_write(address);
  if(rc) return 2;

  i2c_stop();
  */
  rc = i2c_rep_start(0x60, I2C_READ);
  if(rc) return rc;

  for(pos=0; pos < length; pos++, data++)
  {
    *data = (char)i2c_read(pos != (length-1));
  }
  i2c_stop();

  return 0;
}
Beispiel #15
0
uint8_t rtc_read(unsigned char addr) {
     uint8_t ret;
     i2c_start_wait(DevRTC);                  // set device address and write mode
     i2c_write(addr);                         // write address = 0
     i2c_rep_start(DevRTC+1);                 // set device address and read mode
     ret = i2c_readNak();                     // read one byte form address 0
     i2c_stop();                              // set stop condition = release bus
     return ret;
}
Beispiel #16
0
uint8_t adxl345_read(uint8_t register_name) {
	uint8_t ret;
	i2c_start(ADXL345_ADDR + I2C_WRITE);
	i2c_write(register_name);
	i2c_rep_start(ADXL345_ADDR + I2C_READ);
	ret = i2c_readNak();
	i2c_stop();
	return ret;
}
Beispiel #17
0
unsigned char getTemp() {
  i2c_init();
  i2c_start_wait(TC74Address + I2C_WRITE);
  i2c_write(0x00);
  i2c_rep_start(TC74Address + I2C_READ);
  char temp = i2c_readNak();
  i2c_stop();
  return temp;
}
Beispiel #18
0
int16_t MPU6050_signed_readreg(uint8_t accel, uint8_t reg)//read signed 16 bits
{
	i2c_start_wait(accel+I2C_WRITE); // set device address and write mode
	i2c_write(reg);                                  // ACCEL_OUT
	i2c_rep_start(accel+I2C_READ);    // set device address and read mode
	char raw1 = i2c_readAck();                    // read one intermediate byte
	int16_t raw2 = (raw1<<8) | i2c_readNak();        // read last byte
	i2c_stop();
	return raw2;
}
Beispiel #19
0
int main () {
	uint8_t reg0_value;
	i2c_init();
	i2c_start(0xE0);
	i2c_write(0x00);
	i2c_rep_start(0xE0);
	reg0_value = i2c_readNak();
	i2c_stop();
    return 0;
}
uint8_t
lis302_read_register(uint8_t register_address)
{
  uint8_t result = -1;
  i2c_start_wait(I2C_7BIT_WRITE(LIS302_I2C_ADDRESS));
  i2c_write(register_address);
  i2c_rep_start(I2C_7BIT_READ(LIS302_I2C_ADDRESS));
  result = i2c_readNak();
  i2c_stop();
  return result;
}
Beispiel #21
0
size_t i2c_read_to_buf(uint8_t add, void *buf, size_t size) {
  i2c_rep_start((add<<1) | 1);  // I2C read direction
  size_t bytes_read = 0;
  uint8_t *b = (uint8_t*)buf;
  while (size--) {
    /* acknowledge all but the final byte */
    *b++ = i2c_read(size > 0);
    /* TODO catch I2C errors here and abort */
    bytes_read++;
  }
  return bytes_read;
}
Beispiel #22
0
int twi_read_byte(uint8_t addr, uint8_t reg, uint8_t* target){
	i2c_start_wait(addr << 1);
	if(i2c_write(reg)){
		return 1;
	}
	if(i2c_rep_start((addr << 1) + 1)){
		return 1;
	}
	*target = i2c_readNak();
	i2c_stop();
	return 0;
}
Beispiel #23
0
void ADXL345_clearInt(void)
{
    uint8_t dummy;
     /* Read the interupt source and do nothing with it (clears bit)*/
    //i2c_start(ADXL345+I2C_WRITE); // Set device address and write mode
    i2c_start_wait(ADXL345+I2C_WRITE);    // Set device address and write mode
    i2c_write(INT_SOURCE); // Reading here
    i2c_rep_start(ADXL345+I2C_READ);  // Set device address and read mode               
    dummy = i2c_readNak();  
    i2c_stop();

}
Beispiel #24
0
uint8_t ds3231_oscillator_control(uint8_t state)
{
    uint8_t ack = 0;
    ack |= i2c_start(DS3231_ADDRESS | I2C_WRITE);
    ack |= i2c_write(DS3231_STATUS);
    ack |= i2c_rep_start(DS3231_ADDRESS | I2C_READ);
    if (ack != 0)
    {
        i2c_stop();
        return ack;
    }
    uint8_t status = i2c_readNak();
    if ((status & DS3231_OSF) != state)
    {
        ack |= i2c_rep_start(DS3231_ADDRESS | I2C_WRITE);
        ack |= i2c_write(DS3231_STATUS);
        ack |= i2c_write((status &~ DS3231_OSF) | state);
    }
    i2c_stop();
    return ack;
}
Beispiel #25
0
uint8_t ADXL345_devID(void)
{
    uint8_t deviceID;
     /* Read back the device ID */
    //i2c_start(ADXL345+I2C_WRITE); // Set device address and write mode
    i2c_start_wait(ADXL345+I2C_WRITE);    // Set device address and write mode
    i2c_write(ADXL345_IDREG); // Reading here
    i2c_rep_start(ADXL345+I2C_READ);  // Set device address and read mode               
    deviceID = i2c_readNak();  
    i2c_stop();

    return deviceID;
}
Beispiel #26
0
// i2c read
void VZ89::readmem(uint8_t reg, uint8_t buff[], uint8_t bytes) {
	uint8_t i =0;
	i2c_start_wait(VZ89_ADDR | I2C_WRITE);
	i2c_write(reg);
	i2c_rep_start(VZ89_ADDR | I2C_READ);
	for(i=0; i<bytes; i++) {
		if(i==bytes-1)
			buff[i] = i2c_readNak();
		else
			buff[i] = i2c_readAck();
	}
	i2c_stop();
}
Beispiel #27
0
/*
 * get date
 */
void ds1307_getdate(uint8_t *year, uint8_t *month, uint8_t *day, uint8_t *hour, uint8_t *minute, uint8_t *second) {
	i2c_start_wait(DS1307_ADDR | I2C_WRITE);
	i2c_write(0x00);//stop oscillator
	i2c_stop();

	i2c_rep_start(DS1307_ADDR | I2C_READ);
	*second = ds1307_bcd2dec(i2c_readAck() & 0x7F);
	*minute = ds1307_bcd2dec(i2c_readAck());
	*hour = ds1307_bcd2dec(i2c_readAck());
	i2c_readAck();
	*day = ds1307_bcd2dec(i2c_readAck());
	*month = ds1307_bcd2dec(i2c_readAck());
	*year = ds1307_bcd2dec(i2c_readNak());
	i2c_stop();
}
Beispiel #28
0
//Read the compensation pixel 16 bit data
int readCPIX_MLX90620()
{
    i2c_start_wait(MLX90620_WRITE);
    i2c_write(CMD_READ_REGISTER); //Command = read register
    i2c_write(0x91);
    i2c_write(0x00);
    i2c_write(0x01);
    i2c_rep_start(MLX90620_READ);

    byte cpixLow = i2c_readAck(); //Grab the two bytes
    byte cpixHigh = i2c_readAck();
    i2c_stop();

    return ( (int)(cpixHigh << 8) | cpixLow);
}
void MLX90614::read() {
  int data_low = 0;
  int data_high = 0;

  i2c_start_wait(_device+I2C_WRITE); //send start condition and write bit
  i2c_write(0x07); //send command for device to action
  i2c_rep_start(_device+I2C_READ); //send repeated start condition, device will ack
  data_low = i2c_readAck(); //Read 1 byte and then send ack
  data_high = i2c_readAck(); //Read 1 byte and then send ack
  i2c_readNak(); //Read error check byte and send Nack to tell device no more data to send
  i2c_stop(); //Release bus, end transaction

  // This masks off the error bit of the high byte, then moves it left 8 bits and adds the low byte.
  _tempData = ((double)(((data_high & 0x007F) << 8) + data_low) * _tempFactor)-0.01;
}
Beispiel #30
0
static inline uint8_t readMCP23017port (uint8_t bank, uint8_t adr)
{
	uint8_t inputByte = 0xFF; // Taster nicht betaetigt, aber Reedkontakte offen
	if (0 == i2c_start (MCP23x17_ADDR + adr + I2C_WRITE))
	{
		// read input at port:
		i2c_write (MCP23x17_GPIOA + bank); // select either GPIOA or GPIOB via bank
		i2c_rep_start (MCP23x17_ADDR + adr + I2C_READ);
		inputByte = i2c_readNak ();
	}
	// else expanderActive = false; // Ausnahme: EMV-Sicherung
	i2c_stop ();

	return inputByte;
}