|
主要设计与实现一种基于LabVIEW的温度控制器的设计。能进行精准且快速的控温。 1、能够在LabVIEW上显示出来并且控制其温度。 2、能制冷到15摄氏度。 3、偏差能小于0.5摄氏度。 对电烤箱温度控制系统建立模型,将传统PID控制与模糊理论或遗传算法相结合,设计了一种新的PID控制器。该控制器,能够实现PID的三个参数自动整定,避免了人工整定的麻烦。仿真结果表明,这种控制算法具有结构简单、响应速度快、控制精度高、鲁棒性强的特点。 程序包括PID,PWM,DS18B20
Altium Designer画的原理图PCB
单片机源程序如下: - #include "delay.h"
- #include "sys.h"
- #include "oled.h"
- #include <stdio.h>
- #include "ds18b20.h"
- #include "ds18b20_2.h"
- #include "pid.h"
- #include "stm32f10x.h"
- #include "usart.h"
- #include "PWM_Output.h"
- static TIM_OCInitTypeDef PWMBASE;
- static uint16_t per_tmp;
- extern float Kp;
- extern float Ki;
- extern float Kd;
- static int ind;
- static unsigned char tmp2[60];
- void init_pwm(uint16_t per, uint16_t pre)
- {
- TIM_TimeBaseInitTypeDef TIMBASE;
-
- GPIO_InitTypeDef GPIOBASE;
-
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
-
- GPIOBASE.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIOBASE.GPIO_Speed = GPIO_Speed_50MHz;
- GPIOBASE.GPIO_Pin = GPIO_Pin_8;
- GPIO_Init(GPIOB, &GPIOBASE);
-
- PWMBASE.TIM_OCMode = TIM_OCMode_PWM1;
- PWMBASE.TIM_OutputState = TIM_OutputState_Enable;
- PWMBASE.TIM_Pulse = per / 2;
- PWMBASE.TIM_OCPolarity = TIM_OCPolarity_High;
-
- TIM_OC3Init(TIM4, &PWMBASE);
-
- TIM_OC3PreloadConfig(TIM4, TIM_OCPreload_Enable);
- TIMBASE.TIM_ClockDivision = TIM_CKD_DIV1;
- TIMBASE.TIM_CounterMode = TIM_CounterMode_Up;
- TIMBASE.TIM_Period = per - 1;
- TIMBASE.TIM_Prescaler = pre;
- TIM_TimeBaseInit(TIM4, &TIMBASE);
-
- TIM_ARRPreloadConfig(TIM4, ENABLE);
-
- TIM_Cmd(TIM4, ENABLE);
-
- per_tmp = per;
- }
- void Cold_pwm(uint16_t pwm1)
- {
- if (pwm1 > per_tmp) pwm1 = per_tmp;
-
- PWMBASE.TIM_Pulse = pwm1;
- TIM_OC3Init(TIM4, &PWMBASE);
- }
- void Fan_pwm(uint16_t pwm1)
- {
- if (pwm1 > per_tmp) pwm1 = per_tmp;
-
- PWMBASE.TIM_Pulse = pwm1;
- TIM_OC2Init(TIM4, &PWMBASE);
- }
- void Fan_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
-
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- GPIO_SetBits(GPIOB, GPIO_Pin_7);
- }
- float USART_Scanf(void)
- {
- if(USART_GetFlagStatus(USART2, USART_FLAG_RXNE) != RESET)
- {
- tmp2[ind++] = (USART_ReceiveData(USART2));
- ind = 0;
- while (ind < 5)
- {
- while (USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == RESET)
- {}
- tmp2[ind++] = (USART_ReceiveData(USART2));
- }
- tmp2[ind] = '\0';
- ind = 0;
- if(tmp2[4] == 'C')
- {
- return(tmp2[0]-0x30)*10 + (tmp2[1]-0x30) + (tmp2[3]-0x30)*0.1;
- }
- }
- return 0;
- }
- int main(void)
- {
- u8 datH[30];
- u8 datL[30];
- u8 Radiator[30];
- u16 Kpf[7] = {10000, 10000, 10000, 5000, 4000, 0, 0};
-
- float set = 0;
- float set_USART = 0;
- u8 set_key[3] = {' ', ' ', ' '};
- float dat0 = 0;
- float dat1 = 0;
- float PRO = 0; //放置PID返回值,防止pid运算多次
- int i = 0;
-
- init_pwm(10000,72);
- delay_init();
- OLED_Init(); //初始化OLED
- OLED_Clear();
- DS18B20_Init();
- DS18B20_Init_2();
- USART2_Config();
- // printf("\r\nInit OK");
- Fan_Init();
-
- set = 20.00;
- Cold_pwm(10000);
- Fan_pwm(0);
- while(1)
- {
- i++;
- //printf("%d\r\n", i);
- dat0 = DS18B20_Get_Temp();
- dat0 = dat0/10;
- dat1 = DS18B20_Get_Temp_2();
- dat1 = dat1/10;
- set_USART = USART_Scanf();
- if(set_USART != 0)
- {
- set = set_USART;
- }
- if((dat0 - set) > 15)
- {
- Kp = Kpf[0];
- }
- else if((dat0 - set) > 3 && (dat0 - set) <= 5)
- {
- Kp = Kpf[1];
- }
- else if((dat0 - set) > 2 && (dat0 - set) <= 3)
- {
- Kp = Kpf[2];
- }
- else if((dat0 - set) <= 2 && (dat0 - set) >= 0.5)
- {
- Kp = Kpf[3];
- }
- else if((dat0 - set) < 0.5 && (dat0 - set) >= 0)
- {
- Kp = Kpf[4];
- }
- else if((dat0 - set) <= 0)
- {
- Kp = Kpf[5];
- }
-
- if((dat0 - set) >= 0)
- {
- Cold_pwm(0);
- }
- else
- {
- Cold_pwm(10000);
- }
-
- if(dat1 >= 43)
- {
- OLED_ShowString(0, 0, "WARNING");
- }
- else
- {
- OLED_ShowString(0, 0, " ");
- }
-
- PRO = inc_pid(dat0, set);
- //printf(" PWM:%.2f%%",(PRO/100));
- //printf(" Fan:%.2f",dat1);
- Fan_pwm(PRO);
-
- // sprintf(pwm, "PWM:%.2f%", (PRO/100));
- sprintf(datH, "Current:%.2f", dat0);
- sprintf(Radiator, "Radiator:%.2f", dat1);
-
- // OLED_ShowString(0, 0, pwm);
- OLED_ShowString(0, 4, datL);
- OLED_ShowString(0, 6, Radiator);
- sprintf(datL, "Setting:%.2f", set);
- OLED_ShowString(0, 2, datH);
- if(i == 23)
- {
- i = 0;
- printf("A%.2fB%.2fD",dat0,dat1 );
- }
- }
- }
复制代码
下载(设计中还存在一些瑕疵,希望各位大佬指导):
|
-
-
上位机.zip
54.8 KB, 下载次数: 143, 下载积分: 黑币 -5
-
-
硬件.zip
1.12 MB, 下载次数: 117, 下载积分: 黑币 -5
-
-
智能空调2.0.zip
385.72 KB, 下载次数: 158, 下载积分: 黑币 -5
|