#include <STC15F2K60S2.H>
#include <ds1302.h>
#include <onewire.h>
#include <iic.h>
#include <intrins.h>
sbit TX=P1^0;
sbit RX=P1^1;
#define Contr_Peri(X,Y) P0=Y;P2=X;P2=0
typedef unsigned char uchar;
typedef unsigned int uint;
uchar dsp_code[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff};
uchar dsp_buff[8]={10,10,10,10,10,10,10,10};
uchar read_number[7];
uchar read_i;
uint temp;
uint count_temp;
uint volt;
uchar count_adc;
char hor1,min1,sec1;
char hor,min,sec;
uint distance;
uint count_dis;
uchar key_stat;
uchar count_key;
uchar key_back;
uchar count_press;
bit flag;
bit busy;
void Delay13us() //@12.000MHz
{
unsigned char i;
_nop_();
_nop_();
i = 36;
while (--i);
}
void Delay10ms() //@12.000MHz
{
unsigned char i, j;
i = 117;
j = 184;
do
{
while (--j);
} while (--i);
}
void Send_Wave()
{
uchar i=0;
for(i=0;i<16;i++)
{
TX=~TX;
Delay13us();
}
}
uint Get_Distance()
{
uint dis;
TH1=TL1=0;
Send_Wave();
TR1=1;
while((RX==1)||(TF1==1));
TR1=0;
if(TH1!=0)
{
dis=(TH1<<8|TL1)*0.017+0.5;
}
else
{
dis=999;
TF1=0;
}
return dis;
}
void Scan_Key()
{
uchar key_val=0,key_x=0,key_y=0;
P3=0X0F;P4=0;
if(!P30) key_x=3;
else if(!P31)key_x=2;
else if(!P32)key_x=1;
else if(!P33)key_x=0;
P3=0xf0;P4=0XFF;
if(!P34) key_y=4;
else if(!P35)key_y=3;
else if(!P42)key_y=2;
else if(!P44)key_y=1;
key_val=key_x+key_y*4;
switch(key_stat)
{
case 0:
if(key_val!=0)
key_stat=1;
break;
case 1:
if(key_val==0)
key_stat=0;
else
{
key_stat=2;
// switch(key_val)
// {
//
// }
}
break;
case 2:
if(key_val==0)
key_stat=3;
else
{
key_stat=0;
key_back=key_val;
if(++count_press>100)
{
flag=1;
// switch(key_val)
// {
//
// }
}
}
break;
case 3:
if(key_val==0)
{
if(!flag)
{
// switch(key_val)
// {
//
// }
}
flag=0;
key_stat=0;
count_press=0;
key_back=0;
}
break;
default:
flag=0;
key_stat=0;
count_press=0;
key_back=0;
break;
}
}
void Timer0_Init(void) //1毫秒@12.000MHz
{
AUXR |= 0x80; //定时器时钟1T模式
TMOD &= 0xF0; //设置定时器模式
TL0 = 0x20; //设置定时初值
TH0 = 0xD1; //设置定时初值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
ET0=1;
EA=1;
}
void Timer0_Service() interrupt 1
{
static uchar dsp_com=0;
Contr_Peri(0xc0,0);
Contr_Peri(0xe0,dsp_code[dsp_buff[dsp_com]]);
Contr_Peri(0xc0,1<<dsp_com);
if(++dsp_com==8) dsp_com=0;
++count_key;
++count_temp;
++count_adc;
++count_dis;
}
void UartInit(void) //4800bps@12.000MHz
{
SCON = 0x50; //8位数据,可变波特率
AUXR |= 0x01; //串口1选择定时器2为波特率发生器
AUXR |= 0x04; //定时器2时钟为Fosc,即1T
T2L = 0x8F; //设定定时初值
T2H = 0xFD; //设定定时初值
AUXR |= 0x10; //启动定时器2
ES=1;
EA=1;
}
void Uart_Service() interrupt 4
{
if(TI)
{
TI=0;
busy=0;
}
if(RI)
{
read_number[read_i++]=SBUF;
RI=0;
}
}
void Send(unsigned char dat)
{
while(busy);
busy=1;
SBUF=dat;
}
void Send_String(char *s)
{
while(*s)
{
Send(*s++);
}
}
void System_Init()
{
uchar i=0;
Contr_Peri(0X80,0XFF);
Contr_Peri(0xa0,0);
for(i=0;i<70;i++)
temp=read_temp()*100+0.5;
for(i=0;i<10;i++)
volt=read_adc(0x01)*1.96+0.5;
Timer0_Init();
Set_Time(23,59,50);
UartInit();
Delay10ms();
}
void main()
{
System_Init();
while(1)
{
if(count_temp>749)
{
count_temp=0;
temp=read_temp()*100+0.5;
}
if(count_adc>149)
{
count_adc=0;
volt=read_adc(0x03)*1.96+0.5;
}
if(TH0<0XF0)
{
hor1=Read_Ds1302_Byte(0x85);
min1=Read_Ds1302_Byte(0x83);
sec1=Read_Ds1302_Byte(0x81);
hor=(hor1/16)*10+hor1%16;
min=(min1/16)*10+min1%16;
sec=(sec1/16)*10+sec1%16;
}
if(count_dis>999)
{
count_dis=0;
distance=Get_Distance();
}
// dsp_buff[0]=temp/1000%10;
// dsp_buff[1]=temp/100%10;
// dsp_buff[2]=temp/10%10;
// dsp_buff[3]=temp%10;
//
// dsp_buff[4]=10;
// dsp_buff[5]=volt/100%10;
// dsp_buff[6]=volt/10%10;
// dsp_buff[7]=volt%10;
// dsp_buff[0]=distance/100%10;
// dsp_buff[1]=distance/10%10;
// dsp_buff[2]=distance%10;
// dsp_buff[3]=min/10%10;
//
// dsp_buff[4]=min%10;
// dsp_buff[5]=10;
// dsp_buff[6]=sec/10%10;
// dsp_buff[7]=sec%10;
if(read_i>0)
{
if(read_number[read_i-1]=='\n')
{
if((read_number[0]=='S')&&(read_number[1]=='T')&&(read_number[2]=='\r'))
{
Send_String("$");
Send(distance/10%10+'0');
Send(distance%10+'0');
Send(',');
Send((temp/1000%10)+'0');
Send((temp/100%10)+'0');
Send('.');
Send((temp/10%10)+'0');
Send((temp%10)+'0');
Send_String("\r\n");
}
else if((read_number[0]=='P')&&(read_number[1]=='A')&&(read_number[2]=='R')&&(read_number[3]=='A')&&(read_number[4]=='\r'))
{
Send_String("#");
Send(volt/1000%10+'0');
Send(volt/100%10+'0');
Send('.');
Send(volt/10%10+'0');
Send(volt%10+'0');
Send_String("\r\n");
}
read_i=0;
}
else if(read_i==6)
{
Send_String("ERROR\r\n");
read_i=0;
}
}
// if(count_key>9)
// {
// count_key=0;
// Scan_Key();
// }
}
}