#include <reg52.h>
sfr P1ASF=0x9d;
sfr ADC_CONTR =0xbc; //power speed1 speed0 flag st chs2 chs1 chs0
sfr ADC_RES=0xbd;
sfr ADC_RESL=0xbe;
#define uint unsigned int
#define uchar unsigned char
sbit ser=P2^1; //LED显示 595数据输入
sbit srclk1=P2^2; //
sbit rclk1=P2^3; //
uchar idata LED[ ]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xfe,0xbf};//共阳极段码表说明
void outbyte(uchar a,b)
{
uchar j;
for(j=0;j<8;j++) //位码移位串行输出
{ if(a&0x80) ser=1; else ser=0;
a=a<<1;
srclk1=0;
srclk1=1;
}
for(j=0;j<8;j++) //位码移位串行输出
{ if(b&0x80) ser=1; else ser=0;
b=b<<1;
srclk1=0;
srclk1=1;
}
rclk1=0; //595锁存脉冲
rclk1=1;
}
//--------------------------------------------------------------------
uint GET_AD_Result()//启动AD转换并返回转换值
{
uchar temp;
uint data_temp;
data_temp=0;
ADC_RES=0;
ADC_RESL=0;
ADC_CONTR|=0x08;//启动AD
re: temp=0x10;
temp&=ADC_CONTR;//查询ADC_FLAG,忙标志,转换完否
if(temp==0)
goto re;
ADC_CONTR&=0xe0;//P1.0为AD输入口
data_temp=ADC_RES;
data_temp<<=2;
data_temp+=ADC_RESL;
return data_temp;
}
//--------------------------------------------------------------------
void main(void) using 0
{
uint result,temp,i,a,c;
uchar aa[3]={0,0,0};
// P1M0=0x01;
// P1M1=0x01;
P1ASF=0x01;
ADC_CONTR=0xe0;//P1.0为AD输入口
while(1)
{
result=GET_AD_Result();
aa[2]=result*5/1024;
temp=result*5%1024;
temp=temp*10;
aa[1]=temp/1024;
temp=temp%1024;
temp=temp*10;
aa[0]=temp/1024;
a=0xfe;
for(i=0;i<3;i++)
{
c=LED[aa[i]];
if(i==2) c=c&0x7f;
outbyte(a,c);
// delay(1);
a=(a<<1)|0x01;
}
}
} |