编写程序利用ADC0804 AD转换器采集电位器的模拟电压,将采集到的数字量在程序中换算成实际采集的模拟电压,并用数码管显示所采集的实际模拟电压值。(如:AD转换后的数字量为128,则数码管应该显示2.5)
程序:
#include<reg51.h>
#define DataPort P1
#define WeiPort P2
sbit ADCS=P3^0;
sbit ADRD=P3^1;
sbit ADWR=P3^2;
sbit ADINTR=P3^3;
unsigned char led[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
unsigned char seg[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
unsigned char Tab[4];
void Delay(unsigned int x) {
unsigned int i;
for(i=0;i<x;i++);
}
int main() {
unsigned int temp,ge,shifen,baifen;
unsigned char m,a=250;
while(1) {
ADCS=0;
ADWR=0;
Delay(1);
ADWR=1;
while(ADINTR);
ADRD=0;
Delay(1);
temp=P0;
ADRD=1;
ADCS=1;
ge=temp/51;
shifen=temp%5110/51;
baifen=temp%5110%51*10/51;
Tab[0]=led[0];
Tab[1]=led[ge]&0x7f;
Tab[2]=led[shifen];
Tab[3]=led[baifen];
while(a–){
WeiPort=~seg[m];
DataPort=Tab[m];
Delay(300);
m++;if(m==4)m=0;
}
}
}