放代码了(2.max6675热电偶温度控制器)(详细代码工程见附件)
演示视频链接:
#include "reg52.h"
#include "lcd.h"
#include "key.h"
#include "max6675.h"
//PWM 状态指示灯
sbit LED1=P1^4;
sbit LED2=P1^5;
sbit LED3=P1^6;
sbit LED4=P1^7;
//继电器控制端
sbit RELAY=P3^2;
uint temp,save=30,save_D=30;
bit temp_flag,LED_flag,wait_flag,wait;
uchar mode,k1_value,k2_value,k3_value,i,k;
void main()
{
LED1=1;LED2=1;LED3=1;LED4=1; //PWM LED指示灯初始化
RELAY=1; //继电器初始化
wait_flag=1;
LcdInit(); //LCD1602初始化
TMOD|=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
ET0=1;
TR0=1;
EA=1;
//LCD1602液晶初始化
LcdWriteCom(0x80);
LcdWriteData('W');
LcdWriteCom(0x81);
LcdWriteData('E');
LcdWriteCom(0x82);
LcdWriteData('L');
LcdWriteCom(0x83);
LcdWriteData('C');
LcdWriteCom(0x84);
LcdWriteData('O');
LcdWriteCom(0x85);
LcdWriteData('M');
LcdWriteCom(0x86);
LcdWriteData('E');
LcdWriteCom(0x87);
LcdWriteData(':');
LcdWriteCom(0x89);
LcdWriteData('S');
LcdWriteCom(0x80+10);
LcdWriteData('Y');
LcdWriteCom(0x80+11);
LcdWriteData('S');
LcdWriteCom(0x80+12);
LcdWriteData('T');
LcdWriteCom(0x80+13);
LcdWriteData('E');
LcdWriteCom(0x80+14);
LcdWriteData('M');
while(1)
{
keysan();
switch(mode) //模式切换
{
//显示模式
case 0:
if(temp_flag==1)
{
temp_flag=0;
temp=MAX6675_ReadReg();
temp= temp<<1;
temp= temp>>4;
temp= temp/4;
}
//温度
LcdWriteCom(0x80+0x40);
LcdWriteData('t');
LcdWriteCom(0x80+0x40+1);
LcdWriteData('e');
LcdWriteCom(0x80+0x40+2);
LcdWriteData('m');
LcdWriteCom(0x80+0x40+3);
LcdWriteData('p');
LcdWriteCom(0x80+0x40+4);
LcdWriteData(':');
LcdWriteCom(0x80+0x40+5);
LcdWriteData(temp/1000+0x30);
LcdWriteCom(0x80+0x40+6);
LcdWriteData(temp%1000/100+0x30);
LcdWriteCom(0x80+0x40+7);
LcdWriteData(temp%100/10+0x30);
LcdWriteCom(0x80+0x40+8);
LcdWriteData(temp%10+0x30);
LcdWriteCom(0x80+0x40+9);
LcdWriteData('c');
break;
//设置模式
case 1:
if(k1_value==1)
{
k1_value=0;
save_D+=1;
if(save_D==10000)
{
save_D=0;
}
}
else if(k2_value==1)
{
k2_value=0;
save_D-=1;
if(save_D==0)
{
save_D=9999;
}
}
if(k3_value==1)
{
k3_value=0;
save=save_D;
}
LcdWriteCom(0x80+0x40);
LcdWriteData('t');
LcdWriteCom(0x80+0x40+1);
LcdWriteData('e');
LcdWriteCom(0x80+0x40+2);
LcdWriteData('m');
LcdWriteCom(0x80+0x40+3);
LcdWriteData('p');
LcdWriteCom(0x80+0x40+4);
LcdWriteData('H');
LcdWriteCom(0x80+0x40+5);
LcdWriteData(':');
LcdWriteCom(0x80+0x40+6);
LcdWriteData(save_D/1000+0x30);
LcdWriteCom(0x80+0x40+7);
LcdWriteData(save_D%1000/100+0x30);
LcdWriteCom(0x80+0x40+8);
LcdWriteData(save_D%100/10+0x30);
LcdWriteCom(0x80+0x40+9);
LcdWriteData(save_D%10+0x30);
LcdWriteCom(0x80+0x40+10);
LcdWriteData('c');
break;
}
if(wait==1) //跳过上电不稳定区
{
if(temp<=save) //温度不够 PWM控制输出
{
RELAY=0;
if(temp+20<save) //温差大 PWM最大输出
{
if(LED_flag==1)
{
LED_flag=0;
k++;
if(k>=5)k=0;
switch(k)
{
case 1: LED1=0; break;
case 2: LED2=0; break;
case 3: LED3=0; break;
case 4: LED4=0; break;
}
}
else if(LED_flag==0)
{
LED1=1;LED2=1;LED3=1;LED4=1;
}
}
else if(temp+15<save)
{
if(LED_flag==1)
{
LED_flag=0;
k++;
if(k>=4)k=0;
switch(k)
{
case 1: LED1=0; break;
case 2: LED2=0; break;
case 3: LED3=0; break;
}
}
else if(LED_flag==0)
{
LED1=1;LED2=1;LED3=1;
}
}
else if(temp+10<save)
{
if(LED_flag==1)
{
LED_flag=0;
k++;
if(k>=3)k=0;
switch(k)
{
case 1: LED1=0; break;
case 2: LED2=0; break;
}
}
else if(LED_flag==0)
{
LED1=1;LED2=1;
}
}
else if((temp+5<save)|(temp<=save))
{
if(LED_flag==1)
{
LED_flag=0;
LED1=0;
}
else if(LED_flag==0)
{
LED1=1;
}
}
}
else //温度够 PWM控停止输出
{
RELAY=1; LED1=1;LED2=1;LED3=1;LED4=1;
}
}
else if(wait==0)
{
RELAY=1; LED1=1;LED2=1;LED3=1;;LED4=1;
}
}
}
void time() interrupt 1
{
uint count1,count2,t;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
count1++;count2++;
if(count1==20)
{
count1=0;
temp_flag=1;
if(wait_flag==1)
{
t++;
if(t==2)
{
t=0;
wait=1;
}
}
}
if(count2==5)
{
count2=0;
LED_flag=1;
}
}
|