数码管接在P0口上.#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
uchar a;
//uchar temp=0;
uint adval,volt;
sbit K1=P3^2;
sbit K2=P3^3;
sbit relay=P3^6;
sbit OE=P2^7;
sbit EOC=P2^6;
sbit START=P2^5;
sbit CLK=P2^4;
sbit CS0=P2^0;
sbit CS1=P2^1;
sbit CS2=P2^2;
sbit CS3=P2^3;
uchar tab[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,
0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E };
void delayms(uint ms)
{
uchar j;
while(ms--)
{
for(j=0;j<120;j++);
}
}
void int1() interrupt 0
{
a=1;
}
void int2() interrupt 2
{
a=0;
}
void int_int(void)
{
IE=0x9F;
TCON=0x00;
}
void ADC_read()
{
START=0;
START=1;
START=0;
while(EOC==0);
OE=1;
adval=P1;
OE=0;
}
void volt_result()
{
volt=adval*10.55;
}
void disp_volt(uint date)
{
CS0=1;CS1=0;CS2=0;CS3=0;
P0=tab[date/1000];
delayms(1);
P0=0xFF;
CS0=0;CS1=1;CS2=0;CS3=0;
P0=tab[date%1000/100];
delayms(1);
P0=0xFF;
CS0=0;CS1=0;CS2=1;CS3=0;
P0=~((~tab[date%100/10])|0x80);
delayms(1);
P0=0xFF;
CS0=0;CS1=0;CS2=0;CS3=1;
P0=tab[date%10];
delayms(1);
P0=0xFF;
}
void t0() interrupt 1
{
CLK=~CLK;
}
void t0_init()
{
TMOD=0x22;
TH0=0x14;
TL0=0x00;
TR0=1;
ET0=1;
EA=1;
}
void SendByte(char ch)
{
SBUF=ch;
while(TI==0);
TI=1;
}
void SendString(char code *str)
{
while(*str)SendByte(*str++);
}
/*void SCON_init(void)
{
SCON=0x40;
TMOD=0x22;
PCON=0x00;
TH1=0xF4;
TL1=0xF4;
TI=0;
TR1=1;
ES=1;
EA=1;
}*/
void main(void)
{ P1=0xFF;
// SCON_init();
a=0;
int_int();
relay=1;
t0_init();
while(1)
{
ADC_read();
volt_result();
disp_volt(volt);
TI=0;
// while(TI==1);
// temp=P1;
// SBUF=temp;
if(a==1)
{
relay=1;
}
else if(a==0)
{
relay=0;
}
}
}
|