这个实验主要是加深对PID控制的了解
单片机源程序如下:
- #include "stm32f4xx.h"
- #include "usart.h"
- #include "delay.h"
- #include "key.h"
- #include "led.h"
- #include "PWM.h"
- #include "dianji.h"
- #include "ADC.h"
- #include "oled.h"
- #include "PID.h"
- #define angle_1_amendment -7.5 ; //角度修正
- void CNM_ZZ(void);
- void CNM_SB(void);
- extern float TargetVal;
-
- extern float angle_1;
-
- extern int PIDout;
- float z;
-
-
-
- u8 ve[32]={0};
- u8 ze[30]={0};
- u8 ke[30]={0};
- int a=0;
- u16 adcx; //采集值
- float temp; //当前电压值
- u16 c;
- int main(void)
- {
-
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
- uart_init(9600);
- delay_init(168); //初始化延时函数
- key_init();
- dianji_Init();
- LED_Init();
- TIM14_PWM_Init(5000,0);
- TIM10_PWM_Init(8000,0);
- Adc_Init();
- OLED_Init();
- OLED_Show_Font(0,4,0);
- OLED_Show_Font(16,4,1);
- OLED_Show_Font(32,4,3);
- OLED_Show_Font(48,4,4);
- OLED_Show_Font(64,4,2);
- OLED_Show_Font(80,4,5);
- OLED_Show_Font(0,22,0);
- OLED_Show_Font(16,22,1);
- OLED_Show_Font(32,22,7);
- OLED_Show_Font(48,22,8);
- OLED_Show_Font(64,22,2);
- OLED_Show_Font(80,22,5);
- OLED_Show_Font(0,40,9);
- OLED_Show_Font(16,40,10);
- OLED_Show_Font(32,40,11);
- OLED_Show_Font(48,40,12);
- OLED_Show_Font(64,40,13);
- OLED_Show_Font(80,40,14);
- OLED_Show_Font(96,40,5);
- OLED_Show_Font2(110,48,0);
-
- while(1)
- {
- c=USART_ReceiveData(USART1);
- if(c=='A'||c=='B'||c=='H')
- CNM_ZZ(); //设定值加1
- else if(c=='E'||c=='D'||c=='F')
- CNM_SB(); //设定值减1
- else if(c=='G')
- {
-
- PIDout=0;
-
- } //停止
-
-
-
- adcx=Get_Adc_Average(ADC_Channel_5,20); //采集20次的平均通道值
- temp=(float)adcx*(3.3/4096); //当前电压值
- angle_1=temp*(360/3.3)+angle_1_amendment; //当前角度值
-
- PID_Calculate();
- z=PIDout/50;
-
- TIM_SetCompare1(TIM14,PIDout);
-
- sprintf((char*)ve,"%.1f",angle_1);
- sprintf((char*)ze,"%.1f",TargetVal);
- sprintf((char*)ke,"%.1f",z);
- OLED_ShowString(98,4,ve,12);
- OLED_ShowString(98,22,ze,12);
- OLED_ShowString(90,48,ke,12);
-
- OLED_Refresh_Gram();//更新显示到OLED
-
-
- }
-
- }
- void CNM_ZZ(void)
- {
- TargetVal=TargetVal+1;
- GPIO_ResetBits(GPIOF,GPIO_Pin_8);
- GPIO_SetBits(GPIOF,GPIO_Pin_10);
- }
- void CNM_SB(void)
- {
- TargetVal=TargetVal-1;
- GPIO_ResetBits(GPIOF,GPIO_Pin_8);
- GPIO_SetBits(GPIOF,GPIO_Pin_10);
- }
复制代码
所有资料51hei提供下载:
帆板2.5版.rar
(503.4 KB, 下载次数: 24)
|