前言
第一次写博客,肯定有很多写的不好的地方,希望大家有疑问或者好的建议可以提出来。我就先写一个大概的文章,之后一点点完善。
材料清单
stc12C5A60S2单片机一块,adc0809芯片一个
0.96英寸OLED屏幕一块(spi)
100W LED灯
电机驱动模块
12V散热风扇
150w升压板
按键若干
其他元件若干
设计目标
设计目标:
1:100w光源无极调光
2:pid智能控温
3:自动或手动控制出风口开合
4:儿童模式
5:实时显示温度 电压 亮度
6:多级菜单和按键
主要电路图
主要程序
pid部分程序
int pidcontrol(float set_value, real_value) //执行一次pid程序
{
float i,erro,last_erro1,last_erro2,last_erro3,last_erro4;
int P,I,D;
int pid_out;
i=erro;
erro= real_value-set_value;
P=(int)Kp*erro;
last_erro4=last_erro3;
last_erro3=last_erro2;
last_erro2=last_erro1;
last_erro1=i;
I= (int)Ki*(erro+last_erro1+last_erro2+last_erro3+last_erro4);
D=(int)Kd*(erro-i);
pid_out=P+I+D+KK;
if(pid_out>255)
pid_out=255;
else if (pid_out<0)
pid_out=0;
return(pid_out);
}
多级菜单与按键部分程序
typedef struct
{
unsigned char current; //当前状态索引号
unsigned char up; //
unsigned char down;
unsigned char enter;
unsigned char back;
void(*current_operation)();//
}key_table;
#define SIZE_OFKEYBD_MENU 23
key_table table[SIZE_OFKEYBD_MENU]=
{
{0,0,0,1,0,(*fun01)},
{1,5,3,2,0,(*fun02)},
{2,8,6,19,1,(*fun03)},
{3,1,4,9,0,(*fun021)},
{4,3,5,14,0,(*fun022)},
{5,4,1,16,0,(*fun023)},
{6,2,7,20,1,(*fun031)},
{7,6,8,21,1,(*fun032)},
{8,7,2,22,1,(*fun033)},
{9,12,10,2,3,(*fun0210)},
{10,9,11,2,3,(*fun0211)},
{11,10,12,2,3,(*fun0212)},
{12,11,9,13,3,(*fun0213)},
{13,13,13,13,12,(*fun02130)},
{14,14,14,15,4,(*fun0220)},
{15,15,15,14,14,(*fun02200)},
{16,18,17,16,5,(*fun0230)},
{17,16,18,17,5,(*fun0231)},
{18,17,16,18,5,(*fun0232)},
{19,19,19,19,2,(*fun030)},
{20,20,20,20,6,(*fun0310)},
{21,21,21,21,7,(*fun0320)},
{22,22,22,22,8,(*fun0330)},
};
if(func_index!=19||func_index!=20||func_index!=21||func_index!=22)
{
adclight(1,1,1);
}
temperature(&Temptrans,&Templight);
Temp1=(int)100*Temptrans;
Temp2=(int)100*Templight;
if((keyup==0)||(keydown==0)||(keyenter==0)||(keyback==0))
{
delay_ms(1);
if(keyup==0)
{
int long i=0;
xcoord=0; ycoord=128;
func_index=table[func_index].up;
while(!keyup)
{
i++;
if(i>90000)
{
while(motor_switch1!=0)
{
OLED_ShowCHinese(32,3,"开出风口");
motor_AIN1=1;//电机驱动
motor_AIN2=0;//电机驱动
}
motor_AIN1=0;//电机驱动
motor_AIN2=0;//电机驱动
}
}
}
if(keydown==0)
{
int long i=0;
xcoord=0; ycoord=128;
func_index=table[func_index].down;
while(!keydown)
{
i++;
if(i>90000)
{
while(motor_switch2!=0)
{
OLED_ShowCHinese(32,3,"关出风口");
motor_AIN1=0;//电机驱动
motor_AIN2=1;//电机驱动
}
motor_AIN1=0;//电机驱动
motor_AIN2=0;//电机驱动
}
}
}
if(keyenter==0)
{
int long i=0;
xcoord=0; ycoord=128;
func_index=table[func_index].enter;
while(!keyenter)
{
i++;
if(ON==0&&i>90000)
{
ON=1;
OLED_Clear();
OLED_ShowCHinese(16,3,"关闭儿童锁");
delay_ms(500);
}
else if(ON==1&&i>90000)
{
ON=0;
OLED_Clear();
OLED_ShowCHinese(16,3,"开启儿童锁");
delay_ms(500);
}
}
}
if(keyback==0)
{
int long i=0;
xcoord=0; ycoord=128;
func_index=table[func_index].back;
while(!keyback)
{
i++;
if(i>90000)
{
func_index=0;
}
}
}
OLED_Clear();
}
current_operation_index=table[func_index].current_operation;
(*current_operation_index)(Temp1,Temp2);
}
一些效果图片
链接: 我是代码.