1.SPI简介 
SPI 全称是 Serial Perripheral Interface,也就是串行外围设备接口。SPI 是 Motorola 公司推出的一种同步串行接口 技术,是一种高速、全双工的同步通信总线,SPI 时钟频率相比 I2C 要高很多,最高可以工作 在上百 MHz。SPI 以主从方式工作,通常是有一个主设备和一个或多个从设备。设备连接示意图如下: 
 
SPI的工作模式有四种,由CPOL和CPHA控制,CPOL位空闲是时钟的电平。CPHA为时钟的第一个跳变沿采集数据还是第二个跳变沿采集数据。SPI通讯时序图如下所示: 
 
2.ICM-20607简介 
ICM-20607 是 InvenSense 出品的一款 6 轴 MEMS 传感器,包括 3 轴加速度和 3 轴陀螺仪。 ICM-20608 尺寸非常小,只有 3x3x0.75mm,采用 16P 的 LGA 封装。ICM-20608 内部有一个 512 字节的 FIFO。陀螺仪的量程范围可以编程设置,可选择±250,±500,±1000 和±2000°/s, 加速度的量程范围也可以编程设置,可选择±2g,±4g,±8g 和±16g。陀螺仪和加速度计都 是 16 位的 ADC,并且支持 I2C 和 SPI 两种协议,使用 I2C 接口的话通信速度最高可以达到400KHz,使用 SPI 接口的话通信速度最高可达到 8MHz: 硬件原理图,引脚没有引出来,就只进行这个ICM-20607的验证,不然还可以进行拓展,如一些SPI驱动的屏幕: 3.代码设计 读寄存器: 写寄存器: 读数据: 主函数: - #include "stdio.h"
 
 - #include "unistd.h"
 
 - #include "sys/types.h"
 
 - #include "sys/stat.h"
 
 - #include "sys/ioctl.h"
 
 - #include "fcntl.h"
 
 - #include "stdlib.h"
 
 - #include "string.h"
 
 - #include <poll.h>
 
 - #include <sys/select.h>
 
 - #include <sys/time.h>
 
 - #include <signal.h>
 
 - #include <fcntl.h>
 
  
- #define ICM20607_DEV "/dev/icm20607"
 
  
- int main(int argc, char *argv[])
 
 - {
 
 -         int fd;
 
 -         signed int databuf[7];
 
 -         unsigned char data[14];
 
 -         signed int gyro_x_adc, gyro_y_adc, gyro_z_adc;
 
 -         signed int accel_x_adc, accel_y_adc, accel_z_adc;
 
 -         signed int temp_adc;
 
 -  
 
 -         float gyro_x_act, gyro_y_act, gyro_z_act;
 
 -         float accel_x_act, accel_y_act, accel_z_act;
 
 -         float temp_act;
 
 -  
 
 -         int ret = 0;
 
 -  
 
 -         fd = open(ICM20607_DEV, O_RDWR);
 
 -         if(fd < 0) {
 
 -                 printf("can't open file %s\r\n", ICM20607_DEV);
 
 -                 return -1;
 
 -         }
 
 -  
 
 -         while (1) {
 
 -                 ret = read(fd, databuf, sizeof(databuf));
 
 -                 if(ret == 0) {                         /* ?????? */
 
 -                         gyro_x_adc = databuf[0];
 
 -                         gyro_y_adc = databuf[1];
 
 -                         gyro_z_adc = databuf[2];
 
 -                         accel_x_adc = databuf[3];
 
 -                         accel_y_adc = databuf[4];
 
 -                         accel_z_adc = databuf[5];
 
 -                         temp_adc = databuf[6];
 
 -  
 
 -                         /* ????? */
 
 -                         gyro_x_act = (float)(gyro_x_adc)  / 16.4;
 
 -                         gyro_y_act = (float)(gyro_y_adc)  / 16.4;
 
 -                         gyro_z_act = (float)(gyro_z_adc)  / 16.4;
 
 -                         accel_x_act = (float)(accel_x_adc) / 2048;
 
 -                         accel_y_act = (float)(accel_y_adc) / 2048;
 
 -                         accel_z_act = (float)(accel_z_adc) / 2048;
 
 -                         temp_act = ((float)(temp_adc) - 25 ) / 326.8 + 25;
 
 -  
 
 -  
 
 -                         printf("\r\n");
 
 -                         printf("raw value:\r\n");
 
 -                         printf("gx = %d, gy = %d, gz = %d\r\n", gyro_x_adc, gyro_y_adc, gyro_z_adc);
 
 -                         printf("ax = %d, ay = %d, az = %d\r\n", accel_x_adc, accel_y_adc, accel_z_adc);
 
 -                         printf("temp = %d\r\n", temp_adc);
 
  
-                         printf("\r\n");
 
 -                         printf("act value:\r\n");
 
 -                         printf("act gx = %.2f度/S, act gy = %.2f度/S, act gz = %.2f度/S\r\n", gyro_x_act, gyro_y_act, gyro_z_act);
 
 -                         printf("act ax = %.2fg, act ay = %.2fg, act az = %.2fg\r\n", accel_x_act, accel_y_act, accel_z_act);
 
 -                         printf("act temp = %.2f摄氏度\r\n", temp_act);
 
 -                 }
 
 -                 sleep(1); /*1s */
 
 -         }
 
 -         close(fd);        
 
 -         return 0;
 
 - }
 
 
  复制代码4.验证 编译代码生产可执行文件,拖入开发板,添加可执行权限,运行代码: 
 
 |