主函数代码:
#include "main.h"
//211
uint16_t t1,t2,t3;
short Accel[3];
short Gyro[3];
float Temp;
float Angle_Y,Angle_X,Angle_Z;
int pitch,roll,yaw;
int main(void)
{
init_ALL(); //初始化所有函数
while(1)
{
MPU6050ReadAcc(Accel);//获取加速度原始值
MPU6050ReadGyro(Gyro);//获取角速度原始值
MPU6050_ReturnTemp(&Temp);//获取温度值
Angle_X=atan2(Accel[0],Accel[2])*57.3-Gyro[0]/16.4*0.005;//换算
Angle_Y=atan2(Accel[1],Accel[2])*57.3-Gyro[1]/16.4*0.005;
pitch=(int)yijiehubu_P(Angle_X,Gyro[0]/16.4); //一阶互补滤波
roll=-(int)yijiehubu_R(Angle_Y,Gyro[1]/16.4);
}
}
//初始化所有函数:
void init_ALL(void)
{
Usart1_Init(115200); //初始化串口1
printf("HELLO \r\n"); //串口1 测试重定向Printf
SysTick_Init(72);
Timer2_Init();
// Timer3_PWM_init(2000,719);
i2c_GPIO_Config(); //IIC初始化
MPU6050_Init(); //mpu6050初始化
MPU6050ReadID(); //读取器件ID
}
void TIM2_IRQHandler(void)
{
if (TIM_GetITStatus(TIM2, TIM_IT_Update) == SET)
{
// printf("Angle_X=%lf",Angle_X);
// printf("\r\n");
// printf("Angle_Y=%lf",Angle_Y);
// printf("\r\n");
// printf("\r\n");
// printf("Temp=%lf",Temp);
printf("pitch=%d",pitch);
printf("\r\n");
printf("roll=%d",roll);
printf("\r\n");
printf("yaw=%d",yaw);
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);//清出中断寄存器标志位,用于退出中断
}
}
|