- #include "all.h"
- #include "timer.h"
- #include "pwm.h"
- #include "delay.h"
- #include "usart.h"
- #include "I2C.h"
- #include "math.h"
- #include "key.h"
- #include "mpu6050.h"
- #include "inv_mpu.h"
- #include "inv_mpu_dmp_motion_driver.h"
- #include "OLED_I2C.h"
- int main(void)
- {
- float pitch,roll,yaw;int t=0,s=0;
- char YAW[10]={0},PITCH[10]={0},ROLL[10]={0};
- SystemInit();
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
- delay_init();
- uart_init(115200);
- TIM3_PWM_Init(1999,719);
- TIM2_PWM_Init(1999,719);
- TIM4_Int_Init(999,7199);
- KEY_Init();
- MPU_Init();
- I2C_Configuration();//OLED
- OLED_Init();
- OLED_CLS();//清屏
- while(mpu_dmp_init())
- {
- OLED_ShowStr(0,0,"MPU6050 Error",2);
- delay_ms(200);
- }
- OLED_CLS();
- OLED_ShowStr(0,0,"MPU6050 OK",1);
- OLED_ShowStr(0,2,"yaw",1);OLED_ShowStr(0,1,"pitch",1);OLED_ShowStr(0,3,"roll",1);
- while(1)
- {
- if(flag==0&&KEY1==1)
- {
- s=s+50;
- while(KEY1);
- if(s>=300)s=0;
- TIM_SetCompare1(TIM3,s);
-
- }
- if(mpu_dmp_get_data(&pitch,&roll,&yaw)==0&&flag==1)
- {
-
- TIM_SetCompare1(TIM3,(int)(yaw+107));
- TIM_SetCompare2(TIM3,(int)(pitch+93));
- TIM_SetCompare2(TIM2,(int)(-roll+67));
- }
- if(t%10==0&&mpu_dmp_get_data(&pitch,&roll,&yaw)==0)
- {
- sprintf(YAW,"%6.1f",yaw);OLED_ShowStr(30,2,YAW,1);
- sprintf(PITCH,"%6.1f",pitch);OLED_ShowStr(30,1,PITCH,1);
- sprintf(ROLL,"%6.1f",roll);OLED_ShowStr(30,3,ROLL,1);
-
- t=0;
- }
- t++;
-
- }
- }
復(fù)制代碼
Keil代碼下載:
c8t6通過傳感器控制舵機.7z
(274.83 KB, 下載次數(shù): 74)
2021-12-7 04:17 上傳
點擊文件名下載附件
|