void S11059::cycle(){ static int flag=0; char data[3]; if(millis()-time>=5){ time=millis(); if(flag==0){ if(mode==LOW){ data[0]=0x00;data[1]=0x84; i2cWrite(0x54,data,2,TX); flag=1; }else if(mode==HIGH){ data[0]=0x00;data[1]=0x89; i2cWrite(0x54,data,2,TX); flag=1; } }else if(flag==1){ if(mode==LOW){ data[0]=0x00;data[1]=0x04; i2cWrite(0x54,data,2,TX); flag=2; }else if(mode==HIGH){ data[0]=0x00;data[1]=0x09; i2cWrite(0x54,data,2,TX); flag=2; } }else if(flag==2) flag=3; else if(flag==3){ data[0]=0x03; i2cWrite(0x54,data,1,TX); //flag=0; } } }
int m41t81_tod_kick_start(void) { char byte; int polls; int status; /* * Reset ST bit to "0" . */ status = i2cRead(M41T81_SMBUS_CHAN, M41T81_CCR_ADDRESS, I2C_DEVICE_TYPE_RTC_M41T48, M41T81REG_SC, 1, &byte); if (byte & 0x80) { byte &= 0x7f; status = i2cWrite(M41T81_SMBUS_CHAN, M41T81_CCR_ADDRESS, I2C_DEVICE_TYPE_RTC_M41T48, M41T81REG_SC, 1, &byte); } /* * Reset HT bit to "0" to update registers with current time. */ status = i2cRead(M41T81_SMBUS_CHAN, M41T81_CCR_ADDRESS, I2C_DEVICE_TYPE_RTC_M41T48, M41T81REG_AHR, 1, &byte); if (byte & M41T81REG_AHR_HT) { byte &= ~M41T81REG_AHR_HT; status = i2cWrite(M41T81_SMBUS_CHAN, M41T81_CCR_ADDRESS, I2C_DEVICE_TYPE_RTC_M41T48, M41T81REG_AHR, 1, &byte); } /* * Try to read from the device. If it does not * respond, fail. We may need to do this for up to 300ms. */ for (polls = 0; polls < 300; polls++) { taskDelay(1); status = i2cRead(M41T81_SMBUS_CHAN, M41T81_CCR_ADDRESS, I2C_DEVICE_TYPE_RTC_M41T48, 0, 1, &byte); if (status == OK) break; /* read is ok */ } return (status == OK) ? 0 : -1; }
static void mpu6050DmpBankInit(void) { uint8_t i, j; uint8_t incoming[9]; for (i = 0; i < 7; i++) { mpu6050DmpBankSelect(i); for (j = 0; j < 16; j++) { uint8_t start_addy = j * 0x10; i2cWrite(MPU6050_ADDRESS, DMP_MEM_START_ADDR, start_addy); i2cWriteBuffer(MPU6050_ADDRESS, DMP_MEM_R_W, 16, (uint8_t *) & dmpMem[i][j][0]); } } mpu6050DmpBankSelect(7); for (j = 0; j < 8; j++) { uint8_t start_addy = j * 0x10; i2cWrite(MPU6050_ADDRESS, DMP_MEM_START_ADDR, start_addy); i2cWriteBuffer(MPU6050_ADDRESS, DMP_MEM_R_W, 16, (uint8_t *) & dmpMem[7][j][0]); } i2cWrite(MPU6050_ADDRESS, DMP_MEM_START_ADDR, 0x80); i2cWriteBuffer(MPU6050_ADDRESS, DMP_MEM_R_W, 9, (uint8_t *) & dmpMem[7][8][0]); i2cRead(MPU6050_ADDRESS, DMP_MEM_R_W, 8, incoming); }
void hmc5883lFinishCal(void) { // leave test mode i2cWrite(MAG_ADDRESS, ConfigRegA, SampleAveraging_8 << 5 | DataOutputRate_75HZ << 2 | NormalOperation); i2cWrite(MAG_ADDRESS, ConfigRegB, magGain); i2cWrite(MAG_ADDRESS, ModeRegister, ContinuousConversion); }
void interpretHPPot() { static uint8_t record[AVG] = { 0 }; static int recordLoc = 0; int val = 255; if (HPGPIOCrossed != -1) val = (HPGPIOCrossed * 7)/8; val = 255-val; if (val < 0) val = 0; record[recordLoc++] = val; if (recordLoc == AVG) recordLoc = 0; int tot = 0; for (int i = 0; i < AVG; i++) tot += record[i]; int goal = (tot / AVG)/2+100; if (goal != HPVolume) i2cWrite(LED_ADDR, LED_R, LED_R_BRIGHTNESS, false); else i2cWrite(LED_ADDR, LED_R, 0, false); HPVolume = goal; }
static void l3g4200dInit(uint16_t lpf) { bool ack; uint8_t mpuLowPassFilter = L3G4200D_DLPF_32HZ; switch (lpf) { default: case 32: mpuLowPassFilter = L3G4200D_DLPF_32HZ; break; case 54: mpuLowPassFilter = L3G4200D_DLPF_54HZ; break; case 78: mpuLowPassFilter = L3G4200D_DLPF_78HZ; break; case 93: mpuLowPassFilter = L3G4200D_DLPF_93HZ; break; } delay(100); ack = i2cWrite(L3G4200D_ADDRESS, L3G4200D_CTRL_REG4, L3G4200D_FS_SEL_2000DPS); if (!ack) failureMode(FAILURE_ACC_INIT); delay(5); i2cWrite(L3G4200D_ADDRESS, L3G4200D_CTRL_REG1, L3G4200D_POWER_ON | mpuLowPassFilter); }
void hmc5883lFinishCal(void) { // leave test mode i2cWrite(MAG_ADDRESS, 0x00, 0x70); // Configuration Register A -- 0 11 100 00 num samples: 8 ; output rate: 15Hz ; normal measurement mode i2cWrite(MAG_ADDRESS, 0x01, 0x20); // Configuration Register B -- 001 00000 configuration gain 1.3Ga i2cWrite(MAG_ADDRESS, 0x02, 0x00); // Mode register -- 000000 00 continuous Conversion Mode }
static void bma280Init(accDev_t *acc) { i2cWrite(MPU_I2C_INSTANCE, BMA280_ADDRESS, BMA280_PMU_RANGE, 0x08); // +-8g range i2cWrite(MPU_I2C_INSTANCE, BMA280_ADDRESS, BMA280_PMU_BW, 0x0E); // 500Hz BW acc->acc_1G = 512 * 8; }
void mpu6050DmpLoop(void) { uint8_t temp; uint8_t buf[2]; if (mpu6050DmpFifoReady()) { LED1_ON; mpu6050DmpGetPacket(); i2cRead(MPU6050_ADDRESS, MPU_RA_INT_STATUS, 1, &temp); if (dmp_firstPacket) { delay(1); mpu6050DmpBankSelect(0x00); mpu6050DmpBankSelect(0x00); // bank i2cWrite(MPU6050_ADDRESS, MPU_RA_MEM_START_ADDR, 0x60); i2cWriteBuffer(MPU6050_ADDRESS, MPU_RA_MEM_R_W, 4, "\x04\x00\x00\x00"); // data mpu6050DmpBankSelect(0x01); i2cWrite(MPU6050_ADDRESS, MPU_RA_MEM_START_ADDR, 0x62); i2cRead(MPU6050_ADDRESS, DMP_MEM_R_W, 2, buf); dmp_firstPacket = false; mpu6050DmpFifoReady(); } if (dmp_fifoCountL == 42) { mpu6050DmpProcessQuat(); } LED1_OFF; } }
static void l3g4200dInit(gyroDev_t *gyro) { bool ack; uint8_t mpuLowPassFilter = L3G4200D_DLPF_32HZ; switch (gyro->lpf) { default: case 3: // BITS_DLPF_CFG_42HZ mpuLowPassFilter = L3G4200D_DLPF_32HZ; break; case 2: // BITS_DLPF_CFG_98HZ mpuLowPassFilter = L3G4200D_DLPF_54HZ; break; case 1: // BITS_DLPF_CFG_188HZ mpuLowPassFilter = L3G4200D_DLPF_78HZ; break; case 0: // BITS_DLPF_CFG_256HZ mpuLowPassFilter = L3G4200D_DLPF_93HZ; break; } delay(100); ack = i2cWrite(MPU_I2C_INSTANCE, L3G4200D_ADDRESS, L3G4200D_CTRL_REG4, L3G4200D_FS_SEL_2000DPS); if (!ack) failureMode(FAILURE_ACC_INIT); delay(5); i2cWrite(MPU_I2C_INSTANCE, L3G4200D_ADDRESS, L3G4200D_CTRL_REG1, L3G4200D_POWER_ON | mpuLowPassFilter); }
static void bma280Init(void) { i2cWrite(BMA280_ADDRESS, BMA280_PMU_RANGE, 0x08, BMA280_BUS); // +-8g range i2cWrite(BMA280_ADDRESS, BMA280_PMU_BW, 0x0E, BMA280_BUS); // 500Hz BW acc_1G = 512 * 8; }
static void l3g4200dInit(uint8_t lpf) { bool ack; uint8_t mpuLowPassFilter = L3G4200D_DLPF_32HZ; // Conversion from MPU6XXX LPF values switch (lpf) { default: case 3: // BITS_DLPF_CFG_42HZ mpuLowPassFilter = L3G4200D_DLPF_32HZ; break; case 2: // BITS_DLPF_CFG_98HZ mpuLowPassFilter = L3G4200D_DLPF_54HZ; break; case 1: // BITS_DLPF_CFG_188HZ mpuLowPassFilter = L3G4200D_DLPF_78HZ; break; case 0: // BITS_DLPF_CFG_256HZ mpuLowPassFilter = L3G4200D_DLPF_93HZ; break; } delay(100); ack = i2cWrite(L3G4200D_ADDRESS, L3G4200D_CTRL_REG4, L3G4200D_FS_SEL_2000DPS); if (!ack) failureMode(FAILURE_ACC_INIT); delay(5); i2cWrite(L3G4200D_ADDRESS, L3G4200D_CTRL_REG1, L3G4200D_POWER_ON | mpuLowPassFilter); }
bool ak8975Init() { bool ack; uint8_t buffer[3]; uint8_t status; UNUSED(ack); ack = i2cWrite(AK8975_MAG_I2C_ADDRESS, AK8975_MAG_REG_CNTL, 0x00); // power down before entering fuse mode delay(20); ack = i2cWrite(AK8975_MAG_I2C_ADDRESS, AK8975_MAG_REG_CNTL, 0x0F); // Enter Fuse ROM access mode delay(10); ack = i2cRead(AK8975_MAG_I2C_ADDRESS, AK8975A_ASAX, 3, &buffer[0]); // Read the x-, y-, and z-axis calibration values delay(10); ack = i2cWrite(AK8975_MAG_I2C_ADDRESS, AK8975_MAG_REG_CNTL, 0x00); // power down after reading. delay(10); // Clear status registers ack = i2cRead(AK8975_MAG_I2C_ADDRESS, AK8975_MAG_REG_STATUS1, 1, &status); ack = i2cRead(AK8975_MAG_I2C_ADDRESS, AK8975_MAG_REG_STATUS2, 1, &status); // Trigger first measurement ack = i2cWrite(AK8975_MAG_I2C_ADDRESS, AK8975_MAG_REG_CNTL, 0x01); return true; }
/*----------------------------------------------------------------------* * Write multiple bytes to RTC RAM. * * Valid address range is 0x00 - 0xFF, no checking. * * Number of bytes (nBytes) must be between 1 and 31 (Wire library * * limitation). * * Returns the I2C status (zero if successful). * *----------------------------------------------------------------------*/ byte DS3232RTC::writeRTC(byte addr, byte *values, byte nBytes) { i2cBeginTransmission(RTC_ADDR); i2cWrite(addr); for (byte i=0; i<nBytes; i++) i2cWrite(values[i]); return i2cEndTransmission(); }
/*-------------------------------------------------------*/ static void FRamSaveValue(INT16U nMWCnt,INT16U nMDCnt,M_DEF *pMemData ) { INT16U i = 0; INT16U Addr = 0; Addr = MW_FRAM_ADDR; for (i = 0; i < nMWCnt; i++) { if (pMemData->m_pMW[i] != pMemData->m_pMWLast[i]) { //写入铁电 Addr += i*2; i2cWrite(1, FRAM3164, (INT8U *) & pMemData->m_pMW[i] , Addr, 1, sizeof(INT16U)); while (i2cGetFlag(1) == I2C_BUSY); } } Addr = MD_FRAM_ADDR; for (i = 0; i < nMDCnt; i++) { if (pMemData->m_pMD[i] != pMemData->m_pMDLast[i]) { //写入铁电 Addr += i*4; i2cWrite(1, FRAM3164, (INT8U *) & pMemData->m_pMW[i] , Addr, 1, sizeof(INT32U)); while (i2cGetFlag(1) == I2C_BUSY); } } //更新FRAMCB的值 }
bool hmc5883lInit() { mag_present_ = false; // Wait for the chip to power up last_update_ms_ = millis(); next_update_ms_ = millis(); // Detect Magnetometer cmd = 0; if (i2cWrite(HMC58X3_ADDR, 0xFF, cmd) != true) { mag_present_ = false; return false; } else { bool result = true; // Configure HMC5833L result &= i2cWrite(HMC58X3_ADDR, HMC58X3_CRA, HMC58X3_CRA_DO_75 | HMC58X3_CRA_NO_AVG | HMC58X3_CRA_MEAS_MODE_NORMAL ); // 75 Hz Measurement, no bias, no averaging result &= i2cWrite(HMC58X3_ADDR, HMC58X3_CRB, HMC58X3_CRB_GN_390); // 390 LSB/Gauss result &= i2cWrite(HMC58X3_ADDR, HMC58X3_MODE, HMC58X3_MODE_CONTINUOUS); // Continuous Measurement Mode mag_present_ = true; return result; } }
void NCD32Relay::turnOffAllRelays(int bank){ if(numberOfRelays <= 8){ if(!i2cWrite(address1, 0x0A, 0)){ Serial.println("Turn Off all relays in bank failed"); initialized = false; } initialized = true; readStatus(address1); return; } byte addr = 0; if(bank == 1 || bank == 2){ addr = address1; }else{ if(bank == 3 || bank == 4){ addr = address2; }else{ //Bad bank value return; } } bank = 17+bank; if(!i2cWrite(addr, bank, 0)){ Serial.println("Turn Off all relays in bank failed"); initialized = false; } initialized = true; readStatus(addr); return; }
void updateLED() { static int currentState = 0; bool i2cNotBusy = ((*pTWI_FIFO_STAT) & XMTSTAT) == 0; if (currentState != LEDState && i2cNotBusy) { // Do a diff, then bring one of the changed elements in line uint8_t diff = currentState ^ LEDState; // Figure out which one to change (first) uint8_t changed = LED_G, reg = LED_G_REG, brightness = LED_G_BRIGHTNESS; if (diff & LED_Y) { changed = LED_Y; reg = LED_Y_REG; brightness = LED_G_BRIGHTNESS; } else if (diff & LED_R) { changed = LED_R; reg = LED_R_REG; brightness = LED_R_BRIGHTNESS; } // Turn it on or off if ((currentState & changed) == 0) i2cWrite(LED_ADDR, reg, 0, false); else i2cWrite(LED_ADDR, reg, brightness, false); } }
/****************************************************************************** / 函数功能:HMC5883校准 / 修改日期:none / 输入参数:none / 输出参数:none / 使用说明:启动-中断-查询 (查询) ******************************************************************************/ void HMC5883L_Calibrate(void) { i2cWrite(HMC5883L_Addr,HMC5883L_REGA,0x15); //30Hz,启动自检模式 i2cWrite(HMC5883L_Addr,HMC5883L_MODE,0x01); //单一测量模式 Delayms(10); i2cWrite(HMC5883L_Addr,HMC5883L_REGA,0x14); i2cWrite(HMC5883L_Addr,HMC5883L_MODE,0x00); //回到工作模式 }
void i2c_write_reg(u8 dev_adr, u8 reg_adr, u8 value) { i2cStart(); i2cWrite(dev_adr); /*slave address, write command*/ i2cWrite(reg_adr); /*send reg address*/ i2cWrite(value); i2cStop(); }
void rtcInit(void) { i2cInit(); i2cStart(); i2cWrite(0xD0); // Address the RTC to write i2cWrite(0x07); i2cWrite(0x00); i2cStop(); }
void initMPU6500(mpu6500_t *mpu6500) { uint8_t i2cBuffer[5]; // Buffer for I2C data i2cBuffer[0] = i2cRead(MPU6500_ADDRESS, MPU6500_WHO_AM_I); if (i2cBuffer[0] == MPU6500_WHO_AM_I_ID) { // Read "WHO_AM_I" register #if UART_DEBUG UARTprintf("MPU-6500 found\n"); #endif } else if (i2cBuffer[0] == MPU9250_WHO_AM_I_ID) { #if UART_DEBUG UARTprintf("MPU-9250 found\n"); #endif } else { #if UART_DEBUG UARTprintf("Could not find MPU-6500 or MPU-9250: %2X\n", i2cBuffer[0]); #endif while (1); } i2cWrite(MPU6500_ADDRESS, MPU6500_PWR_MGMT_1, (1 << 7)); // Reset device, this resets all internal registers to their default values delay(100); while (i2cRead(MPU6500_ADDRESS, MPU6500_PWR_MGMT_1) & (1 << 7)) { // Wait for the bit to clear }; delay(100); i2cWrite(MPU6500_ADDRESS, MPU6500_PWR_MGMT_1, (1 << 3) | (1 << 0)); // Disable sleep mode, disable temperature sensor and use PLL as clock reference i2cBuffer[0] = 0; // Set the sample rate to 1kHz - 1kHz/(1+0) = 1kHz i2cBuffer[1] = 0x03; // Disable FSYNC and set 41 Hz Gyro filtering, 1 KHz sampling i2cBuffer[2] = 3 << 3; // Set Gyro Full Scale Range to +-2000deg/s i2cBuffer[3] = 2 << 3; // Set Accelerometer Full Scale Range to +-8g i2cBuffer[4] = 0x03; // 41 Hz Acc filtering i2cWriteData(MPU6500_ADDRESS, MPU6500_SMPLRT_DIV, i2cBuffer, 5); // Write to all five registers at once // Set accelerometer and gyroscope scale factor from datasheet mpu6500->gyroScaleFactor = MPU6500_GYRO_SCALE_FACTOR_2000; mpu6500->accScaleFactor = MPU6500_ACC_SCALE_FACTOR_8; /* Enable Raw Data Ready Interrupt on INT pin and enable bypass/passthrough mode */ i2cBuffer[0] = (1 << 5) | (1 << 4) | (1 << 1); // Enable LATCH_INT_EN, INT_ANYRD_2CLEAR and BYPASS_EN // When this bit is equal to 1, the INT pin is held high until the interrupt is cleared // When this bit is equal to 1, interrupt status is cleared if any read operation is performed // When asserted, the I2C_MASTER interface pins (ES_CL and ES_DA) will go into 'bypass mode' when the I2C master interface is disabled i2cBuffer[1] = (1 << 0); // Enable RAW_RDY_EN - When set to 1, Enable Raw Sensor Data Ready interrupt to propagate to interrupt pin i2cWriteData(MPU6500_ADDRESS, MPU6500_INT_PIN_CFG, i2cBuffer, 2); // Write to both registers at once // Set INT input pin SysCtlPeripheralEnable(GPIO_MPU_INT_PERIPH); // Enable GPIO peripheral SysCtlDelay(2); // Insert a few cycles after enabling the peripheral to allow the clock to be fully activated GPIOPinTypeGPIOInput(GPIO_MPU_INT_BASE, GPIO_MPU_INT_PIN); // Set as input delay(100); // Wait for sensor to stabilize while (calibrateMPU6500Gyro()) { // Get gyro zero values // Loop until calibration is successful } }
void hmc5883lCal(uint8_t calibration_gain) { // force positiveBias (compass should return 715 for all channels) i2cWrite(MAG_ADDRESS, ConfigRegA, SampleAveraging_8 << 5 | DataOutputRate_75HZ << 2 | PositiveBiasConfig); delay(50); // set gains for calibration i2cWrite(MAG_ADDRESS, ConfigRegB, calibration_gain); i2cWrite(MAG_ADDRESS, ModeRegister, SingleConversion); }
//set one of the four digits [0 - 3] //value [0 - 9] void tm1637::writeDigit(uint8_t digit, uint8_t value) { i2cStart(); i2cWrite(0xc0 | digit); i2cAck(); i2cWrite(data[value]); i2cAck(); i2cStop(); }
//1, 2, 4, 8, or 16 relays void NCD32Relay::setAddress(int a0, int a1, int a2){ address1 = 0x20; if(a0 == 1){ address1 = address1 | 1; } if(a1 == 1){ address1 = address1 | 2; } if(a2 == 1){ address1 = address1 | 4; } Wire.begin(); if(numberOfRelays == 16 || numberOfRelays ==8){ byte writeData[2] = {0,0}; if(numberOfRelays == 16){ if(!i2cWrite(address1, 0, writeData, 2)){ Serial.println("Initialization of relay controller failed"); return; } }else{ if(!i2cWrite(address1, 0, writeData, 1)){ Serial.println("Initialization of relay controller failed"); return; } } }else{ byte writeData[1]; byte writeData2[1]; switch(numberOfRelays){ case 1: writeData[0] = oneRelayMux; writeData2[0] = oneRelayMux; break; case 2: writeData[0] = twoRelayMux; writeData2[0] = twoRelayMux; break; case 4: writeData[0] = fourRelayMux; writeData2[0] = fourRelayMux; break; } if(!i2cWrite(address1, 0x00, writeData, 1)){ Serial.println("Initialization of relay controller failed"); return; } if(!i2cWrite(address1, 0x06, writeData2, 1)){ Serial.println("Initialization of relay controller failed"); return; } } initialized = true; readStatus(address1); }
void rtcSetTime(unsigned char h,unsigned char m, unsigned char s) { i2cStart(); i2cWrite(0xD0); i2cWrite(0); i2cWrite(s); i2cWrite(m); i2cWrite(h); i2cStop(); }
void hmc5883lInit(void) { delay(100); // force positiveBias i2cWrite(MAG_ADDRESS, 0x00, 0x71); // Configuration Register A -- 0 11 100 01 num samples: 8 ; output rate: 15Hz ; positive bias delay(50); // set gains for calibration i2cWrite(MAG_ADDRESS, 0x01, 0x60); // Configuration Register B -- 011 00000 configuration gain 2.5Ga i2cWrite(MAG_ADDRESS, 0x02, 0x01); // Mode register -- 000000 01 single Conversion Mode // this enters test mode }
void mag3110Init() { bool ack; UNUSED(ack); ack = i2cWrite(MAG3110_MAG_I2C_ADDRESS, MAG3110_MAG_REG_CTRL_REG1, 0x01); // active mode 80 Hz ODR with OSR = 1 delay(20); ack = i2cWrite(MAG3110_MAG_I2C_ADDRESS, MAG3110_MAG_REG_CTRL_REG2, 0xA0); // AUTO_MRST_EN + RAW delay(10); }
void i2c_OLED_send_cmd(uint8_t command) { switch (OLED_Type) { case 1: i2cWrite(curOLED_address, 0x80, (uint8_t)command); break; case 2: i2cWrite(curOLED_address, 0x00, (uint8_t)command); break; } }
static inline void mma8451ConfigureInterrupt(void) { #ifdef MMA8451_INT_PIN IOInit(IOGetByTag(IO_TAG(MMA8451_INT_PIN)), OWNER_MPU_EXTI, 0); // TODO - maybe pullup / pulldown ? IOConfigGPIO(IOGetByTag(IO_TAG(MMA8451_INT_PIN)), IOCFG_IN_FLOATING); #endif i2cWrite(MPU_I2C_INSTANCE, MMA8452_ADDRESS, MMA8452_CTRL_REG3, MMA8452_CTRL_REG3_IPOL); // Interrupt polarity (active HIGH) i2cWrite(MPU_I2C_INSTANCE, MMA8452_ADDRESS, MMA8452_CTRL_REG4, MMA8452_CTRL_REG4_INT_EN_DRDY); // Enable DRDY interrupt (unused by this driver) i2cWrite(MPU_I2C_INSTANCE, MMA8452_ADDRESS, MMA8452_CTRL_REG5, 0); // DRDY routed to INT2 }