#include <STC15F2K60S2.H>
#include<intrins.h>
#define nop _nop_();
#define uchar unsigned char
#define uint unsigned int
typedef unsigned char BYTE; //8位
typedef unsigned int WORD; //16位
/************18b20*************/
sbit DQ=P1^2;
/************蜂鸣器*************/
sbit BB=P2^6;
/************继电器*************/
sbit jdq=P2^7;
/************DS1302*************/
sbit SCLK=P2^5;
sbit IO=P2^4;
sbit RST=P2^3;
/************人体感应*************/
sbit rtgy=P1^4;
/***********写时分秒地址**********/
#define write_nian 0x8c
#define write_xqt 0x8a
#define write_yue 0x88
#define write_ri 0x86
#define write_shi 0x84
#define write_fen 0x82
#define write_miao 0x80
/***********读时分秒地址**********/
#define read_nian 0x8d
#define read_xqt 0x8b
#define read_yue 0x89
#define read_ri 0x87
#define read_shi 0x85
#define read_fen 0x83
#define read_miao 0x81
char shi,fen,miao,ri,yue,xqt,nian;
/***********水位地址**********/
uchar cm=0; //水位
uchar bfb=0; //百分比
uchar swh=0; //计算水位
/***********页面地址**********/
uchar ym=0; //页面
uchar num_1=0; //页面
uchar num_2=0;
uchar num_3=0;
uchar num_4=0;
/*********设置按键地址********/
uchar kzy=0;
uchar kzy_data=0;
uchar num_5=0;
uchar num_6=0;
uchar num_7=0;
uchar num_8=0;
uchar num_9=0;
/***********加减按键地址**********/
uchar kzy_w=0;
uchar kzy_h=0;
uchar num_10=0;
uchar num_11=0;
uchar num_12=0;
uchar num_13=0;
uchar num_14=0;
/***********启动地址**********/
uchar kzy_1=0;
uchar num_15=0;
uchar num_16=0;
uchar num_17=0;
uchar num_18=0;
uchar flag_get1=0; //时间设置标记
uchar flag_get3=0; //水位设置标记
/***********计时地址**********/
uchar js1=0;
uchar js2=0;
uchar js3=60;
uchar js4=0;
uchar js5=10;
/***********睡眠地址**********/
uchar sm1=0;
uchar sm2=120;
uchar sm3=0;
/************IAP/EEPROM**********/
#define CMD_IDLE 0 //空闲模式
#define CMD_READ 1 //IAP字节读命令
#define CMD_PROGRAM 2 //IAP字节编程命令
#define CMD_ERASE 3 //IAP扇区擦除命令
//#define ENABLE_IAP 0x80 //if SYSCLK<30MHz
//#define ENABLE_IAP 0x81 //if SYSCLK<24MHz
//#define ENABLE_IAP 0x82 //if SYSCLK<20MHz
#define ENABLE_IAP 0x83 //if SYSCLK<12MHz
#define IAP_ADDRESS1 0x1500 // sscs地址
#define IAP_ADDRESS2 0x1600 // zgsw地址
#define IAP_ADDRESS3 0x1800 // adsw地址
uchar zgsw=0;
uchar zdsw=0;
uchar sscs=0;
uchar zdss=1;
void IapIdle(); //关闭IAP
BYTE IapReadByte(WORD addr); //读一字节数据
void IapProgramByte(WORD addr, BYTE dat); //写一字节数据
void IapEraseSector(WORD addr); //扇区擦除
uchar CODE[10] = {0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39}; //ASCII 0123456789
uchar num1,num2,num3;
uchar TPH,TPL,temp; //存放温度值
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void DelayXus(uchar n)
{
while (n--);
{
_nop_();
_nop_();
}
}
uchar read_1302(uchar add) //读函数
{
uchar i,date;
RST=0; //禁止读写数据
for(i=0;i<8;i++)
{
RST=1;
SCLK=0;
IO=add&0x01;
add>>=1;
SCLK=1;
}
for(i=0;i<8;i++)
{
if(IO)
date=date|0x80;
else
date=date|0x00;
SCLK=1;
date>>=1;
SCLK=0;
}
RST=0;
date=date/16*10+date%16; //进制转换 16转10进制
IO=0; //数据读完后,IO口必须为0,不然小时一直显示85
return date;
}
void write_1302(uchar add,uchar dat5) //写函数
{
uchar i;
RST=0; //禁止读写数据
// SCLK=0;
for(i=0;i<8;i++) //写地址
{
RST=1; //允许读写数据
SCLK=0;
IO=add&0x01;
add>>=1;
SCLK=1;
}
for(i=0;i<8;i++) //写数据
{
RST=1; //允许读写数据
SCLK=0;
IO=dat5&0x01;
dat5>>=1;
SCLK=1;
}
RST=0;
}
void read_sf()
{
miao=read_1302(read_miao);
fen=read_1302(read_fen);
shi=read_1302(read_shi);
ri=read_1302(read_ri);
yue=read_1302(read_yue);
xqt=read_1302(read_xqt);
nian=read_1302(read_nian);
}
/**************************************
复位DS18B20,并检测设备是否存在
**************************************/
void DS18B20_Reset()
{
CY = 1;
while (CY)
{
DQ = 0; //送出低电平复位信号
DelayXus(240); //延时至少480us
DelayXus(240);
DQ = 1; //释放数据线
DelayXus(60); //等待60us
CY = DQ; //检测存在脉冲
DelayXus(240); //等待设备释放数据线
DelayXus(180);
}
}
/**************************************
从DS18B20读1字节数据
**************************************/
uchar DS18B20_ReadByte()
{
uchar i;
uchar dat1 = 0;
for (i=0; i<8; i++) //8位计数器
{
dat1 >>= 1;
DQ = 0; //开始时间片
DelayXus(1); //延时等待
DQ = 1; //准备接收
DelayXus(1); //接收延时
if (DQ) dat1 |= 0x80; //读取数据
DelayXus(60); //等待时间片结束
}
return dat1;
}
/**************************************
向DS18B20写1字节数据
**************************************/
void DS18B20_WriteByte(uchar dat1)
{
char i;
for (i=0; i<8; i++) //8位计数器
{
DQ = 0; //开始时间片
DelayXus(1); //延时等待
dat1 >>= 1; //送出数据
DQ = CY;
DelayXus(60); //等待时间片结束
DQ = 1; //恢复数据线
DelayXus(1); //恢复延时
}
}
void DS18B20()
{
DS18B20_Reset(); //设备复位
DS18B20_WriteByte(0xCC); //跳过ROM命令
DS18B20_WriteByte(0x44); //开始转换命令
while (!DQ); //等待转换完成
DS18B20_Reset(); //设备复位
DS18B20_WriteByte(0xCC); //跳过ROM命令
DS18B20_WriteByte(0xBE); //读暂存存储器命令
TPL = DS18B20_ReadByte(); //读温度低字节
TPH = DS18B20_ReadByte(); //读温度高字节
temp=(TPL+TPH*256)*0.0625;
}
/*********串口送数据**********/
void write_COM(uchar COM)
{
SBUF=COM;
while(!TI);
TI=0;
}
/*********发送文本串**********/
void write_txt(char *s)
{
int i=0;
while(s[i]!=0)
{
write_COM(s[i]);
i++;
}
}
/*********发送结束符**********/
void write_END(void)
{
write_COM(0xFF);
write_COM(0xFF);
write_COM(0xFF);
}
/*********串口初始化**********/
void init()
{
//串口1 初始化
S1_USE_P36P37(); //UART1 使用P36 P37口
SCON = 0x50;
AUXR &= 0xBE;
AUXR |= 0x00;
TMOD &= 0x0F;
TMOD |= 0x20;
TH1 = 0xfd;
TL1 = 0xfd;
TR1=1; //定时器1启动
ES = 1; //开放串口中断
EA = 1;
//串口2 初始化
S2CON = 0x10;
T2L = 0xE8;
T2H = 0xFF;
AUXR &= 0xE3;
AUXR |= 0x10;
IE2 &= 0xFE;
IE2 |= 0x01;
}
/************UART1 中断服务程序*******************/
void Uart2(void) interrupt 4 using 1
{
uchar a=0;
if(RI) //如果是接收中断
{
RI=0;
a = SBUF; //将接收缓冲区的数据保存到a变量中
/***********页面接收**********/
if(a==0x66)
num_1=1; //页面
if(num_1==1)
{
num_2++;
if(num_2==2)
{
num_3=a; //页面ID号
}
if(a==0xff) //结束
num_4++;
if(num_4==3)
{
ym=num_3;
num_4=0;
num_1=0;
num_2=0;
}
}
/***********设置按键接收**********/
if(a==0x65)
num_5=1;
if(num_5==1)
{
num_6++;
if(num_6==2)
num_7=a;
if(num_6==3)
num_8=a;
if(a==0xff)
{
num_9++;
if(num_9==3)
{
kzy=num_7;
kzy_data=num_8;
num_9=0;
num_5=0;
num_6=0;
}
}
}
/***********加减按键接收**********/
if(a==0x64)
num_10=1;
if(num_10==1)
{
num_11++;
if(num_11==2)
num_12=a;
if(num_11==3)
num_13=a;
if(a==0xff)
num_14++;
if(num_14==3)
{
kzy_w=num_12;
kzy_h=num_13;
num_14=0;
num_10=0;
num_11=0;
}
}
/***********启动按键接收**********/
if(a==0x71)
num_15=1;
if(num_15==1)
{
num_16++;
if(num_16==2)
num_17=a;
if(a==0xff)
num_18++;
if(num_18==3)
{
kzy_1=num_17;
num_15=0;
num_16=0;
num_18=0;
}
}
}
else //如果是发送中断,将TI清0
TI = 0;
}
/************UART2 中断服务程序*******************/
void Uart4() interrupt 8 using 2
{
if(S2CON & 0x01) //如果是接收中断
{
S2CON &= 0xFE; //接收中断标志位清0
cm = S2BUF; //将接收缓冲区的数据保存到mydata变量中
}
if(S2CON & 0x02) //如果是发送中断
{
S2CON &= 0xFD; //发送中断标志清0
}
}
/*----------------------------
关闭IAP
----------------------------*/
void IapIdle()
{
IAP_CONTR = 0; //关闭IAP功能
IAP_CMD = 0; //清除命令寄存器
IAP_TRIG = 0; //清除触发寄存器
IAP_ADDRH = 0x80; //将地址设置到非IAP区域
IAP_ADDRL = 0;
}
/*----------------------------
从ISP/IAP/EEPROM区域读取一字节
----------------------------*/
BYTE IapReadByte(WORD addr)
{
BYTE dat; //数据缓冲区
IAP_CONTR = ENABLE_IAP; //使能IAP
IAP_CMD = CMD_READ; //设置IAP命令
IAP_ADDRL = addr; //设置IAP低地址
IAP_ADDRH = addr >> 8; //设置IAP高地址
IAP_TRIG = 0x5a; //写触发命令(0x5a)
IAP_TRIG = 0xa5; //写触发命令(0xa5)
_nop_(); //等待ISP/IAP/EEPROM操作完成
dat = IAP_DATA; //读ISP/IAP/EEPROM数据
IapIdle(); //关闭IAP功能
return dat; //返回
}
/*----------------------------
写一字节数据到ISP/IAP/EEPROM区域
----------------------------*/
void IapProgramByte(WORD addr, BYTE dat)
{
IAP_CONTR = ENABLE_IAP; //使能IAP
IAP_CMD = CMD_PROGRAM; //设置IAP命令
IAP_ADDRL = addr; //设置IAP低地址
IAP_ADDRH = addr >> 8; //设置IAP高地址
IAP_DATA = dat; //写ISP/IAP/EEPROM数据
IAP_TRIG = 0x5a; //写触发命令(0x5a)
IAP_TRIG = 0xa5; //写触发命令(0xa5)
_nop_(); //等待ISP/IAP/EEPROM操作完成
IapIdle();
}
/*----------------------------
扇区擦除
----------------------------*/
void IapEraseSector(WORD addr)
{
IAP_CONTR = ENABLE_IAP; //使能IAP
IAP_CMD = CMD_ERASE; //设置IAP命令
IAP_ADDRL = addr; //设置IAP低地址
IAP_ADDRH = addr >> 8; //设置IAP高地址
IAP_TRIG = 0x5a; //写触发命令(0x5a)
IAP_TRIG = 0xa5; //写触发命令(0xa5)
_nop_(); //等待ISP/IAP/EEPROM操作完成
IapIdle();
}
void page0()
{
uchar a=0,b=0,c=0,d=0,e=0,f=0;
swh=zgsw+30-cm; //计算水位
bfb=100*swh/zgsw; //计算百分比
a=CODE[bfb%1000/100];
b=CODE[bfb%100/10];
c=CODE[bfb%100%10];
if(bfb>=1)
if(bfb<=100)
{
write_txt("n4.val="); //发送文本
write_COM(a);
write_COM(b);
write_COM(c);
write_END();
delay(100);
}
num1=CODE[miao/10];
num2=CODE[miao%10];
write_txt("n8.val="); //发送文本
write_COM(num1);
write_COM(num2);
write_END();
a=CODE[temp/10]; //发送温度数据
b=CODE[temp%10];
write_txt("t4.txt="); //发送文本
write_COM(34);
write_COM(a);
write_COM(b);
write_txt("度"); //发送文本
write_COM(34);
write_END();
a=CODE[shi/10]; //发送时数据
b=CODE[shi%10];
c=CODE[fen/10]; //发送分钟数据
d=CODE[fen%10];
write_txt("t1.txt="); //发送文本
write_COM(34);
write_COM(a);
write_COM(b);
write_COM(0x3a);
write_COM(c);
write_COM(d);
write_COM(34);
write_END();
a=CODE[nian/10]; //发送数据
b=CODE[nian%10];
c=CODE[yue/10]; //发送数据
d=CODE[yue%10];
e=CODE[ri/10]; //发送温度数据
f=CODE[ri%10];
write_txt("t2.txt="); //发送文本
write_COM(34);
write_txt("20"); //发送文本
write_COM(a);
write_COM(b);
write_COM(0x2d);
write_COM(c);
write_COM(d);
write_COM(0x2d);
write_COM(e);
write_COM(f);
write_COM(34);
write_END();
write_txt("t3.txt="); //发送文本
write_COM(34);
write_txt("星期");
if(xqt==1)
write_txt("一");
if(xqt==2)
write_txt("二");
if(xqt==3)
write_txt("三");
if(xqt==4)
write_txt("四");
if(xqt==5)
write_txt("五");
if(xqt==6)
write_txt("六");
if(xqt==7)
write_txt("日");
write_COM(34);
write_END();
}
void page1()
{
if(flag_get1==0)
{
num1=CODE[fen/10]; //发送分钟数据
num2=CODE[fen%10];
write_txt("n0.val="); //发送文本
write_COM(num1);
write_COM(num2);
write_END();
delay(100);
num1=CODE[shi/10]; //发送时数据
num2=CODE[shi%10];
write_txt("n1.val="); //发送文本
write_COM(num1);
write_COM(num2);
write_END();
delay(100);
num1=CODE[ri/10]; //发送温度数据
num2=CODE[ri%10];
write_txt("n2.val="); //发送文本
write_COM(num1);
write_COM(num2);
write_END();
delay(100);
num1=CODE[yue/10]; //发送温度数据
num2=CODE[yue%10];
write_txt("n3.val="); //发送文本
write_COM(num1);
write_COM(num2);
write_END();
delay(100);
num1=CODE[xqt%10]; //发送温度数据
write_txt("n4.val="); //发送文本
write_COM(num1);
write_END();
delay(100);
num1=CODE[nian/10]; //发送温度数据
num2=CODE[nian%10];
write_txt("n5.val="); //发送文本
write_COM(num1);
write_COM(num2);
write_END();
write_1302(0x8e,0x00); //保护取消
write_1302(write_miao,0x80);
kzy=0;
kzy_data=0;
flag_get1=1;
}
}
void page2()
{
uchar a=0,b=0,c=0;
swh=zgsw+30-cm;
a=CODE[swh%1000/100];
b=CODE[swh%100/10];
c=CODE[swh%100%10];
write_txt("n0.val="); //发送文本
write_COM(a);
write_COM(b);
write_COM(c);
write_END();
delay(100);
if(flag_get3==0)
{
//a=CODE[sscs%1000/100];
b=CODE[sscs%100/10];
c=CODE[sscs%100%10];
write_txt("n1.val="); //发送文本
//write_COM(a);
write_COM(b);
write_COM(c);
write_END();
delay(100);
a=CODE[zgsw%1000/100];
b=CODE[zgsw%100/10];
c=CODE[zgsw%100%10];
write_txt("n2.val="); //发送文本
write_COM(a);
write_COM(b);
write_COM(c);
write_END();
delay(1000);
a=CODE[zdsw%1000/100];
b=CODE[zdsw%100/10];
c=CODE[zdsw%100%10];
write_txt("n3.val="); //发送文本
write_COM(a);
write_COM(b);
write_COM(c);
write_END();
delay(100);
}
}
void keyscan()
{
if(kzy==1)
{
if(kzy_data==1)
{
//write_txt("n0.bco=63488"); //发送文本
//write_END();
if(kzy_w==1)
{
if(kzy_h==0)
{
kzy_h=1;
fen++;
if(fen>=60)
fen=1;
fen=fen/10*16+fen%10; //转为16进制
write_1302(write_fen,fen); //写1302
read_sf(); //读出
num1=CODE[fen/10]; //发送时数据
num2=CODE[fen%10];
write_txt("n0.val="); //发送文本
write_COM(num1);
write_COM(num2);
write_END();
flag_get1=1;
BB=1;
delay(500);
BB=0;
}
}
if(kzy_w==2)
{
if(kzy_h==0) //按键-
{
kzy_h=1;
fen--;
if(fen==0x00)
fen=59;
fen=fen/10*16+fen%10; //转为16进制
write_1302(write_fen,fen); //写1302
read_sf();
num1=CODE[fen/10]; //发送时数据
num2=CODE[fen%10];
write_txt("n0.val="); //发送文本
write_COM(num1);
write_COM(num2);
write_END();
flag_get1=1;
BB=1;
delay(500);
BB=0;
}
}
}
if(kzy_data==0x02)
{
// write_txt("n1.bco=63488");
//write_END();
if(kzy_w==1)
{
if(kzy_h==0)
{
while(!kzy_h)
kzy_h=1;
shi++;
if(shi>=24)
shi=0;
shi=shi/10*16+shi%10; //转为16进制
write_1302(write_shi,shi); //写1302
read_sf();
num1=CODE[shi/10]; //发送时数据
num2=CODE[shi%10];
write_txt("n1.val="); //发送文本
write_COM(num1);
write_COM(num2);
write_END();
flag_get1=1;
BB=1;
delay(500);
BB=0;
}
}
if(kzy_w==2)
{
if(kzy_h==0)
{
kzy_h=1;
shi--;
if(shi==-1)
shi=23;
shi=shi/10*16+shi%10; //转为16进制
write_1302(write_shi,shi); //写1302
read_sf();
num1=CODE[shi/10]; //发送时数据
num2=CODE[shi%10];
write_txt("n1.val="); //发送文本
write_COM(num1);
write_COM(num2);
write_END();
flag_get1=1;
BB=1;
delay(500);
BB=0;
}
}
}
if(kzy_data==3)
{
if(kzy_w==1)
{
if(kzy_h==0)
{
while(!kzy_h)
kzy_h=1;
ri++;
if(ri>=32)
ri=1;
ri=ri/10*16+ri%10; //转为16进制
write_1302(write_ri,ri); //写1302
read_sf();
num1=CODE[ri/10]; //发送时数据
num2=CODE[ri%10];
write_txt("n2.val="); //发送文本
write_COM(num1);
write_COM(num2);
write_END();
flag_get1=1;
BB=1;
delay(500);
BB=0;
}
}
if(kzy_w==2)
{
if(kzy_h==0)
{
kzy_h=1;
ri--;
if(ri==1)
ri=31;
ri=ri/10*16+ri%10; //转为16进制
write_1302(write_ri,ri); //写1302
read_sf();
num1=CODE[ri/10]; //发送时数据
num2=CODE[ri%10];
write_txt("n2.val="); //发送文本
write_COM(num1);
write_COM(num2);
write_END();
flag_get1=1;
BB=1;
delay(500);
BB=0;
}
}
}
if(kzy_data==4)
{
if(kzy_w==1)
{
if(kzy_h==0)
{
kzy_h=1;
yue++;
if(yue>=13)
yue=1;
yue=yue/10*16+yue%10; //转为16进制
write_1302(write_yue,yue); //写1302
read_sf();
num1=CODE[yue/10]; //发送时数据
num2=CODE[yue%10];
write_txt("n3.val="); //发送文本
write_COM(num1);
write_COM(num2);
write_END();
flag_get1=1;
BB=1;
delay(500);
BB=0;
}
}
if(kzy_w==2)
{
if(kzy_h==0)
{
while(!kzy_h)
kzy_h=1;
yue--;
if(yue>=1)
yue=12;
yue=yue/10*16+yue%10; //转为16进制
write_1302(write_yue,yue); //写1302
read_sf();
num1=CODE[yue/10]; //发送时数据
num2=CODE[yue%10];
write_txt("n3.val="); //发送文本
write_COM(num1);
write_COM(num2);
write_END();
flag_get1=1;
BB=1;
delay(500);
BB=0;
}
}
}
if(kzy_data==5)
{
if(kzy_w==1)
{
if(kzy_h==0)
{
kzy_h=1;
xqt++;
if(xqt>=7)
xqt=1;
xqt=xqt/10*16+xqt%10; //转为16进制
write_1302(write_xqt,xqt); //写1302
read_sf();
num1=CODE[xqt/10]; //发送时数据
num2=CODE[xqt%10];
write_txt("n4.val="); //发送文本
write_COM(num1);
write_COM(num2);
write_END();
flag_get1=1;
BB=1;
delay(500);
BB=0;
}
}
if(kzy_w==2)
{
if(kzy_h==0)
{
kzy_h=1;
xqt--;
if(xqt==1)
xqt=7;
xqt=xqt/10*16+xqt%10; //转为16进制
write_1302(write_xqt,xqt); //写1302
read_sf();
num1=CODE[xqt/10]; //发送时数据
num2=CODE[xqt%10];
write_txt("n4.val="); //发送文本
write_COM(num1);
write_COM(num2);
write_END();
flag_get1=1;
BB=1;
delay(500);
BB=0;
}
}
}
if(kzy_data==6)
{
if(kzy_w==1)
{
if(kzy_h==0)
{
kzy_h=1;
nian++;
if(nian>=30)
nian=1;
nian=nian/10*16+nian%10; //转为16进制
write_1302(write_nian,nian); //写1302
read_sf();
num1=CODE[nian/10]; //发送时数据
num2=CODE[nian%10];
write_txt("n5.val="); //发送文本
write_COM(num1);
write_COM(num2);
write_END();
flag_get1=1;
BB=1;
delay(500);
BB=0;
}
}
if(kzy_w==2)
{
if(kzy_h==0)
{
kzy_h=1;
nian--;
if(nian==1)
nian=30;
nian=nian/10*16+nian%10; //转为16进制
write_1302(write_nian,nian); //写1302
read_sf();
num1=CODE[nian/10]; //发送时数据
num2=CODE[nian%10];
write_txt("n5.val="); //发送文本
write_COM(num1);
write_COM(num2);
write_END();
flag_get1=1;
BB=1;
delay(500);
BB=0;
}
}
}
}
}
void keyscan1()
{
uchar a=0,b=0,c=0;
if(kzy==0x02)
{
if(kzy_data==0x01)
{
//write_txt("n0.bco=63488"); //发送文本
//write_END();
if(kzy_w==0x04)
{
if(kzy_h==0x00)
{
kzy_h=1;
sscs++;
if(sscs==61)
sscs=1;
IapEraseSector(IAP_ADDRESS1); //擦除EEPROM数据
IapProgramByte(IAP_ADDRESS1,sscs);//写EEPROM数据
b=CODE[sscs%100/10];
c=CODE[sscs%100%10];
write_txt("n1.val="); //发送文本
write_COM(b);
write_COM(c);
write_END();
BB=1;
delay(500);
BB=0;
flag_get3=1;;
}
}
if(kzy_w==0x05)
{
if(kzy_h==0x00) //按键-
{
kzy_h=1;
sscs--;
if(sscs==0)
sscs=60;
IapEraseSector(IAP_ADDRESS1); //擦除EEPROM数据
IapProgramByte(IAP_ADDRESS1,sscs);//写EEPROM数据
b=CODE[sscs%100/10];
c=CODE[sscs%100%10];
write_txt("n1.val="); //发送文本
write_COM(b);
write_COM(c);
write_END();
BB=1;
delay(500);
BB=0;
flag_get3=1;;
}
}
}
if(kzy_data==0x02)
{
//write_txt("n0.bco=63488"); //发送文本
//write_END();
if(kzy_w==0x04)
{
if(kzy_h==0x00)
{
kzy_h=1;
zgsw++;
if(zgsw>=201)
zgsw=1;
IapEraseSector(IAP_ADDRESS2); //擦除EEPROM数据
IapProgramByte(IAP_ADDRESS2,zgsw);//写EEPROM数据
a=CODE[zgsw%1000/100];
b=CODE[zgsw%100/10];
c=CODE[zgsw%100%10];
write_txt("n2.val="); //发送文本
write_COM(a);
write_COM(b);
write_COM(c);
write_END();
BB=1;
delay(500);
BB=0;
flag_get3=1;;
}
}
if(kzy_w==0x05)
{
if(kzy_h==0x00) //按键-
{
kzy_h=1;
zgsw--;
if(zgsw==0)
zgsw=200;
IapEraseSector(IAP_ADDRESS2); //擦除EEPROM数据
IapProgramByte(IAP_ADDRESS2,zgsw);//写EEPROM数据
a=CODE[zgsw%1000/100];
b=CODE[zgsw%100/10];
c=CODE[zgsw%100%10];
write_txt("n2.val="); //发送文本
write_COM(a);
write_COM(b);
write_COM(c);
write_END();
BB=1;
delay(500);
BB=0;
flag_get3=1;;
}
}
}
if(kzy_data==0x03)
{
//write_txt("n0.bco=63488"); //发送文本
//write_END();
if(kzy_w==0x04)
{
if(kzy_h==0x00)
{
kzy_h=1;
zdsw++;
if(zdsw>=201)
zdsw=1;
IapEraseSector(IAP_ADDRESS3); //擦除EEPROM数据
IapProgramByte(IAP_ADDRESS3,zdsw);//写EEPROM数据
a=CODE[zdsw%1000/100];
b=CODE[zdsw%100/10];
c=CODE[zdsw%100%10];
write_txt("n3.val="); //发送文本
write_COM(a);
write_COM(b);
write_COM(c);
write_END();
BB=1;
delay(500);
BB=0;
flag_get3=1;;
}
}
if(kzy_w==0x05)
{
if(kzy_h==0x00) //按键-
{
kzy_h=1;
zdsw--;
if(zdsw==0)
zdsw=200;
IapEraseSector(IAP_ADDRESS3); //擦除EEPROM数据
IapProgramByte(IAP_ADDRESS3,zdsw);//写EEPROM数据
a=CODE[zdsw%1000/100];
b=CODE[zdsw%100/10];
c=CODE[zdsw%100%10];
write_txt("n3.val="); //发送文本
write_COM(a);
write_COM(b);
write_COM(c);
write_END();
BB=1;
delay(500);
BB=0;
flag_get3=1;;
}
}
}
}
}
/***********上水计时***********/
void ssjs()
{
if(jdq==0)
{
if(js2!=miao)
{
js2=miao;
js3--;
if(js3==0)
{
js3=60;
js1--;
}
}
if(js1==0)
{
jdq=1;
delay(100);
jdq=1;
js4=1;
zdss=1; //手动
js1=sscs;
}
num1=CODE[js1/10];
num2=CODE[js1%10];
write_txt("n9.val=");
write_COM(num1);
write_COM(num2);
write_END();
}
if(jdq==0) //上水超时后报警
{
write_txt("t7.txt=");
write_COM(34);
write_txt("上水");
write_COM(34);
write_END();
}
if(jdq==1)
{
js1=sscs;
if(js4==1)
{
write_txt("t7.txt=");
write_COM(34);
write_txt("超时");
write_COM(34);
write_END();
if(js2!=miao)
{
js2=miao;
js5--;
BB=1;
if(js5==0)
{
js5=10;
BB=0;
js4=0;
}
}
}
else
write_txt("t7.txt="); //发送文本
write_COM(34);
write_txt("停止");
write_COM(34);
write_END();
}
}
/***********启动控制**********/
void qdkz()
{
if(kzy==0) //启动
{
if(kzy_data==1)
{
kzy_data=0;
if(kzy_1==1)
jdq=0;
if(kzy_1==0)
jdq=1;
}
}
if(jdq==0) // 继电器自动关
if(swh>=zgsw) //当水位等于设定值时关继电器
{
jdq=1;
BB=1;
delay(100);
BB=0;
jdq=1;
}
if(zdss==0) // 继电器自动开
if(swh<=zdsw) //当水位等于设定值时关继电器
{
jdq=0;
BB=1;
delay(100);
BB=0;
}
if(kzy==0) // 自动/手动
{
if(kzy_data==3)
{
kzy_data=0;
if(kzy_1==0) //自动
zdss=0;
if(kzy_1==1) //手动
zdss=1;
}
delay(100);
if(zdss==0)
{
write_txt("bt0.val=0");
write_END();
}
if(zdss==1)
{
write_txt("bt0.val=1");
write_END();
}
}
}
/***********睡眠控制**********/
void smkz()
{
if(rtgy==0) //查继人体开关状态
{
write_txt("sleep=0"); //屏幕退出睡眠
write_END();
sm2=120;
}
if(jdq==0) //查继电器状态
{
write_txt("sleep=0"); //屏幕退出睡眠
write_END();
sm2=120;
}
if(jdq==1)
if(rtgy==1)
{
if(sm1!=miao)
{
sm1=miao;
sm2--;
if(sm2==0)
{
sm2=120;
write_txt("sleep=1"); //屏幕进入睡眠
write_END();
}
}
}
}
/***********页面**********/
void ymkz()
{
if(ym==0) //页面0
{
page0();
}
if(ym==1) // 页面1
{
page1();
keyscan();
}
if(ym==2) // 页面2
{
page2();
keyscan1();
}
if(ym==4)// 页面4
{
flag_get3=0;
if(flag_get1==1)
{
write_1302(write_miao,0x00);
write_1302(0x8e,0x80); //保护启动
flag_get1=0;
}
}
}
/***********主**********/
void main()
{
init(); //初始化 串口
BB=0;
jdq=1;
rtgy=1;
zdss=1;
zgsw=IapReadByte(IAP_ADDRESS2); //读EEPROM数据
zdsw=IapReadByte(IAP_ADDRESS3); //读EEPROM数据
sscs=IapReadByte(IAP_ADDRESS1); //读EEPROM数据
delay(500);
delay(500);
delay(500);
delay(500);
js1=sscs;
while(1) //主循环
{
read_sf(); //读时间
ssjs(); //上水计时
qdkz(); //启动
smkz(); //睡眠控制
ymkz(); //页面
if(miao%10==5)
DS18B20(); //读温度
}
}
HMI端代码