NOT FINISHED YET!!! My first try to get a self built fully working Quadrocopter based on an mbed, a self built frame and some other more or less cheap parts.
I2C_Sensor.cpp
00001 #include "I2C_Sensor.h" 00002 00003 // calculate the 8-Bit write/read I2C-Address from the 7-Bit adress of the device 00004 #define GET_I2C_WRITE_ADDRESS(ADR) (ADR << 1&0xFE) // ADR & 1111 1110 00005 #define GET_I2C_READ_ADDRESS(ADR) (ADR << 1|0x01) // ADR | 0000 0001 00006 00007 I2C_Sensor::I2C_Sensor(PinName sda, PinName scl, char i2c_address) : i2c_init(sda, scl), i2c(sda, scl), local("local") 00008 { 00009 I2C_Sensor::i2c_address = i2c_address; 00010 i2c_init.frequency(400000); // standard speed 00011 i2c.frequency(400000); // standard speed 00012 //i2c.frequency(1500000); // ultrafast! 00013 } 00014 00015 void I2C_Sensor::saveCalibrationValues(float values[], int size, char * filename) 00016 { 00017 FILE *fp = fopen(strcat("/local/", filename), "w"); 00018 for(int i = 0; i < size; i++) 00019 fprintf(fp, "%f\r\n", values[i]); 00020 fclose(fp); 00021 } 00022 00023 void I2C_Sensor::loadCalibrationValues(float values[], int size, char * filename) 00024 { 00025 FILE *fp = fopen(strcat("/local/", filename), "r"); 00026 for(int i = 0; i < size; i++) 00027 fscanf(fp, "%f", &values[i]); 00028 fclose(fp); 00029 } 00030 00031 //-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 00032 // ATTENTION!!! there was a problem with other interrupts disturbing the i2c communication of the chip... that's why I use I2C to initialise the sonsors and MODI2C to get the data (only made with readMultiRegister) 00033 // IT DIDN'T WORK STABLE IN OTHER COMBINATIONS (if someone has an idea why please let me know) 00034 //-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 00035 00036 char I2C_Sensor::readRegister(char reg) 00037 { 00038 char value = 0; 00039 00040 i2c_init.write(i2c_address, ®, 1); 00041 i2c_init.read(i2c_address, &value, 1); 00042 00043 return value; 00044 } 00045 00046 void I2C_Sensor::writeRegister(char reg, char data) 00047 { 00048 char buffer[2] = {reg, data}; 00049 i2c_init.write(i2c_address, buffer, 2); 00050 } 00051 00052 void I2C_Sensor::readMultiRegister(char reg, char* output, int size) 00053 { 00054 i2c.write (i2c_address, ®, 1); // tell register address of the MSB get the sensor to do slave-transmit subaddress updating. 00055 i2c.read (i2c_address, output, size); // tell it where to store the data read 00056 }
Generated on Tue Jul 12 2022 20:54:01 by 1.7.2