pid闭环调制基于stm32f4的程序控制的单相逆变电源程序
单片机源程序如下:
- #include "stm32f4xx.h"
- #include "usart.h"
- #include "delay.h"
- #include "bsp_advance_tim.h"
- #include "bsp_spwm.h"
- #include "ShuangLu.h"
- #include "PID.h"
- #include "bsp_adc.h"
- #include "SD_io.h"
- #include "oled.h"
- #include "filter.h"
- #include "bsp_spwm.h"
- #define current_threshold 3
- u8 DispADBuff[2][30]; //用于存放字符
- extern u16 ADC_ConvertedValue[NOFCHANEL];//DAC_DMA传值处
- extern PID pid_voltage; //存放电压PID的参数
- extern float ADC_Final_DisplayValue[NOFCHANEL]; //用于存放最终显示值
- extern u16 ADC_filter_Value[NOFCHANEL]; //用于存放滤波后值
- extern u8 AD_Flag;
- u32 Display_flag=0;//是否显示,PID是否更新
- extern PID pid_voltage;
- u8 PID_start=0;
- int main(void)
- {
-
- delay_init(168);
- ShuangLu_Init();
- ADC_X_Init();
- TIMx_Configuration();
- OLED_Init();
- PID_init(0.001,0.001,0,SPWM_default_ratio);
-
- while(1)
- {
-
-
- if(Display_flag%1000==0)
- {
- AD_Flag=AD_voltage; ADC_filter_Value[0]=middleValueFilter();
- AD_Flag=AD_current; ADC_filter_Value[1]=middleValueFilter();
- ADC_Final_DisplayValue[0] =(float) ADC_filter_Value[0]/4096*3.3*36.7862-46.285; //电压检测
- ADC_Final_DisplayValue[1] =(float) ADC_filter_Value[1]/4096*3300*1.448-18; //电流检测
- //PID电压计算输出,设定值24V,输入电压检测值
- if((ADC_Final_DisplayValue[0]/24>0.98)&&(ADC_Final_DisplayValue[0]/24)<1.02)PID_start=1;
- if(PID_start==1)PID_Calc(ADC_Final_DisplayValue[0],24,0.8,0.2);
-
- }
- if((Display_flag>10000)||(Display_flag==1))
- {
- sprintf(DispADBuff[0],"V=%.2fV\0",ADC_Final_DisplayValue[0]);//电压显示
- sprintf(DispADBuff[1],"I1=%.3fA\0",ADC_Final_DisplayValue[1]);//电流显示
- OLED_ShowString(0,0,DispADBuff[0]);
- OLED_ShowString(0,2,DispADBuff[1]);
- Display_flag=2;
- }
-
- // if(ADC_Final_DisplayValue[2]>current_threshold)
- // {
- // GPIO_SetBits(SD_io_PORT,SD_io_I1|SD_io_I2);
- // delay_ms(1000);delay_ms(1000);delay_ms(1000);delay_ms(1000);delay_ms(1000);delay_ms(1000);
- // pid_voltage.SEk=0;
- // GPIO_ResetBits(SD_io_PORT,SD_io_I1|SD_io_I2);
- // }
-
- }
- }
复制代码
所有资料51hei提供下载:
互补SPWM+模拟死区开关方波+PID+ADC (单极性)1.7z
(326.91 KB, 下载次数: 191)
|