uint16_t TSL_readCH1(uint8_t addr){ uint16_t data; //PowerUp TSL I2C_start(); I2C_sendAddr(addr); // Modo escrita I2C_sendByte(0x80); // Program control register I2C_sendByte(0x03); // Power up I2C_stop(); I2C_start(); // READ BLOCK I2C_sendAddr(addr); // I2C_sendByte(0x8E); // Command LSB // 1st READ LSB // I2C_repeatStart(); // I2C_sendAddr(addr+1); // reading mode // data = I2C_receiveByte_NACK(); // I2C_stop(); // // THEN I2C_start(); // READ MSB I2C_sendAddr(addr); // I2C_sendByte(0x8F); // Command MSB // // I2C_repeatStart(); // I2C_sendAddr(addr+1); // data |= (I2C_receiveByte_NACK() <<8); // I2C_stop(); // return data; }
uint8_t I2C_readreg(uint8_t reg) { uint8_t tmp; I2C_start(i2c_dev, SLAVE_ADDRESS, I2C_Direction_Transmitter); // start a transmission in Master transmitter mode USART1_puts("start!\r\n"); I2C_write(i2c_dev, (uint8_t) reg); // write one byte to the slave USART1_puts("write!\r\n"); I2C_stop(i2c_dev); // stop the transmission USART1_puts("stop!\r\n"); delay(100); I2C_start(i2c_dev, SLAVE_ADDRESS, I2C_Direction_Receiver); // start a transmission in Master receiver mode USART1_puts("master received!\r\n"); tmp = I2C_read_nack(i2c_dev); USART1_puts("read nack!\r\n"); I2C_stop(i2c_dev); // stop the transmission USART1_puts("stop!\r\n"); delay(100); return tmp; }
void AK8963_Init(void) { int i = 0; Delay(500000); // First extract the factory calibration for each magnetometer axis uint8_t buffer[3]; // x/y/z gyro calibration data stored here AK8963_WriteRegister(AK8963_CNTL, MODE0_POWER_DOWN); // Power down magnetometer AK8963_WriteRegister(AK8963_CNTL, MODE6_FUSE_ROM_ACCESS); // Enter Fuse ROM access mode I2C_start(I2C1, AK8963_ADDRESS<<1, I2C_Direction_Transmitter); I2C_write(I2C1, AK8963_ASAX); I2C_stop(I2C1); // stop the transmission // start a transmission in Master receiver mode I2C_start(I2C1, AK8963_ADDRESS<<1, I2C_Direction_Receiver); // read one byte and request another byte for(i = 0; i < 3-1; i++){ buffer[i] = I2C_read_ack(I2C1); } // read one byte and don't request another byte, stop transmission buffer[3-1] = I2C_read_nack(I2C1); mag_calibration[0] = (float)(buffer[0] - 128)/256.0 + 1.0; // Return x-axis sensitivity adjustment values, etc. mag_calibration[1] = (float)(buffer[1] - 128)/256.0 + 1.0; mag_calibration[2] = (float)(buffer[2] - 128)/256.0 + 1.0; AK8963_WriteRegister(AK8963_CNTL, MODE0_POWER_DOWN); // Power down magnetometer AK8963_WriteRegister(AK8963_CNTL, RESOLUTION_16_BIT << 4 | MODE3_CONTINUOUS_100_HZ); // Set magnetometer data resolution and sample ODR Delay(500000); }
void read_acc() { //acc I2C_start(I2C1, LSM303DL_A_ADDRESS, I2C_Direction_Transmitter); I2C_write(I2C1,0x28| (1 << 7)); I2C_stop(I2C1); I2C_start(I2C1, LSM303DL_A_ADDRESS, I2C_Direction_Receiver); xla = I2C_read_ack(I2C1); xha = I2C_read_ack(I2C1); yla = I2C_read_ack(I2C1); yha = I2C_read_ack(I2C1); zla = I2C_read_ack(I2C1); zha = I2C_read_nack(I2C1); I2C_stop(I2C1); // stop the transmission char buffer_out[100]; ax=0,ay=0,az=0; ax = ((int16_t)(xha << 8 | xla)) >> 4; sprintf(buffer_out,"ax%i\r\n",ax); usb_cdc_printf(buffer_out); ay = ((int16_t)(yha << 8 | yla)) >> 4; buffer_out[0]='\0'; sprintf(buffer_out,"ay%i\r\n",ay); usb_cdc_printf(buffer_out); az = ((int16_t)(zha << 8 | zla)) >> 4; buffer_out[0]='\0'; sprintf(buffer_out,"az%i\r\n",az); usb_cdc_printf(buffer_out); //acc }
void I2C_recieve(uint8_t slaveAddress, uint8_t regAddress, uint8_t data[], uint8_t dataLength) { uint8_t status, i; status = I2C_start(); //Send start condition if (status != TW_START) //Check value of TWI Status Register. Mask prescaler bits. ERROR(status); //If status different from start go to error TWDR = (slaveAddress + TW_WRITE) << 1; //Load SLA_W into TWDR Register status = I2C_transmit(); //transmit if (status != TW_MT_SLA_ACK) //Check value of TWI Status Register. Mask prescaler bits. ERROR(status); //If status different from MR_SLA_ACK go to ERROR TWDR = regAddress; //Load register address into TWDR Register status = I2C_transmit(); //transmit if (status != TW_MT_SLA_ACK) //Check value of TWI Status Register. Mask prescaler bits. ERROR(status); //If status different from MR_SLA_ACK go to ERROR status = I2C_start(); //Send start condition if (status != TW_START) //Check value of TWI Status Register. Mask prescaler bits. ERROR(status); //If status different from start go to error TWDR = (slaveAddress << 1) + TW_READ; //Load SLA_R into TWDR Register status = I2C_transmit(); //transmit if (status != TW_MR_SLA_ACK) //Check value of TWI Status Register. Mask prescaler bits. ERROR(status); //If status different from MR_SLA_ACK go to ERROR status = I2C_transmit(); //transmit if (status != TW_MR_DATA_ACK) //Check value of TWI Status Register. Mask prescaler bits. ERROR(status); //If status different from MR_SLA_ACK go to ERROR data[0] = TWDR; //TWCR &= ~(1<<TWEA); //generate a NACK to let the slave now that the master is done I2C_stop(); //Send stop condition }
void read_mag() { //mag I2C_start(I2C1, LSM303DL_M_ADDRESS, I2C_Direction_Transmitter); I2C_write(I2C1,0x03); //LSM303_OUT_X_H_M 0x03 I2C_stop(I2C1); I2C_start(I2C1, LSM303DL_M_ADDRESS, I2C_Direction_Receiver); xhm = I2C_read_ack(I2C1); xlm = I2C_read_ack(I2C1); yhm = I2C_read_ack(I2C1); ylm = I2C_read_ack(I2C1); zhm = I2C_read_ack(I2C1); zlm = I2C_read_nack(I2C1); I2C_stop(I2C1); char buffer_out[100]; mx=0,my=0,mz=0; mx = (int16_t)(xhm << 8 | xlm); sprintf(buffer_out,"mx%i\r\n",mx); usb_cdc_printf(buffer_out); my = (int16_t)(yhm << 8 | ylm); buffer_out[0]='\0'; sprintf(buffer_out,"my%i\r\n",my); usb_cdc_printf(buffer_out); mz = (int16_t)(zhm << 8 | zlm); buffer_out[0]='\0'; sprintf(buffer_out,"mz%i\r\n",mz); usb_cdc_printf(buffer_out); //mag }
uint8_t readRegister(uint8_t sensorAddress, uint8_t _register){ I2C_start(ULTRASONICSENSOR_I2C, sensorAddress, I2C_Direction_Transmitter); I2C_write(ULTRASONICSENSOR_I2C, _register); I2C_stop(ULTRASONICSENSOR_I2C); I2C_start(ULTRASONICSENSOR_I2C, sensorAddress, I2C_Direction_Receiver); return I2C_read_nack(ULTRASONICSENSOR_I2C); }
void read_gyro() { //gyro I2C_start(I2C1, LSM303DL_G_ADDRESS, I2C_Direction_Transmitter); I2C_write(I2C1, 0x28| (1 << 7) ); //L3G_OUT_X_L 0x28 I2C_stop(I2C1); I2C_start(I2C1, LSM303DL_G_ADDRESS, I2C_Direction_Receiver); xlg = I2C_read_ack(I2C1); xhg = I2C_read_ack(I2C1); ylg = I2C_read_ack(I2C1); yhg = I2C_read_ack(I2C1); zlg = I2C_read_ack(I2C1); zhg = I2C_read_nack(I2C1); I2C_stop(I2C1); char buffer_out[100]; gx=0,gy=0,gz=0; gx = (int16_t)(xhg << 8 | xlg); sprintf(buffer_out,"gx%i\r\n",gx); usb_cdc_printf(buffer_out); gy = (int16_t)(yhg << 8 | ylg); buffer_out[0]='\0'; sprintf(buffer_out,"gy%i\r\n",gy); usb_cdc_printf(buffer_out); gz = (int16_t)(zhg << 8 | zlg); buffer_out[0]='\0'; sprintf(buffer_out,"gz%i\r\n",gz); usb_cdc_printf(buffer_out); //gyro }
// ds1307 rtc read bit rtrd (void) { unsigned char i; // return 0=ok 1=error bit err; I2C_start(); err = I2C_write(0xd0); // address & r/w bit if (!err) { printf("in rtrd"); err = I2C_write(0x00); // start register addr=0 if (!err) { printf("in rtrd"); I2C_stop(); I2C_delay(); I2C_start(); err = I2C_write(0xd1); // address & r/w bit if (!err) { printf("in rtrd"); for (i=0;i<=6;i++) { TIMBUF[i] = I2C_read(); } TIMBUF[7] = I2C_readn(); // last byte } } } I2C_stop(); return (err); }
char checkInterrupt(){ char int_values; I2C_start(I2C_BASE, ACCELEROMETER_ADDR, 0); I2C_write(I2C_BASE, 0x30, 0); I2C_start(I2C_BASE, ACCELEROMETER_ADDR, 1); int_values = I2C_read(I2C_BASE, 1); return int_values; }
byte I2C_read_register(byte device_register){ byte data = 0; I2C_start(); I2C_send_address(DS1307); I2C_write(device_register); I2C_start(); I2C_send_address(DS1307+1);//Read operation data = I2C_read_nack(); I2C_stop(); return data; }
uint8_t ov7670_get(uint8_t reg) { uint8_t data = 0; I2C_start(I2C2, 0x42, I2C_Direction_Transmitter); I2C_write(I2C2, reg); I2C_stop(I2C2); delayx(1000); I2C_start(I2C2, 0x43, I2C_Direction_Receiver); data = I2C_read_nack(I2C2); I2C_stop(I2C2); delayx(1000); return data; }
uint8_t I2C_Read_Reg(I2C_TypeDef* I2Cx, uint8_t Device, uint8_t Register) { uint8_t data; I2C_start(I2Cx, Device <<1, I2C_Direction_Transmitter); // start a transmission in Master transmitter mode I2C_write(I2Cx, Register); // Select the register you want to read from. I2C_stop(I2Cx); // stop the transmission I2C_start(I2Cx, Device <<1, I2C_Direction_Receiver); // start a transmission in Master receiver mode data = I2C_read_nack(I2Cx); // read one byte from the register of interest. I2C_stop(I2Cx); return data; }
u08 PCF8583_read(u08 address) { u08 a; a=(PCF8583_A0<<1)|0xa0; I2C_start(); I2C_write(a); I2C_write(address); I2C_start(); I2C_write(a|1); a=I2C_read(1); I2C_stop(); return a; }
void initHt16(){ I2C_start(I2C1, slave_address<<1, I2C_Direction_Transmitter); // start a transmission in Master transmitter mode I2C_write(I2C1, osci); // write one byte to the slave I2C_stop(I2C1); /*Blink off*/ I2C_start(I2C1, slave_address<<1, I2C_Direction_Transmitter); // start a transmission in Master transmitter mode I2C_write(I2C1, HT16K33_BLINK_CMD | HT16K33_BLINK_DISPLAYON | (HT16K33_BLINK_OFF << 1)); I2C_stop(I2C1); /*max brightness*/ I2C_start(I2C1, slave_address<<1, I2C_Direction_Transmitter); // start a transmission in Master transmitter mode I2C_write(I2C1, HT16K33_CMD_BRIGHTNESS| 2); I2C_stop(I2C1); }
void I2C_ReadMultipleRegisters(unsigned char address, unsigned char startreg, unsigned char* buffer, unsigned short length) { I2C_start(); // Send I2C Start Transfer I2C_write(address << 1); // Send identifier I2C address - Write I2C_write(startreg); // Send register address I2C_stop(); // Send I2C Stop Transfer I2C_start(); // Send I2C Start Transfer I2C_write((address << 1) | 0x01); // Send identifier I2C address - Read for (int i = 0; i < length; i++) { buffer[i] = I2C_read(i < length - 1 ? 1 : 0); // Read } I2C_stop(); }
unsigned char I2C_ReadRegister(unsigned char address, unsigned char reg) { unsigned char data; I2C_start(); // Send I2C Start Transfer I2C_write(address << 1); // Send identifier I2C address - Write I2C_write(reg); // Send register address I2C_stop(); // Send I2C Stop Transfer I2C_start(); // Send I2C Start Transfer I2C_write((address << 1) | 0x01); // Send identifier I2C address - Read data = I2C_read(0); // Read I2C_stop(); return data; }
uint8_t ADXL345_read_ack(I2C_TypeDef * I2Cx, uint8_t regaddr,uint8_t devread ){ I2C_start(I2Cx, devread, I2C_Direction_Transmitter); I2C_SendData(I2Cx, regaddr); //send register address to be read from while (I2C_GetFlagStatus(I2Cx, I2C_FLAG_BTF) == RESET) I2C_stop(I2Cx); I2C_start(I2Cx,(uint8_t)ADXL345_SLAVE_READ_ADDR , I2C_Direction_Receiver); I2C_AcknowledgeConfig(I2Cx, ENABLE); // wait until one byte has been received while( !I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_RECEIVED) ); // read data from I2C data register and return data byte uint8_t data = I2C_ReceiveData(I2Cx); I2C_stop(I2Cx); return data; }
/*---------------------------------------------------------------------------- //Writes a byte to GPIO register of the GPIO device //Input: byteWrite ( LSB is GP0, MSB is GP7 ) //IMPORTANT: Only pins configured as OUTPUT will be effected *----------------------------------------------------------------------------*/ void I2C_GPIO_Write(uint8_t writeByte){ I2C_start(I2C3, GPIO_ADDRESS, I2C_Direction_Transmitter); // start a transmission in Master transmitter mode I2C_write(I2C3, GPIO_OLAT_REG); //Set device pointer to GPIO register I2C_write(I2C3, writeByte); //Write the byte to the device I2C_stop(I2C3); }
void I2C_enable_square_wave(){ I2C_start(); I2C_send_address(DS1307); I2C_write(0x07); I2C_write(0b00010000); I2C_stop(); }
void I2C_write_register(byte device_register, byte data){ I2C_start(); I2C_send_address(DS1307); I2C_write(device_register); I2C_write(data); I2C_stop(); }
u8 PCF8574_write(u8 address, u8 value) { /** In Master Transmitter mode, the first byte transmitted contains the slave address of the receiving device (seven bits) and the Read/Write (R/W) bit. In this case, the R/W bit will be logic ‘0’. Serial data is transmitted eight bits at a time. After each byte is transmitted, an Acknowledge bit is received. Start and Stop conditions are output to indicate the beginning and the end of a serial transfer. */ u8 bRet = 0x00; I2C_start(); I2C_write(address | I2C_WRITE); if (SSPCON2bits.ACKSTAT) //Si AckStat == 1, on n'a pas reçu d'acquittement { I2C_stop(); return bRet; } I2C_write(value); if (!SSPCON2bits.ACKSTAT ) //Si on reçoit un acquitement, on retourne 1 { bRet = 0x01; } I2C_stop(); return bRet; }
/*---------------------------------------------------------------------------- //Reads the GPIO register of the GPIO device //Returns: GPIO register byte ( LSB is GP0, MSB is GP7 ) //IMPORTANT: Data is only valid for pins configured as INPUT pins! *----------------------------------------------------------------------------*/ uint8_t I2C_GPIO_Read(void){ uint8_t rxData = 0x00; //Indicate we'd like to read the GPIO register I2C_start(I2C3, GPIO_ADDRESS, I2C_Direction_Transmitter); I2C_write(I2C3, GPIO_GPIO_REG); I2C_stop(I2C3); //Then open in RX mode and get the data I2C_start(I2C3, GPIO_ADDRESS, I2C_Direction_Receiver); rxData = I2C_read_nack(I2C3); I2C_stop(I2C3); // stop the transmission return rxData; }
void TSL_softPowerOn(uint8_t addr){ I2C_start(); I2C_sendAddr(addr); // Modo escrita I2C_sendByte(0x80); // Program timing register I2C_sendByte(0x03); // Power up device I2C_stop(); }
/*---------------------------------------------------------------------------- //Sets the direction of each of the GPIO pins //Input: direction byte ( LSB is GP0 , MSB is GP7) //Input: '1' is INPUT , '0' is OUTPUT *----------------------------------------------------------------------------*/ void I2C_GPIO_Direction(uint8_t direction){ I2C_start(I2C3, GPIO_ADDRESS, I2C_Direction_Transmitter); // start a transmission in Master transmitter mode I2C_write(I2C3, GPIO_IODIR_REG); //Set device pointer to IO DIRECTION register I2C_write(I2C3, direction); //Write the byte to the device I2C_stop(I2C3); }
void I2C_WriteRegister(unsigned char address, unsigned char reg, unsigned char value) { I2C_start(); // Send I2C Start Transfer I2C_write(address << 1); // Send identifier I2C address - Write I2C_write(reg); // Send register address I2C_write(value); // Send value I2C_stop(); // Send I2C Stop Transfer }
void I2C_write(uint8_t slaveAddress, uint8_t reg, uint8_t data[], uint8_t dataLength, uint8_t registerSend) { uint8_t status, i; status = I2C_start(); //Send start condition if (status != TW_START) //Check value of TWI Status Register. Mask prescaler bits. ERROR(status); //If status different from start go to error TWDR = (slaveAddress + TW_WRITE) << 1;//Load SLA_W into TWDR Register status = I2C_transmit(); //transmit if (status != TW_MT_SLA_ACK) //Check value of TWI Status Register. Mask prescaler bits. ERROR(status); //If status different from MT_SLA_ACK go to ERROR if (registerSend > 0) //if writing to a register { TWDR = reg; //Load register into TWDR Register. status = I2C_transmit(); //transmit if (status != TW_MT_DATA_ACK) //Check value of TWI Status Register. Mask prescaler bits. ERROR(status); //If status different from MT_DATA_ACK go to ERROR } for (i = 0; i < dataLength; i++) { TWDR = data[i]; //Load DATA into TWDR Register. status = I2C_transmit(); //transmit if (status != TW_MT_DATA_ACK) //Check value of TWI Status Register. Mask prescaler bits. ERROR(status); //If status different from MT_DATA_ACK go to ERROR } I2C_stop(); //Send stop condition }
/*---------------------------------------------------------------------------- //Starts the I2C temperature sensor *----------------------------------------------------------------------------*/ void I2C_Temp_Init(void){ //Select config register values uint8_t config_reg_data = 0x00; uint8_t one_shot = 0; uint8_t resolution = 11; //12-bit resolution uint8_t fault_queue = 00; uint8_t alert_polarity = 0; uint8_t comparator = 0; uint8_t shutdown = 0; //Enable //Merge config reg values into a single byte config_reg_data |= (one_shot << 7); config_reg_data |= (resolution << 5); config_reg_data |= (fault_queue << 3); config_reg_data |= (alert_polarity << 2); config_reg_data |= (comparator << 1); config_reg_data |= (shutdown << 0); //Initialise by writing the config variable to the CONFIG register I2C_start(I2C3, TEMP_ADDRESS, I2C_Direction_Transmitter); // start a transmission in Master transmitter mode I2C_write(I2C3, TEMP_CONFIG_REG); //Set device pointer to configuration register I2C_write(I2C3, config_reg_data); //Write the config reg data to the config reg I2C_stop(I2C3); Delay_Millis(5); //Ensure config values are implemented before continuing }
uint8_t ITG3200_read_register(I2C_TypeDef * I2Cx, uint8_t devwrite, uint8_t regaddr){ I2C_start(I2Cx, devwrite, I2C_Direction_Transmitter); I2C_SendData(I2Cx,regaddr); while (I2C_GetFlagStatus(I2Cx, I2C_FLAG_BTF) == RESET); I2C_stop(I2Cx); I2C_start(I2Cx,0xD1, I2C_Direction_Receiver); I2C_AcknowledgeConfig(I2Cx, ENABLE); // wait until one byte has been received while( !I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_RECEIVED) ); // read data from I2C data register and return data byte uint8_t data = I2C_ReceiveData(I2Cx); I2C_AcknowledgeConfig(I2Cx,DISABLE); I2C_stop(I2Cx); return data; }
/********************************n****************************************/ void TSL_init_INT(uint8_t addr, uint8_t tInt, uint16_t lowTh, uint16_t highTh, uint8_t nCycles){ I2C_start(); I2C_sendAddr(addr); //slave address I2C_sendAddr(0x81); //command to write on 0x81 register I2C_sendByte(tInt); //data to write I2C_repeatStart(); I2C_sendAddr(addr); // Modo escrita I2C_sendByte(0x82); // Lower Th I2C_sendByte((uint8_t)(lowTh&0x00ff)); // Power up device I2C_repeatStart(); I2C_sendAddr(addr); // Modo escrita I2C_sendByte(0x83); // Lower Th I2C_sendByte((uint8_t)((lowTh>>8) &0x00ff)); // Power up device I2C_repeatStart(); I2C_sendAddr(addr); // Modo escrita I2C_sendByte(0x84); // Lower Th I2C_sendByte((uint8_t)(highTh&0x00ff)); // Power up device I2C_repeatStart(); I2C_sendAddr(addr); // Modo escrita I2C_sendByte(0x85); // Lower Th I2C_sendByte((uint8_t)((highTh>>8) &0x00ff)); // Power up device I2C_sendAddr(addr); // Modo escrita I2C_sendByte(0x86); // Program Interrupt Register I2C_sendByte(nCycles); // Any value ou th Interruption I2C_stop(); }