STC12单片机温度检测与报警

#include "stc12c5a60s2.h"
#include
#define uint unsigned int
#define uchar unsigned char

sbit psb=P2^1;
sbit cs=P2^4;
sbit sid=P2^5;
sbit sclk=P2^6;
sbit led0=P2^7;

sbit sda=P3^7;
sbit scl=P3^6;
sbit war=P3^5;

uchar disp[3]; //数据处理结果存放

uchar show[8]={0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48}; //ABCDEFGH

uchar address[8]={0x80,0x84,0x90,0x94,0x88,0x8c,0x98,0x9c}; //每一路温度显示起始地址

uchar code ch[]={0x88,0x89,0x8a,0x8b,0x8c,0x8d,0x8e,0x8f}; //AD通道选择

uchar code p1asf[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80}; //P1ASF / AD通道选择

uchar code a1[]="温度范围设置";

uchar code a2[]={"入口密码:"};

uchar code a3[]={"通道号:"};

uchar code a4[]={"温度范围:"};

uchar code a5[]={"警告!"};

uchar code a6[]={"密码输入错误"};

uchar code a7[]={"输入密码:"};

uchar code a8[]={"****************"};

uchar code a9[]="请选择:";

uchar code a10[]="1->";

uchar code a11[]="2->";

uchar code a12[]="设定温度上限";

uchar code a13[]="修改密码";

uchar code a14[]="通道选择0-7";

uchar code a15[]="上限值:";

uchar code a16[]="整体修改上限";

uchar code a17[]="单一修改上限";

uchar code a18[]="上限值:";

uchar code a19[]="新密码:";



void command_dat(uchar choose,uchar date); //12864写数据,写指令
void delay(uint t); //延时
void init(); //AD初始化
void lcd_init(); // 12864初始化
void display(uchar *adess,uchar *sow); // 显示
uchar admax(uchar k); // AD八路转换结果
void time_pros(uchar k); // 数据处理
uchar key(); // 按键
void write(uchar number,uchar address,uchar *p);/// 写
void warm(); //警告
void write_dat(uchar number,uchar address,uchar ppp);
void panduan_in_mima();
void temp_revise();
void up_temp(uchar ppt);
void up_temp_all();
void xiugai_mima();


void start_24(void);
void over_24(void);
void ask_24(void);
void asknot_24(void);
void write_byte_24(uint dat);
uint read_byte_24(void);
void delaymm(void);
void inct(void);
void write_24(uchar add,uint dat); //24C02写函数
uint read_24(uchar add); //24C02读函数


void main()
{

init();
lcd_init();
while(1)
{
display(address,show);
delay(2000);
}
}
/*-----------------------------------------------
名称:温度参数和密码的修改
日期:2013.11
修改:无
内容:
------------------------------------------------*/
void temp_revise()
{
uchar ppt;
command_dat(0xf8,0x01);
write(6,0x80,a9);
write(3,0x90,a10);
write(12,0x92,a12);

write(3,0x88,a11);
write(8,0x8a,a13);
while(1)
{
ppt=key();
switch(ppt)
{
case 1:
command_dat(0xf8,0x01);
write(6,0x80,a9);
write(3,0x90,a1

0);
write(8,0x92,a16);

write(3,0x88,a11);
write(8,0x8a,a17);
while(1)
{
ppt=key();
switch(ppt)
{

case 1:
up_temp_all();
return;
break;

case 2:
command_dat(0xf8,0x01);
write(11,0x90,a14);
ppt=key();
switch(ppt)
{
case 0:
up_temp(ppt);
return;
break;
case 1:
up_temp(ppt);
return;
break;
case 2:
up_temp(ppt);
return;
break;
case 3:
up_temp(ppt);
return;
break;
case 4:
up_temp(ppt);
return;
break;
case 5:
up_temp(ppt);
return;
break;
case 6:
up_temp(ppt);
return;
break;
case 7:
up_temp(ppt);
return;
break;
}
break;



}
}
break;
case 2:
command_dat(0xf8,0x01);
write(16,0x88,a8);
write(16,0x98,a8);
write(8,0x80,a13);
write(8,0x90,a19);
command_dat(0xf8,0x0f);
xiugai_mima();
return;
break;
}
}
}
/*-----------------------------------------------
名称:警告
日期:2013.11
修改:无
内容:
------------------------------------------------*/
void warm()
{

command_dat(0xf8,0x01); // 清屏
write(5,0x80,a5);
write(12,0x91,a6);
write(16,0x88,a8);
write(16,0x98,a8);
delay(10000);
command_dat(0xf8,0x01); // 清屏
command_dat(0xf8,0x90);
write(10,0x90,a7);
write(16,0x88,a8);
write(16,0x98,a8);
}

