找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1548|回复: 0
收起左侧

风力摆系统STM32源程序

[复制链接]
ID:746808 发表于 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. //主函数中显示数据共需要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);         //设置NVIC中断分组2:2位抢占优先级,2位响应优先级
  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,"设置长度:");
  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,"设置角度:");
  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,"设置半径:");
  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,"设置完毕");
  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中断服务程序
  86. void TIM3_IRQHandler(void) //TIM3 中断
  87. {
  88.         if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET) //检查TIM3更新中断是否发生
  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. }
复制代码

所有程序51hei提供下载:
风力摆控制系统.zip (399 KB, 下载次数: 32)
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表