ADC0809模数转换
程序员文章站
2024-02-25 14:47:39
...
目标
将模拟电压通过AD转换,通过AT89C51单片机接数码管显示转换结果。
代码
ADC0809的芯片介绍:ADC0809的芯片简介
#include "reg52.h"
char code ShuMaGuan[16]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71}; //共阴
#define led P0 //P1端口控制数码管段选端
#define wei P2 //P3端口控制位选端
#define Vref 5.0
sbit ALE=P3^0;
sbit ST=P3^1;
sbit EOC=P3^2;
sbit OE=P3^3;
int Volt1[3]={0,0,0}; //将各位数值存储
long double Data1=0; //数码管通道0电压值
void delay(int i)
{ while(i--);} //延时
void Display()
{
char i;
//存放Data1值:
Volt1[2]=(int)(Data1*100)%10;
Volt1[1]=(int)(Data1*10)%10;
Volt1[0]=(int)Data1%10;
//送显Data1
for(i=0;i<3;i++)
{
wei=(0x01<<i);
if(i==0)
led=~ShuMaGuan[Volt1[i]]+0x80; //加小数点
else
led=~ShuMaGuan[Volt1[i]];
delay(1000);
led=~0x00; //消隐
}
}
void main()
{
ALE=1;
while(1)
{
//读取通道0的电压值:
float n=0;
ST=0;
ST=1;
ALE=1;
ST=0;
while(!EOC);
OE=1;
n=P1;
Data1=Vref*n/255; //AD转换
Display();
}
}