æè¿æ¯ç¨STCåçï¼åºè¯¥å¾å®¹æ移æ¤å°MPS430ä¸çç»ä½ åèä¸ä¸ã
#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit scl=P1^3;
sbit sda=P1^4;
sbit key1=P1^6;
sbit key2=P1^7;
sbit key3=P2^0;
sbit key4=P2^1;
sbit lcrs=P3^7;//æ°æ®/å½ä»¤
sbit lcwr=P3^5;//读/å
sbit lcden=P3^4;//使è½
sbit DS=P2^2;
/*sbit lcrs=P3^4;//æ°æ®/å½ä»¤
sbit lcwr=P3^7;//读/å
sbit lcden=P3^5;//使è½
*/
sbit jrk=P2^2;
sbit cyk=P2^3;
sbit xhk=P2^4;
bit flag=0,rsg=0,not=0,he=0,in=0;
int acon=0,bcon=0,dcon=0,econ=0,
temp=0,y=0,j=0,l=0,cfj=0,ec=0,dc=0,at;
uchar code table[]={48,49,50,51,52,53,54,55,56,57};
uchar code ta1[]={"Temperature UP"};
uchar code ta2[]={"Temperature DN"};
uchar code ta3[]={"Inflator Cycle"};
uchar code ta4[]={"Inflator Time "};
uchar code ta5[]={" Heating UP "};
uchar code ta6[]={" Inflator "};
uchar code table7[]={"Temperature"};
uchar table1[]={0,0,0,'.',0};
uchar table3[]={"AptitudeAquarium"};
uchar table4[]={0,0,0,0,0};
uchar n,c=0;
void delay(uchar);
void wen_kong();
void xh();
void rso();
void weno();
void Init_Com(void)
{
TMOD = 0x11;
PCON = 0x00;
TH1=0x61;
TL1=0x99;
EA=1;
ET1=1;
TR1=1;
}
void delay(uchar count) //delay
{
uint i;
while(count)
{
i=200;
while(i>0)
i--;
count--;
}
}
////åå§å18B20/////////
bit init18b20(void)
{
uint i;
bit no;
DS=0;
i=103;
while(i>0)i--;
DS=1;
i=4;
while(i>0)i--;
no=DS;
if(no==0)
{
DS=1;
i=100;
while(i>0)i--;
no=DS;
if(no==1)
not=0;
else
not=1;
}
else
not=1;
return (not);
}
bit tmpreadbit(void) //读ä¸ä½
{
uint i;
bit dat;
DS=0;
i++;
DS=1;
i++;i++;
dat=DS;
i=8;while(i>0)i--;
return (dat);
}
uchar tmpread(void) //读ä¸ä¸ªåè
{
uchar i,j,dat;
dat=0;
for(i=1;i<=8;i++)
{
j=tmpreadbit();
dat=(j<<7)|(dat>>1); //读åºçæ°æ®æä½ä½å¨æåé¢ï¼è¿æ ·å好ä¸ä¸ªåèå¨DATé
}
return(dat);
}
void tmpwritebyte(uchar dat) //åä¸ä¸ªåèå° ds18b20
{
uint i;
uchar j;
bit testb;
for(j=1;j<=8;j++)
{
testb=dat&0x01;
dat=dat>>1;
if(testb) //write 1
{
DS=0;
i++;i++;
DS=1;
i=8;while(i>0)i--;
}
else
{
DS=0; //write 0
i=8;
while(i>0)i--;
DS=1;
i++;i++;
}
}
}
int tmp() //DS18B20温度读å
{
float tt;
int a,b;
if(init18b20()==0)
{
WDT_CONTR=0x36; /////åç
EA=0;
delay(1);
tmpwritebyte(0xcc); // è·³è¿è¯»ROMæä½
tmpwritebyte(0x44); // å¯å¨æ¸©åº¦è½¬æ¢
delay(10);
init18b20();
delay(1);
tmpwritebyte(0xcc);
tmpwritebyte(0xbe);
a=tmpread();
b=tmpread();
temp=b;
temp<<=8; //å°é«åè温度æ°æ®ä¸ä½åè温度æ°æ®æ´å
temp=temp|a;
c=b>>4;
tt=temp*0.0625;
temp=tt*10+0.5; //æ¾å¤§10åè¾åºå¹¶åèäºå
¥
EA=1;
return temp;
}
else
not=1;
}
//////1062/////////
void ydelay(uint x)
{
uint a,b;
for(a=x;a>0;a--)
for(b=10;b>0;b--);
}
void write_com(uchar com)
{
P0=com;
lcwr=0;
lcrs=0;
lcden=0;
ydelay(10);
lcden=1;
ydelay(10);
lcden=0;
lcwr=1;
}
void write_date(uchar date)//åæ°æ®
{
P0=date;
lcwr=0;
lcrs=1;
lcden=0;
ydelay(10);
lcden=1;
ydelay(10);
lcden=0;
lcwr=1;
}
void init1602()//åå§å
{
write_com(0x38);//设置æ¾ç¤ºæ¨¡å¼
ydelay(20);
write_com(0x0c);//å¼æ¾ç¤º
ydelay(20);
write_com(0x06);//æéåå
æ èªå¨å ä¸
ydelay(20);
write_com(0x01);//æ¸
å±æ令
ydelay(20);
}
///////æ¾ç¤ºç¨åº//////
void display(int num)
{
uint i,A1,A2;
WDT_CONTR=0x35; /////åç
if(c!=0)
num=~num+1;
A1=num/1000;
A2=num%1000/100;
if(not==0)
{
if(c!=0)
{
c=0;
table1[0]='-';
}
else if(A1==0)
table1[0]=' ';
else
table1[0]=table[A1];
if(A1==0)
if(A2==0)
table1[1]=' ';
else
table1[1]=table[A2];
table1[2]=table[num%1000%100/10];
table1[4]=table[num%1000%100%10];
}
else
{
table1[0]='?';
table1[1]='?';
table1[2]='?';
table1[4]='?';
}
write_com(0x80);
for(i=0;i<11;i++)
{write_date(table7[i]);
delay(2);}
write_com(0x8b);
for(i=0;i<5;i++)
{write_date(table1[i]);
delay(2);}
write_com(0xc0);
for(i=0;i<16;i++)
{
if(he==1)
write_date(ta5[i]);
else if(in==1)
write_date(ta6[i]);
else
write_date(table3[i]);
}
c=0;
WDT_CONTR=0x35; /////åç
}
////æ¾ç¤º2////////////////////
display2(uchar bh,int dat)
{
uchar a,A,B;
WDT_CONTR=0x35; /////åç
//write_com(0x01);//æ¸
å±æ令
y=dat;
y=y&0x8000;
if(y!=0)
dat=~dat+1;
A=dat/1000;
B=dat%1000/100;
if((bh!=4)&&(bh!=5))
{
if(A!=0)
table4[0]=table[dat/1000];
else if((c!=0)||(y!=0))
{
c=0;y=0;
table4[0]='-';
}
else
table4[0]=' ';
if(B!=0)
table4[1]=table[B];
else
table4[1]=' ';
table4[2]=table[dat%1000%100/10];
table4[3]='.';
table4[4]=table[dat%1000%100%10];
}
else
{
table4[0]=' ';
if((c!=0)||(y!=0))
{
c=0;y=0;
table4[1]='-';
}
else
table4[1]=' ';
table4[2]=' ';
table4[3]=table[dat%1000%100/10];
table4[4]=table[dat%1000%100%10];
}
write_com(0xc4);
delay(2);
for(a=0;a<5;a++)
write_date(table4[a]);
delay(2);
write_com(0x80);
switch(bh)
{
case 1:for(a=0;a<14;a++)write_date(ta1[a]);break;
case 2:for(a=0;a<14;a++)write_date(ta2[a]);break;
case 3:for(a=0;a<14;a++)write_date(ta3[a]);break;
case 4:for(a=0;a<14;a++)write_date(ta4[a]);break;
default:break;
}
}
///////////x24c02//////////////////
void delay24()
{ ;; }
void init24c02() //åå§å
{
sda=1;
delay24();
scl=1;
delay24();
}
void start() //å¼å§ä¿¡å·
{
sda=1;
delay24();
scl=1;
delay24();
sda=0;
delay24();
}
void stop() //åæ¢
{
sda=0;
delay24();
scl=1;
delay24();
sda=1;
delay24();
}
void respons() //åºç
{
uchar i;
scl=1;
delay24();
while((sda==1)&&(i<250))i++;
scl=0;
delay24();
}
void write_byte(uchar date) // åæ°æ®åå½æ°
{
uchar i,temp;
temp=date;
for(i=0;i<8;i++)
{
temp=temp<<1;
scl=0;
delay24();
sda=CY;
delay24();
scl=1;
delay24();
}
scl=0;
delay24();
sda=1;
delay24();
}
uchar read_byte() // 读æ°æ®åå½æ°
{
uchar i,k;
scl=0;
delay24();
sda=1;
delay24();
for(i=0;i<8;i++)
{
scl=1;
delay24();
k=(k<<1)|sda;
scl=0;
delay24();
}
return k;
}
///////åæ°æ®å½æ°///////////////////
void write_add(uchar address,uint date)
{
start();
write_byte(0xa0);
respons();
write_byte(address);
respons();
write_byte(date/256);
respons();
write_byte(date%256);
respons();
stop();
}
uchar read_add(uchar address) //读æ°æ®å½æ°
{
uchar date;
start();
write_byte(0xa0);
respons();
write_byte(address);
respons();
start();
write_byte(0xa1);
respons();
date=read_byte();
stop();
return date;
}
void delay1ms(uchar ms)
{
uchar i;
while(ms--)
{
for(i = 0; i< 250; i++)
{
_nop_();
_nop_();
_nop_();
_nop_();
}
}
}
int keyf(int *num,int up,int dn)
{
uint i;
uchar z;
for(i=0;i<600;i++)
{
display2(n,*num);
if(key1==0)
{
delay1ms(30);
if(key1==0)
{
i=0;
n++;
if(n>=9)
n=0;
while(!key1)
display2(n,*num);
break;
}
}
if(key2==0)
{
delay1ms(10);
if(key2==0)
{
i=0;
if(*num>=up)
*num=up;
else if(n!=4)
*num=*num+1;
else if(*num<100)
*num=*num+5;
else
*num=*num+10;
for(z=0;z<65;z++)
{
display2(n,*num);
if(key2!=0)
break;
}
while(!key2)
{
for(z=0;z<2;z++)
display2(n,*num);
if(*num>=up)
*num=up;
else if(n!=4)
*num=*num+1;
else if(*num<100)
*num=*num+5;
else
*num=*num+10;
}
}
}
if(key3==0)
{
delay1ms(10);
if(key3==0)
{
i=0;
if(*num<=dn)
*num=dn;
else if(n!=4)
*num=*num-1;
else if(*num<100)
*num=*num-5;
else
*num=*num-10;
for(z=0;z<65;z++)
{
display2(n,*num);
if(key3!=0)
break;
}
while(!key3)
{
for(z=0;z<2;z++)
display2(n,*num);
if(*num<=dn)
*num=dn;
else if(n!=4)
*num=*num-1;
else if(*num<100)
*num=*num-5;
else
*num=*num-10;
}
}
}
}
return(*num);
}
void keyjc()
{
uchar i=0;
if(key1==0)
{
delay1ms(10);
if(key1==0)
{
EA=0;
for(i=0;i<20;i++)
{
display(tmp());
}
if(key1==0)
{
write_com(0x01);//æ¸
å±æ令
n++;
if(n>=5)
n=0;
while(!key1)
{
switch(n)
{
case 1:display2(n,acon);break;
case 0:break;
}
}
if(n==1)
{
keyf(&acon,1250,-530);
if((acon-bcon)<3)
bcon=acon-3;
}
if(n==2)
{
keyf(&bcon,1240,-550);
if((acon-bcon)<3)
acon=bcon+3;
}
write_add(1,acon);//A
delay1ms(15);
write_add(3,bcon);//B
n=0;
write_com(0x01);//æ¸
å±æ令
}
EA=1;
}
}
}
key()
{
uint i;
if(key4==0)
delay1ms(50);
if(key4==0)
{
write_com(0x01);//æ¸
å±æ令
for(i=0;i<500;i++)
{
if(key4==0)
{
delay1ms(15);
if(key4==0)
{
i=0;
n++;
if(n>=5)
n=0;
while(!key4)
{
switch(n)
{
case 1: display2(1,acon);break;
case 2: display2(2,bcon);break;
default: break;
}
}
}
}
switch(n)
{
case 1: display2(1,acon);break;
case 2: display2(2,bcon);break;
default: break;
}
}
n=0;
}
}
///////滤波////////
int filter()
{
int tm,buf[6];
uchar i,j;
EA=0;
for(i=0;i<6;i++)
{
buf[i]=tmp();
delay1ms(20);
WDT_CONTR=0x35; /////åç
}
for(j=0;j<5;j++)
for(i=0;i<5-j;i++)
if(buf[i]>buf[i+1])
{
tm=buf[i];
buf[i]=buf[i+1];
buf[i+1]=tm;
}
tm=((buf[2]+buf[3])/2);
EA=1;
return (tm);
}
void main()
{
uchar b,c;
Init_Com();
init1602();
init24c02();
b=read_add(1);
delay1ms(15);
c=read_add(2);
delay1ms(15);
acon=b*256+c;
b=read_add(3);
delay1ms(15);
c=read_add(4);
delay1ms(15);
bcon=b*256+c;
AUXR=0x01;// ç¦æ¢ALEè¾åº
WDT_CONTR=0x35; //å¯å¨çé¨ç
write_com(0x01);//æ¸
å±æ令
while(1)
{
at=filter();
display(at);
keyjc();
key();
wen_kong();
weno();
}
}
//////温度æ§å¶//////////////
void wen_kong()
{
if((flag==0)&&(not==0))
{
at=filter();
if(at<=bcon)
{
flag=1;
jrk=0;
xhk=0;
he=1;
}
}
}
void weno()
{
if(flag)
{
at=filter();
if(at>=acon)
{
flag=0;
jrk=1;
if(rsg)
xhk=0;
else
xhk=1;
he=0;
}
}
if(not==1)
{
flag=0;
jrk=1;
if(rsg)
xhk=0;
else
xhk=1;
he=0;
}
}
温馨提示:内容为网友见解,仅供参考