基于STM32F407,输出双路互补的SPWM波,AD采集用DMA DAC的方式,具有PID调节,代码注释详细
spwm数组生成软件:http://www.51hei.com/bbs/dpj-171674-1.html
单片机源程序如下:
- #include "stm32f4xx.h"
- #include "string.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"
- #include "led.h"
- #define current_threshold 3 //阈值电流为3A
- 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是否更新
- int main(void)
- {
-
- delay_init(168);
- //ShuangLu_Init();
- ADC_X_Init();
- TIMx_Configuration();
- LED_Init(); //初始化LED端口
- OLED_Init();
- PID_init(1,1,0,0.5);
- OLED_ShowNum(0,0,22,3,16);
- OLED_Display_On();
- TIM3_Int_Init(3360-1,0);
- 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*6+0.2; //电压检测
- ADC_Final_DisplayValue[1] =(float) ADC_filter_Value[1]/4096*3300*1.448-18; //电流检测
- //PID电压计算输出,设定值24V,输入电压检测值
- PID_Calc(ADC_Final_DisplayValue[0],24,0.7,0.2); //24是设定值,0.7是上限.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]);
- OLED_Display_Off();
- Display_flag=2;
- }
-
- if(ADC_Final_DisplayValue[1]>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 (双极性) - 副本.7z
(308.7 KB, 下载次数: 167)
|