tlc1549测电压,程序如下
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit adclk=P0^1; //时钟线
sbit adcs=P0^3; //CS
sbit addata=P0^2; //数据线
sbit lcdrs =P0^7;
sbit lcdrw =P0^6;
sbit lcden =P0^5;
sbit lcdpsb=P0^4;
uchar table[]="0123456789";
void delay (uint z) //延时函数
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
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;
}
uint ad_read(void)
{
uchar i;
uint 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);
}
void display_1549()
{
long int 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');
delay (500);
}
void main()
{
int i=0;
lcdpsb=1;
lcdinit();
while(1)
{
display_1549();
}
}
当被测电压1v左右误差很小,大于1.5v时就有明显误差,2v时读数误差极大,3v以上时全部显示为4,8~5v之间。同时发现芯片发热,换过芯片后依旧发热。电路焊接中,只是按上底座焊接,没有加任何电容稳定取样电压。经测量引脚间未短路,断路。纠结好几天了,求高手指点 |