//MCU:AT89S51

//晶振:12M

//采用查询方式读AD转换后的值

#include

#include "math.h"

#include "intrins.h"

unsigned charcode numcode[]={ 0xc0,0xf9,0xa4,0xb0,0x99,

0x92,0x82,0xf8,0x80,0x90,

0xC1,0XB7,0xFF

};//数字0~9及"U="”“共阳数码管代码

unsigned charcode dot_numcode[]={0X40,0X79,0X24,0X30,0X19,

0X12,0X02,0X78,0X00,0X10

};//带数点的0~9共阳数码管代码

unsigned charcode bitcode[]={0xfe,0xfd,0xfb,0xf7,

0xef,0xdf,0xbf,0x7f};//数码管位选代码

unsigned chardispbuff[8]={0,0,0,11,10,12,12,12};

unsigned chardisp_count;

unsigned charflag=1;

unsigned charflag_count;

unsigned long intgetdata;

sbit START=P3^0;

sbit EOC=P3^2;

sbit OE=P3^1;

sbit AD_ALE=P3^3;

voidmain(void)

{

TMOD=0x01;//选择定时器T0,方式为1(16位定时器)

TH0=(65536-3000)/256;//定时3MS初值

TL0=(65536-3000)%6;

TR0=1;

ET0=1;

EA=1;

AD_ALE=0;

START=0;

OE=0;

while(1)

{

if(flag==1)//每隔40ms启动一次AD采样

{

flag=0;

AD_ALE=1;//锁存输入通道

START=0;//驱动AD转换

START=1;

AD_ALE=0;

START=0;//保持低电平

while(EOC==0);//等待AD转换结束

TR0=0;//AD转换结束时先停止T0

P0=0XFF;//关闭数码管显示,呵呵,其实人眼是感觉不到数码管熄灭的

P1=0XFF;// 这个很重要

OE=1;//转换结束,允许AD值输出

getdata=P1;//读出AD值,如果硬件连接上改成P2口来读AD值,这句就改成"getdata=P2;"

OE=0;//禁止输出

getdata=(unsigned long int)((unsigned long int)(getdata*500)/255);//转换成电压

dispbuff[2]=getdata/100;

dispbuff[1]=(getdata0)/10;

dispbuff[0]=getdata;

TR0=1;//重新启动T0

}

}

}

voidt0(void) interrupt1

{

TH0=(65536-3000)/256;

TL0=(65536-3000)%6;

if(disp_count==2)

P0=dot_numcode[dispbuff[disp_count]];

else

P0=numcode[dispbuff[disp_count]];

P1=bitcode[disp_count];

disp_count++;

flag_count++;

if(disp_count==8)

{

disp_count=0;

}

if(flag_count==100)

{

flag=1;

flag_count=0;

}

}

推荐内容