// 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); }
static PyObject *I2CDev_read(I2CDev *self, PyObject *args, PyObject *kwds) { uint32_t n_bytes, i, addr; PyObject *data, *byte_obj; uint8_t *rxbuf; if(!PyArg_ParseTuple(args, "II", &addr, &n_bytes)) { return NULL; } if (self->slave_addr != addr) { if (setSlaveAddress(self->i2c_fd, addr) < 0) { PyErr_SetString(PyExc_IOError, "could not configure I2C interface"); return NULL; } self->slave_addr = addr; } rxbuf = malloc(n_bytes); if (I2C_read(self->i2c_fd, (void *) rxbuf, n_bytes) < 0) { PyErr_SetString(PyExc_IOError, "could not read from I2C device"); free(rxbuf); return NULL; } data = PyList_New(0); for (i=0; i<n_bytes; i++) { byte_obj = PyInt_FromLong((long) rxbuf[i]); PyList_Append(data, byte_obj); Py_DECREF(byte_obj); } free(rxbuf); return data; }
unsigned int I2C_read_int(unsigned char address){ unsigned char temp1, temp2; I2C_start(); address = address << 1; /* 0ビット目をR/Wビットにするためシフト*/ address |= 0b00000001; /* 0ビット目をRに */ I2C_send(address); /* スレーブアドレスを送信 */ temp1 = I2C_read(); I2C_send_ACK(0); /* ACK */ temp2 = I2C_read(); I2C_send_ACK(1); /* NACK */ I2C_stop(); return (temp1 << 8) | (0xFF & temp2); }
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; }
//read value of channel 1 of the light sensor uint16_t read_ch1(){ unsigned char message[2]; message[0] = 0x9E; I2C_write(LIGHT_SENSOR_ADDR, message, 1, FALSE); do{ I2C_read(LIGHT_SENSOR_ADDR, message, 2, TRUE); } while(USI_TWI_Get_State_Info() == USI_TWI_NO_ACK_ON_ADDRESS); return (*((uint16_t*)message)); }
void MPU6050_Read_Sensor(void){ uint8_t Gyro_accel_raw[14]; I2C_read(MPU6050_I2C_ADDRESS , MPU6050_DATA_REGISTER , (uint8_t *) &Gyro_accel_raw, 14); accel[0]= twoByte(Gyro_accel_raw[ 0], Gyro_accel_raw[ 1]); accel[1]= twoByte(Gyro_accel_raw[ 2], Gyro_accel_raw[ 3]); accel[2]= twoByte(Gyro_accel_raw[ 4], Gyro_accel_raw[ 5]); gyro[0]= twoByte(Gyro_accel_raw[ 8], Gyro_accel_raw[ 9]); gyro[1]= twoByte(Gyro_accel_raw[10], Gyro_accel_raw[11]); gyro[2]= twoByte(Gyro_accel_raw[12], Gyro_accel_raw[13]); }
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(); }
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; }
unsigned char I2C_read_char(unsigned char address){ unsigned char data; I2C_start(); address = address << 1; /* 0ビット目をR/Wビットにするためシフト*/ address |= 0b00000001; /* 0ビット目をRに */ I2C_send(address); /* スレーブアドレスを送信 */ data = I2C_read(); I2C_send_ACK(1); /* NACK */ I2C_stop(); return data; }
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; }
//----------------------------------------------------------------------------- // /brief Read data from a register on the AIC3106. // // /param uint8_t in_reg_addr: The address of the register to be read from. // // /param uint8_t * dest_buffer: Pointer to buffer to store retrieved data. // // /return uint32_t ERR_NO_ERROR on sucess // //----------------------------------------------------------------------------- uint32_t AIC3106_readRegister(uint8_t in_reg_addr, uint8_t *dest_buffer) { uint32_t rtn; // write the register address that we want to read. rtn = I2C_write(I2C_PORT_AIC3106, I2C_ADDR_AIC3106, &in_reg_addr, 1, SKIP_STOP_BIT_AFTER_WRITE); if (rtn != ERR_NO_ERROR) return (rtn); // clock out the register data. rtn = I2C_read(I2C_PORT_AIC3106, I2C_ADDR_AIC3106, dest_buffer, 1, SKIP_BUSY_BIT_CHECK); return (rtn); }
alt_u8 i2c_hdmi_rx_hdmi_map_r(alt_u8 reg) { uint32_t offset = reg; uint32_t data; pcie_i2c_read(0x68>>1, offset, &data, 1); return data; #if 0 I2C_start(I2C_CTRL_BASE,0x68>>1,0); //address the chip in write mode data = I2C_write(I2C_CTRL_BASE,reg,0); // set command to read input register. I2C_start(I2C_CTRL_BASE,0x68>>1,1); //send start again but this time in read mode data = I2C_read(I2C_CTRL_BASE,1); // read the input register and send stop return data; #endif }
// Чтение из регистра кодека uint16 aic3204_get(uint16 regnum, uint16* regval) { // Локальные данные int16 retcode = 0; uint8 cmd[1]; // Сформировать пакет I2C cmd[0] = regnum & 0x007F; // 7 бит адреса регистра // Выдать команду retcode |= I2C_write(AIC3204_ICADDR, cmd, 1); // Получить ответ retcode |= I2C_read(AIC3204_ICADDR, cmd, 1); // Ожидание завершения переходных процессов c5515_wait(10); // Возврат результата *regval = cmd[0]; return retcode; }
int16_t tmp100_read_temp(deviceTMP100 device) { deviceTMP100_struct *thisDevice; thisDevice = (deviceTMP100_struct*) device; int8_t dataBytes[2]; int8_t error=0; error = I2C_read( thisDevice->bus, thisDevice->address, 0, 2, dataBytes); if (error==0) { // read ok - return data return dataBytes[0]<<8 | dataBytes[1]; } return 0; }
// \return uint32_t // ERR_NO_ERROR - register read correctly. // //----------------------------------------------------------------------------- uint32_t CDCE913_readByte(uint8_t in_offset, uint8_t *dest_buffer) { uint32_t rtn; // set bit to indicate this is a byte read. SETBIT(in_offset, BYTE_READ_WRITE_BIT); // write the register address that we want to read. rtn = I2C_write(I2C_PORT_CDCE913, I2C_ADDR_CDCE913, &in_offset, 1, SKIP_STOP_BIT_AFTER_WRITE); if (rtn != ERR_NO_ERROR) return (rtn); // clock out the register data. rtn = I2C_read(I2C_PORT_CDCE913, I2C_ADDR_CDCE913, dest_buffer, 1, SKIP_BUSY_BIT_CHECK); return (rtn); }
//Reads the x,y and z registers and stores the contents into x,y and z variables //returns 1 //usage: gyro.update(); //Note: update must be called before using the getX, getY or getZ functions in order // to obtain the most recent values from the gyro void Gyro_Update(gyro_data_t gyro_data) { char aux0=0,aux1=0; I2C_read(ITG_ADDR, GYRO_XOUT_H, &aux1); I2C_read(ITG_ADDR, GYRO_XOUT_L, &aux0); gyro_data->x = (aux1<<8)|aux0; I2C_read(ITG_ADDR, GYRO_YOUT_H, &aux1); I2C_read(ITG_ADDR, GYRO_YOUT_L, &aux0); gyro_data->y = (aux1<<8)|aux0; I2C_read(ITG_ADDR, GYRO_ZOUT_H, &aux1); I2C_read(ITG_ADDR, GYRO_ZOUT_L, &aux0); gyro_data->z = (aux1<<8)|aux0; I2C_read(ITG_ADDR, TEMP_OUT_H, &aux1); I2C_read(ITG_ADDR, TEMP_OUT_L, &aux0); gyro_data->temp = (aux1<<8)|aux0; }
Uint16 AIC_Read(Uint16 regAddr) { CSL_Status status; Uint16 startStop; Uint16 read_buffer[2]; volatile Uint16 looper; startStop = ((CSL_I2C_START) | (CSL_I2C_STOP)); /* Read data */ status = I2C_read(read_buffer, 1, CSL_I2C_CODEC_ADDR, (Uint16 *)®Addr, 1, TRUE, startStop, CSL_I2C_MAX_TIMEOUT, FALSE); if (status != CSL_SOK) { printf("I2C Read ERROR!\n"); return status; } /* Give some delay */ for(looper = 0; looper < CSL_I2C_MAX_TIMEOUT; looper++){;} return read_buffer[0]; }
/** * \brief Tests I2C polled mode operation * * \param none * * \return Test result */ CSL_Status CSL_i2cPolledTest(void) { CSL_Status status; CSL_Status result; Uint16 startStop; volatile Uint16 looper; result = CSL_I2C_TEST_FAILED; /* Assign the EEPROM page address */ gI2cWrBuf[0] = 0x0; gI2cWrBuf[1] = 0x0; for(looper = 0; looper < CSL_I2C_DATA_SIZE; looper++) { gI2cWrBuf[looper + CSL_EEPROM_ADDR_SIZE] = looper; gI2cRdBuf[looper] = 0x0000; } /* Initialize I2C module */ status = I2C_init(CSL_I2C0); if(status != CSL_SOK) { printf("I2C Init Failed!!\n"); return(result); } /* Setup I2C module */ i2cSetup.addrMode = CSL_I2C_ADDR_7BIT; i2cSetup.bitCount = CSL_I2C_BC_8BITS; i2cSetup.loopBack = CSL_I2C_LOOPBACK_DISABLE; i2cSetup.freeMode = CSL_I2C_FREEMODE_DISABLE; i2cSetup.repeatMode = CSL_I2C_REPEATMODE_DISABLE; i2cSetup.ownAddr = CSL_I2C_OWN_ADDR; i2cSetup.sysInputClk = CSL_I2C_SYS_CLK; i2cSetup.i2cBusFreq = CSL_I2C_BUS_FREQ; startStop = ((CSL_I2C_START) | (CSL_I2C_STOP)); status = I2C_setup(&i2cSetup); if(status != CSL_SOK) { printf("I2C Setup Failed!!\n"); return(result); } /* Write data */ status = I2C_write(gI2cWrBuf, (CSL_I2C_DATA_SIZE + CSL_EEPROM_ADDR_SIZE), CSL_I2C_EEPROM_ADDR, TRUE, startStop, CSL_I2C_MAX_TIMEOUT); if(status != CSL_SOK) { printf("I2C Write Failed!!\n"); return(result); } printf("I2C Write Complete\n"); /* Give some delay */ for(looper = 0; looper < CSL_I2C_MAX_TIMEOUT; looper++){;} /* Write data EEPROM page address for read operation */ status = I2C_write(gI2cWrBuf, CSL_EEPROM_ADDR_SIZE, CSL_I2C_EEPROM_ADDR, TRUE, startStop, CSL_I2C_MAX_TIMEOUT); if(status != CSL_SOK) { printf("I2C Write Failed!!\n"); return(result); } /* Give some delay */ for(looper = 0; looper < CSL_I2C_MAX_TIMEOUT; looper++){;} /* Read data */ status = I2C_read(gI2cRdBuf, CSL_I2C_DATA_SIZE, CSL_I2C_EEPROM_ADDR, TRUE, startStop, CSL_I2C_MAX_TIMEOUT, FALSE); if(status != CSL_SOK) { printf("I2C Read Failed!!\n"); return(result); } printf("I2C Read Complete\n"); /* Compare the buffers */ for(looper = 0; looper < CSL_I2C_DATA_SIZE; looper++) { if(gI2cWrBuf[looper + CSL_EEPROM_ADDR_SIZE] != gI2cRdBuf[looper]) { printf("Read Write Buffers Does not Match!!\n"); return(result); } } if(looper == CSL_I2C_DATA_SIZE) { printf("Read Write Buffers Match!!\n"); } result = CSL_I2C_TEST_PASSED; return(result); }
/** * \brief Tests I2C polled mode operation * * \param none * * \return Test result */ CSL_Status CSL_i2cPolledTest(void) { CSL_Status status; CSL_Status result; Uint16 startStop; volatile Uint16 looper; volatile int i; result = CSL_I2C_TEST_FAILED; /* Initialize I2C module */ status = I2C_init(CSL_I2C0); if(status != CSL_SOK) { LOG_printf(&trace, "I2C Init Failed!!"); return(result); } /* Setup I2C module */ i2cSetup.addrMode = CSL_I2C_ADDR_7BIT; // EEPROM I2C device address is 7-bit (101.0xxx) i2cSetup.bitCount = CSL_I2C_BC_8BITS; // I2C bit count is 8-bit i2cSetup.loopBack = CSL_I2C_LOOPBACK_DISABLE; i2cSetup.freeMode = CSL_I2C_FREEMODE_DISABLE; i2cSetup.repeatMode = CSL_I2C_REPEATMODE_DISABLE; i2cSetup.ownAddr = CSL_I2C_OWN_ADDR; i2cSetup.sysInputClk = CSL_I2C_SYS_CLK; i2cSetup.i2cBusFreq = CSL_I2C_BUS_FREQ; startStop = ((CSL_I2C_START) | (CSL_I2C_STOP)); status = I2C_setup(&i2cSetup); if(status != CSL_SOK) { LOG_printf(&trace, "I2C Setup Failed!!"); return(result); } for(i = 0; i < CSL_I2C_PAGES; i++){ LOG_printf(&trace, "Page %d", i); /* Assign the EEPROM page address */ gI2cWrBuf[0] = (CSL_I2C_PAGE_SIZE*i)>>8; gI2cWrBuf[1] = (CSL_I2C_PAGE_SIZE*i)&0xFF; for(looper = 0; looper < CSL_I2C_PAGE_SIZE; looper++) { gI2cWrBuf[looper + CSL_EEPROM_ADDR_SIZE] = i*CSL_I2C_PAGE_SIZE + looper; gI2cRdBuf[looper] = 0x0000; } /* Write data */ status = I2C_write(gI2cWrBuf, (CSL_I2C_PAGE_SIZE + CSL_EEPROM_ADDR_SIZE), CSL_I2C_EEPROM_ADDR, TRUE, startStop, CSL_I2C_MAX_TIMEOUT); if(status != CSL_SOK) { LOG_printf(&trace, "\tI2C Write Failed!!"); return(result); } LOG_printf(&trace, "\tI2C Write Complete"); /* Give some delay */ for(looper = 0; looper < CSL_I2C_MAX_TIMEOUT; looper++){;} /* Read data */ status = I2C_read(gI2cRdBuf, CSL_I2C_PAGE_SIZE, CSL_I2C_EEPROM_ADDR, gI2cWrBuf, CSL_EEPROM_ADDR_SIZE, TRUE, startStop, CSL_I2C_MAX_TIMEOUT, FALSE); if(status != CSL_SOK) { LOG_printf(&trace, "\tI2C Read Failed!!"); return(result); } LOG_printf(&trace, "\tI2C Read Complete"); /* Compare the buffers */ for(looper = 0; looper < CSL_I2C_PAGE_SIZE; looper++) { if(gI2cWrBuf[looper + CSL_EEPROM_ADDR_SIZE] != gI2cRdBuf[looper]) { LOG_printf(&trace, "\tRead Write Buffers Does not Match!!"); return(result); } } if(looper == CSL_I2C_PAGE_SIZE) { LOG_printf(&trace, "\tRead Write Buffers Match!!"); } } result = CSL_I2C_TEST_PASSED; return(result); }
u8 updGyro(void){ // Takes ~0.21ms when no error.. return I2C_read(Gy_addr,0x28|0x80,(u8 *)sens.GyrRaw,6); }
int readFrom(byte _dev_address,byte address, int num, byte _buff[]) { int a=0,i=0; int timeout; uint8_t flag1=0,flag2=0; a=i2c_beginTransmission(I2Ci,_dev_address,i2c_write); // start transmission to device if(a<0) return -1; // usart_printfm(USART1,(const int *)"Reading from this device address: %2x\n\r",_dev_address); a=i2c_sendData(I2Ci,address); // sends address to read from if(a<0) return -1; //usart_printfm(USART1,(const int *)"sent this data which is the address to read from: %2x\n\r",address); // if(a<0) // return (a-1); //i2c_stopTransmission(I2Cx); // end transmission // if(a<0) // return (a-2); a=i2c_beginTransmission(I2Ci,_dev_address,i2c_read); // start transmission to device if(a<0) return -1; //usart_printfm(USART1,(const int *)"Initiated the register read cycle: %2x\n\r",_dev_address); // if(a<0) // return a; // request 6 bytes from device if(num==1){ I2C_AcknowledgeConfig(I2Ci, DISABLE); /* Test on I2C1 EV8 and clear it */ timeout = I2C_TIMEOUT_MAX; /* Initialize timeout value */ // wait until one byte has been received while( !I2C_CheckEvent(I2Ci, I2C_EVENT_MASTER_BYTE_RECEIVED) ){ ms_delay(100); if((timeout--)==0){ flag1 = I2Ci->SR1; flag2 = I2Ci->SR2; usart_printf(USARTx,"Flag1:%04x \n\r Flag2:%04x\n\r",flag1,flag2); usart_printf(USARTx,"Failing at read nnack stage\n\r"); return -1; } } // wait until one byte has been received I2C_GenerateSTOP(I2Ci, ENABLE); // read data from I2C data register and return data byte _buff[i] = I2C_ReceiveData(I2Ci); return 0; } if(num==2) { timeout = I2C_TIMEOUT_MAX; // while(I2C_GetFlagStatus(I2Ci,I2C_FLAG_ADDR)!=SET){ // ms_delay(100); // if((timeout--)==0){ // flag1 = I2Ci->SR1; // flag2 = I2Ci->SR2; // usart_printf(USARTx,"Flag1:%04x \n\r Flag2:%04x\n\r",flag1,flag2); // usart_printf(USARTx,"Failing at reading 2 bytes stage\n\r"); // return ; // }} I2C_AcknowledgeConfig(I2Ci, DISABLE); I2C_NACKPositionConfig(I2Ci, I2C_NACKPosition_Next); timeout = I2C_TIMEOUT_MAX; while(I2C_GetFlagStatus(I2Ci,I2C_FLAG_BTF)!=SET){ ms_delay(100); if((timeout--)==0){ flag1 = I2Ci->SR1; flag2 = I2Ci->SR2; usart_printf(USARTx,"Flag1:%04x \n\r Flag2:%04x\n\r",flag1,flag2); usart_printf(USARTx,"Failing at reading 2 bytes stage2\n\r"); return -1; }} i2c_stopTransmission(I2Ci); _buff[i] = I2C_ReceiveData(I2Ci); i++; _buff[i] = I2C_ReceiveData(I2Ci); num-=2; // receive a byte //usart_printfm(USART1,(const int *)"this byte read from accel: %2x\n\r",i); return 0; } while(num>0) // device may send less than requested (abnormal) if(num==3){ //_buff[i]=I2C_read_ack(I2Cx); timeout = I2C_TIMEOUT_MAX; while(I2C_GetFlagStatus(I2Ci,I2C_FLAG_BTF)!=SET){ ms_delay(100); if((timeout--)==0){ flag1 = I2Ci->SR1; flag2 = I2Ci->SR2; usart_printf(USARTx,"Flag1:%04x \n\r Flag2:%04x\n\r",flag1,flag2); usart_printf(USARTx,"Failing at reading 2 bytes stage2\n\r"); return -1; }} I2C_AcknowledgeConfig(I2Ci, DISABLE); _buff[i]=I2C_ReceiveData(I2Ci); i++; num--; while(I2C_GetFlagStatus(I2Ci,I2C_FLAG_BTF)!=SET) { ms_delay(100); if((timeout--)==0){ flag1 = I2Ci->SR1; flag2 = I2Ci->SR2; usart_printf(USARTx,"Flag1:%04x \n\r Flag2:%04x\n\r",flag1,flag2); usart_printf(USARTx,"Failing at reading 2 bytes stage2\n\r"); return -1; }} i2c_stopTransmission(I2Ci); _buff[i]=I2C_ReceiveData(I2Ci); i++; _buff[i]=I2C_ReceiveData(I2Ci); i++; num-=2; //usart_print(USART1,"This byte read from nack: %2x\n\r",i); return 0; } else{ a=I2C_read(I2Ci,_buff[i]); if(a<0) return -1; i++; num--; } // if(i != num){ // status = ADXL345_ERROR; // error_code = ADXL345_READ_ERROR; // } //ms_delay(10); //i2c_stopTransmission(I2Cx); // end transmission //end : // usart_printf(USARTx,"Error occured !! Restarting the interface!!"); // ms_delay(5000); // return -1; return 0; }
u8 updAcc(void){ return I2C_read(XM_addr,0x28|0x80,(u8 *)sens.AccRaw,6); }
u8 updMag(void){ return I2C_read(XM_addr,0x08|0x80,(u8 *)sens.MagRaw,6); }
u8 I2C1_read(u8 address, u8 *buffer, u8 length) { return I2C_read(I2C1, address, buffer, length); }
u8 updTemp(void){ u16 tmp,ret; ret=I2C_read(XM_addr,0x05|0x80,(u8 *)&tmp,2); sens.TempRaw=(float)(((s16)(tmp<<4))>>4)/8.0+21; return ret; }
void MMA8452_getData() { I2C_read(MMA8452_Address, 0x01, 6, (unsigned char *)&MMA8452_raw); }
int main() { CyGlobalIntEnable; UART_Start(); printf("Start\r\n"); /* //IR receiver// ---------------------------------------------------- unsigned int IR_val; for(;;) { IR_val = get_IR(); printf("%x\r\n\n",IR_val); } ///---------------------------------------------------------- */ /* //Ambient// ---------------------------------------------------- I2C_Start(); uint16 value =0; I2C_write(0x29,0x80,0x00); value = I2C_read(0x29,0x80); printf("%x ",value); I2C_write(0x29,0x80,0x03); value = I2C_read(0x29,0x80); printf("%x\r\n",value); value = I2C_read(0x29,0x81); printf("%x\r\n",value); for(;;) { uint8 Data0Low,Data0High,Data1Low,Data1High; Data0Low = I2C_read(0x29,CH0_L); Data0High = I2C_read(0x29,CH0_H); Data1Low = I2C_read(0x29,CH1_L); Data1High = I2C_read(0x29,CH1_H); uint8 CH0, CH1; CH0 = convert_raw(Data0Low,Data0High); CH1 = convert_raw(Data1Low,Data1High); // printf("%d %d %d %d\r\n",Data0Low,Data0High, Data1Low,Data1High); // printf("%d %d\r\n",CH0,CH1); // printf("%f\r\n",(float)CH1/CH0); double Ch0 = CH0; double Ch1 = CH1; double data = 0; data = getLux(Ch0,Ch1); printf("%lf\r\n",data); } ///---------------------------------------------------------- */ /* //nunchuk// ---------------------------------------------------- nunchuk_start(); nunchuk_init(); for(;;) { nunchuk_read(); } //----------------------------------------------------*/ //accelerometer// //-------------------------------------------------------------- I2C_Start(); uint8 X_L_A, X_H_A, Y_L_A, Y_H_A, Z_L_A, Z_H_A; int16 X_AXIS, Y_AXIS, Z_AXIS; I2C_write(ACCEL_MAG_ADDR, ACCEL_CTRL1_REG, 0x37); // set accelerometer & magnetometer into active mode I2C_write(ACCEL_MAG_ADDR, ACCEL_CTRL7_REG, 0x22); for(;;) { //print out accelerometer output X_L_A = I2C_read(ACCEL_MAG_ADDR, OUT_X_L_A); X_H_A = I2C_read(ACCEL_MAG_ADDR, OUT_X_H_A); X_AXIS = convert_raw(X_L_A, X_H_A); Y_L_A = I2C_read(ACCEL_MAG_ADDR, OUT_Y_L_A); Y_H_A = I2C_read(ACCEL_MAG_ADDR, OUT_Y_H_A); Y_AXIS = convert_raw(Y_L_A, Y_H_A); Z_L_A = I2C_read(ACCEL_MAG_ADDR, OUT_Z_L_A); Z_H_A = I2C_read(ACCEL_MAG_ADDR, OUT_Z_H_A); Z_AXIS = convert_raw(Z_L_A, Z_H_A); //printf("ACCEL: %d %d %d %d %d %d \r\n", X_L_A, X_H_A, Y_L_A, Y_H_A, Z_L_A, Z_H_A); value_convert_accel(X_AXIS, Y_AXIS, Z_AXIS); printf("\n"); CyDelay(50); } ///---------------------------------------------------------- /* //ultra// ---------------------------------------------------- ultra_isr_StartEx(ultra_isr_handler); // Ultra Sonic Interrupt Ultra_Start(); // Ultra Sonic Start function for(;;) { CyDelay(100); Trig_Write(1); // Trigger High CyDelayUs(10); // 10 micro seconds for trigger input signals Trig_Write(0); // Trigger Low } //----------------------------------------------------*/ /* //reflectance// ---------------------------------------------------- sensor_isr_StartEx(sensor_isr_handler); Refelctance_Start(); IR_led_Write(1); for(;;) { reflectance_period(); //print out each period of reflectance sensors reflectance_digital(); //print out 0 or 1 according to results of reflectance period CyDelay(500); } ///----------------------------------------------------*/ /* //motor// ---------------------------------------------------- motor_Start(); // motor start motor_forward(50,2000); // moving forward motor_turn(10,50,2000); // turn motor_turn(50,10,2000); // turn motor_backward(50,2000); // movinb backward motor_Stop(); // motor stop for(;;) { } ///----------------------------------------------------*/ /* //gyroscope// //----------------------------------------------------- I2C_Start(); uint8 X_AXIS_L, X_AXIS_H, Y_AXIS_L, Y_AXIS_H, Z_AXIS_L, Z_AXIS_H; int16 X_AXIS, Y_AXIS, Z_AXIS; I2C_write(GYRO_ADDR, GYRO_CTRL1_REG, 0x0F); // set gyroscope into active mode I2C_write(GYRO_ADDR, GYRO_CTRL4_REG, 0x30); // set full scale selection to 2000dps for(;;) { //print out gyroscope output X_AXIS_L = I2C_read(GYRO_ADDR, OUT_X_AXIS_L); X_AXIS_H = I2C_read(GYRO_ADDR, OUT_X_AXIS_H); X_AXIS = convert_raw(X_AXIS_H, X_AXIS_L); Y_AXIS_L = I2C_read(GYRO_ADDR, OUT_Y_AXIS_L); Y_AXIS_H = I2C_read(GYRO_ADDR, OUT_Y_AXIS_H); Y_AXIS = convert_raw(Y_AXIS_H, Y_AXIS_L); Z_AXIS_L = I2C_read(GYRO_ADDR, OUT_Z_AXIS_L); Z_AXIS_H = I2C_read(GYRO_ADDR, OUT_Z_AXIS_H); Z_AXIS = convert_raw(Z_AXIS_H, Z_AXIS_L); //printf("X_AXIS_L: %d, X_AXIS_H: %d, average: %d \r\n", X_AXIS_L, X_AXIS_H, (X_AXIS_H+X_AXIS_L)/2); //printf("Y_AXIS_L: %d, Y_AXIS_H: %d, average: %d \r\n", Y_AXIS_L, Y_AXIS_H, (Y_AXIS_H+Y_AXIS_L)/2); //printf("Z_AXIS_L: %d, Z_AXIS_H: %d, average: %d \r\n", Z_AXIS_L, Z_AXIS_H, (Z_AXIS_H+Z_AXIS_L)/2); //printf("H L : %d %d %d %d %d %d \r\n", X_AXIS_L, X_AXIS_H, Y_AXIS_L, Y_AXIS_H, Z_AXIS_L, Z_AXIS_H); //printf("%d %d %d \r\n", X_AXIS, Y_AXIS, Z_AXIS); printf("%d %d %d \r\n", value_convert_gyro(X_AXIS), value_convert_gyro(Y_AXIS), value_convert_gyro(Z_AXIS)); CyDelay(50); } ///-----------------------------------------------------------------*/ /* //magnetometer// //-------------------------------------------------------------- I2C_Start(); uint8 X_L_M, X_H_M, Y_L_M, Y_H_M, Z_L_M, Z_H_M; int16 X_AXIS, Y_AXIS, Z_AXIS; I2C_write(ACCEL_MAG_ADDR, ACCEL_CTRL1_REG, 0x37); // set accelerometer & magnetometer into active mode I2C_write(ACCEL_MAG_ADDR, ACCEL_CTRL5_REG, 0x10); // set a data rate of 50Hz I2C_write(ACCEL_MAG_ADDR, ACCEL_CTRL6_REG, 0x60); // set the full scale selection to +/- 12 Gauss I2C_write(ACCEL_MAG_ADDR, ACCEL_CTRL7_REG, 0x80); // set to continuous-conversion mode for(;;) { X_L_M = I2C_read(ACCEL_MAG_ADDR, OUT_X_L_M); X_H_M = I2C_read(ACCEL_MAG_ADDR, OUT_X_H_M); X_AXIS = convert_raw(X_L_M, X_H_M); Y_L_M = I2C_read(ACCEL_MAG_ADDR, OUT_Y_L_M); Y_H_M = I2C_read(ACCEL_MAG_ADDR, OUT_Y_H_M); Y_AXIS = convert_raw(Y_L_M, Y_H_M); Z_L_M = I2C_read(ACCEL_MAG_ADDR, OUT_Z_L_M); Z_H_M = I2C_read(ACCEL_MAG_ADDR, OUT_Z_H_M); Z_AXIS = convert_raw(Z_L_M, Z_H_M); heading(X_AXIS, Y_AXIS); // printf("MAGNET: %d %d %d %d %d %d \r\n", X_L_M, X_H_M, Y_L_M, Y_H_M, Z_L_M, Z_H_M); //printf("%d %d %d \r\n", X_AXIS,Y_AXIS, Z_AXIS); CyDelay(50); } ///----------------------------------------------------------*/ }
void getAngle(){ /* Reads gyroscope measurements by issuing command to read * from gyroscope register 0x1D. This register value will * automatically increment until a command is received to * end the read. Gyroscope measurements are expressed as * 16 bit signed integers and have 3 degrees of freedom * along the X, Y, and Z axes. Each 16 bit value is stored * in two registers thus a total of 6 reads are required * to receive data from all six registers(0x1D to 0x22). */ I2C_start(I2C_BASE, GYROSCOPE_ADDR, 0); I2C_write(I2C_BASE, 0x1D, 0); I2C_start(I2C_BASE, GYROSCOPE_ADDR, 1); gyro_x_high = I2C_read(I2C_BASE, 0); //Reads and stores the most significant byte for the X axis gyro_x_low = I2C_read(I2C_BASE, 0); //Reads and stores the least significant byte for the X axis gyro_y_high = I2C_read(I2C_BASE, 0); //Reads and stores the most significant byte for the Y axis gyro_y_low = I2C_read(I2C_BASE, 0); //Reads and stores the least significant byte for the Y axis gyro_z_high = I2C_read(I2C_BASE, 0); //Reads and stores the most significant byte for the Z axis gyro_z_low = I2C_read(I2C_BASE, 1); //Reads and stores the least significant byte for the Z axis /* Reads accelerometer measurements by issuing command to read * from accelerometer register 0x32. This register value will * automatically increment until a command is received to * end the read. Accelerometer measurements are expressed as * 16 bit signed integers and have 3 degrees of freedom * along the X, Y, and Z axes. Each 16 bit value is stored * in two registers thus a total of 6 reads are required * to receive data from all six registers(0x32 to 0x37). */ I2C_start(I2C_BASE, ACCELEROMETER_ADDR, 0); I2C_write(I2C_BASE, 0x32, 0); I2C_start(I2C_BASE, ACCELEROMETER_ADDR, 1); acc_x_low = I2C_read(I2C_BASE, 0); //Reads and stores the least significant byte for the X axis acc_x_high = I2C_read(I2C_BASE, 0); //Reads and stores the most significant byte for the X axis acc_y_low = I2C_read(I2C_BASE, 0); //Reads and stores the least significant byte for the Y axis acc_y_high = I2C_read(I2C_BASE, 0); //Reads and stores the most significant byte for the Y axis acc_z_low = I2C_read(I2C_BASE, 0); //Reads and stores the least significant byte for the Z axis acc_z_high = I2C_read(I2C_BASE, 1); //Reads and stores the most significant byte for the Z axis /* Performs bit shift operation to combine higher and lower * bytes of X, Y, and Z angles into a single variable. * The MSB is shifted 8 bits to the left and the LSB * is moved into the lower 8 bits. */ gyro_x_high <<= 8; gyro_x_high |= gyro_x_low; // gyro_x_high -= GYRO_X_OFFSET; gyro_y_high <<= 8; gyro_y_high |= gyro_y_low; // gyro_y_high += GYRO_Y_OFFSET; gyro_z_high <<= 8; gyro_z_high |= gyro_z_low; // gyro_z_high += GYRO_Z_OFFSET; acc_x_high <<= 8; acc_x_high |= acc_x_low; // acc_x_high += ACCEL_X_OFFSET; acc_y_high <<= 8; acc_y_high |= acc_y_low; // acc_y_high -= ACCEL_Y_OFFSET; acc_z_high <<= 8; acc_z_high |= acc_z_low; // acc_z_high += ACCEL_Z_OFFSET; // printf("Acc X:%d Y:%d Z:%d || Gyro X:%d Y:%d Z:%d\n", // acc_x_high, acc_y_high, acc_z_high, gyro_x_high, gyro_y_high, gyro_z_high); /* Calculates the current angle as measured by the accelerometer * and converts the angle from radians to degrees. * This is done using trigonometry, the atan function, to get the * principal value of the tangent of Y and Z. This value is then * converted to degrees to simplify further calculations. */ accXAngle = (float) (atan2(acc_y_high, acc_z_high)+M_PI)*RAD_TO_DEG; accYAngle = (float) (atan2(acc_x_high, acc_z_high)+M_PI)*RAD_TO_DEG; accXAngle -= ACCEL_X_OFFSET; accYAngle += ACCEL_Y_OFFSET; //Change the rotation value of the accelerometer to -/+ 180 if (accXAngle > 180) { accXAngle -= (float)360.0; } if (accYAngle > 180) { accYAngle -= (float)360.0; } /* Calculates the robot's current rotation in degrees per second * This is done by multiplying the raw data acquired from the * X axis of the gyroscope by the sensitivity scale factor */ rate_gyr_x = (float) gyro_x_high*G_GAIN; rate_gyr_y = (float) gyro_y_high*G_GAIN; /* Calculate the robot's current angle as measured by the gyroscope. * This is done by multiplying the loop period by the rotation rate * in the X axis. */ gyroXAngle = rate_gyr_x*DT; gyroYAngle = rate_gyr_y*DT; /* Combines the two angular measurements by the accelerometer * and the gyroscope into a single value to reduce the effects * of drift on the gyroscope and noise in the accelerometer. * A complementary filter is used which trusts the gyroscope * for short periods of time and the accelerometer for longer * periods of time. */ CFangleX = 0.98*(CFangleX+gyroXAngle)+(0.02)*accXAngle; CFangleY = 0.98*(CFangleY+gyroYAngle)+(0.02)*accYAngle; //printf("The angle is: %f %f %f\n", CFangleY, gyroYAngle, accYAngle); }
void instrCall_I2C(char instruction, char* datain, char datainLength) { switch (instruction) { case I2C_WRITE: { I2C_error = I2C_write(datain[0], &datain[1]); break; } case I2C_READ: { I2C_read(datain[0], datain[1], &datain[2]); setReplyInherentData(S_I2C, I2C_READ, datain[1], 0x00); break; } case I2C_SETUP: { I2C_error = I2C_setup(); break; } case I2C_SEND_START: { I2C_error = ERROR_I2C_NOERROR; I2C_SCL_LO; QDEL; I2C_SDL_HI; QDEL; I2C_SCL_HI; QDEL; //ensure the lines are in the correct state before issuing the start I2C_START; break; } case I2C_SEND_STOP: { I2C_STOP; break; } case I2C_SEND_BYTES: { char ack = 0; // send the data char writeDataLength = datain[0]; char* writeData = &datain[1]; while (writeDataLength--) ack |= i2cPutbyte(*writeData++); I2C_SDL_LO; // clear data line if (ack == 0) //all bytes acked I2C_error = ERROR_I2C_NOERROR; else I2C_error = ERROR_I2C_NOACK; break; } case I2C_READ_BYTES: { char readDataLength = datain[0]; char j = readDataLength; while (j--) buffer_REPLY[IN_REPORT_HEADER_LENGTH + readDataLength - 1 - j] = i2cGetbyte(j == 0); I2C_SDL_HI; // clear data line and setReplyInherentData(S_I2C, I2C_READ_BYTES, readDataLength, 0x00); break; } case I2C_CHECK_ERROR: { setReply(S_I2C, I2C_CHECK_ERROR, 1, 0x00, &I2C_error); I2C_error = ERROR_I2C_NOERROR; //reset the error code break; } case I2C_BUS_SCAN: { I2C_busScan(datain[0], datain[1], datain[2]); break; } } }