#include "reg52.h"float shuju;
unsigned char sj;
unsigned int gata;
unsigned char gw,sw,bw;
unsigned char kk,tdao;
本文引用地址:http://www.ex-cimer.com/article/201611/320304.htm//4聯(lián)共陽數(shù)碼管,最高位顯示通道號,其他三位為電壓值
unsigned char code shuzi[]={
0xC0,0xF9,0xA4,0xB0,0x99,
0x92,0x82,0xF8,0x80,0x90};
unsigned char code duanxuan[]={0x0f,0x8f,0x4f,0xcf,0x2f,0xaf,0x6f,0xff};
sbit START=P3^1; //ALE也連接P3.1
sbit OE=P3^3;
sbit EOC=P3^2;
sbit clock=P3^0;
void delay()
{
unsigned char i;
for(i=200;i>0;i--);
}
//timer init
void Timer()
{
TMOD=0x21;
TH0=0x3c;//20ms
TL0=0xb0;
TH1=0xff;//100khz
TL1=0xfd;
}
void AD_zh()
{
START=0; //ADC0809的啟動信號
START=1;
START=0;
while(EOC==0);//等待轉(zhuǎn)換結(jié)束
OE=1; //輸出轉(zhuǎn)換數(shù)據(jù)
sj=P1; //P1口接收數(shù)據(jù)
shuju=sj;//轉(zhuǎn)換為浮點型
shuju=(shuju/51.0)*100.00;//轉(zhuǎn)換成百位數(shù)據(jù);
gata=shuju;//轉(zhuǎn)換為整型
bw=gata/100;//分離百位
sw=(gata/10)%10;//分離十位
gw=gata%10;//分離個位
}
//timer0/counter0 interrupt
void timer0(void) interrupt 1 //T0用于產(chǎn)生秒信號
{
TH0=0x3c;
TL0=0xb0;
kk++;
if(kk==20)
{
kk=0;
tdao++; //用于轉(zhuǎn)換通道,測量各個通道數(shù)據(jù)
if(tdao==8)
{
tdao=0;
}
}
}
void timer1(void) interrupt 3
{
clock=~clock; //定時器產(chǎn)生ADC0809所需的時鐘信號
}
void display()
{
P0=shuzi[gw]; //個位
P2=0xf8&duanxuan[tdao];
delay();
P0=shuzi[sw];//十位
P2=0xf4&duanxuan[tdao];
delay();
P0=shuzi[bw]&0x7f; //電壓百位
P2=0xf2&duanxuan[tdao];
delay();
P0=shuzi[tdao+1]; //通道從1~8,每秒換一個通道
P2=0xf1&duanxuan[tdao];
delay();
}
//the main fun
void main(void)
{
uchar i;
Timer();
TR0=1;
ET0=1;
TR1=1;
ET1=1;
EA=1;
while(1)
{
AD_zh();
for(i=0;i<10;i++) //降低刷新速度,使顯示穩(wěn)定
{
display();//顯示程序
}
}
}
評論