// Function to store a single byte of data in the EEPROM void writeEEPROM( unsigned int address, unsigned char data ) { int i, j; // Enable the I2C port SSPCON2bits.SEN = 1; while (SSPCON2bits.SEN) {} // Send the EEPROM at address 7 a message that we are going to write // some data to it. i2cWriteByte(0b10101110); // Send over the data address we want to store the data at i2cWriteByte((address >> 8) & 0xFF); i2cWriteByte(address & 0xFF); // Then write the actual data to the device i2cWriteByte(data); // and close the i2c connection SSPCON2bits.PEN = 1; // Now, wait for the EEPROM to save it's data for (i = 0; i < 20; i++) { for (j = 0; j < 750; j++) { } } }
int8_t Magnetometer::begin() { uint8_t buff[3]; i2cAddr_ = HMC5833L_I2CADD; // Join the I2C bus as master WIRE.begin(); // read the ID registers if (i2cReadBytes(HMC5833L_REG_IDA, buff, 3) != 0) return HMC5833L_ERROR_I2CREAD; // compare the ID registers if (buff[0] != HMC5833L_REG_IDA_ID || buff[1] != HMC5833L_REG_IDB_ID || buff[2] != HMC5833L_REG_IDC_ID) return HMC5833L_ERROR_WRONG_ID; // set data rate speed to 30hz if (i2cWriteByte(HMC5833L_REG_CFGA, 0x14) != 0) return HMC5833L_ERROR_I2CWRITE; // set to continuous mode // single mode not supported by lib if (i2cWriteByte(HMC5833L_REG_MODE, 0) != 0) return HMC5833L_ERROR_I2CWRITE; // set default gain if (setGain(HMC5833L_GAIN_1090) != 0) return HMC5833L_ERROR_I2CWRITE; return 0; }
// Function to read a single byte of data from the EEPROM unsigned char readEEPROM( unsigned int address ) { unsigned char data = 0; // Enable the I2C port SSPCON2bits.SEN = 1; while (SSPCON2bits.SEN) {} // Send the EEPROM at address 7 a message that we are going to write // some data to it. i2cWriteByte(0b10101110); // Send over the data address we want to read from. i2cWriteByte((address >> 8) & 0xFF); i2cWriteByte(address & 0xFF); // Now, reset communications and read the data in. SSPCON2bits.SEN = 1; while (SSPCON2bits.SEN) {} // Send the EEPROM at address 7 a message that we are going to read // some data from it. i2cWriteByte(0b10101111); // Read in the byte of data SSPCON2bits.RCEN = 1; while ( !SSPSTATbits.BF ); data = SSPBUF; // and close the i2c connection SSPCON2bits.PEN = 1; return data; }
//------------------------- initAccel() ------------------- // Inicializa o Aceletormetro, retorna um erro caso não consiga unsigned char initAccel(void) { unsigned char error = 0; error += i2cWriteByte(i2c_ADXL345,ADXL345_DATA_FORMAT,0x0A); error += i2cWriteByte(i2c_ADXL345,ADXL345_POWER_CTL,0x08); error += i2cWriteByte(i2c_ADXL345,ADXL345_BW_RATE,0x0A); return error; }
void LcdI2cOneNible(unsigned char lcd_nible) { lcd_nible &= 0x0F; lcd_nible <<= 4; lcd_nible |= iDataFlag; lcd_nible |= iBacklightFlag; i2c_data = lcd_nible; i2cWriteByte(handle_i2c, i2c_data); i2c_data |= 0x04; i2cWriteByte(handle_i2c, i2c_data); // assert E usleep(200); i2c_data &= 0xFB; i2cWriteByte(handle_i2c, i2c_data); // remove E usleep(200); }
PUBLIC uint8_t i2c0_burst_read(uint8_t slaveAddr, uint8_t memAddr, uint16_t byteCount, uint8_t* data) { i2c_start(); i2cWriteByte(clr_bit(slaveAddr,0)); }
/***************************************************************************** * @brief Reads from I2C EEPROM using DMA. * * @param deviceAddress * I2C address of EEPROM * * @param offset * The offset (address) to start reading from * * @param data * Pointer to the data buffer * * @param length * Number of bytes to read *****************************************************************************/ void i2cDmaRead(uint8_t deviceAddress, uint8_t offset, uint8_t *data, uint8_t length) { /* Wait for any previous transfer to finish */ while ( transferActive ) { EMU_EnterEM1(); } /* Abort if an error has occured */ if ( i2cError ) { return; } /* Clear all pending interrupts prior to starting transfer. */ I2C0->IFC = _I2C_IFC_MASK; /* Write address to read from. Note that this is specific to the EEPROM on the * EFM32GG-DK3750 and may need to be changed when using a different device. */ i2cWriteByte(deviceAddress, offset); /* Send the device address. I2C_CMD_START must be written before * TXDATA since this is a repeated start. */ I2C0->CMD = I2C_CMD_START; I2C0->TXDATA = deviceAddress | I2C_READ_BIT; /* Wait for ACK on the address */ if ( !i2cWaitForAckNack() ) { i2cErrorAbort(); return; } /* Do not start DMA if an error has occured */ if ( i2cError ) { return; } /* Automatically ACK received bytes */ I2C0->CTRL |= I2C_CTRL_AUTOACK; /* These are used by the RX interrupt handler * to fetch the last two bytes of the transaction */ rxPointer = data + length - 2; bytesLeft = 2; /* Set transfer active flag. Cleared by interrupt handler * when STOP condition has been sent. */ transferActive = true; /* Activate DMA */ DMA_ActivateBasic(DMA_CHANNEL_I2C_RX, /* RX DMA channel */ true, /* Primary descriptor */ false, /* No burst */ (void *)data, /* Write to rx buffer */ (void *)&(I2C0->RXDATA), /* Read from RXDATA */ length - 3 ); /* Number of transfers */ }
int i2cMasterWrite(uint8_t i2c_addr, uint8_t intaddr_size, uint32_t int_addr, uint8_t data) { MUTEX_TAKE(i2c_mutex, -1); pm_lock(); i2cMasterConf(i2c_addr, intaddr_size, int_addr, I2CMASTER_WRITE); int rc = i2cWriteByte(data); pm_unlock(); MUTEX_GIVE(i2c_mutex); return rc; }
int main(int argc, char* argv[]) { gpioInitialise(); int spiHandle = i2cOpen(1, 0x26, 0); i2cWriteByte(spiHandle, 3); gpioDelay(100); i2cClose(spiHandle); return 0; }
void LcdI2cBackLightDim (void) { while (1) { if (back_light_duty_cycle >= 100) {iBacklightFlag = 0x08; sleep(2);} else if (back_light_duty_cycle == 0 ) {iBacklightFlag = 0x00; sleep(2);} else { iBacklightFlag = 0x08; i2c_data |= 0x08; i2cWriteByte(handle_i2c, i2c_data); usleep(100 * back_light_duty_cycle); //10 ms back light period iBacklightFlag = 0x00; i2c_data &= 0xF7; i2cWriteByte(handle_i2c, i2c_data); usleep(100 * (100 - back_light_duty_cycle)); } } }
void LcdI2cBackLightOn(int gpio, int level, unsigned int tick) { if ((level == 0) && (tick > lcd_backlight_on_tick + LCD_BACKLIGHT_ON_TIME)) { lcd_backlight_on_tick = tick; iBacklightFlag = 0x08; i2c_data |= 0x08; i2cWriteByte(handle_i2c, i2c_data); printf("gpio %d became %d at %ud\n", gpio, level, tick); WriteLog("%s:%d - LCD ON button pressed \n", __FILE__, __LINE__); } }
void LcdI2c_Init () { i2cWriteByte(handle_i2c, 0); // write 0 iDataFlag = COMMAND; // show command mode //iBacklightFlag = BACKLIGHT_OFF; //8 bit mod just to resets all previous LCD settings LcdI2cOneNible (3); // 8 bit mode LcdI2cOneNible (3); // 8 bit mode LcdI2cOneNible (8); // 2 lines mode; 5x7 dots LcdI2cOneNible (2); // 4 bit mode LcdI2cOneNible (2); // 4 bit mode LcdI2cOneNible (8); // 2 lines mode; 5x7 dots LcdI2cByte ( 0b00001100 ); // turn on display and cursor off: non-blinking LcdI2cByte ( 0b00000001 ); // clears display and cursor to home LcdI2cByte ( 0b00000011 ); // increment address and shift cursor with each character }
int8_t Magnetometer::setGain(uint8_t gain) { uint8_t data = 0; if (gain > 7) return HMC5833L_ERROR_GAINOUTOFRANGE; gain_ = gain; data = gain_ << 5; data = data & 0xE0; if (i2cWriteByte(HMC5833L_REG_CFGB, data) != 0) return HMC5833L_ERROR_I2CWRITE; // do a read to apply the new gain int16_t x, y, z; readRaw(&x, &y, &z); return 0; }
/***************************************************************************** * @brief Writes bytes to I2C EEPROM using DMA. * * @param deviceAddress * I2C address of EEPROM * * @param offset * The offset (address) to start writing from * * @param data * Pointer to the data buffer * * @param length * Number of bytes to write *****************************************************************************/ void i2cDmaWrite(uint8_t deviceAddress, uint8_t offset, uint8_t *data, uint8_t length) { /* Abort if an error has been detected */ if ( i2cError ) { return; } /* Send address to write to. Note that this is specific to the EEPROM on the * EFM32GG-DK3750 and may need to be changed when using a different device. */ i2cWriteByte(deviceAddress, offset); /* Abort if an error has been detected */ if ( i2cError ) { return; } /* Automatically generate a STOP condition when there is no * more data to send. The DMA must be fast enough to * keep up (normally not a problem unless the DMA is * been prescaled). */ I2C0->CTRL |= I2C_CTRL_AUTOSE; /* Set transfer active flag. Cleared by interrupt handler * when STOP condition has been sent. */ transferActive = true; /* Activate DMA */ DMA_ActivateBasic(DMA_CHANNEL_I2C_TX, /* TX DMA channel */ true, /* Primary descriptor */ false, /* No burst */ (void *)&(I2C0->TXDATA), /* Write to TXDATA */ (void *)data, /* Read from txBuffer */ length - 1 ); /* Number of transfers */ }
Status I2CGpioHardwareBus::Send(byte value) { checkStatus(i2cWriteByte(_i2cHandle, value), Status::SendFail); return Status::Ok; }
void Touchpanel_FT5X06::setup() { uint8_t b, i; power = 0; mouseX = mouseY = 0; mouseButtonState = 0; mouseZoom = 0; // set analog pins to input pinMode(AXM, INPUT); pinMode(AXP, INPUT); pinMode(AYM, INPUT); pinMode(AYP, INPUT); // set interrupt pin to input pinMode(INT_PIN, INPUT); digitalWrite(INT_PIN, HIGH); // pull-up on // wait for startup (check chip vendor id) #if DEBUG > 0 Serial.println(F("TP: init chip...")); #endif for(i=0; i < 5; i++) { b = i2cReadByte(REG_CIPHER); if(b == CHIP_VENDOR_ID) { break; } else { #if DEBUG > 0 Serial.print(F("TP: Chip Vendor wrong 0x")); Serial.println(b, HEX); #endif #if USE_WATCHDOG > 0 wdt_reset(); #endif digitalWrite(LED_RED, HIGH); delay(250); digitalWrite(LED_RED, LOW); delay(250); } } // read settings #if DEBUG > 0 b = i2cReadByte(REG_CIPHER); Serial.print(F("TP: Chip Vendor 0x")); Serial.println(b, HEX); b = i2cReadByte(REG_FWID); Serial.print(F("TP: FW 0x")); Serial.println(b, HEX); b = i2cReadByte(REG_FTCHIPID); Serial.print(F("TP: Chip ID 0x")); Serial.println(b, HEX); b = i2cReadByte(REG_DEVICE_MODE); Serial.print(F("TP: Device Mode 0x")); Serial.println(b, HEX); Serial.println(F("TP: Reg 0x80...0xE0")); for(i = 0x80; i <= 0xE0; i++) { Serial.print(i, HEX); Serial.print(" "); Serial.println(i2cReadByte(i)); } #endif // write settings i2cWriteByte(REG_DEVICE_MODE, 0); // Device Mode, 0x00 = normal operating, 0x40 = test i2cWriteByte(REG_MODE, 0); // Interrupt status to host, 0 = polling, 1 = trigger i2cWriteByte(REG_THGROUP, 35); // Valid touching detect threshold i2cWriteByte(REG_ENTERMONITOR, 120); // Delay to enter 'Monitor' status (s) /* i2cWriteByte(REG_THGROUP, 35); // Valid touching detect threshold i2cWriteByte(REG_THPEAK, 60); // Valid touching peak detect threshold i2cWriteByte(REG_THCAL, 140); // Touch focus threshold i2cWriteByte(REG_THWATER, 211); // Threshold when there is surface water i2cWriteByte(REG_THTEMP, 235); // Threshold of temperature compensation i2cWriteByte(REG_THDIFF, 160); // Touch difference threshold i2cWriteByte(REG_CTRL, 1); // Power Control Mode i2cWriteByte(REG_ENTERMONITOR, 200); // Delay to enter 'Monitor' status (s) i2cWriteByte(REG_PERIODACTIVE, 6); // Period of 'Active' status (ms) 3-14 i2cWriteByte(REG_PERIODMONITOR, 40); // Timer to enter ‘idle’ when in 'Monitor' (ms) 3-14 */ // check error register for(i=0; i < 10; i++) { b = i2cReadByte(REG_ERR); if(b == 0) // okay { on(); // touchpanel on break; } else // error { #if DEBUG > 0 Serial.print(F("TP: error 0x")); Serial.println(b, HEX); #endif } } }
void DataAquisition::run() { //Variablendeklaration int handle = 0; int dataready = 0; int winkel_old= 999; while(1) { int dataX_MSB = 0; int dataX_LSB = 0; int dataY_MSB = 0; int dataY_LSB = 0; int dataZ_MSB = 0; int dataZ_LSB = 0; signed short int dataX = 0; signed short int dataY = 0; signed short int dataZ = 0; int winkel = 0; double tangens = 0; double dataX_ABS = 0; double dataZ_ABS = 0; //i2c handle = i2cOpen(1,0x55,0); i2cSwitchCombined(1); // WICHTIG! Enable "Repeated Start"!! gpioWrite(13,1); // MMA8491_EN = 1 gpioDelay(2000); // Ton (minimales Delay) i2cWriteByte(handle,0x00); dataready = i2cReadByte(handle); if((dataready & 0x08) == 0x08) // Falls neue Daten Verfügbar { dataX_MSB = i2cReadByteData(handle,0x01); dataX_LSB = i2cReadByteData(handle,0x02); dataY_MSB = i2cReadByteData(handle,0x03); dataY_LSB = i2cReadByteData(handle,0x04); dataZ_MSB = i2cReadByteData(handle,0x05); dataZ_LSB = i2cReadByteData(handle,0x06); dataX = dataX_LSB + (dataX_MSB << 8); dataY = dataY_LSB + (dataY_MSB << 8); dataZ = dataZ_LSB + (dataZ_MSB << 8); //Absolutwert und Wandlung in Double dataX_ABS = abs(dataX); dataZ_ABS = abs(dataZ); //Berechnung Tangens tangens = (dataX_ABS)/(dataZ_ABS); //Berechnung Winkel winkel = (atan(tangens))*180 / PI; //Unterscheidung Quadrant if((dataZ >= 0) & (dataX >= 0)) // Rechts unten winkel = winkel; else if((dataZ <= 0) & (dataX >= 0)) // Rechts oben winkel = 180 - winkel; else if((dataZ <= 0) & (dataX <= 0)) // Links oben winkel = 180 + winkel; else if((dataZ >= 0) & (dataX <= 0)) // Links unten winkel = 360 - winkel ; } i2cClose(handle); gpioWrite(13,0); // MMA8491_EN = 0 if (winkel < 3 || winkel > 357) { winkel = 0; } //Filter gegen Stösse und Schläge if(winkel_old != 999) { if(winkel > (winkel_old+10)) { winkel = (winkel_old+10); } else if(winkel < (winkel_old-10)) { winkel = (winkel_old-10); } } if(fps == "2") { if(i%5 == 0) { angleArray[(i/5)] = winkel; } } else if(fps == "30") { angleArray[i] = winkel; angleArray[++i] = winkel; angleArray[++i] = winkel; } else { angleArray[i] = winkel; } winkel_old = winkel; i++; msleep(92); } }
void LcdI2cBackLightOff (void) { iBacklightFlag = 0x00; i2c_data &= 0xF7; i2cWriteByte(handle_i2c, i2c_data); }