#include<reg51.h> #define uchar unsigned char #define uint unsigned int sbit wr=P3^6; sbit rd=P3^7; sbit cs=P3^5; uchar code tab[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};//共阴数码管码表 uchar s,g,f,l,x;//数码管显示变量 float A;
uint K; void delay(uint); void adc(); void adc() { //启动ADC0804采样// cs=0;wr=0;rd=1; delay(50); cs=1;wr=1;rd=1; //WR由低到高时,AD开始转换 delay(100); //读AD数值// cs=0;wr=1;rd=0;//读取转换数据结果 delay(50); A=P1;//P1的新数据赋值给adval,用于数码管显示的s g f的分离 A=A*144/256; K=P1*144/256; cs=1;wr=1;rd=1;//停止AD读取 } void main() { uint s,g,f,l,x; while(1) { adc();
s=K/10; l=s/10; x=s%10; g=K%10; f=(A-K)*10; P2=0XFE; //11111110 选中十位数码管 P0=tab[l]; //显示 delay(50); P2=0XFD; //11111110 选中十位数码管 P0=tab[x]; //显示 delay(50); P2=0XFB; //11111101 选中个位数码管 P0=tab[g]+0x80; //显示 delay(50);
P2=0XF7;//11111011 选中小数点后数码管 P0=tab[f]; //显示 delay(50); } } void delay(uint m)//延时子程序,约500个机器周期 { while(m--);}
|