[2024-feb-29] Sad news: Eric Layton aka Nocturnal Slacker aka vtel57 passed away on Feb 26th, shortly after hospitalization. He was one of our Wiki's most prominent admins. He will be missed.
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
interfacing_i2c_devices [2014/03/22 22:10 (UTC)] – [Communicating With An I2C Device] louigi600 | howtos:hardware:arm:interfacing_i2c_devices [2014/12/29 13:24 (UTC)] – interfacing_i2c_devices renamed to howtos:hardware:arm:interfacing_i2c_devices (This was misplaced in the Wiki namespace root) alienbob | ||
---|---|---|---|
Line 151: | Line 151: | ||
| | ||
#define I2C_DEVICE "/ | #define I2C_DEVICE "/ | ||
- | #define I2C_DEV_ADDR 0x69 | ||
- | #define I2C_DEV_SELF 0x0 | ||
- | #define I2C_DEV_INT 0x1a | ||
- | #define I2C_DEV_REG_START_ADDR 0x1b | ||
- | #define I2C_DEV_REG_END_ADDR 0x22 | ||
| | ||
- | # | + | |
- | # | + | |
- | # | + | #define ITG3200_SELF 0x0 |
+ | #define ITG3200_INT 0x1a | ||
+ | #define ITG3200_TH 0x1b /*2 bytes Hight byte and Low byte*/ | ||
+ | #define ITG3200_TL 0x1c | ||
+ | #define ITG3200_XRH 0x1d /*2 byte Hight byte and Low byte*/ | ||
+ | #define ITG3200_XRL 0x1e | ||
+ | #define ITG3200_YRH 0x1f /*2 byte Hight byte and Low byte*/ | ||
+ | #define ITG3200_YRL 0x20 | ||
+ | #define ITG3200_ZRH 0x21 /*2 byte Hight byte and Low byte*/ | ||
+ | #define ITG3200_ZRL 0x22 /*2 byte Hight byte and Low byte*/ | ||
+ | #define ITG3200_TEMP_RAW_OFFSET | ||
+ | # | ||
+ | # | ||
+ | #define ITG3200_ROT_RAW_SENSITIVITY 14.375 | ||
| | ||
- | | + | |
+ | { int retval; | ||
+ | if( twoscomplimentdata > 32768 ) retval = twoscomplimentdata - 65536; | ||
+ | else retval = twoscomplimentdata; | ||
+ | return retval; | ||
+ | } | ||
+ | |||
+ | float ITG3200_rot_conv(int rawdata) | ||
+ | { float retval; | ||
+ | int raw; | ||
+ | |||
+ | raw=twosc2int(rawdata); | ||
+ | retval = (float)raw / (float)ITG3200_ROT_RAW_SENSITIVITY; | ||
+ | return retval; | ||
+ | } | ||
+ | |||
+ | float ITG3200_temp_conv(int rawdata) | ||
+ | { float retval; | ||
+ | int raw; | ||
+ | |||
+ | raw=twosc2int(rawdata); | ||
+ | retval = (float)ITG3200_TEMP_OFFSET + (((float)raw + ITG3200_TEMP_RAW_OFFSET) / ITG3200_TEMP_RAW_SENSITIVITY); | ||
+ | return retval; | ||
+ | } | ||
| | ||
- | void read_registers | + | void ITG3200_read |
- | { char buf[256] = {0}; | + | { __s32 res; |
int i,j,k; | int i,j,k; | ||
| | ||
- | /*For some unexpected reason I'm getting responses from the other I2C devices | + | for(i=0; |
- | on the same bus so I'm ignoring data that does not match the WHO AM I reg | + | |
- | and also data that has not set the interrupt register | + | for (j=0;j<2;j++) |
- | not really avalible) | + | { |
- | */ | + | |
- | | + | |
- | { if (read(file,buf,sizeof(buf)) != sizeof(buf)) | + | exit(1); |
- | { /* ERROR HANDLING: i2c transaction failed */ | + | } |
- | printf(" | + | if (j == 0) k=(int)res << 8; |
- | exit(1); | + | else |
+ | { k += (int)res; | ||
+ | *(raw + (i/2))=k; | ||
+ | } | ||
} | } | ||
- | } | ||
- | | ||
- | j=0; | ||
- | for(i=I2C_DEV_REG_START_ADDR; | ||
- | { k= (buf[i] << 8) + buf[i+1]; | ||
- | if ( k > 32768 ) *(raw+j)= k - 65536; | ||
- | else *(raw+j)=k; | ||
i++; | i++; | ||
- | j++; | + | |
- | | + | |
} | } | ||
| | ||
main () | main () | ||
- | { int file,buffer; | + | { int file; |
int i,j,k; | int i,j,k; | ||
- | | + | float data[4]={0}; |
- | | + | |
- | char buf[256] = {0}; | + | |
| | ||
- | if ((file = open(I2C_DEVICE, | + | |
- | { /* ERROR HANDLING: you can check errno to see what went wrong */ | + | ITG3200_YRH, |
- | | + | int ITG3200_RAW_DATA[4]; |
+ | float ITG3200_DATA[4]; | ||
+ | |||
+ | | ||
+ | { perror(" | ||
exit(1); | exit(1); | ||
- | } | + | } |
| | ||
- | if (ioctl(file, | + | if (ioctl(file, |
{ printf(" | { printf(" | ||
- | /* ERROR HANDLING; you can check errno to see what went wrong */ | ||
exit(1); | exit(1); | ||
} | } | ||
| | ||
- | | + | /*Take an avarage over 10 consecuitve readings on the ITG3200*/ |
- | { read_registers(file,&raw_data[0]); | + | |
- | tdata[i]=TEMP_OFFSET + (((float)raw_data[0] + TEMP_RAW_OFFSET) / TEMP_RAW_SENSITIVITY); | + | { ITG3200_read(file,&ITG3200_RAW_DATA[0],& |
- | rxdata[i]=(float)raw_data[1]/ ROT_RAW_SENSITIVITY; | + | |
- | rydata[i]=(float)raw_data[2]/ ROT_RAW_SENSITIVITY; | + | |
- | rzdata[i]=(float)raw_data[3]/ | + | |
- | } | + | |
- | close(file); | + | |
| | ||
- | for(i=0; | + | |
- | { data[0]=data[0] + tdata[i]; | + | data[1] |
- | data[1]=data[1] + rxdata[i]; | + | data[2] |
- | data[2]=data[2] + rydata[i]; | + | data[3] |
- | data[3]=data[3] + rzdata[i]; | + | |
} | } | ||
- | for(i=0; | + | for(i=0; |
| | ||
printf(" | printf(" | ||
Line 230: | Line 251: | ||
printf(" | printf(" | ||
printf(" | printf(" | ||
+ | | ||
+ | close(file); | ||
} | } | ||
+ | |||
+ | |||
====== Voltage Level Shifting ====== | ====== Voltage Level Shifting ====== | ||
You may end up with heterogeneous voltage level devices and if you have many devices the correct way to work around this problem is by using bidirectional I2C voltage level shifters like the [[ http:// | You may end up with heterogeneous voltage level devices and if you have many devices the correct way to work around this problem is by using bidirectional I2C voltage level shifters like the [[ http:// |