[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 revision | Next revisionBoth sides next revision | ||
interfacing_i2c_devices [2014/04/03 21:25 (UTC)] – [Communicating With An I2C Device] louigi600 | interfacing_i2c_devices [2014/04/04 09:23 (UTC)] – [Communicating With An I2C Device] louigi600 | ||
---|---|---|---|
Line 139: | Line 139: | ||
I hate showing my poor C programming capabilities but here's some code that uses i2c-dev to read stuff from the ITG3200 and takes an average over 10 readings: | I hate showing my poor C programming capabilities but here's some code that uses i2c-dev to read stuff from the ITG3200 and takes an average over 10 readings: | ||
- | |||
- | #include < | ||
- | #include < | ||
- | #include < | ||
- | #include < | ||
- | #include < | ||
- | #include < | ||
- | #include < | ||
- | #include < | ||
- | #include < | ||
- | | ||
- | #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 TEMP_RAW_OFFSET 13200 | ||
- | #define TEMP_RAW_SENSITIVITY 280 | ||
- | #define TEMP_OFFSET 35 | ||
- | | ||
- | #define ROT_RAW_SENSITIVITY 14.375 | ||
- | | ||
- | void read_registers (int file, int *raw) | ||
- | { char buf[256] = {0}; | ||
- | int i,j,k; | ||
- | | ||
- | /*For some unexpected reason I'm getting responses from the other I2C devices | ||
- | 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 (meaning that data is | ||
- | not really avalible) | ||
- | */ | ||
- | while (buf[0] != 0x69 || buf[26] == 0) | ||
- | { if (read(file, | ||
- | { /* ERROR HANDLING: i2c transaction failed */ | ||
- | printf(" | ||
- | exit(1); | ||
- | } | ||
- | } | ||
- | | ||
- | 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++; | ||
- | j++; | ||
- | } | ||
- | } | ||
- | | ||
- | main () | ||
- | { int file, | ||
- | int i,j,k; | ||
- | int raw_data[4]; | ||
- | float data[4], | ||
- | char buf[256] = {0}; | ||
- | | ||
- | if ((file = open(I2C_DEVICE, | ||
- | { /* ERROR HANDLING: you can check errno to see what went wrong */ | ||
- | perror(" | ||
- | exit(1); | ||
- | } | ||
- | | ||
- | if (ioctl(file, | ||
- | { printf(" | ||
- | /* ERROR HANDLING; you can check errno to see what went wrong */ | ||
- | exit(1); | ||
- | } | ||
- | | ||
- | for(i=0; | ||
- | { read_registers(file,& | ||
- | tdata[i]=TEMP_OFFSET + (((float)raw_data[0] + TEMP_RAW_OFFSET) / TEMP_RAW_SENSITIVITY); | ||
- | rxdata[i]=(float)raw_data[1]/ | ||
- | rydata[i]=(float)raw_data[2]/ | ||
- | rzdata[i]=(float)raw_data[3]/ | ||
- | } | ||
- | close(file); | ||
- | | ||
- | for(i=0; | ||
- | { data[0]=data[0] + tdata[i]; | ||
- | data[1]=data[1] + rxdata[i]; | ||
- | data[2]=data[2] + rydata[i]; | ||
- | data[3]=data[3] + rzdata[i]; | ||
- | } | ||
- | for(i=0; | ||
- | | ||
- | printf(" | ||
- | printf(" | ||
- | printf(" | ||
- | printf(" | ||
- | } | ||
- | |||
- | With some help from LQ forum I realized that it's a bad idea to bulk dump the whole register set, instaad it's better to probe each register separately. The code that follows does not yet make the avarage like the one above bit it takes 10 consecutive readings without having to skip any data. | ||
#include < | #include < | ||
Line 263: | Line 169: | ||
#define ITG3200_ROT_RAW_SENSITIVITY 14.375 | #define ITG3200_ROT_RAW_SENSITIVITY 14.375 | ||
| | ||
- | void read_itg3200 | + | |
+ | { 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; | ||
+ | } | ||
+ | |||
+ | | ||
{ __s32 res; | { __s32 res; | ||
int i,j,k; | int i,j,k; | ||
Line 269: | Line 200: | ||
for(i=0; | for(i=0; | ||
{ k=0; | { k=0; | ||
- | printf(" | ||
for (j=0; | for (j=0; | ||
{ | { | ||
Line 279: | Line 209: | ||
else | else | ||
{ k += (int)res; | { k += (int)res; | ||
- | | + | *(raw + (i/2))=k; |
- | | + | |
} | } | ||
} | } | ||
Line 288: | Line 217: | ||
| | ||
main () | main () | ||
- | { int file,buffer; | + | { int file; |
int i,j,k; | int i,j,k; | ||
- | float data[4], | + | float data[4]={0}; |
- | char buf[256] = {0}; | + | |
| | ||
int ITG3200_REGS[8]={ITG3200_TH, | int ITG3200_REGS[8]={ITG3200_TH, | ||
Line 308: | Line 236: | ||
} | } | ||
| | ||
+ | /*Take an avarage over 10 consecuitve readings on the ITG3200*/ | ||
for (i=0; | for (i=0; | ||
- | { read_itg3200(file,& | + | { ITG3200_read(file,& |
- | | + | |
+ | | ||
+ | data[1] += ITG3200_rot_conv(ITG3200_RAW_DATA[1]); | ||
+ | data[2] += ITG3200_rot_conv(ITG3200_RAW_DATA[2]); | ||
+ | data[3] += ITG3200_rot_conv(ITG3200_RAW_DATA[3]); | ||
} | } | ||
+ | for(i=0; | ||
+ | | ||
+ | printf(" | ||
+ | printf(" | ||
+ | printf(" | ||
+ | printf(" | ||
| | ||
- | for (i=0; | ||
- | { printf(" | ||
- | } | ||
- | printf(" | ||
close(file); | close(file); | ||
} | } | ||
+ | |||
====== Voltage Level Shifting ====== | ====== Voltage Level Shifting ====== |