#include <reg52.h> //52单片机头文件
#include <intrins.h>
#include<stdio.h>
#define uchar unsigned char //宏定义
#define uint unsigned int
sbit dula=P2^6;
sbit wela=P2^7;
sbit wr=P3^6;
sbit rd=P3^7;
uchar flag,ad;
uchar flag_uart,flag_time,flag_on,a,i,t0_num,ad_val;
float ad_vo;
uchar code tab[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
void delay (uint z) //延时函数,x的值为延时的时间(ms)
{
uint c, d;
for(c = z; c>0;c--)
for(d=113;d>0;d--);
}
void init()
{
TMOD=0x21;
TH0=(65536-45872)/256;
TL0=(65536-45872)%256;
TH1=0xfd;
TL1=0xfd;
TR1=1;
ET0=1;
SM0=0;
SM1=1;
REN=1;
EA=1;
ES=1;
}
void display(uchar val)
{
uchar b,s,g;
b=val/100;
s=val%100/10;
g=val%10;
dula=1;
P0=tab[b];
dula=0;
P0=0xff;
wela=1;
P0=0x7e;
wela=0;
delay(5);
dula=1;
P0=tab[s];
dula=0;
P0=0xff;
wela=1;
P0=0x7d;
wela=0;
delay(5);
dula=1;
P0=tab[g];
dula=0;
P0=0xff;
wela=1;
P0=0x7b;
wela=0;
delay(5);
}
uchar get_ad()
{
uchar adval;
wr=1;
_nop_();
wr=0;
_nop_();
wr=1;
P1=0xff;
rd=1;
_nop_();
rd=0;
_nop_();
adval=P1;
rd=1;
return adval;
}
void main(void)
{
init();
wela=1;
P0=0x7f;
_nop_();
wela=0;
while(1)
{
if(flag_uart==1)
{
flag_uart=0;
ES=0;
TI=1;
switch(flag_on)
{
case 0:
puts("Turn on ad!\n");
TR0=1;
break;
case 1:
printf("Turn off ad!\n");
TR0=0;
break;
case 2:
puts("ERROR!\n");
break;
default:break;
}
while(!TI);
TI=0;
ES=1;
}
if(flag_time==1)
{
flag_time=0;
ad_val=get_ad;
ad_vo=(float)ad*5.0/256.0;
ES=0;
TI=1;
printf("The voltage is %f V\n",ad_vo);
while(!TI);
TI=0;
ES=1;
}
display(ad_val);
}
}
void timer_0() interrupt 1
{
TH0 = (65536 -45872)/256; //重装初值
TL0 = (65536 - 45872)%256;
t0_num++;
if(t0_num==20)
{
t0_num=0;
flag_time=1;
}
}
void ser() interrupt 4
{
RI=0;
a=SBUF;
flag_uart=1;
if(a==1)
flag_on=0;
else if(a==2)
flag_on=1;
else
flag_on=2;
} |