参考电压5v时。待测电压大于三伏是读数误差很小,当电压低于三伏是最大误差达到0.7v。芯片管脚直接连在单片机上。程序如下。求解
#include<reg52.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit adclk=P0^0; //时钟线
sbit adcs=P0^2; //CS
sbit addata=P0^3; //数据线
sbit lcdrs =P0^4;
sbit lcdrw =P0^5;
sbit lcden =P0^6;
sbit lcdpsb=P0^7;
uchar table[]="0123456789";
void delay(unsigned int t)
{
uchar i,j;
for(i=0;i<t;i++)
for(j=0;j<110;j++);
}
write_com(uchar com)
{
uchar temp=0;
lcdrs=0;
lcdrw=1;
/*do//读是否忙碌
{
lcden=1;
delay(5);
temp=P2;
lcden=0;
}
while(temp&0x80);*/
lcdrw=0;
P2=com;
lcden=1;
delay(5);
lcden=0;
}
void lcdinit()
{
lcdrs=1;
write_com(0x30);
delay(5);
write_com(0x02);
delay(5);
write_com(0x0c);
delay(5);
write_com(0x01);
delay(5);
write_com(0x06);
}
write_date(uchar date)
{
uchar temp=0;
lcdrs=0;
lcdrw=1;
do
{
lcden=1;
delay(5);
temp=P2;
lcden=0;
}
while(temp&0x80);
lcdrs=1;
lcdrw=0;
P2=date;
lcden=1;
delay(5);
lcden=0;
}
/**********************TLC1549****************************/
//名称:ad_read()
//功能:读十位AD TLC1549 返回值是一个整形的数
unsigned int ad_read(void)
{
unsigned char i;
unsigned int adtemp;
adcs=1; //禁止I/O CLOCK
adcs=0; //开启控制电路,使能DATA OUT和I/O CLOCK
adtemp=0; //清转换变量
for(i=0;i<10;i++) //采集10次 ,即10bit
{
adclk=0;
adtemp*=2;
if(addata) adtemp++;
adclk=1;
}
adcs=1;
return(adtemp);
}
/**************TLC1549显示处理函数******************/
void display_1549()
{
unsigned long temp;
temp=ad_read();
temp=(temp*5000)/1023;
write_com(0x80);
write_date(table[temp/1000]);
write_date('.');
write_date(table[temp%1000/100]);
write_date(table[temp%100/10]);
write_date(table[temp%10]);
write_date('V');
write_com(0xdf);
write_com(0x02);
delay(200);
}
void main()
{
int i=0;
lcdpsb=1;
lcdinit();
while(1)
{
display_1549();
}
} |