这里是程序,请各位帮忙看看为什么会显示错误的读数
#include <reg52.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit STR=P1^0;
sbit EOC=P1^1;
sbit OE=P1^2;
sbit CLK=P1^3;
sbit adda = P1^4;
sbit ale = P1^5;
#define adc0809_data P3
uchar AD_DATA[1];
uint addata2;
#define LCM_Data P0
#define Busy 0x80
sbit LCM_RW = P2^6;
sbit LCM_RS = P2^5;
sbit LCM_E = P2^7;
void WriteDataLCM (uchar WDLCM);
void WriteCommandLCM (uchar WCLCM,BuysC);
uchar ReadStatusLCM(void);
void DisplayOneChar(uchar X,uchar Y,uchar ASCII);
void LCMInit(void);
void delayms(uint ms);
void delay(uchar i);
void DisplayListChar(uchar X,uchar Y,uchar delayms, uchar code *DData);
void judge_xianshi(void);
void init();
void AD();
uchar flag1;
main()
{
init();
adda = 0;
LCMInit();
while(1)
{
AD();
judge_xianshi();
delayms(100);
}
}
void init()
{
EA=1;
TMOD=0x02;
TH0=0x216;
TL0=0x216;
TR0=1;
ET0=1;
STR=0;
OE=0;
}
void judge_xianshi()
{
addata2=AD_DATA[0]*100;
DisplayListChar(0,1,0, "xingxiangrong");
DisplayListChar(1,0,0, "AD=");
DisplayOneChar( 1, 3, AD_DATA[0]/100+0x30);
DisplayOneChar( 1, 4, AD_DATA[0]/10%10+0x30);
DisplayOneChar( 1, 5, AD_DATA[0]%10+0x30);
DisplayListChar(1,7,0, "Vin=");
DisplayOneChar( 1, 11, AD_DATA[0]/51+0x30);
DisplayListChar(1,12,0, ".");
DisplayOneChar( 1, 13, addata2/51/10%10+0x30);
DisplayOneChar( 1, 14, addata2/51%10+0x30);
DisplayListChar(1,15,0, "V");
}
void AD()
{
STR=0;
delay(10);
STR=1;
ale=1;
delay(10);
STR=0;
ale=0;
delay(1);
while(0==EOC);
delay(1);
OE=1;
delay(1);
AD_DATA[0]=adc0809_data;
OE=0;
}
void t0(void) interrupt 1 using 0
{
CLK=~CLK;
}
void DisplayListChar(uchar X,uchar Y,uchar ms, uchar code *DData)
{
unsigned char ListLength;
ListLength = 0;
X &= 0x1;
Y &= 0xF;
while (DData[ListLength]!='\0')
{
if (Y <= 0xF) //X×ø±êӦСÓÚ0xF
{
DisplayOneChar(X, Y, DData[ListLength]);
ListLength++;
Y++;
delayms(ms);
}
else
break;
}
}
void LCMInit(void)
{
LCM_Data = 0;
WriteCommandLCM(0x38,0);
delayms(5);
WriteCommandLCM(0x38,0);
delayms(5);
WriteCommandLCM(0x38,0);
delayms(5);
WriteCommandLCM(0x38,1);
WriteCommandLCM(0x08,1);
WriteCommandLCM(0x01,1);
WriteCommandLCM(0x06,1);
WriteCommandLCM(0x0C,1);
delayms(100);
}
void WriteDataLCM(uchar WDLCM)
{
ReadStatusLCM(); //¼ì²âæ
LCM_Data = WDLCM;
LCM_RS = 1;
LCM_RW = 0;
LCM_E = 0;
delay(10);
LCM_E = 0;
LCM_E = 1;
}
void WriteCommandLCM(uchar WCLCM,BuysC)
{
if (BuysC) ReadStatusLCM();
LCM_Data = WCLCM;
LCM_RS = 0;
LCM_RW = 0;
LCM_E = 0;
delay(10);
LCM_E = 0;
LCM_E = 1;
}
uchar ReadStatusLCM(void)
{
LCM_Data = 0xFF;
LCM_RS = 0;
LCM_RW = 1;
LCM_E = 0;
LCM_E = 0;
LCM_E = 1;
while (LCM_Data & Busy);
return(LCM_Data);
}
void DisplayOneChar( uchar X, uchar Y, uchar ASCII)
{
X &= 0x1;
Y &= 0xF;
if (X) Y |= 0x40;
Y |= 0x80;
WriteCommandLCM(Y, 0);
WriteDataLCM(ASCII);
}
void delayms(uint Ms)
{
uint i,TempCyc;
for(i=0;i<Ms;i++)
{
TempCyc =70;
while(TempCyc--);
}
}
void delay(uchar i)
{
uchar j;
while(i--)
{
for(j=125;j>0;j--)
;
}
}
|