/**
www.st.com/SLA0044
*/
/* Includes ------------------------------------------------------------------/
#include “JY61P.h”
#include “stdio.h”
#include
/
#define MPU_ADDR 0x50<<1
#define MPU_ADDR_Read (0x50<<1)|0x01
/ USER CODE END PFP */
/* Private user code ---------------------------------------------------------/
/ USER CODE BEGIN 0 /
//static void ShortToChar(short sData,unsigned char cData[])
//{
// cData[0]=sData&0xff;
// cData[1]=sData>>8;
//}
static short CharToShort(unsigned char cData[])
{
int temp =cData[1];
return (temp<<8)|cData[0];
}
/ USER CODE END 0 /
//fun:功能字. 0XA0~0XAF
//data:数据缓存区,最多28字节!!
//len:data区有效数据个数
//void RDSS_CRC(uint8_t fun,uint8_tdata,uint8_t len)
char RDSS_CRC_xor(char*data,uint8_t len){
int i;
unsigned char xor;
xor = data[1];
for(i=2;i
xor = xor ^ data[i];
}
return xor;
}
//得到加速度值
//gx,gy,gz:陀螺仪x,y,z轴的原始读数(带符号)
//返回值:0,成功
// 其他,错误代码
static uint8_t MPU_Get_Accelerometer(float a,char Register_add)
{
uint8_t buf[6];
//float temp[3];
//HAL_I2C_Mem_Read(&hi2c1, MPU_ADDR_Read,Register_add,1,&buf[0],6, 0x20);
if(HAL_I2C_Mem_Read(&hi2c1, MPU_ADDR_Read,Register_add,1,&buf[0],6, 0x20)==HAL_OK){
a[0] = (float)CharToShort(&buf[0])/3276816;
a[1] = (float)CharToShort(&buf[2])/3276816;
a[2] = (float)CharToShort(&buf[4])/3276816;
}else {
return 1;
}
return 0;
}
//得到陀螺仪值
//gx,gy,gz:陀螺仪x,y,z轴的原始读数(带符号)
//返回值:0,成功
// 其他,错误代码
static uint8_t MPU_Get_Gyroscope(float w,char Register_add)
{
uint8_t buf[6];
if(HAL_I2C_Mem_Read(&hi2c1, MPU_ADDR_Read,Register_add,1,&buf[0],6, 0x20)==HAL_OK){
w[0] = (float)CharToShort(&buf[0])/327682000;
w[1] = (float)CharToShort(&buf[2])/327682000;
w[2] = (float)CharToShort(&buf[4])/327682000;
}else{
return 1;
}
return 0;
}
//得到dmp处理后的数据(注意,本函数需要比较多堆栈,局部变量有点多)
//pitch:俯仰角 精度:0.1° 范围:-180.0° <—> +180.0°
//roll:横滚角 精度:0.1° 范围:-180.0°<—> +180.0°
//yaw:航向角 精度:0.1° 范围:-180.0°<—> +180.0°
//返回值:0,正常
// 其他,失败
char mpu_dmp_get_data(float Angle,char Register_add){
uint8_t buf[6];
if(HAL_I2C_Mem_Read(&hi2c1, MPU_ADDR_Read,Register_add,1,&buf[0],6, 0x20)==HAL_OK){
Angle[0] = (float)CharToShort(&buf[0])/32768180;
Angle[1] = (float)CharToShort(&buf[2])/32768180;
Angle[2] = (float)CharToShort(&buf[4])/32768180;
}else{
return 1;
}
return 0;
}
//返回值:0,正常
// 其他,失败
static void RDSS_Transmit_Protocols(unsigned char str,float aa,float gy,float Angle_){
static unsigned char frame_count;
//unsigned char arr_str[100],checksum;
unsigned char checksum;
unsigned char array_temp[2];
//str=arr_str;
frame_count=frame_count+1;//帧计数
sprintf((char)str,"$MEMS,%02d,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f",frame_count,aa[0],aa[1],aa[2],gy[0],gy[1],gy[2],Angle_[0],Angle_[1],Angle_[2]);
checksum = RDSS_CRC_xor((char)str,strlen((char)str)-1);
sprintf((char*)array_temp,“%02x”,checksum);
strcat((char*)str,(char*)array_temp);
}
/* USER CODE BEGIN 2 /
void JY61P_Angle_data_sampling(){
float a[3],w[3],Angle[3];
unsigned char str[100];
if(MPU_Get_Accelerometer(a,AX)==0){
//HAL_Delay(5);
}
if(MPU_Get_Gyroscope(w,GX)==0){
//HAL_Delay(5);
}
if(mpu_dmp_get_data(Angle,Roll)==0){
//HAL_Delay(5);
}
RDSS_Transmit_Protocols(str,a,w,Angle);
HAL_UART_Transmit(&huart1,str,strlen((char)str),0XFFFF);
}
/* USER CODE END 2 /
/*********************** © COPYRIGHT STMicroelectronics *END OF FILE/