标题:
微波炉温度控制系统单片机源码
[打印本页]
作者:
SUPERNOVA1
时间:
2018-4-6 09:56
标题:
微波炉温度控制系统单片机源码
单片机源程序如下:
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
uchar i;
sbit lcdrs=P2^6;
sbit lcdrw=P2^5;
sbit lcden=P2^7;
sbit d1=P1^0;
sbit d2=P1^1;
sbit d3=P2^0;
sbit d4=P2^1;
sbit d5=P2^2;
sbit sound=P1^2;
uchar code t0[]="the temperature ";
uchar code t1[]=" is ";
uchar code wendu[]="0123456789"; //利用一个温度表解决温度显示乱码
uchar second;
uchar timer;
void t1_init()
{
TMOD=0x10;
IE=0X88;
TH1=0X3c;
TL1=0Xb0;
}
sbit DQ = P3^7; //定义ds18B20总线IO
//液晶显示模块
void delay(uint z)
{
uint x,y;
for(x=100;x>1;x--);
for(y=z;y>1;y--);
}
void write_com(uchar com)
{
lcdrs=0;
P0=com;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void write_date(uchar date)
{
lcdrs=1;
P0=date;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void init_lcd()
{
lcden=0;
lcdrw=0;
write_com(0x38);
write_com(0x01);
write_com(0x0c);
write_com(0x06);
write_com(0x80);
for(i=0;i<16;i++)
{
write_date(t0[i]);
delay(0);
}
write_com(0x80+0x40);
for(i=0;i<16;i++)
{
write_date(t1[i]);
delay(0);
}
}
//温度采集模块
void tmpDelay(int num) //延时函数
{
while(num--);
}
void Init_DS18B20() //初始化ds1820
{
unsigned char x=0;
DQ = 1; //DQ复位
tmpDelay(8);
DQ = 0; //单片机将DQ拉低
tmpDelay(80); //精确延时 大于 480us
DQ = 1; //拉高总线
tmpDelay(14);
x=DQ;
tmpDelay(20);
}
unsigned char ReadOneChar() //读一个字节
{
unsigned char i=0;
unsigned char dat=0;
for (i=8;i>0;i--)
{
DQ = 0; // 给脉冲信号
dat>>=1;
DQ = 1; // 给脉冲信号
if(DQ)
dat|=0x80;
tmpDelay(4);
}
return(dat);
}
void WriteOneChar(unsigned char dat) //写一个字节
{
unsigned char i=0;
for (i=8; i>0; i--)
{
DQ = 0;
DQ = dat&0x01;
tmpDelay(5);
DQ = 1;
dat>>=1;
}
}
unsigned int Readtemp() //读取温度
{
unsigned char a=0;
unsigned char b=0;
unsigned int t=0;
float tt=0;
Init_DS18B20();
WriteOneChar(0xCC); // 跳过读序号列号的操作
WriteOneChar(0x44); // 启动温度转换
Init_DS18B20();
WriteOneChar(0xCC); //跳过读序号列号的操作
WriteOneChar(0xBE); //读取温度寄存器
a=ReadOneChar(); //连续读两个字节数据 //读低8位
b=ReadOneChar(); //读高8位
t=b;
t<<=8;
t=t|a; //两字节合成一个整型变量。
tt=t*0.0625; //得到真实十进制温度值,因为DS18B20可以精确到0.0625度,所以读回数据的最低位代表的是0.0625度
t= tt*10+0.5; //放大十倍,这样做的目的将小数点后第一位也转换为可显示数字,同时进行一个四舍五入操作
return(t);
}
void display1()
{
unsigned int num,num1;
unsigned int shi,ge,xiaoshu;
num=Readtemp();
num1=num/10;
if(num1>24)
{d3=0;delay(5000);} ;
if(num1>25)
{d4=0;delay(5000);}
if(num1>26)
{d5=0;delay(5000);}
else
{d3=1;d4=1;d5=1;}
}
void display2()
{
unsigned int num,num1;
unsigned int shi,ge,xiaoshu;
num=Readtemp();
num1=num/10;
if(num1>27)
{sound=0;d1=0;d2=1;delay(5000);}
if(num1<23)
{
d1=1;
d2=0;
sound=0;
delay(500);
}
else
{
sound=1;
d1=1;
d2=1;
}
shi=num/100;
ge=num/10%10;
xiaoshu=num%10;
write_com(0x80+0x40+5);
write_date(wendu[shi]);
write_com(0x80+0x40+6);
write_date(wendu[ge]);
write_com(0x80+0x40+7);
write_date(0x2e);
write_com(0x80+0x40+8);
write_date(wendu[xiaoshu]);
}
void t1_func()
{
TH1=0X3c;
TL1=0Xb0;
if(timer<20)
{
timer=timer+1;
}
else if(timer==20)
{
timer=0;
if(second==0)
{ sound=0;delay(20000);
second=9;
}
else{second=second-1;sound=1;}
}
}
void main()
{
init_lcd();
while(1)
{
display1();
display2();
delay(10);
}
t1_init();
second=9;
timer=0;
while(1);
}
复制代码
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1