bool i2cWriteBuffer(uint8_t addr, uint8_t reg, uint8_t len, uint8_t * data) { int i; if (!I2C_Start()) return false; I2C_SendByte(addr << 1 | I2C_Direction_Transmitter); if (!I2C_WaitAck()) { I2C_Stop(); return false; } I2C_SendByte(reg); I2C_WaitAck(); for (i = 0; i < len; i++) { I2C_SendByte(data[i]); if (!I2C_WaitAck()) { I2C_Stop(); return false; } } I2C_Stop(); return true; }
uint8_t temperature_DeviceShutdown (void) { // put the device in SHUTDOWN mode to save power r = I2C_Start(); // len = sprintf(str, "\n\r I2C_Start: 0x%x\n\r", r); // outputStringToUART0(str); if (r == TW_START) { r = I2C_Write(TCN75A_ADDR_WRITE); // address the device, say we are going to write // len = sprintf(str, "\n\r I2C_Write(TCN75A_ADDR_WRITE): 0x%x\n\r", r); // outputStringToUART0(str); if (r == TW_MT_SLA_ACK) { r = I2C_Write(TCN75A_CONFIG); // len = sprintf(str, "\n\r I2C_Write(TCN75A_CONFIG): 0x%x\n\r", r); // outputStringToUART0(str); if (r == TW_MT_DATA_ACK) { r = I2C_Write(TCN75A_SHUTDOWN_BIT); // len = sprintf(str, "\n\r I2C_Write(TCN75A_SHUTDOWN_BIT): 0x%x\n\r", r); // outputStringToUART0(str); I2C_Stop(); return I2C_OK; } else { // could not write data to device I2C_Stop(); return errNoI2CDataAck; } } else { // could not address device I2C_Stop(); return errNoI2CAddressAck; } I2C_Stop(); // outputStringToUART0("\n\r STOP completed \n\r"); } else { // could not START return errNoI2CStart; } }
int I2C_SW::read_regs(uint8_t SlaveAddress, uint8_t startRegister, uint8_t*out, int count) { int i; if (!I2C_Start()) { return -1; } I2C_SendByte(SlaveAddress&0xFE); if (!I2C_WaitAck()) { I2C_Stop(); return -1; } I2C_SendByte(startRegister); I2C_WaitAck(); I2C_Start(); I2C_SendByte((SlaveAddress&0xFE)|0x01); I2C_WaitAck(); for(i=0; i<count; i++) { out[i] = I2C_ReceiveByte(); if (i==count-1) I2C_SendNoAck(); else I2C_SendAck(); } I2C_Stop(); return 0; }
bool i2cRead(I2CDevice device, uint8_t addr, uint8_t reg, uint8_t len, uint8_t *buf) { UNUSED(device); if (!I2C_Start()) { return false; } I2C_SendByte(addr << 1 | I2C_Direction_Transmitter); if (!I2C_WaitAck()) { I2C_Stop(); i2cErrorCount++; return false; } I2C_SendByte(reg); I2C_WaitAck(); I2C_Start(); I2C_SendByte(addr << 1 | I2C_Direction_Receiver); I2C_WaitAck(); while (len) { *buf = I2C_ReceiveByte(); if (len == 1) { I2C_NoAck(); } else { I2C_Ack(); } buf++; len--; } I2C_Stop(); return true; }
// Generic function to set a single ADXL345 register to a value uint8_t setADXL345Register (uint8_t reg, uint8_t val) { uint8_t r; r = I2C_Start(); if (r == TW_START) { // outputStringToUART0("\n\r setADXL345Register: about to write address \n\r"); r = I2C_Write(ADXL345_ADDR_WRITE); // address the device, say we are going to write if (r == TW_MT_SLA_ACK) { // outputStringToUART0("\n\r setADXL345Register: about to write data \n\r"); r = I2C_Write(reg); // tell the device the register we are going to want if (r == TW_MT_DATA_ACK) { // outputStringToUART0("\n\r setADXL345Register: about to write value \n\r"); r = I2C_Write(val); // set the value // outputStringToUART0("\n\r setADXL345Register: about to Stop \n\r"); I2C_Stop(); // outputStringToUART0("\n\r setADXL345Register: Stop completed \n\r"); return I2C_OK; } else { // could not write data to device I2C_Stop(); return errNoI2CDataAck; } } else { // could not address device I2C_Stop(); return errNoI2CAddressAck; } } else { // could not START return errNoI2CStart; } };
static unsigned int readTemp() { //int ret; unsigned long temp = 0; int cnt = 0; repeat: if (cnt++ > 100) { return -1; } I2C_Stop(); mdelay(1); I2C_Start(); if (I2C_Send(MLX90615_ADDR, 1) == -1) goto repeat; if (I2C_Send(0x27, 1) == -1) goto repeat; //I2C_Stop(); I2C_Start(); if (I2C_Send(MLX90615_ADDR | 1, 1) == -1) goto repeat; temp = IIC_Receive(1) | (IIC_Receive(1) << 8); IIC_Receive(1); I2C_Stop(); /* gTemp = temp; gTemp = temp * 2 - 27315;*/ return temp; }
bool i2cWriteBuffer(I2CDevice device, uint8_t addr, uint8_t reg, uint8_t len, uint8_t * data) { UNUSED(device); int i; if (!I2C_Start()) { i2cErrorCount++; return false; } I2C_SendByte(addr << 1 | I2C_Direction_Transmitter); if (!I2C_WaitAck()) { I2C_Stop(); return false; } I2C_SendByte(reg); I2C_WaitAck(); for (i = 0; i < len; i++) { I2C_SendByte(data[i]); if (!I2C_WaitAck()) { I2C_Stop(); i2cErrorCount++; return false; } } I2C_Stop(); return true; }
// send address via IIC. BOOL I2C_SendAddr(BYTE DevAddr, WORD Addr, BYTE ReadWriteFlag, BYTE I2cDevice) { I2C_Start(I2cDevice); if(!I2C_WriteByte(DevAddr, I2cDevice)) { WaitMs(EEPROM_WRITE_TIME); // Wairt for Programming-time. I2C_Start(I2cDevice); if(!I2C_WriteByte(DevAddr, I2cDevice))// | PageAddr)) { I2C_Stop(I2cDevice); return FALSE; } } if(!I2C_WriteByte((BYTE)Addr, I2cDevice)) { I2C_Stop(I2cDevice); return FALSE; } if(ReadWriteFlag == IIC_READ) { I2C_Start(I2cDevice); if(!I2C_WriteByte((DevAddr | IIC_READ), I2cDevice)) { I2C_Stop(I2cDevice); return FALSE; } } return TRUE; }
//************************************************************ //BU9792 refresh display unsigned char BU9792_Refresh(unsigned char * Src, unsigned int Len) { unsigned char temp = 0, i; Len = Len; //I2C_Stop(I2C_BUS_0); I2C_Start(I2C_BUS_0); while (I2C_WriteByte(BU9799_ADDR, I2C_BUS_0) && (temp < 218)) { I2C_Stop(I2C_BUS_0); DelayMs(1); I2C_Start(I2C_BUS_0); temp++; } if (temp >= 218) { I2C_Stop(I2C_BUS_0); return (0xff); } //I2C_WriteByte(LCD_ICSET_M|0x80,I2C_BUS_0) ; /*high address*/ I2C_WriteByte(LCD_ICSET_M | 0x80, I2C_BUS_0); /* 高地址 */ //I2C_WriteByte(0x00,I2C_BUS_0) ; /*send data low address*/ I2C_WriteByte(0x00, I2C_BUS_0); /* 发送数据低地址*/ for (i = 0; i < 18; i++) { //I2C_WriteByte(*Src++,I2C_BUS_0) ; /*send device address*/ I2C_WriteByte(*Src++, I2C_BUS_0); /* 发送器件地址 */ } I2C_Stop(I2C_BUS_0); return 0; }
// Generic function to read a value from a single ADXL345 register uint8_t readADXL345Register (uint8_t reg, uint8_t *valp) { uint8_t r; r = I2C_Start(); if (r == TW_START) { // outputStringToUART0("\n\r setADXL345Register: about to write address \n\r"); r = I2C_Write(ADXL345_ADDR_WRITE); // address the device, say we are going to write if (r == TW_MT_SLA_ACK) { // outputStringToUART0("\n\r setADXL345Register: about to write data \n\r"); r = I2C_Write(reg); // tell the device the register we are going to want if (r == TW_MT_DATA_ACK) { r = I2C_Start(); // restart, preparatory to reading r = I2C_Write(ADXL345_ADDR_READ); // address the device, say we are going to read // outputStringToUART0("\n\r setADXL345Register: about to read value \n\r"); *valp = I2C_Read(0); // do NACK, since this is the last and only byte // r = I2C_Write(val); // set the value // outputStringToUART0("\n\r setADXL345Register: about to Stop \n\r"); I2C_Stop(); // outputStringToUART0("\n\r setADXL345Register: Stop completed \n\r"); return I2C_OK; } else { // could not write data to device I2C_Stop(); return errNoI2CDataAck; } } else { // could not address device I2C_Stop(); return errNoI2CAddressAck; } } else { // could not START return errNoI2CStart; } };
bool i2cRead(uint8_t addr, uint8_t reg, uint8_t len, uint8_t *buf) { if (!I2C_Start()) return false; I2C_SendByte(addr << 1 | I2C_Direction_Transmitter); if (!I2C_WaitAck()) { I2C_Stop(); return false; } I2C_SendByte(reg); I2C_WaitAck(); I2C_Start(); I2C_SendByte(addr << 1 | I2C_Direction_Receiver); I2C_WaitAck(); while (len) { *buf = I2C_ReceiveByte(); if (len == 1) I2C_NoAck(); else I2C_Ack(); buf++; len--; } I2C_Stop(); return true; }
int I2C_SW::write_regs(uint8_t SlaveAddress, uint8_t startRegister, const uint8_t*data, int count) { int i; if (!I2C_Start()) { return -1; } I2C_SendByte(SlaveAddress&0xFE); if (!I2C_WaitAck()) { I2C_Stop(); return -1; } I2C_SendByte(startRegister&0xFF); I2C_WaitAck(); for(i=0; i<count; i++) { I2C_SendByte(data[i]&0xFF); if (!I2C_WaitAck()) { I2C_Stop(); return -1; } } I2C_Stop(); return 0; }
I2C_Status InitTempSensor(uint8_t index) { I2C_Status retVal = I2C_OK; // Write the config values into the TX buffer i2cTxBuffer[0] = TMP102_CONFIG_1_VAL; i2cTxBuffer[1] = TMP102_CONFIG_2_VAL; taskENTER_CRITICAL(); // TODO: log something if the sensor fails to initialize do { // I2C Write I2C_Start(SLB_I2C, GetTmp102Addr(index), 0); retVal = I2C_WriteByte(SLB_I2C, TMP102_CONFIG_ADDR); if(retVal != I2C_OK) { break; } retVal = I2C_WaitForTX(SLB_I2C); if(retVal != I2C_OK) { break; } I2C_Stop(SLB_I2C); // I2C Write I2C_Start(SLB_I2C, GetTmp102Addr(index), 0); retVal = I2C_WriteBytes(SLB_I2C, i2cTxBuffer, 2); if(retVal != I2C_OK) { break; } retVal = I2C_WaitForTX(SLB_I2C); if(retVal != I2C_OK) { break; } I2C_Stop(SLB_I2C); // I2C Read I2C_Start(SLB_I2C, GetTmp102Addr(index), 1); if((retVal = I2C_ReadBytes(SLB_I2C, i2cRxBuffer, 2)) != I2C_OK) { break; } I2C_Stop(SLB_I2C); }while(0); taskEXIT_CRITICAL(); return retVal; }
/********************单字节写入***********************/ uint8_t MPU6050_WriteByte(uint8_t SlaveAddress,uint8_t REG_Address,uint8_t REG_data) { if(!I2C_Start())return 0; I2C_SendByte(SlaveAddress); //发送设备地址+写信号//I2C_SendByte(((REG_Address & 0x0700) >>7) | SlaveAddress & 0xFFFE);//设置高起始地址+器件地址 if(!I2C_WaitAck()){I2C_Stop(); return 0;} I2C_SendByte(REG_Address ); //设置低起始地址 if(!I2C_WaitAck()){I2C_Stop(); return 0;}//I2C_WaitAck(); I2C_SendByte(REG_data); if(!I2C_WaitAck()){I2C_Stop(); return 0;}//I2C_WaitAck(); I2C_Stop(); return 1; }
uint16_t Single_Write(unsigned char SlaveAddress,unsigned char REG_Address,unsigned char REG_data) //void { if(!I2C_Start())return FALSE; I2C_SendByte(SlaveAddress); //发送设备地址+写信号//I2C_SendByte(((REG_Address & 0x0700) >>7) | SlaveAddress & 0xFFFE);//设置高起始地址+器件地址 if(!I2C_WaitAck()){I2C_Stop(); return FALSE;} I2C_SendByte(REG_Address ); //设置低起始地址 I2C_WaitAck(); I2C_SendByte(REG_data); I2C_WaitAck(); I2C_Stop(); delay5ms(); return TRUE; }
bool Single_Write(unsigned char SlaveAddress,unsigned char REG_Address,unsigned char REG_data) //void { if(!I2C_Start())return FALSE; I2C_SendByte(SlaveAddress); //发送设备地址+写信号 if(!I2C_WaitAck()){I2C_Stop(); return FALSE;} I2C_SendByte(REG_Address ); //设置低起始地址 I2C_WaitAck(); I2C_SendByte(REG_data); I2C_WaitAck(); I2C_Stop(); delay5ms(); return TRUE; }
//************************************************************ //BU9792 initialization unsigned char BU9792_Init(void) { unsigned char temp = 0, i; //BU9792_Set(LCD_OFF); //turn off display first BU9792_Set(LCD_OFF); //先关闭显示 //I2C_Stop(I2C_BUS_0); I2C_Start(I2C_BUS_0); while (I2C_WriteByte(BU9799_ADDR, I2C_BUS_0) && (temp < 3)) { I2C_Stop(I2C_BUS_0); DelayMs(1); I2C_Start(I2C_BUS_0); temp++; } if (temp >= 3) { I2C_Stop(I2C_BUS_0); return (0xff); } //I2C_WriteByte(LCD_ICSET|0x80,I2C_BUS_0) ; //reset I2C_WriteByte(LCD_ICSET | 0x80, I2C_BUS_0); //复位 //I2C_WriteByte(LCD_POWER_NORMAL|0x80,I2C_BUS_0) ; //normal power consumption I2C_WriteByte(LCD_POWER_NORMAL | 0x80, I2C_BUS_0); //正常功耗 // I2C_WriteByte(LCD_BLINK_OFF|0x80,I2C_BUS_0) ; //turn off flicker I2C_WriteByte(LCD_BLINK_OFF | 0x80, I2C_BUS_0); //关闭闪烁 I2C_WriteByte(LCD_PIXEL_NORMAL | 0x80, I2C_BUS_0); // I2C_WriteByte(LCD_ICSET_M | 0x80, I2C_BUS_0); // //I2C_WriteByte(0x00,I2C_BUS_0) ; /*send data low address*/ I2C_WriteByte(0x00, I2C_BUS_0); /* 发送数据低地址*/ for (i = 0; i < 18; i++) { // I2C_WriteByte(0xff,I2C_BUS_0) ; /* send device address I2C_WriteByte(0xff, I2C_BUS_0); /* 发送器件地址 */ } I2C_Stop(I2C_BUS_0); //BU9792_Set(LCD_ON); //start LCD BU9792_Set(LCD_ON); //开启LCD return 0; }
/***************************************** * try to detect whether the ADXL345 Accelerometer is there, and functioning * returns with global "motionFlags.accelerometerIsThere" set or cleared *****************************************/ uint8_t findADXL345 (void) { uint8_t r; outputStringToUART0("\n\r entered findADXL345 routine \n\r"); motionFlags &= ~(1<<accelerometerIsThere); // flag cleared, until accel found r = I2C_Start(); len = sprintf(str, "\n\r I2C_Start: 0x%x\n\r", r); outputStringToUART0(str); if (r == TW_START) { r = I2C_Write(ADXL345_ADDR_WRITE); // address the device, say we are going to write len = sprintf(str, "\n\r I2C_Write(ADXL345_ADDR_WRITE): 0x%x\n\r", r); outputStringToUART0(str); if (r == TW_MT_SLA_ACK) { motionFlags |= (1<<accelerometerIsThere); // accel found, set flag r = I2C_Write(ADXL345_REG_DEVID); // tell the device the register we are going to want len = sprintf(str, "\n\r I2C_Write(ADXL345_REG_DEVID): 0x%x\n\r", r); outputStringToUART0(str); if (r == TW_MT_DATA_ACK) { r = I2C_Start(); // restart, preparatory to reading len = sprintf(str, "\n\r ReStart: 0x%x\n\r", r); outputStringToUART0(str); if (r == TW_REP_START){ r = I2C_Write(ADXL345_ADDR_READ); // address the device, say we are going to read len = sprintf(str, "\n\r I2C_Write(ADXL345_ADDR_READ): 0x%x\n\r", r); outputStringToUART0(str); if (r == TW_MR_SLA_ACK){ r = I2C_Read(0); // do NACK, since this is the last byte len = sprintf(str, "\n\r I2C_Read(0): 0x%x\n\r", r); outputStringToUART0(str); } } else { I2C_Stop(); return errNoI2CRepStart; } } else { // could not write data to device I2C_Stop(); return errNoI2CDataAck; } } else { // could not address device I2C_Stop(); return errNoI2CAddressAck; } } else { // could not START return errNoI2CStart; } I2C_Stop(); outputStringToUART0("\n\r I2C_Stop completed \n\r"); return I2C_OK; } // end of findAccelerometer
/* ------------------------------------------------------------------- Name: I2C_TxRepeat - Purpose: To transmit the same data to I2C slave device repeatly via I2C. Passed: UB8 bSLA = I2C slave address. UW16 wREG = I2C register address. UB8 bCNT = The number of data which will be transmitted excluding slave and register address (bCNT: 1..255). UB8 bDATA = The repeated data. Returns: None. Notes: ------------------------------------------------------------------- */ void I2C_TxRepeat ( UB8 bSLA, /* BITEK slave address */ UW16 wREG, /* BITEK register address */ UB8 bCNT, /* The number of data which will be transmitted */ UB8 bDATA /* The repeated DATA */ ) { UB8 bIdx; if (bCNT == 0) return; I2C_Start(); I2C_TxData(bSLA | (((wREG) >> 7) & 0x1E)); I2C_GetACK(); I2C_TxData(wREG); I2C_GetACK(); /* -------------------------------- Write Data -------------------------------- */ for (bIdx = bCNT; bIdx; bIdx--) { I2C_TxData(bDATA); I2C_GetACK(); } /* for */ I2C_Stop(); } /* I2C_TxRepeat */
unsigned char initRTCmodule(unsigned char devADDR){ unsigned char btemp; Nop(); Nop(); // I2C_Close(); I2C_Init(39); // I2C 100 KHZ, 16 MHZ OSC (see note in "myI2C.c") Nop(); Nop(); // I2C_Start(); I2C_Idle(); btemp = I2C_WriteByte(devADDR); Nop(); Nop(); Nop(); if (btemp != 0){ Nop(); Nop(); I2C_Stop(); I2C_Close(); return 0xFF; } else { btemp = DS3231_GetInfo(0x0F); Nop(); Nop(); return btemp; }; }
void ReadNbyte(u8 addr, u8 *p, u8 number) /* WordAddress,First Data Address,Byte lenth */ //F0=0,right, F0=1,error { I2C_Start(); I2C_WriteAbyte(SLAW); I2C_Check_ACK(1); if(!F0) { I2C_WriteAbyte(addr); I2C_Check_ACK(1); if(!F0) { I2C_Start(); I2C_WriteAbyte(SLAR); I2C_Check_ACK(1); if(!F0) { do { *p = I2C_ReadAbyte(); p++; if(number != 1) S_ACK(); //send ACK } while(--number); S_NoACK(); //send no ACK } } } I2C_Stop(); }
I2C_Status AccelerometerRegWrite(uint8_t reg, uint8_t val) { I2C_Status retVal = I2C_OK; uint8_t tmp[2]; tmp[0] = reg; tmp[1] = val; do { I2C_Start(ALB_I2C, LIS2DH_ADDR, 0); retVal = I2C_WriteBytes(ALB_I2C, tmp, 2); if(retVal != I2C_OK) { break; } retVal = I2C_WaitForTX(ALB_I2C); if(retVal != I2C_OK) { break; } I2C_Stop(ALB_I2C); } while(0); return retVal; }
I2C_Status AccelerometerRegRead(uint8_t reg, uint8_t* val) { I2C_Status retVal = I2C_OK; do { I2C_Start(ALB_I2C, LIS2DH_ADDR, 0); retVal = I2C_WriteByte(ALB_I2C, reg); if(retVal != I2C_OK) { break; } retVal = I2C_WaitForTX(ALB_I2C); if(retVal != I2C_OK) { break; } I2C_Start(ALB_I2C, LIS2DH_ADDR, 1); if((retVal = I2C_ReadBytes(ALB_I2C, val, 1)) != I2C_OK) { break; } I2C_Stop(ALB_I2C); } while(0); return retVal; }
I2C_Status PressureSensorRegRead(uint8_t reg, uint8_t* val) { I2C_Status retVal = I2C_OK; do { I2C_Start(ALB_I2C, MPL311_ADDR, 0); retVal = I2C_WriteByte(ALB_I2C, reg); if(retVal != I2C_OK) { break; } retVal = I2C_WaitForTX(ALB_I2C); if(retVal != I2C_OK) { break; } I2C_Start(ALB_I2C, MPL311_ADDR, 1); if((retVal = I2C_ReadBytes(ALB_I2C, val, 1)) != I2C_OK) { break; } I2C_Stop(ALB_I2C); } while(0); return retVal; }
I2C_Status PressureSensorRegWrite(uint8_t reg, uint8_t val) { I2C_Status retVal = I2C_OK; uint8_t tmp[2]; tmp[0] = reg; tmp[1] = val; do { I2C_Start(ALB_I2C, MPL311_ADDR, 0); retVal = I2C_WriteBytes(ALB_I2C, tmp, 2); if(retVal != I2C_OK) { break; } retVal = I2C_WaitForTX(ALB_I2C); if(retVal != I2C_OK) { break; } I2C_Stop(ALB_I2C); } while(0); return retVal; }
unsigned char I2C_ReadRegister(unsigned char u8SlaveAddress, unsigned char u8RegisterAddress) { unsigned char result; I2C_Start(); I2C0_D = u8SlaveAddress << 1; /* Send I2C device address with W/R bit = 0 */ I2C_Wait(); I2C0_D = u8RegisterAddress; /* Send register address */ I2C_Wait(); I2C_RepeatedStart(); I2C0_D = (u8SlaveAddress << 1) | 0x01; /* Send I2C device address this time with W/R bit = 1 */ I2C_Wait(); I2C_EnterRxMode(); I2C_DisableAck(); result = I2C0_D; I2C_Wait(); I2C_Stop(); result = I2C0_D; Pause(50); return result; }
void write_RTC_I2c() { I2C_Idle(); I2C_Start(); // Inicializa a comunicação I2c I2C_Idle(); //Verifica e aguarda até o barramento I2C estar disponível. I2C_Write_Byte(0xD0); // End. fixo para DS1307: 1101000X, onde x = 0 é para gravação. I2C_Idle(); //Verifica e aguarda até o barramento I2C estar disponível. I2C_Write_Byte(0x00); // End. onde começa a programação do relógio, end. dos segundos. I2C_Idle(); //Verifica e aguarda até o barramento I2C estar disponível. //I2C_Write_Byte(0x00); // Inicializa com 00 segundos. I2C_Write_Byte(0x00); I2C_Idle(); //Verifica e aguarda até o barramento I2C estar disponível. //I2C_Write_Byte(0x08); // Inicializa com 8 minutos. I2C_Write_Byte(0x30); I2C_Idle(); //Verifica e aguarda até o barramento I2C estar disponível. //I2C_Write_Byte(0x08); // Inicializa com 08:00hs (formato 24 horas). I2C_Write_Byte(0x14); //19 horas I2C_Idle(); //Verifica e aguarda até o barramento I2C estar disponível. //I2C_Write_Byte(0x04); // Inicializa com terça I2C_Write_Byte(0x04); I2C_Idle(); //Verifica e aguarda até o barramento I2C estar disponível. //I2C_Write_Byte(0x17); // Inicializa com dia 17 I2C_Write_Byte(0x22);// dia 20 do mês I2C_Idle(); //Verifica e aguarda até o barramento I2C estar disponível. //I2C_Write_Byte(0x04); // Inicializa com mês 04 I2C_Write_Byte(0x01); //mês 1, janeiro I2C_Idle(); //Verifica e aguarda até o barramento I2C estar disponível. //I2C_Write_Byte(0x13); // Inicializa com ano 13 I2C_Write_Byte(0x16); //ano 16 I2C_Idle(); //Verifica e aguarda até o barramento I2C estar disponível. I2C_Stop(); // Finaliza a comunicação I2c }
bool i2cWrite(uint8_t addr, uint8_t reg, uint8_t data) { if (!I2C_Start()) return false; I2C_SendByte(addr << 1 | I2C_Direction_Transmitter); if (!I2C_WaitAck()) { I2C_Stop(); return false; } I2C_SendByte(reg); I2C_WaitAck(); I2C_SendByte(data); I2C_WaitAck(); I2C_Stop(); return true; }
void BME280_WriteRegister(uint8_t regaddr, uint8_t data){ I2C_Start(); I2C_Send(BME280_I2C_ADDR<<1); I2C_Send(regaddr); I2C_Send(data); I2C_Stop(); }
/** * @brief I2C write mutiple data * @param[in] instance instance of i2c moudle * \param[in] chipAddr i2c slave addr * \param[in] addr i2c slave register offset * \param[in] addrLen len of slave register addr(in byte) * \param[in] buf data buf * \param[in] len data length * \retval 0 success * \retval 1 failure */ int I2C_BurstWrite(uint32_t instance ,uint8_t chipAddr, uint32_t addr, uint32_t addrLen, uint8_t *buf, uint32_t len) { uint8_t *p; uint8_t err; p = (uint8_t*)&addr; err = 0; chipAddr <<= 1; I2C_Start(); I2C_SendByte(chipAddr); err += I2C_WaitAck(); while(addrLen--) { I2C_SendByte(*p++); err += I2C_WaitAck(); } while(len--) { I2C_SendByte(*buf++); err += I2C_WaitAck(); } I2C_Stop(); return err; }