Esempio n. 1
0
uint8_t cy8c20180_read(uint8_t address) {
	// request data like described in the datasheet page 9 - one byte of data
	// BUG: data only reads from chip #0 when both chips are sampled, and turns
	// 			up in the space for chip #1 - maybe it takes too long?
	
  uint8_t ret = 0x00;
	
  // request Register 00h: INPUT_PORT0

  i2c_start_wait(address);
  i2c_write(INPUT_PORT0);
  i2c_stop();

  i2c_start_wait(address);
	ret = i2c_readNak() << 4;
  i2c_stop();

//	if (address == 0) { ret |= 1 << 4; }

  // request Register 01h: INPUT_PORT1
  i2c_start_wait(address);
  i2c_write(INPUT_PORT1);
  i2c_stop();

  i2c_start_wait(address);
	ret |= i2c_readNak();
  i2c_stop();

  return ret;
}
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
}
Esempio n. 3
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;
	}
}
Esempio n. 4
0
static matrix_row_t read_cols(uint8_t mcp23018_status, uint8_t row)
{
    if (row < 7) {
        if (mcp23018_status) { // if there was an error
            return 0;
        } else {
            uint8_t data = 0;
            uint8_t err = 0x20;
            err = i2c_start(I2C_ADDR_WRITE);    if (err) goto out;
            err = i2c_write(GPIOB);             if (err) goto out;
            err = i2c_start(I2C_ADDR_READ);     if (err) goto out;
            data = i2c_readNak();
            data = ~data;
        out:
            i2c_stop();
            return data;
        }
    } else {
        // read from teensy
        return
            (PINF&(1<<0) ? 0 : (1<<0)) |
            (PINF&(1<<1) ? 0 : (1<<1)) |
            (PINF&(1<<4) ? 0 : (1<<2)) |
            (PINF&(1<<5) ? 0 : (1<<3)) |
            (PINF&(1<<6) ? 0 : (1<<4)) |
            (PINF&(1<<7) ? 0 : (1<<5)) ;
    }
}
// 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... */
	}
}
Esempio n. 6
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;
}
Esempio n. 8
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
}
Esempio n. 9
0
uint8_t Clicker_readOverflow(uint8_t *p) {

    int ret;

    ret = i2c_start((clicker_address<<1) + I2C_WRITE);
    if (ret == 1) {
        i2c_stop();
        return ret;
    }

    i2c_write(0x42);
    
    i2c_stop();

    i2c_start((clicker_address<<1) + I2C_READ);
    if (ret == 1) {
        i2c_stop();
        return ret;
    }

    *p = i2c_readNak();

    i2c_stop();

    return 0;
}
Esempio n. 10
0
static matrix_row_t read_cols(uint8_t row)
{
    if (row < 9) {
        if (mcp23018_status) { // if there was an error
            return 0;
        } else {
            uint8_t data = 0;
            mcp23018_status = i2c_start(I2C_ADDR_WRITE);    if (mcp23018_status) goto out;
            mcp23018_status = i2c_write(GPIOB);             if (mcp23018_status) goto out;
            mcp23018_status = i2c_start(I2C_ADDR_READ);     if (mcp23018_status) goto out;
            data = i2c_readNak();
            data = ~data;
        out:
            i2c_stop();
            return data;
        }
    } else {
        _delay_us(30);  // without this wait read unstable value.
        // read from teensy
        return
            (PINF&(1<<0) ? 0 : (1<<0)) |
            (PINF&(1<<1) ? 0 : (1<<1)) |
            (PINF&(1<<4) ? 0 : (1<<2)) |
            (PINF&(1<<5) ? 0 : (1<<3)) |
            (PINF&(1<<6) ? 0 : (1<<4)) |
            (PINF&(1<<7) ? 0 : (1<<5)) ;
    }
}
Esempio n. 11
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();
	}
}
Esempio n. 12
0
void classic_maj_data(CLASSIC * classic) {
	u8 i;
	u8 data[6];
	//reset le pointeur de lecture
	i2c_start(0xa4);//adresse classic en écriture
	i2c_write(0);
	i2c_stop();

	_delay_us(200);//minimum observé fonctionnel : _delay_us(150);
	i2c_start_wait(0xa5);//adresse en lecture
	for(i=0; i<5; i++) {
		data[i]=i2c_readAck();
	}
	data[5]=i2c_readNak();//le dernier caractère lu ne doit pas avoir d'acknowledge
	i2c_stop();
	/*for(i=0;i<6;i++){
		phex(data[i]);
		print(" ");
	}*/
	classic->bouton1=~data[4];
	classic->bouton2=~data[5];
	classic->rx=(data[2]>>7)+(data[1]>>(6-1))+(data[0]>>(6-3));
	classic->ry=data[2]&0x1f;
	classic->lx=data[0]&0x3f;
	classic->ly=data[1]&0x3f;
	classic->lt=(data[3]>>5)+((data[2]&0x60)>>(5-3));
	classic->rt=data[1]&0x3f;
}
Esempio n. 13
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;
    }
