#include <stc15f2k60s2.h>
#include <intrins.h>
#include <string.h>
#include <onewire.h>
#include <iic.h>
#define uchar unsigned char
#define somenop{_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();};
sbit TX=P1^0;
sbit RX=P1^1;
code uchar discode[8]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
uchar dis[9]={0,0,0,0,0,0,0,0,0};
code uchar tab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10,0xbf,0xff,0xc6,0xc7,0xc8,0x8c};
uchar str[20]={""};
code uchar str1[]={"ST\r\n"};
code uchar str2[]={"PARA\r\n"};
uchar str3[20]={""};
uchar str4[20]={""};
uchar code err[]={"error\r\n"};
uchar i_serial;
bit flag_DP;
bit flag_senddat;
bit flag_sendp;
bit flag_sende;
bit flag_3,flag_12,flag_13;
bit flag_2,flag_9,flag_8;
int time13,time12;
uchar temp_wendu,temp_juli;
bit flag_DA=1;
uchar P_wendu=30,P_juli=35;
int N;
uchar t_200ms,t_20ms,t_50ms;
bit flag_200ms,flag_50ms,flag_20ms;
uchar flag_shuju,flag_canshu;
uchar distance_dat=99;
long temperature;
void delayms(int ms);
void allinit();
void display();
void key_scan();
long temper();
void DAtrans(uchar temp);
void store_write(uchar add,uchar temp);
uchar store_read(uchar add);
void sendwave();
int distance_super();
void UartInit(void);
void Timer0Init(void);
void Timer1Init(void);
void senddat(uchar temp);
void sendstring(uchar *str);
void DAC_out();
void led();
void main()
{
allinit();
UartInit();
Timer0Init();
Timer1Init();
N=store_read(0x21);delayms(2);
str3[0]='$';str3[3]=',';str3[6]='.';str3[9]='\r';str3[10]='\n';
str4[0]='#';str4[3]=',';str4[6]='\r';str4[7]='\n';
while(1)
{
DAC_out();
led();
temperature=temper();
if((str[0]=='S')&&(str[1]=='T')&&(flag_senddat==1))
{
flag_senddat=0;
str3[1]=distance_dat/10+'0';
str3[2]=distance_dat%10+'0';
str3[4]=temperature/100000+'0';
str3[5]=temperature/10000%10+'0';
str3[7]=temperature/1000%10+'0';
str3[8]=temperature/100%10+'0';
sendstring(str3);
i_serial=0;
}
if((str[0]=='P')&&(str[1]=='A')&&(str[2]=='R')&&(str[3]=='A')&&(flag_sendp==1))
{
flag_sendp=0;
str4[1]=P_juli/10+'0';
str4[2]=P_juli%10+'0';
str4[4]=P_wendu/10+'0';
str4[5]=P_wendu%10+'0';
sendstring(str4);
i_serial=0;
}
if(((str[0]!='S')||(str[1]!='T'))&&((str[0]!='P')||(str[1]!='A')||(str[2]!='R')||(str[3]!='A'))&&(flag_sende==1))
{
sendstring(err);i_serial=0;flag_sende=0;
}
if(flag_DP==0)
{
if(flag_shuju==0)
{
dis[5]=temperature/100000;dis[6]=temperature/10000%10+10;dis[7]=temperature/1000%10;dis[8]=temperature/100%10;
dis[1]=22;dis[2]=21;dis[3]=21;dis[4]=21;
}
else if(flag_shuju==1)
{
if(flag_20ms==1)
{
flag_20ms=0;
distance_dat=distance_super();
}
dis[5]=21;dis[6]=21;dis[7]=distance_dat/10;dis[8]=distance_dat%10;
dis[1]=23;dis[2]=21;dis[3]=21;dis[4]=21;
}
else if(flag_shuju==2)
{
if((N>=0)&&(N<10))
{
dis[5]=21;dis[6]=21;dis[7]=21;dis[8]=N;
dis[1]=24;dis[2]=21;dis[3]=21;dis[4]=21;
}
else if((N>=10)&&(N<100))
{
dis[5]=21;dis[6]=21;dis[7]=N/10;dis[8]=N%10;
dis[1]=24;dis[2]=21;dis[3]=21;dis[4]=21;
}
else if((N>=100)&&(N<1000))
{
dis[5]=21;dis[6]=N/100;dis[7]=N/10%10;dis[8]=N%10;
dis[1]=24;dis[2]=21;dis[3]=21;dis[4]=21;
}
else if((N>=1000)&&(N<10000))
{
dis[5]=N/1000;dis[6]=N/100%10;dis[7]=N/10%10;dis[8]=N%10;
dis[1]=24;dis[2]=21;dis[3]=21;dis[4]=21;
}
else if((N>=10000)&&(N<100000))
{
dis[5]=N/1000%10;dis[6]=N/100%10;dis[7]=N/10%10;dis[8]=N%10;
dis[1]=24;dis[2]=21;dis[3]=21;dis[4]=N/10000;
}
}
}
else if(flag_DP==1)
{
if(flag_canshu==0)
{
dis[5]=21;dis[6]=21;dis[7]=P_wendu/10;dis[8]=P_wendu%10;
dis[1]=25;dis[2]=21;dis[3]=21;dis[4]=1;
}
else if(flag_canshu==1)
{
dis[5]=21;dis[6]=21;dis[7]=P_juli/10;dis[8]=P_juli%10;
dis[1]=25;dis[2]=21;dis[3]=21;dis[4]=2;
}
}
display();
key_scan();
}
}
void delayms(int ms)
{
int i,j;
for(i=0;i<ms;i++)
for(j=0;j<125;j++);
}
void allinit()
{
P2=0X80;P0=0XFF;
P2=0XA0;P0=0X00;
P2=0XC0;P0=0XFF;
P2=0XE0;P0=0XFF;
}
void display()
{
uchar i;
for(i=0;i<8;i++)
{
P2=0XC0;P0=discode[i];
P2=0XE0;P0=tab[dis[i+1]];
delayms(4);
P2=0XC0;P0=0XFF;
P2=0XE0;P0=0XFF;
}
}
void key_scan()
{
uchar temp;
P44=0;P42=1;P3=0x7f;
temp=P3&0x0f;
if(temp!=0x0f)
{
delayms(3);
temp=P3&0x0f;
if(temp!=0x0f)
{
switch(temp)
{
case 0x0e: break;
case 0x0d: break;
case 0x0b: break;
case 0x07: break;
}
while(temp!=0x0f)
{
temp=P3&0x0f;
}
}
}
P44=1;P42=0;P3=0xbf;
temp=P3&0x0f;
if(temp!=0x0f)
{
delayms(3);
temp=P3&0x0f;
if(temp!=0x0f)
{
switch(temp)
{
case 0x0e: sendstring(str1); break;
case 0x0d: break;
case 0x0b:
flag_8=1;flag_2=1;
break;
case 0x07:
flag_9=1;flag_2=1;
break;
}
}
}
P44=1;P42=0;P3=0xbf;
temp=P3&0x0f;
if((temp==0x0f)&&(flag_2==1))
{
delayms(3);
temp=P3&0x0f;
if(temp==0x0f)
{
flag_2=0;
if(flag_8==1)
{
flag_8=0;
if(flag_DP==1)
{
if(flag_canshu==0)
{
P_wendu=P_wendu+2;
}
else if(flag_canshu==1)
{
P_juli=P_juli+5;
}
}
}
else if(flag_9==1)
{
flag_9=0;
if(flag_DP==1)
{
if(flag_canshu==0)
{
P_wendu=P_wendu-2;
}
else if(flag_canshu==1)
{
P_juli=P_juli-5;
}
}
}
}
}
P44=1;P42=1;P3=0xdf;
temp=P3&0x0f;
if(temp!=0x0f)
{
delayms(3);
temp=P3&0x0f;
if(temp!=0x0f)
{
flag_3=1;
switch(temp)
{
case 0x0e: break;
case 0x0d: break;
case 0x0b: flag_13=1; time13++; break;
case 0x07: flag_12=1;time12++; break;
}
}
}
P44=1;P42=1;P3=0xdf;
temp=P3&0x0f;
if((temp==0x0f)&&(flag_2==1))
{
delayms(3);
temp=P3&0x0f;
if(temp==0x0f)
{
flag_3=1;
}
}
P44=1;P42=1;P3=0xdf;
temp=P3&0x0f;
if((temp==0x0f)&&(flag_3==1))
{
delayms(3);
temp=P3&0x0f;
if(temp==0x0f)
{
flag_3=0;
if(flag_13==1)
{
flag_13=0;
if(time13<=80)
{
if(flag_DP==0)
{
flag_DP=1;
temp_wendu=P_wendu;
temp_juli=P_juli;
}
else if(flag_DP==1) flag_DP=0;
if((temp_wendu!=P_wendu)||(temp_juli!=P_juli))
{
N++;store_write(0x21,N);delayms(2);
}
time13=0;
}
else if(time13>80)
{
time13=0;
if(flag_DA==0) flag_DA=1;
else if(flag_DA==1) flag_DA=0;
}
}
if(flag_12==1)
{
flag_12=0;
if(time12<=80)
{
if(flag_DP==0)
{
if(flag_shuju==0) flag_shuju=1;
else if(flag_shuju==1) flag_shuju=2;
else if(flag_shuju==2) flag_shuju=0; time12=0;
}
else if(flag_DP==1)
{
if(flag_canshu==0) flag_canshu=1;
else if(flag_canshu==1) flag_canshu=0;time12=0;
}
}
else if(time12>80)
{
N=0;time12=0;store_write(0x21,N);delayms(2);
}
}
}
}
P44=1;P42=1;P3=0xef;
temp=P3&0x0f;
if(temp!=0x0f)
{
delayms(3);
temp=P3&0x0f;
if(temp!=0x0f)
{
switch(temp)
{
case 0x0e: break;
case 0x0d: break;
case 0x0b: break;
case 0x07: break;
}
while(temp!=0x0f)
{
temp=P3&0x0f;
}
}
}
}
long temper()
{
uchar low,high;
long temp;
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0x44);
Delay_OneWire(200);
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0xbe);
low=Read_DS18B20();
high=Read_DS18B20();
temp=high&0x0f;
temp=(temp<<8)|low;
temp=temp*625;
return temp;
}
void DAtrans(uchar temp)
{
IIC_Start();
IIC_SendByte(0x90);
IIC_WaitAck();
IIC_SendByte(0x40);
IIC_WaitAck();
IIC_SendByte(temp);
IIC_WaitAck();
IIC_Stop();
}
void store_write(uchar add,uchar temp)
{
IIC_Start();
IIC_SendByte(0xa0);
IIC_WaitAck();
IIC_SendByte(add);
IIC_WaitAck();
IIC_SendByte(temp);
IIC_WaitAck();
IIC_Stop();
}
uchar store_read(uchar add)
{ uchar temp;
IIC_Start();
IIC_SendByte(0xa0);
IIC_WaitAck();
IIC_SendByte(add);
IIC_WaitAck();
IIC_Stop();
IIC_Start();
IIC_SendByte(0xa1);
IIC_WaitAck();
temp=IIC_RecByte();
IIC_Stop();
return temp;
}
void Timer0Init(void)
{
AUXR |= 0x80;
TMOD &= 0xF0;
TL0 = 0x20;
TH0 = 0xD1;
TF0 = 0;
TR0 = 1;
EA=1;ET0=1;
}
void Timer1Init(void)
{
AUXR |= 0x40;
TMOD &= 0x0F;
TMOD |= 0x10;
TF1 = 0;
}
void Timer0() interrupt 1
{
t_200ms++;t_50ms++;t_20ms++;
if(t_200ms>=200)
{
flag_200ms=1;t_200ms=0;
}
if(t_20ms>=20)
{
flag_20ms=1;t_20ms=0;
}
if(t_50ms>=50)
{
flag_50ms=1;t_50ms=0;
}
}
void sendwave()
{
uchar i;
for(i=0;i<8;i++)
{
TX=0;somenop;somenop;somenop;somenop;somenop;somenop;somenop;somenop;somenop;somenop;
TX=1;somenop;somenop;somenop;somenop;somenop;somenop;somenop;somenop;somenop;somenop;
}
}
int distance_super()
{
int distance;
long t_super;
sendwave();TR1=1;
while((RX==1)&&(TF1==0));
TR1=0;
if(TF1==1)
{
distance=99;TF1=0;
TH1=0;TL1=0;
}
else if(RX==0)
{
t_super=TH1<<8|TL1;
distance=t_super*0.017;
distance=distance/12;
TH1=0;TL1=0;
}
return distance;
}
void UartInit(void)
{
SCON = 0x50;
AUXR |= 0x01;
AUXR |= 0x04;
T2L = 0x8F;
T2H = 0xFD;
AUXR |= 0x10;
EA=1;ES=1;
}
void senddat(uchar temp)
{
SBUF=temp;
while(TI==0);
TI=0;
}
void sendstring(uchar *str)
{
while(*str!='\0')
{
senddat(*str);
str++;
}
}
void uartservice() interrupt 4
{
if(RI==1)
{
RI=0;
str[i_serial++]=SBUF;
flag_senddat=1;
flag_sendp=1;
flag_sende=1;
}
}
void DAC_out()
{
if(flag_DA==1)
{
if(distance_dat<=P_juli)
DAtrans(102);
if(distance_dat>P_juli)
DAtrans(204);
}
else
DAtrans(22);
}
void led()
{
uchar tt;
P2=0x80;P03=1;P04=1;P06=1;P07=1;
tt=temperature/100000*10+temperature/10000%10;
if(tt>P_wendu)
{
P2=0x80;P00=0;
}
else if(tt<=P_wendu)
{
P2=0x80;P00=1;
}
if(distance_dat<P_juli)
{
P2=0x80;P01=0;
}
else
{
P2=0X80;P01=1;
}
if(flag_DA==1)
{
P2=0x80;P02=0;
}
else
{
P2=0x80;P02=1;
}
}