找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

帖子
查看: 1794|回復(fù): 0
收起左側(cè)

風力擺系統(tǒng)STM32源程序

[復(fù)制鏈接]
ID:746808 發(fā)表于 2020-5-8 14:04 | 顯示全部樓層 |閱讀模式
32單片機做的風力擺

單片機源程序如下:
  1. #include "stm32f10x.h"
  2. #include "oled.h"
  3. #include "delay.h"
  4. #include "key.h"
  5. #include "sys.h"
  6. #include "usart.h"
  7. #include "mpu6050.h"
  8. #include "inv_mpu.h"
  9. #include "inv_mpu_dmp_motion_driver.h"
  10. #include "other.h"
  11. #include "motor.h"
  12. #include "pid.h"
  13. #include "timer.h"
  14. #include "key.h"
  15. #include "arithmetic.h"
  16. #include <stdio.h>
  17. #include <math.h>
  18. volatile float last_pitch=0,last_roll=0,last_length_x=0,last_length_y=0;
  19. //主函數(shù)中顯示數(shù)據(jù)共需要20ms左右的時間
  20. /*                pitch<0        1        |y        pitch>0
  21.                                 roll<0        |                roll<0
  22.                         0~90度                |        90~180度                               
  23.                 2        __第三象限        |____第四象限4        x
  24.                                                                 |
  25.                                 90~180度|                0~90度
  26. 第二象限pitch<0        |                pitch>0第一象限
  27.                         roll>0                |3        roll>0        */
  28.                         //pitch: x軸角度,roll:y軸角度
  29. int main(void)
  30. {         
  31.         volatile int key=0,mode=0;
  32.         float set_value_key[3]={0,0,0};
  33.         volatile float set_length=0,set_angle=0,set_radius=0,set_value=0;
  34.         Key_Config();                                  //按鍵初始化
  35.         uart_init(115200);//串口初始化,波特率為115200
  36.         delay_init();        //延時初始化
  37.         NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);         //設(shè)置NVIC中斷分組2:2位搶占優(yōu)先級,2位響應(yīng)優(yōu)先級
  38.         motor_init();        //空心杯初始化
  39.         TIM2_PWM_Init(9999,71);//頻率f=72000000/10000/72=100
  40.         TIM3_Int_Init(1999,719);//定時器中斷,周期為20ms
  41.         motor_a(1,0);motor_c(1,0);
  42.         motor_b(1,0);motor_d(1,0);
  43.         initial_lcd();           //OLED初始化

  44. //        delay_ms(500);
  45.         MPU_Init();                                        //初始化MPU6050
  46.         while(mpu_dmp_init())
  47.         {
  48.                 delay_ms(200);
  49.                 display_GB2312_string(0,72,"error");
  50.         }  
  51.         clear_screen();
  52.         display_GB2312_string(0,1,"風力擺");
  53.         display_GB2312_string(0,72,"OK");
  54.         while(1)
  55.         {
  56.                 key=KeyScan();
  57.                 if(key>10)        {mode=key-10;key_count--;}
  58.                 switch (mode)
  59.                         { case 1: {display_GB2312_string(2,1,"模式一:直線");break;}
  60.                                 case 2:{display_GB2312_string(2,1,"模式二:直線");display_GB2312_string(4,1,"設(shè)置長度:");
  61.                                                                 set_length=set_value/10;oled_display_set_length(4,84,set_length);break;}
  62.                                 case 3:{display_GB2312_string(2,1,"模式三:斜線");display_GB2312_string(4,1,"設(shè)置角度:");
  63.                                                                 set_angle=set_value;                oled_display_set_angle(4,84,set_angle);break;}
  64.                                 case 4: {display_GB2312_string(2,1,"模式四:制動");break;}
  65.                                 case 5:{display_GB2312_string(2,1,"模式五:畫圓");display_GB2312_string(4,1,"設(shè)置半徑:");
  66.                                                                 set_radius=set_value/10;oled_display_set_radius(4,84,set_radius);break;}}
  67.                 if((key>=0)&&(key<10))
  68.                 {set_value_key[key_count-1]=key;set_value=set_value_key[0]*100+set_value_key[1]*10+set_value_key[2];}
  69.                 if(key==10) break;
  70.         }
  71.         display_GB2312_string(6,1,"設(shè)置完畢");
  72. //        motor_init();                                        //空心杯初始化
  73. //        motor_a(1,0);motor_c(1,0);
  74. //        motor_b(1,0);motor_d(1,0);
  75.         while(1)
  76.         {
  77.                 printf("%d  ",mode);
  78.                 while(mode==1){first();}
  79.                 while(mode==2){second(set_length);}
  80.                 while(mode==3){third(set_angle);}
  81.                 while(mode==4){fourth();}
  82.                 while(mode==5){fifth(set_radius);}
  83.         }
  84. }

  85. //定時器3中斷服務(wù)程序
  86. void TIM3_IRQHandler(void) //TIM3 中斷
  87. {
  88.         if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET) //檢查TIM3更新中斷是否發(fā)生
  89.         {
  90.                 TIM_ClearITPendingBit(TIM3, TIM_IT_Update ); //清除TIM3更新中斷標志
  91.                 pid_flag=0;
  92.                 current_stright_speed_x=(length_x-last_length_x)/0.02;
  93.                 current_stright_speed_y=(length_y-last_length_y)/0.02;//計算速度,單位為cm/s
  94.                 last_length_x=length_x;
  95.                 last_length_y=length_y;
  96.                 printf("中斷開");
  97.                 current_angle_speed_x=(pitch-last_pitch)/0.02;
  98.                 current_angle_speed_y=(roll-last_roll)/0.02;//計算速度,單位為度/s
  99.                 last_pitch=pitch;
  100.                 last_roll=roll;
  101.         }
  102. }
復(fù)制代碼

所有程序51hei提供下載:
風力擺控制系統(tǒng).zip (399 KB, 下載次數(shù): 32)


回復(fù)

使用道具 舉報

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

本版積分規(guī)則

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

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

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