#include <reg52.h>
#include <intrins.h>
#include <stdio.h>
#define uchar unsigned char
#define uint unsigned int
sbit dula=P2^6;
sbit wela=P2^7;
sbit adwr=P3^6;
sbit adrd=P3^7;
uint miao=0,ad_val;
unsigned char a,flag_time,t0_num,flag_uart,flag_on,t1_num=0;
float ad_vo;
void delay(uint ms)
{
uint i,j;
for(i=ms;i>0;i--)
for(j=110;j>0;j--);
}
void init()
{
TMOD=0x21;
SCON=0x50;
//PCON=0x80;
TH0=(65536-921)/256;
TL0=(65536-921)%256;
TH1=0xfd;
TL1=0xfd;
TR1=1;
ET0=1;
SM0=0;
SM1=1;
REN=1;
EA=1;
ES=1;
}
uchar get_ad()
{
uchar adval;
adwr=1;
_nop_();
adwr=0;
_nop_();
adwr=1;
delay(1);
P1=0xff;
adrd=1;
_nop_();
adrd=0;
_nop_();
adval=P1;
adrd=1;
return adval;
}
void main ()
{
init();
wela=1;
P0=0x7f;
wela=0;
while(1)
{
if(flag_uart==1)
{
flag_uart=0;
ES=0;
TI=1;
switch(flag_on)
{
case 0:
TR0=1;
break;
case 1:
TR0=0;
break;
case 2:
miao=0;
break;
}
while(!TI);
TI=0;
ES=1;
}
if(flag_time==1)
{
flag_time=0;
ad_val=get_ad();
ad_vo=ad_val*5.0/256.0;
ES=0;
TI=1;
printf("dianya: %f shijian: %d.%d%d%d S\n",ad_vo,miao/1000,miao%1000/100,miao%100/10,miao%10);
while(!TI);
TI=0;
ES=1;
}
}
}
void timer0()interrupt 1
{
TH0=(65536-921)/256;
TL0=(65536-921)%256;
miao++;
t0_num++;
if(t0_num==80)
{
t0_num=0;
flag_time=1;
}
}
void ser()interrupt 4
{
RI=0;
a=SBUF;
flag_uart=1;
if(a==49)
flag_on=0;
else if (a==50)
flag_on=1;
else
flag_on=2;
} |