TLC2543检测不准的问题。求大侠们救命!!
[复制链接]
我自己做了一个用TLC2543AD转换的东东,然后再用LCD显示出来,但是数据就是不对。都快十天了,快崩溃,快疯了,希望你能帮忙。这是程序,麻烦你了!!
/****************************************
D7D6D5D4: address of A/D port;
D3D2: length of data ,01:8b its;
11:16 bits
*0:12 bits
D1:0-> high bit first
1-> low bit first
D0:0-> bin
1-> Complementary Code
******************************************/
#include"reg52.h"
#include"LCD1602.h"
#define AD_PORT P1
sbit CLOCK=P1^0;
sbit AD_IN=P1^1;
sbit AD_OUT=P1^2;
sbit CS=P1^3;
sbit EOC=P1^4;
#define Channal0 0x00
#define Check_P_V 0xD0 //检测正的基准电压
#define Check_N_V 0xC0 //检测负的基准电压
#define Check_NP_V 0xB0
uchar AD_Result_Char[8];
void TLC2543_Delay(int time)
{
int i;
for(i=0;i
;
}
uint AD_Conver(uchar AD_In_Data)
{
uchar i,AD_Temp;
uint ad_value = 0;
AD_PORT=0x00;
CLOCK=0;
CS=1;
TLC2543_Delay(5);
CS= 0;
for (i=0;i<12;i++)
{
AD_Temp=AD_PORT&0x04;
AD_Temp>>=2;
ad_value=ad_value|AD_Temp;
AD_IN=(bit)(AD_In_Data&0x80);
TLC2543_Delay(10);
CLOCK=1;
TLC2543_Delay(10);
CLOCK=0;
AD_In_Data<<=1;
ad_value<<=1;
}
TLC2543_Delay(10);
CLOCK=1;
ad_value>>=1;
return ad_value;
}
float AD_Float(uint input)
{
float output;
output=4.4*input/4095.0;
return output;
}
void Float_TO_Char(float AD_Result_Float)
{
int AD_Temp;
float AD_Float;
if(AD_Result_Float>=0)
{
AD_Result_Char[0]='+';
}
else
{
AD_Result_Char[0]='-';
AD_Result_Float=-1*AD_Result_Float;
}
AD_Temp=(int)AD_Result_Float;
AD_Result_Char[1]=(AD_Temp-AD_Temp%10)/10+48;
AD_Result_Char[2]=AD_Temp%10+48;
AD_Result_Char[3]='.';
AD_Float=AD_Result_Float-AD_Temp;
AD_Result_Char[4]=(int)(AD_Float*10)+48;
AD_Result_Char[5]=(int)(AD_Float*100)%10+48;
}
main()
{ uint AD_Result;
int i,j;
float AD_Result_Float;
LCD_INI();
for(i=0;i<8;i++)
AD_Result_Char=' ';
AD_Result_Char[6]='m';
AD_Result_Char[7]='A';
while(1)
{
//CLOCK=1;
//TLC2543_Delay(50);
//CLOCK=0;
TLC2543_Delay(50);
AD_Result=AD_Conver(Check_P_V); //检测正的基准电压
while(!EOC)
;
AD_Result_Float=AD_Float(AD_Result);
Float_TO_Char(AD_Result_Float);
for(j=0;j<8;j++)
LCD_Display_Char(0,j,AD_Result_Char[j]);
TLC2543_Delay(100);
AD_Result=AD_Conver(Check_N_V); //检测正的基准电压
while(!EOC)
;
AD_Result_Float=AD_Float(AD_Result);
Float_TO_Char(AD_Result_Float);
for(j=0;j<8;j++)
LCD_Display_Char(1,j,AD_Result_Char[j]);
}
}