底层通信用了昨天写好的iic,今天结合官方资料成功读出所有指定寄存器的数据附上源码
include.h主要包括了一些stm32 IO控制的宏定义,具体参考正点原子所有例程中都有的sys.h头文件
inc
#ifndef __PX4FLOW_H #define __PX4FLOW_H #include "include.h" //器件地址 #define PX4FLOW_ADDR 0x42 //寄存器宏定义,如果是2字节一个数据地址指的低字节位,高字节是低字节地址后一位,组合起来就是完整的数据 解释摘自官网 //不积分的数据 //记录总的创建的iic帧数 #define FRAME_COUNT_SUM 0x00 //uint16_t counts created I2C frames [frames] //X轴最新一帧所有像素移动和*10 #define PIXEL_FLOW_X_SUM 0x02 //int16_t latest x flow measurement in pixels*10 [pixels] //Y轴最新一帧所有像素移动和*10 #define PIXEL_FLOW_Y_SUM 0x04 //int16_t latest y flow measurement in pixels*10 [pixels] //X轴速度 #define FLOW_COMP_M_X 0x06 //int16_t x velocity*1000 [meters/sec] //Y轴速度 #define FLOW_COMP_M_Y 0x08 //int16_t y velocity*1000 [meters/sec] //光流图像质量 #define QUAL 0x0a //int16_t Optical flow quality / confidence [0: bad, 255: maximum quality] //X轴陀螺仪速度 #define GYRO_X_RATE 0x0c //int16_t latest gyro x rate [rad/sec] //Y轴陀螺仪速度 #define GYRO_Y_RATE 0x0e //int16_t latest gyro y rate [rad/sec] //Z轴陀螺仪速度 #define GYRO_Z_RATE 0x10 //int16_t latest gyro z rate [rad/sec] //陀螺仪数据范围 #define GYRO_RANGE 0x12 //uint8_t gyro range [0 .. 7] equals [50 deg/sec .. 2000 deg/sec] //超声波数据上次更新时间到现在的时间间隔 #define SONAR_TIMESTAMP1 0x13 //uint8_t time since last sonar update [milliseconds] //地面距离 正值:已知距离, 负值:未知距离 #define GROUND_DISTANCE1 0x14 //int16_t Ground distance in meters*1000 [meters]. Positive value: distance known. //Negative value: Unknown distance //积分后的数据地址 //上次读取数据后数据更新了多少次 #define FRAME_COUNT_SINCE_LAST_READOUT 0x16 //uint16_t number of flow measurements since last I2C readout [frames] //自上次读取iic数据后 X轴速度积分后所得值 #define PIXEL_FLOW_X_INTEGRAL 0x18 //int16_t accumulated flow in radians*10000 around x axis since last I2C readout [rad*10000] //自上次读取iic数据后 Y轴速度积分后所得值 #define PIXEL_FLOW_Y_INTEGRAL 0x1a //int16_t accumulated flow in radians*10000 around y axis since last I2C readout [rad*10000] //自上次读取iic数据后,X轴角速度积分值 #define GYRO_X_RATE_INTEGRAL 0x1c //int16_t accumulated gyro x rates in radians*10000 since last I2C readout [rad*10000] //自上次读取iic数据后,Y轴角速度积分值 #define GYRO_Y_RATE_INTEGRAL 0x1e //int16_t accumulated gyro y rates in radians*10000 since last I2C readout [rad*10000] //自上次读取iic数据后,Z轴角速度积分值 #define GYRO_Z_RATE_INTEGRAL 0x20 //int16_t accumulated gyro z rates in radians*10000 since last I2C readout [rad*10000] //上次和这次读取iic数据的时间间隔 #define INTEGRATION_TIMESPAN 0x22 //uint32_t accumulation timespan in microseconds since last I2C readout [microseconds] //超声波数据上次更新时间到现在的时间间隔 #define SONAR_TIMESTAMP2 0x26 //uint32_t time since last sonar update [microseconds] //地面距离 #define GROUND_DISTANCE2 0x2a //int16_t Ground distance in meters*1000 [meters*1000] //陀螺仪温度 #define GYRO_TEMPERATURE 0x2c //int16_t Temperature * 100 in centi-degrees Celsius [degcelsius*100] //光流积分数据质量 #define QUALITY 0x2e //uint8_t averaged quality of accumulated flow values [0:bad quality;255: max quality] //读指定寄存器指定字节数数据 u8 flow_read_data(u8 addr,u8 reg,u8 len,u8 *buf); //读8位无符号数据 uint8_t readu8_date(u8 addr,u8 reg); //读16位无符号数据 uint16_t readu16_date(u8 addr,u8 reg); //读16位有符号数据 int16_t reads16_date(u8 addr,u8 reg); //读32位无符号数据 uint32_t readu32_date(u8 addr,u8 reg); #endif
src
#include "px4flow.h" #include "include.h" #include "myiic.h" //读指定寄存器指定字节数数据 u8 flow_read_data(u8 addr,u8 reg,u8 len,u8 *buf) { IIC_Start(); IIC_Send_Byte((addr<<1)|0);//发送器件地址+写命令 if(IIC_Wait_Ack()) //等待应答 { IIC_Stop(); return 1; } IIC_Send_Byte(reg); //写寄存器地址 IIC_Wait_Ack(); //等待应答 IIC_Start(); IIC_Send_Byte((addr<<1)|1);//发送器件地址+读命令 IIC_Wait_Ack(); //等待应答 while(len) { if(len==1)*buf=IIC_Read_Byte(0);//读数据,发送nACK else *buf=IIC_Read_Byte(1); //读数据,发送ACK len--; buf++; } IIC_Stop(); //产生一个停止条件 return 0; } //读8位无符号数据 uint8_t readu8_date(u8 addr,u8 reg) { u8 buff[1]; uint8_t date; flow_read_data(addr,reg,1,buff); date = buff[0]; return date; } //读16位无符号数据 uint16_t readu16_date(u8 addr,u8 reg) { u8 buff[2]; uint16_t date; flow_read_data(addr,reg,2,buff); date = buff[1] << 8 | buff[0]; return date; } //读16位有符号数据 int16_t reads16_date(u8 addr,u8 reg) { u8 buff[2]; int16_t date; flow_read_data(addr,reg,2,buff); date = buff[1] << 8 | buff[0]; return date; } //读32位无符号数据 uint32_t readu32_date(u8 addr,u8 reg) { u8 buff[4]; int16_t date; flow_read_data(addr,reg,2,buff); date = buff[3] << 24 | buff[2] << 16 | buff[1] << 8 | buff[0]; return date; }
时间: 2024-10-19 14:29:30