用ADC0832和8951做了一个电压表.但数码管只是显示0000,不能显示当前电压值.
请高人指点.
#include"reg52.h"
sbit DO =P1^2;
sbit CS =P1^0;
sbit CLK =P1^1;
unsigned char adval; //AD值
unsigned char code tab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};//共阴
unsigned char code tab1[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};//共阳
unsigned char A_D(bit CH)
{
unsigned char i;
CS=0; //片选
CLK=0; //时钟拉低
CLK=1; //时钟拉高
DO=1; //启动信号
if(CH==0)
{
CLK=0;
CLK=1;
DO=1; //通道0的第1位
CLK=0;
CLK=1;
DO=0; //通道0的第2位
}
else
{
CLK=0;
CLK=1;
DO=1; //通道1的第1位
CLK=0;
CLK=1;
DO=1; //通道1的第2位
}
CLK=1;
CLK=0;
for(i=0;i<8;i++)
{
CLK=1;
CLK=0;
if(DO)
adval=(adval>>1)|0x80;
else
adval=(adval>>1)|0x00;
}
CS=1;
return(adval);
}
void delay(void)
{
int k;
for(k=0;k<600;k++);
}
void display(int k)
{
P2=0xfe; //1111 0111
P0=tab1[k/1000];
delay();
P2=0xfd;
P0=tab1[k%1000/100];
delay();
P2=0xfb;
P0=tab1[k%100/10];
delay();
P2=0xf7;
P0=tab1[k%10];
delay();
P2=0xff;
}
void main(void)
{
P2=0xff;
P0=0xff;
while(1)
{
A_D(0x00);
display(adval);
}
} |