Esempio n. 14
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;
}
Esempio n. 15
0
uint8_t I2C_ReadRegister(uint8_t busAddr, uint8_t deviceRegister) {
    uint8_t data = 0;
    i2c_start_wait(busAddr+I2C_WRITE);
    i2c_write(deviceRegister);
    i2c_start_wait(busAddr+I2C_READ);
    data = i2c_readNak();
    i2c_stop();
    return data;
}
Esempio n. 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;
}
Esempio n. 17
0
File: rtc.c Progetto: drthth/busware
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;
}
Esempio n. 18
0
void IR_MESSUNG(void)
{

  uint8_t Anzahl=0;
  uint16_t Durchschnitt=0;
  
   INTERRUPTS_OFF
   
   
  i2c_start(0x10 + I2C_WRITE);
  i2c_write(0x49);
  i2c_stop();
  i2c_start(0x10 + I2C_READ);
  IR_DIRECTION = i2c_readAck();
  IR_S1 = i2c_readAck();
  IR_S2 = i2c_readAck();
  IR_S3 = i2c_readAck();
  IR_S4 = i2c_readAck();
  IR_S5 = i2c_readNak();
  i2c_stop();
  
  INTERRUPTS_ON
  
  
  if(IR_S1>10)
  {
   Anzahl++;
   Durchschnitt=Durchschnitt+IR_S1;
  }
  
  if(IR_S2>10)
  {
   Anzahl++;
   Durchschnitt=Durchschnitt+IR_S2;
  }
  
  if(IR_S3>10)
  {
   Anzahl++;
   Durchschnitt=Durchschnitt+IR_S3;
  }
  
  if(IR_S4>10)
  {
   Anzahl++;
   Durchschnitt=Durchschnitt+IR_S4;
  }
  
  if(IR_S5>10)
  {
   Anzahl++;
   Durchschnitt=Durchschnitt+IR_S5;
  }
  
  BALL_ENTFERNUNG=Durchschnitt/Anzahl;
  
}
Esempio n. 19
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;
}
Esempio n. 20
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;
}
Esempio n. 21
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;
}
Esempio n. 22
0
/*=================================================================================
Reads the GY-26 digital compass and returns the degrees value in integer format.
Example:

unsigned int degrees;
degrees = pthRead();

The value in 'degrees' variable could be from 0 to 3650.
If the 'degrees' variable has the value 2568 that means 256.8 degrees.
=================================================================================*/
int pthRead (void)
{
		unsigned int data;
		
		i2c_start(I2C_GY26 + I2C_READ);	// Set device address and read mode		
		data = (i2c_readAck() << 8);	// Read the Most Significant Byte (MSB) from the GY-26 compass. 
		data += i2c_readNak();			// Read the Low Significant Byte (LSB) from the GY-26 compass. 
		i2c_stop();
		
		return (data); //Return the degrees value (16-bit integer from 0-3650).
}
Esempio n. 23
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;
}
Esempio n. 24
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();

}
Esempio n. 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;
}
Esempio n. 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();
}
Esempio n. 27
0
/*******************************************************************************
 * Function: nunchuck_get_data() 
 * Description: Fills the nunchuck_buf array with data from the nunchuck's 
 * registers. The data from this array may be accessed using the get functions
 ******************************************************************************/
void nunchuck_get_data(void)
{
		i2c_start_wait(NUNCHUCK_ADDR+I2C_WRITE);
		i2c_write(0x00);
		i2c_stop();
		_delay_ms(10);
		i2c_start_wait(NUNCHUCK_ADDR+I2C_READ);		
		for (uint8_t i = 0 ; i < 5 ; i++)
		{
				nunchuck_buf[i] = nunchuck_decode_byte(i2c_readAck());
		}
		nunchuck_buf[5] = nunchuck_decode_byte(i2c_readNak());
		i2c_stop();
}
Esempio n. 28
0
File: lm75.c Progetto: muccc/matemat
int16_t lm75_gettemp(uint8_t device){
    device = LM75_BASE + (device << 1);
    if(i2c_start(device + 1)){
        return 100;
    }else{
        int16_t a = i2c_readAck() << 1;
        uint8_t b = i2c_readNak() >> 7;
        i2c_stop();
        if(a & 256)
            a |= 0xfe00;
        return a+b;
        //return 50*2+1;
    }
}
Esempio n. 29
0
void twi_transfer(struct twi_transfer *xfer)
{
#ifdef TWI_SYNC
	twi_size_t i;
	uint8_t *buffer = xfer->buffer;

	if (xfer->write_size) {
		i2c_start(xfer->address << 1);
		for (i = 0; i < xfer->write_size; i++)
			i2c_write(buffer[i]);
		if (!xfer->read_size)
			i2c_stop();
	}
	if (xfer->read_size) {
		i2c_start((xfer->address << 1) | 1);
		for (i = 0; i < xfer->read_size; i++) {
			if (i + 1 == xfer->read_size)
				buffer[i] = i2c_readNak();
			else
				buffer[i] = i2c_readAck();
		}
		i2c_stop();
	}
	if (xfer->callback)
		xfer->callback(xfer, TWI_STAT_FINISHED);

#else /* TWI_SYNC */

	uint8_t sreg;

	xfer->offset = 0;
	xfer->next = NULL;
	xfer->status = 0;
	if (!xfer->write_size)
		xfer->status |= TWI_XFER_READ;
	transfer_set_status(xfer, TWI_STAT_INPROGRESS);

	sreg = irq_disable_save();

	if (twi.last_xfer)
		twi.last_xfer->next = xfer;
	twi.last_xfer = xfer;
	if (!twi.first_xfer) {
		twi.first_xfer = xfer;
		send_start_condition();
	}
	irq_restore(sreg);
#endif
}
Esempio n. 30
0
/// I2C read from camera
unsigned char cameraRead(unsigned char regNum) 
{
	unsigned char write;
	unsigned char read;
	
	///Check these if they start to cause trouble
	write = i2c_start(CAMADDR+I2C_WRITE);   
	write = i2c_write(regNum);          
	i2c_stop();                              
	
	read = i2c_start(CAMADDR+I2C_READ); ; 
	read = i2c_readNak();
	i2c_stop();                              
	return read;
}