/*用的是主程序中轉(zhuǎn)換AD,在以后的使用中要用中斷的方法才能節(jié)約CPU的使用率,本程序中是用P1.0為輸入,用的是單片機本身的10位AD,最大1024分辨率,除以10后最大為102,用兩位的數(shù)碼管,所以只能在99以內(nèi),在程序中大于99的數(shù),就只能顯示99,也可用三位數(shù)碼管顯示.*/
#include< reg52.h>
#define uchar unsigned char
#define uint unsigned int
#define duan P1 //數(shù)碼管段碼輸出端
sfr ADC_CONTR = 0xc5;
sfr ADC_DATA = 0xc6;
sfr ADC_LOW2 = 0xbe;
sfr P1M0 =0x91;
sfr P1M1 =0x92;
sbit sw=P2^7; //數(shù)碼管的十位選通
sbit gw=P2^6; //數(shù)碼管的個位選通
uint shu,ad_shu;
uchar code tab[]={ 0xEE,/*0*/
0x48,/*1*/
0xD6,/*2*/
0xDC,/*3*/
0x78,/*4*/
0xBC,/*5*/
0xBE,/*6*/
0xC8,/*7*/
0xFE,/*8*/
0xFC,/*9*/
};
void delay( )
{
uchar a;
for(a=10;a>0;a--);
}
void desplay(int bb)
{
uint ab;
ab=bb;
if(ab>99) ab=99;
duan=tab[ab/10];
sw=0;
delay();
sw=1;
duan=tab[ab%10];
gw=0;
delay();
gw=1;
}
void main()
{
ADC_CONTR=0xe0; //開A/D轉(zhuǎn)換器
P1M0=0x01; //定義P1.0為AD輸入端口,讓P1.0為高阻態(tài)
P1M1=0x01; //定義P1.0為AD輸入端口,讓P1.0為高阻態(tài)
while(1)
{
ADC_CONTR|=0x08;
while((ADC_CONTR & 0x10)==0);
ADC_CONTR&=0xe7;
ad_shu=ADC_DATA;
ad_shu<<=2;
ad_shu+=ADC_LOW2;
shu=ad_shu/10;
desplay(shu);
}
}
