LOFT-Q 读取 G-Sensor 数据(2)
时间:10-02
整理:3721RD
点击:
1) 输出温度计:
2)输出 3轴加速度 X Y Z 轴的数值
- #include<stdio.h>
- #include<fcntl.h>
- #include<linux/i2c-dev.h>
- #include<linux/i2c.h>
- #include<errno.h>
- #define BMA250_ADDR 0x18
2)输出 3轴加速度 X Y Z 轴的数值
- #include<stdio.h>
- #include<fcntl.h>
- #include<linux/i2c-dev.h>
- #include<linux/i2c.h>
- #include<errno.h>
- #define BMA250_ADDR 0x18
- int main(void)
- {
- int fd;
- unsigned int buf;
- unsigned char i;
- unsigned int acc_x, acc_y, acc_z;
- fd = open( "/dev/i2c-1", O_RDWR );
- if( fd < 0 )
- {
- printf("err: open file: %s\r\n", strerror( errno ) );
- return 1;
- }
- if( ioctl( fd, I2C_SLAVE, BMA250_ADDR ) < 0 )
- {
- printf("err: ioctl error : %s\r\n", strerror(errno) );
- return 1;
- }
- buf = 0x08;
- if( write(fd,&buf,1) < 0 )
- {
- printf("write 0x10 err \r\n");
- }
- while(1)
- {
- buf = 0x02;
- write( fd, &buf, 1 );
- read( fd, &buf, 1 );
- acc_x = buf;
- acc_x >>= 6;
- buf = 0x03;
- write( fd, &buf, 1 );
- read( fd, &buf, 1 );
- acc_x = acc_x + ( buf << 2 ) & 0xFFB;
- buf = 0x04;
- write( fd, &buf, 1 );
- read( fd, &buf, 1 );
- acc_y = buf;
- acc_y >>= 6 ;
- buf = 0x05;
- write( fd, &buf, 1 );
- read( fd, &buf, 1 );
- acc_y = acc_y + ( buf << 2 ) & 0xFFB;
- buf = 0x06;
- write( fd, &buf, 1 );
- read( fd, &buf, 1 );
- acc_z = buf;
- acc_z >>= 6;
- buf = 0x07;
- write( fd, &buf, 1 );
- read( fd, &buf, 1 );
- acc_z = acc_z + ( buf << 2 ) & 0xFFB;
- printf( "acc_x: %d | acc_y: %d | acc_z: %d \r", acc_x, acc_y, acc_z );
- fflush(stdout);
- usleep( 1000000 );
- }
- }
上面发的代码 不知为何缺失. 重发
- #include<stdio.h>
- #include<fcntl.h>
- #include<linux/i2c-dev.h>
- #include<linux/i2c.h>
- #include<errno.h>
- #define BMA250_ADDR 0x18
- int main(void)
- {
- int fd;
- unsigned char buf,i;
- fd = open( "/dev/i2c-1", O_RDWR );
- if( fd < 0 )
- {
- printf("err: open file: %s\r\n", strerror( errno ) );
- return 1;
- }
- if( ioctl( fd, I2C_SLAVE, BMA250_ADDR ) < 0 )
- {
- printf("err: ioctl error : %s\r\n", strerror(errno) );
- return 1;
- }
- buf = 0x08;
- if( write(fd,&buf,1) < 0 )
- {
- printf("write 0x10 err \r\n");
- }
- while(1)
- {
- buf = 0x08;
- write(fd,&buf,1);
- if( read(fd, &buf, 1))
- {
- printf("current temperature[");
- for( i = 0; i < 50; i++ )
- {
- if(i<buf)
- printf("=");
- else if( i == buf )
- printf(">");
- else
- printf(" ");
- }
- printf("]%d C\r",buf);
- }
- else
- {
- printf("error: %d\r\n",buf);
- }
- fflush(stdout);
- usleep( 1000000 );
- }
- }