找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 2082|回復(fù): 0
收起左側(cè)

STM32+MPU6050驅(qū)動舵機程序

[復(fù)制鏈接]
ID:977469 發(fā)表于 2021-12-6 19:20 | 顯示全部樓層 |閱讀模式
  1. #include "all.h"
  2. #include "timer.h"
  3. #include "pwm.h"
  4. #include "delay.h"
  5. #include "usart.h"
  6. #include "I2C.h"
  7. #include "math.h"
  8. #include "key.h"
  9. #include "mpu6050.h"  
  10. #include "inv_mpu.h"
  11. #include "inv_mpu_dmp_motion_driver.h"
  12. #include "OLED_I2C.h"




  13. int main(void)
  14. {       
  15.         float pitch,roll,yaw;int t=0,s=0;
  16.         char YAW[10]={0},PITCH[10]={0},ROLL[10]={0};
  17.         SystemInit();
  18.         NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
  19.         delay_init();
  20.         uart_init(115200);
  21.         TIM3_PWM_Init(1999,719);
  22.         TIM2_PWM_Init(1999,719);
  23.         TIM4_Int_Init(999,7199);
  24.         KEY_Init();
  25.         MPU_Init();
  26.         I2C_Configuration();//OLED
  27.         OLED_Init();
  28.   OLED_CLS();//清屏

  29.         while(mpu_dmp_init())
  30.         {
  31.                 OLED_ShowStr(0,0,"MPU6050 Error",2);
  32.                 delay_ms(200);
  33.         }  
  34.                 OLED_CLS();
  35.                 OLED_ShowStr(0,0,"MPU6050 OK",1);       
  36.                 OLED_ShowStr(0,2,"yaw",1);OLED_ShowStr(0,1,"pitch",1);OLED_ShowStr(0,3,"roll",1);       
  37.         while(1)
  38.         {
  39.                 if(flag==0&&KEY1==1)
  40.                                 {
  41.                                         s=s+50;
  42.                                         while(KEY1);
  43.                                         if(s>=300)s=0;
  44.                                         TIM_SetCompare1(TIM3,s);
  45.                                        
  46.                                 }
  47.                 if(mpu_dmp_get_data(&pitch,&roll,&yaw)==0&&flag==1)
  48.                 {
  49.                        
  50.                         TIM_SetCompare1(TIM3,(int)(yaw+107));
  51.                         TIM_SetCompare2(TIM3,(int)(pitch+93));
  52.                         TIM_SetCompare2(TIM2,(int)(-roll+67));
  53.                 }
  54.                 if(t%10==0&&mpu_dmp_get_data(&pitch,&roll,&yaw)==0)
  55.                 {
  56.                         sprintf(YAW,"%6.1f",yaw);OLED_ShowStr(30,2,YAW,1);
  57.                         sprintf(PITCH,"%6.1f",pitch);OLED_ShowStr(30,1,PITCH,1);
  58.                         sprintf(ROLL,"%6.1f",roll);OLED_ShowStr(30,3,ROLL,1);
  59.                        
  60.                         t=0;
  61.                 }
  62.                 t++;
  63.                
  64.         }
  65. }
復(fù)制代碼

Keil代碼下載: c8t6通過傳感器控制舵機.7z (274.83 KB, 下載次數(shù): 74)

評分

參與人數(shù) 1黑幣 +10 收起 理由
admin + 10 共享資料的黑幣獎勵!

查看全部評分

回復(fù)

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機教程網(wǎng)

快速回復(fù) 返回頂部 返回列表