/*-----------------------------------------------
名称:AD转换
日期:2013.11
修改:无
内容:
------------------------------------------------*/
uchar admax(uchar k) //
{
uint h,l,b;
P1ASF=p1asf[k]; //
ADC_CONTR=ch[k]; //
delay(5);
AUXR1=0x04;
while(ADC_CONTR&0x10==1);
h=ADC_RES;
l=ADC_RESL;
b=h*256+l;
b=(b/1024.0)*10000/99;
return b;
ADC_RESL=0x00;
ADC_RES=0x00;
}

/*-----------------------------------------------
名称:读数据,数据处理
日期:2013.11
修改:无
内容:
------------------------------------------------*/
void t

ime_pros(uchar k)
{
uint ttp;
disp[0]=admax(k)/100;
disp[1]=admax(k)/10%10;
disp[2]=admax(k)%10;
ttp=read_24(1);

if(ttp>50)
{
war=0;
}

else
{
war=1;
}
}
/*-----------------------------------------------
名称:数据显示
日期:2013.11
修改:无
内容:
------------------------------------------------*/
void display(uchar *adess,uchar *sow)
{
uchar k,t;
for(t=0;t<8;t++)
{
time_pros(t);

command_dat(0xf8,*adess++); //写地址

command_dat(0xfa,*sow++); //写标号

command_dat(0xfa,0x20); //空格

for(k=0;k<3;k++)
{
command_dat(0xfa,0x30+disp[k]);
}
}
}
/*-----------------------------------------------
名称:初始化
日期:2013.11
修改:无
内容:
------------------------------------------------*/
void init()
{
psb=0;

IT0=0;
EX0=1;
EA=1;
}
/*-----------------------------------------------
名称:延时函数1ms
日期:2013.11
修改:无
内容:延时1ms
------------------------------------------------*/
void delay(uint t) // 延时
{
uint i,j;
for(i=t;i>0;i--)
for(j=110;j>0;j--);
}
/*-----------------------------------------------
名称:12864显示函数
日期:2013.11
修改:无
内容:
------------------------------------------------*/
void command_dat(uchar choose,uchar date)
{
uchar kt,k;
kt=choose;
sclk=0;
cs=1;
for(k=0;k<8;k++)
{
sid=(bit)(kt&0x80);
sclk=0;
sclk=1;
kt=kt<<1;
}
kt=date;
kt&=0xf0;
for(k=0;k<8;k++)
{
sid=(bit)(kt&0x80);
sclk=0;
sclk=1;
kt=kt<<1;
}
kt=date;
kt<<=4;
for(k=0;k<8;k++)
{
sid=(bit)(kt&0x80);
sclk=0;
sclk=1;
kt=kt<<1;
}
cs=0;
delay(10);
}
/*-----------------------------------------------
名称:12864显示初始化
日期:2013.11
修改:无
内容:
------------------------------------------------*/
void lcd_init()
{
delay(100);
command_dat(0xf8,0x30); //
delay(5);
command_dat(0xf8,0x02); //
delay(5);
command_dat(0xf8,0x06); //
delay(5);
command_dat(0xf8,0x0c); //
delay(5);
command_dat(0xf8,0x01); //
delay(5);
command_dat(0xf8,0x80);
delay(5);
}
/*-----------------------------------------------
名称:外部中断0
日期:2013.11
修改:无
内容:
------------------------------------------------*/
void int0() interrupt 0
{
led0=0;
command_dat(0xf8,0x01); // 清屏
command_dat(0xf8,0x01);
command_dat(0xf8,0x01);
command_dat(0xf8,0x01);
write(16,0x88,a8);
write(16,0x98,a8);
command_dat(0xf8,0x80); // 写初始地址
command_dat(0xfa,0x02); // 开始的图案
write(12,0x81,a1); // “温度范围设置”
command_dat(0xfa,0x02); // 结束的图案

command_dat(0xf8,0x90); // 写第二个地址
write(10,0x90,a2); // “入口密码”
panduan_in_mima();
temp_revise();
}
/*-----------------------------------------------
名称:写数组数据
日期:2013.11
修改:无
内容:
------------------------------------------------*/
void write(uchar number,uchar address,uchar *p)
{
uchar k;
command_dat(0xf8,address);
for(k=0;k{
command_dat(0xfa,*p++);
}
}
/*-----------------------------------------------
名称:写数据
日期:2013.11
修改:无
内容:
------------------------------------------------*/
void write_dat(uchar number,uchar address,uchar ppp)
{
uchar k;
disp[0]=ppp/100;
disp[1]=ppp/10%10;
disp[2]=ppp%10;
command_dat(0xf8,address);
for(k=0;k{
command_dat(0xfa,0x30+disp[k]);
}

}
/*-----------------------------------------------
名称:24C02写函数
日期:2013.11
修改:无
内容:
------------------------------------------------*/
void write_24(uchar add,uint dat)
{
inct();
start_24();
write_byte_24(0xa0);
ask_24();
write_byte_24(add);
ask_24();
write_byte_24(dat);
ask_24();
over_24();
delay(20);
}
/*-----------------------------------------------
名称:24C02读函数
日期:2013.11
修改:无
内容:
------------------------------------------------*/
uint read_24(uchar add)
{
uint k;
inct();
start_24();
write_byte_24(0xa0);
ask_24();
write_byte_24(add);
ask_24();
start_24();
write_byte_24(0xa1);
ask_24();
k=read_byte_24();
asknot_24();
over_24();
return k;
}
/*-----------------------------------------------
名称:初始化
日期:2013.11
修改:无
内容:
------------------------------------------------*/
void inct(void)
{
sda=1;
scl=1;
}
/*-----------------------------------------------
名称:起始信号
日期:2013.11
修改:无
内容:
------------------------------------------------*/
void start_24(void)
{
sda=1;
delaymm();
scl=1;
delaymm();
sda=0;
delaymm();
}
/*-----------------------------------------------
名称:终止信号
日期:2013.11
修改:无
内容:
------------------------------------------------*/
void over_24()
{
sda=0;
delaymm();
scl=1;
delaymm();
sda=1;
delaymm();
}
/*-----------------------------------------------
名称:应答信号
日期:2013.11
修改:无
内容:
------------------------------------------------*/
void ask_24()
{
uchar k;
scl=1;
delaymm();
while((sda==1)&&(k<200))k++;
scl=0;
delaymm();
}
/*-----------------------------------------------
名称:应答非信号
日期:2013.11

改:无
内容:
------------------------------------------------*/
void asknot_24()
{
sda=1;
delaymm();
scl=1;
delaymm();
scl=0;
delaymm();
}
/*-----------------------------------------------
名称:写一个字节
日期:2013.11
修改:无
内容:
------------------------------------------------*/
void write_byte_24(uint dat)
{
uchar k;
scl=0;
for(k=0;k<8;k++)
{
if(dat&0x80)
{
sda=1;
}
else
{
sda=0;
}
dat=dat<<1;
delaymm();
scl=1;
delaymm();
scl=0;
delaymm();
}
sda=1;
delaymm();
}
/*-----------------------------------------------
名称:读一个字节
日期:2013.11
修改:无
内容:
------------------------------------------------*/
uint read_byte_24()
{
uchar k;
uint dat;
scl=0;
delaymm();
sda=1;
delaymm();
for(k=0;k<8;k++)
{

scl=1;
delaymm();
dat=dat<<1;
if(sda)
{
dat++;
}
scl=0;
delaymm();
}
return dat;
}
/*-----------------------------------------------
名称:24C02延时
日期:2013.11
修改:无
内容:
------------------------------------------------*/
void delaymm()
{
_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();
}
/*-----------------------------------------------
名称:矩阵键盘
日期:2013.11
修改:无
内容:
------------------------------------------------*/
uchar key()
{
uchar ky,dat;
while(1)
{
ky=0;
P0=0xf0;
delay(1);
if(P0!=0xf0)
{
delay(1);
if(P0!=0xf0)
{
ky=P0;
P0=ky|0x0f;
ky=P0;
}
}
switch(ky)
{
case 0x77:
// command_dat(0xfa,0x0f);
while(P0!=0xf0)
{
P0=0xf0;
delay(20);
}
dat=0;
return dat;
break;//0
case 0x7e:
// command_dat(0xfa,0x0f);
while(P0!=0xf0)
{
P0=0xf0;
delay(20);
}
dat=1;
return dat;
break;//1 按下相应的键显示相对应的码值
case 0xbe:
// command_dat(0xfa,0x0f);
while(P0!=0xf0)
{
P0=0xf0;
delay(20);
}
dat=2;
return dat;
break;//2
case 0xde:
// command_dat(0xfa,0x0f);
while(P0!=0xf0)
{
P0=0xf0;
delay(20);
}
dat=3;
return dat;
break;//3
case 0x7d:
// command_dat(0xfa,0x0f);
while(P0!=0xf0)
{
P0=0xf0;
delay(20);
}
dat=4;
return dat;
break;//4
case 0xbd:
// command_dat(0xfa,0x0f);

while(P0!=0xf0)
{
P0=0xf0;
delay(20);
}
dat=5;
return dat;
break;//5
case 0xdd:
// command_dat(0 xfa,0x0f);
while(P0!=0xf0)
{
P0=0xf0;
delay(20);
}
dat=6;
return dat;
break;//6
case 0x7b:
// command_dat(0xfa,0x0f);
while(P0!=0xf0)
{
P0=0xf0;
delay(20);
}
dat=7;
return dat;
break;//7
case 0xbb:
// command_dat(0xfa,0x0f);
while(P0!=0xf0)
{
P0=0xf0;
delay(20);
}
dat=8;
return dat;
break;//8
case 0xdb:
// command_dat(0xfa,0x0f);
while(P0!=0xf0)
{
P0=0xf0;
delay(20);
}
dat=9;
return dat;
break;//9


// case 0xb7:a=20;break;//小数点
// case 0xd7:a=11;break;//等于
// case 0xee:a=12;break;//加
// case 0xed:a=13;break;//减
// case 0xeb:a=14;break;//乘
case 0xe7:
// command_dat(0xfa,0x0f);
while(P0!=0xf0)
{
P0=0xf0;
delay(20);
}
dat=16;
return dat;
break;//9
// default:break;
}
}
}
/*-----------------------------------------------
名称:输入密码与校验
日期:2013.11
修改:无
内容:
------------------------------------------------*/
void panduan_in_mima()
{
uchar pu=0;
uchar k;
uint tt=0;
while(1)
{
for(k=0;k<3;k++)
{
pu=key();
command_dat(0xfa,0x0f); //输入密码
delay(200);
if(k==0)
{
tt=pu*100;
pu=0;
}
else if(k==1)
{
tt=tt+pu*10;
pu=0;
}
else if(k==2)
{
tt=tt+pu;
pu=0;
}
}
pu=read_24(10);
// write_dat(3,0x98,pu);
while(key()!=16);

if(tt==pu)
{
command_dat(0xf8,0x01);
tt=0;
break;
}
else
{
warm();
command_dat(0xf8,0x95);
tt=0;
}
}
}
/*-----------------------------------------------
名称:上限温度设定
日期:2013.11
修改:无
内容:
------------------------------------------------*/
void up_temp(uchar ppt)
{
uchar pu,k,tt;
command_dat(0xf8,0x01);
write(16,0x88,a8);
write(16,0x98,a8);
command_dat(0xf8,0x80);
command_dat(0xfa,0x30+ppt);
write(8,0x90,a15);
command_dat(0xf8,0x0f);
for(k=0;k<2;k++)
{
pu=key();
if(k==0)
{
command_dat(0xfa,0x30+pu);

tt=pu*10;
pu=0;
}
else if(k==1)
{
command_dat(0xfa,0x30+pu);
tt=tt+pu;
pu=0;
}
}
write_24(ppt,tt);
ppt=0;
while(key()!=16);
command_dat(0xf8,0x01);
command_dat(0xf8,0x0c);
}
/*-----------------------------------------------
名称:上限温度整体设定
日期:2013.11
修改:无
内容:
------------------------------------------------*/
void up_temp_all()
{
uchar pu,k,tt;
command_dat(0xf8,0x01);
write(16,0x88,a8);
write(16,0x98,a8);
command_dat(0xf8,0x80);
write(4,0x80,a16);
write(8,0x90,a15);
command_dat(0xf8,0x0f);
for(k=0;k<2;k++)
{
pu=key();
if(k==0)
{
command_dat(0xfa,0x30+pu);
tt=pu*10;
pu=0;
}
else if(k==1)
{
command_dat(0xfa,0x30+pu);
tt=tt+pu;
pu=0;
}
}
for(k=0;k<8;k++)
{
write_24(k,tt);
}
// write_dat(3,0x85,tt);
while(key()!=16);
command_dat(0xf8,0x01);
command_dat(0xf8,0x0c);
}

/*-----------------------------------------------
名称:密码修改
日期:2013.11
修改:无
内容:
------------------------------------------------*/
void xiugai_mima()
{
uchar pu=0;
uchar k;
uint tt=0;
for(k=0;k<3;k++)
{
pu=key();
command_dat(0xfa,0x0f); //输入密码
delay(200);
if(k==0)
{
tt=pu*100;
pu=0;
}
else if(k==1)
{
tt=tt+pu*10;
pu=0;
}
else if(k==2)
{
tt=tt+pu;
pu=0;
}
}
write_24(10,tt);
while(key()!=16);
tt=0;
command_dat(0xf8,0x01);
command_dat(0xf8,0x0c);
}

相关文档
最新文档