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; }
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 }
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; }
//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; }
/* 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(); } }
void readBlock(uint8_t reg, int16_t *data){ i2c_start(MPUI2CADD+I2C_WRITE); i2c_write(reg); // register to read i2c_rep_start(MPUI2CADD+I2C_READ); // read a byte *data = (i2c_readAck()<<8); *data |= (i2c_readAck()); data++; *data = (i2c_readAck()<<8); *data |= i2c_readAck(); data++; *data = (i2c_readAck()<<8); *data |= i2c_readNak(); i2c_stop(); }
// 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... */ } }
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; }
// 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; } }
//Reads the PTAT data from the MLX //Returns an unsigned int containing the PTAT unsigned int readPTAT_MLX90620() { i2c_start_wait(MLX90620_WRITE); i2c_write(CMD_READ_REGISTER); //Command = read PTAT i2c_write(0x90); //Start address is 0x90 i2c_write(0x00); //Address step is 0 i2c_write(0x01); //Number of reads is 1 i2c_rep_start(MLX90620_READ); byte ptatLow = i2c_readAck(); //Grab the lower and higher PTAT bytes byte ptatHigh = i2c_readAck(); i2c_stop(); return( (unsigned int)(ptatHigh << 8) | ptatLow); //Combine bytes and return }
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; }
//Reads the current configuration register (2 bytes) from the MLX //Returns two bytes unsigned int readConfig_MLX90620() { i2c_start_wait(MLX90620_WRITE); //The MLX configuration is in the MLX, not EEPROM i2c_write(CMD_READ_REGISTER); //Command = read configuration register i2c_write(0x92); //Start address i2c_write(0x00); //Address step of zero i2c_write(0x01); //Number of reads is 1 i2c_rep_start(MLX90620_READ); byte configLow = i2c_readAck(); //Grab the two bytes byte configHigh = i2c_readAck(); i2c_stop(); return( (unsigned int)(configHigh << 8) | configLow); //Combine the configuration bytes and return as one unsigned int }
//Read raw data to rawX, rawY and rawZ void Gyroscope::cmr_read_rates() { // Read order Z_MSB, Z_LSB, Y_MSB, Y_LSB, X_MSB and X_LSB i2c_start_wait( (ADDR << 1) | I2C_WRITE ); // Start write i2c_write(0x11); // Z_MSB register address i2c_rep_start( (ADDR << 1) | I2C_READ ); // Start reading //Read and combine Z_MSB and Z_LSB rawZ = (unsigned short) ((i2c_readAck() << 8) & 0xFF00); rawZ |= (unsigned short)(i2c_readAck() & 0x00FF); //Read and combine Y_MSB and Y_LSB rawY = (unsigned short) ((i2c_readAck() << 8) & 0xFF00); rawY |= (unsigned short)(i2c_readAck() & 0x00FF); //Read and combine X_MSB and X_LSB rawX = (unsigned short) ((i2c_readAck() << 8) & 0xFF00); rawX |= (unsigned short)(i2c_readNak() & 0x00FF); i2c_stop(); //End condition }
//Reads 64 bytes of pixel data from the MLX //Loads the data into the irData array void readIR_MLX90620() { i2c_start_wait(MLX90620_WRITE); i2c_write(CMD_READ_REGISTER); //Command = read a register i2c_write(0x00); //Start address = 0x00 i2c_write(0x01); //Address step = 1 i2c_write(0x40); //Number of reads is 64 i2c_rep_start(MLX90620_READ); for(int i = 0 ; i < 64 ; i++) { byte pixelDataLow = i2c_readAck(); byte pixelDataHigh = i2c_readAck(); irData[i] = (int)(pixelDataHigh << 8) | pixelDataLow; } i2c_stop(); }
void Magnetometer::hmc_read_rates() { // Read order Z_MSB, Z_LSB, Y_MSB, Y_LSB, X_MSB and X_LSB i2c_start_wait( (ADDR << 1) | I2C_WRITE ); // Start write i2c_write(REG_DATA_X_MSB); // X_MSB register address i2c_rep_start( (ADDR << 1) | I2C_READ ); // Start reading //Read and combine X_MSB and X_LSB magX = (unsigned short) ((i2c_readAck() << 8) & 0xFF00) ; magX |= (unsigned short) (i2c_readAck() & 0x00FF); //Read and combine Y_MSB and Y_LSB magY = (unsigned short) ((i2c_readAck() << 8) & 0xFF00) ; magY |= (unsigned short) (i2c_readAck() & 0x00FF); //Read and combine Z_MSB and Z_LSB magZ = (unsigned short) ((i2c_readAck() << 8) & 0xFF00) ; magZ |= (unsigned short) (i2c_readNak() & 0x00FF); i2c_stop(); }
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; }
/*================================================================================= 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). }
uint8_t lm75_Temp(uint8_t lm75_address) { uint8_t returnCode, temp; returnCode=i2c_start(lm75_address+I2C_READ); if(returnCode==0) { temp = i2c_readAck(); // temp_05 =i2c_readNak(); //get 0.5 degree value, not needed i2c_stop(); return(temp); } else return(255); }
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; }
//store current number of clicks at p. Return 0 on success //if unsuccessful, return 1, no not change p static uint8_t readClicks(uint32_t *p) { uint32_t value = 0; uint8_t ret; ret = i2c_start((clicker_address<<1) + I2C_WRITE); if (ret == 1) { i2c_stop(); return ret; } ret = i2c_write(0x41); //the get clicks command ret = i2c_rep_start((clicker_address<<1) + I2C_READ); if (ret == 1) { i2c_stop(); return ret; } ret = i2c_readAck(); value = ret; value <<= 8; ret = i2c_readAck(); value += ret; value <<= 8; ret = i2c_readAck(); value += ret; value <<= 8; ret = i2c_readNak(); value += ret; i2c_stop(); *p = value; return 0; }
int AccelAddDataToBuffer() { if (i2c_start(ACCEL_ADDR+I2C_WRITE) == 0) // Try to start a read from the accelerometer { i2c_write((0x29)|(0b10000000)); i2c_rep_start(ACCEL_ADDR+I2C_READ); AccelRingBuffer[0][AccelBufferPosition] = i2c_readAck(); i2c_readAck(); AccelRingBuffer[1][AccelBufferPosition] = i2c_readAck(); i2c_readAck(); AccelRingBuffer[2][AccelBufferPosition] = i2c_readNak(); i2c_stop(); if (AccelBufferPosition<9) AccelBufferPosition++; else AccelBufferPosition = 0; return 1; } return 0; }
void ADXL345_updateVector(int16_t *vec) { uint8_t xH, xL, yH, yL, zH, zL; // Read the acceleration registers sequentially //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(DATAX0); // Start reading at xH, auto increments to zL i2c_rep_start(ADXL345+I2C_READ); // Set device address and read mode xL = i2c_readAck(); xH = i2c_readAck(); yL = i2c_readAck(); yH = i2c_readAck(); zL = i2c_readAck(); zH = i2c_readNak(); i2c_stop(); //Data is Right justified vec[0] = (((xH << 8) | xL)) ; vec[1] = (((yH << 8) | yL)) ; vec[2] = (((zH << 8) | zL)) ; }
// 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(); }
accel_vect lis302dl_read_accel(void) { accel_vect return_value; /* return_value.accel_x = lis302_read_register(LIS302_REGISTER_OUT_X); return_value.accel_y = lis302_read_register(LIS302_REGISTER_OUT_Y); return_value.accel_z = lis302_read_register(LIS302_REGISTER_OUT_Z); */ i2c_start_wait(I2C_7BIT_WRITE(LIS302_I2C_ADDRESS)); i2c_write(_BV(7) | LIS302_REGISTER_OUT_X); i2c_rep_start(I2C_7BIT_READ(LIS302_I2C_ADDRESS)); return_value.accel_x = i2c_readAck(); i2c_readAck(); return_value.accel_y = i2c_readAck(); i2c_readAck(); return_value.accel_z = i2c_readNak(); i2c_stop(); return return_value; }
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; } }
/******************************************************************************* * 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(); }
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 }
// don't free returned memory! It's static mem uint8_t *getAudioData(void) { // We read 7 bytes from our Audio µC uint8_t i; if (i2c_start(TWIADDRESSAUDIO | I2C_READ) == 0) { for (i = 0; i < 6; i++) { ret[i] = i2c_readAck(); } ret[6] = i2c_readNak(); i2c_stop(); return ret; } else { return NULL; } }
//Read the 256 bytes from the MLX EEPROM and setup the various constants (*lots* of math) //Note: The EEPROM on the MLX has a different I2C address from the MLX. I've never seen this before. void read_EEPROM_MLX90620() { i2c_start_wait(MLX90620_EEPROM_WRITE); i2c_write(0x00); //EEPROM info starts at location 0x00 i2c_rep_start(MLX90620_EEPROM_READ); //Read all 256 bytes from the sensor's EEPROM for(int i = 0 ; i <= 255 ; i++) eepromData[i] = i2c_readAck(); i2c_stop(); //We're done talking varInitialization(eepromData); //Calculate a bunch of constants from the EEPROM data writeTrimmingValue(eepromData[OSC_TRIM_VALUE]); }