📄 bxfor12c2052.c
字号:
/*#include <absacc.h>
#include <reg2051.h>
#include <stdio.h>
#include <intrins.h> */
#include <display.c>
#define ulong unsigned long
#define uint unsigned int
#define uchar unsigned char
uint tempsheding=60;
uint tempwenchar=10;
sbit ctrl=P3^7; //输出控制//
sbit DQ =P3^5; //DS18B201线串口//
sfr seg=0x90; //P1口//
uint data temp_set; //设定温度值存贮位//
uint data temp_odds; //设定高低温差存贮位(+-差)//
uint data temp_top;
uint data temp_bottom;
uint data gaowen; //存贮的最高温度//
uint data diwen; //存贮的最低温度//
uchar data qdtime; //电机开机时间计时器//
uchar data tjtime; //电机停机时间计时器//
uchar data kaijicount=6; //开机次数计数器,6个周期以后才能计算平均值
bit tiaozhengbz;
uchar data tmpgaowen;
uchar data tmpdiwen;
//uint data shuju[4]=0; //16位数据暂存共4个//
//uchar data sjcount; //数据存放计数器//
uint data shujuequ; //温度数据平均值,做为最后数据判断用//
uchar data zhcount; //DS18B20转换时间计数器,在12位分辨率时为750MS//
bit zhuanhuan; //温度转换启动标志//
bit kjbz; //电机开关机标志//
bit err; //温度转换错误标志//
bit reset; //上电标志位//
void int3()interrupt 3 using 2
{
TR1=0;
TH1=0xb8;
TL1=0x10;
TR1=1;
jishitmp++; //计时计数器更新//
jishi20ms++; //计时时间更新,最终以分钟为单位循环计时//
if (jishi20ms>=50)
{
jishi20ms=0;
jishisec++;
if (jishisec>=60)
{
jishisec=0;
jishimin++;
}
}
}
/*void shujupj()
{
uchar i;
uint temp;
for (i=0;i<=3;i++)
{
temp=temp+shuju[i];
}
shujuequ=temp/4;
}
*/
void htod_change() //十六进制转十进制并查表存入显示缓冲区//
{
uint temp;
uchar a[3];
temp=disbuf16;
if (disbuf16>999)
disbuf16=0;
a[0]=temp/100;
temp=temp-a[0]*100;
a[1]=temp/10;
a[2]=temp-a[1]*10;
disbuffer[1]=dismap[a[0]];
disbuffer[2]=dismap[a[1]];
disbuffer[3]=dismap[a[2]];
}
void buff_to_disbuff() //根据键不同将不同的内容送入缓冲区.//
{
switch (keydata)
{
case (0):
{
disbuf16=shujuequ;
disbuffer[0]=0x73;
break;
}
case (1):
{
disbuf16=shujuequ;
disbuffer[0]=0x73;
break;
}
case 2:
{
disbuf16=gaowen;
disbuffer[0]=0x33;
break;
}
case 3:
{
disbuf16=diwen;
disbuffer[0]=0x3c;
break;
}
case 4:
{
disbuf16=qdtime;
disbuffer[0]=0x79;
break;
}
case 5:
{
disbuf16=tjtime;
disbuffer[0]=0x4f;
break;
}
case 0x10:
{
disbuf16=tempset;
disbuffer[0]=0x36;
break;
}
case 0x20:
{
disbuf16=tempodds;
disbuffer[0]=0x09;
break;
}
}
htod_change();
}
bit Init_DS18B20() //18b20初始化//
{
unsigned char x=0;
uchar c=0;
DQ = 1; //DQ复位
delay(8); //稍做延时
DQ = 0; //单片机将DQ拉低
delay(230); //精确延时 大于 480us
DQ = 1; //拉高总线
delay(30);
x=DQ; //稍做延时后 如果x=0则初始化成功 x=1则初始化失败
delay(180);
if (x==0) return 0;
else return 1;
}
//读一个字节
ReadOneChar(void)
{
unsigned char i=0;
unsigned char dat = 0;
for (i=8;i>0;i--)
{
DQ = 0; // 给脉冲信号
dat>>=1;
DQ = 1; // 给脉冲信号
delay(5);
if(DQ)
dat|=0x80;//最高位置1;
delay(15);
}
return(dat);
}
//写一个字节
WriteOneChar(unsigned char dat) //向DS1820 写入一个字节
{
unsigned char i=0;
for (i=8; i>0; i--)
{
DQ = 0;
delay(3);
DQ = dat&0x01;
delay(25);
DQ = 1;
dat>>=1;
}
delay(4);
}
void ds18b20() //18b20温度转换程序//
{
unsigned char a=0, b=0;
unsigned int t=0;
bit c;
if (zhuanhuan==1&&zhcount==jishitmp) //如果有温度转换标志且转换时间到则读取温度转换数据//
{
c=Init_DS18B20();
if (c==0)
{
WriteOneChar(0xCC); //跳过读序号列号的操作
WriteOneChar(0xBE); //读取温度寄存器等(共可读9个寄存器) 前两个就是温度
a=ReadOneChar();
b=ReadOneChar();
t=b;
t<<=8;
t=t|a;
if (t>4096)
{
t=0;
}
else t=t*10/16;
shujuequ=t;
zhuanhuan=0;
zhcount=jishitmp+20;
err=0;
}
else err=1;
}
else if (zhuanhuan==0&&zhcount==jishitmp)
{
zhuanhuan=1;
zhcount=jishitmp+40;
c=Init_DS18B20();
if (c==0)
{
WriteOneChar(0xCC); // 跳过读序号列号的操作
WriteOneChar(0x44); // 启动温度转换
err=0;
}
else err=1;
}
else if (zhcount!=jishitmp)
{
}
}
main()
{
uchar y,z;
start:
y=0x55;
z=0x79;
ET1=1;
EA=1;
TMOD=0x10;
TH1=0xb8;
TL1=0x00;
TR1=1;
reset=1;
gaowen=20;
diwen=500;
qdtime=tjtime=7;
jishimin=0;
tempset=tempsheding; //设定温度值存贮位//
tempodds=tempwenchar;
while (y==0x55&&z==0x79)
{
if (reset==1) //第一次开机,达到温度才关机,但不做为求值的数据
{
if (jishimin>2) //第一次开机时延时3分钟
{
ctrl=0;
delay(10);
if (jishimin>12) //第一次开机时间不得小于10分钟//
{
if (shujuequ<tempset-tempodds)
{
ctrl=1;
delay(10);
qdtime=jishimin-2;
jishimin=0;
reset=0;
}
}
}
}
else //非第一次开机,进入正常运行状态
{
if (ctrl==1) //关机时温度高于最高温度或时间大于50分钟
{
if (jishimin>10)
{
if (shujuequ>tempgao||jishimin>50)
{
ctrl=0;
delay(10);
tjtime=jishimin;
jishimin=0;
tiaozhengbz=1;
}
}
}
else //开机时间必须大于10分钟后降到最低温度值//
{
if (shujuequ<tempdi&&jishimin>10)
{
ctrl=1;
delay(10);
qdtime=jishimin;
jishimin=0;
}
}
}
if (tiaozhengbz==1)
{
tiaozhengbz=0;
gaowen=tmpgaowen;
diwen=tmpdiwen;
tmpgaowen=2;
tmpdiwen=500;
}
//高低温存贮
if (shujuequ>tmpgaowen) tmpgaowen=shujuequ;
if (shujuequ<tmpdiwen) tmpdiwen=shujuequ;
ds18b20(); //温度转换
buff_to_disbuff(); //显示缓冲区
key_dis(); //检测键盘及显示
tempgao=tempset+tempodds;
tempdi=tempset-tempodds;
}
goto start;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -