我的设计是用单片机自带10位ADC测电压,通过公式(10/K)U+10*log10(I/R)算得一个光功率值,在LCD上显示出来,也可以按键控制传入PC机,但是现在测出来LCD显示的数据反推回去与测的电压完全不符,按键也发不出数据进PC,求帮看下哪有问题,急急急急急:'(:'(:'(!!!我看有说我测的这电压不是实际电压要用带隙电压方式来算。不懂什么意思
uchar ch = 0; //选择ADC通道
uchar P_test[]={"8.64"};
double result;
double K = 0.18, Iz = 100e-9, R = 0.4;
uchar testh=0xa4;
uchar testl=0x02;
uchar code testA[] = {"ce shi shuju"};
uchar code testB[] = {"ce shi num2"};
/****************LCD配置函数***************
*******************************************/
bit LcdBusy()
{
bit busy;
RS = 0;
RW = 1;
EN = 1;
Delayus(2);
busy = (bit)(P0&0x80); //读取P0最高位的值
EN = 0;
return busy;
}
void WrCmd(uchar cmd) //LCD写指令
{
while(LcdBusy()); //执行查忙检测
RS = 0;
RW = 0;
EN = 0;
P0 = cmd;
Delayus(1); //模拟一个脉冲
EN = 1;
Delayus(1);
EN = 0;
}
void WrData(uchar dat)
{
while(LcdBusy());
RS = 1;
RW = 0;
EN = 0;
P0 = dat; //数据写入
Delayus(1); //模拟1个脉冲
EN = 1;
Delayus(1);
EN = 0;
}
/************功率计算函数**************
***************************************/
double MathPower(uchar PoData)
{
double Uout,PowerDB;
Uout = PoData*4.97/1024;
PowerDB= (10/K)*Uout+10*log10(Iz/R);
return PowerDB;
}
double ADCResult(uchar ch)
{
uchar TenData;
double fan;
ADC_CONTR = ADC_POWER|ADC_SPEEDLL|ch|ADC_START;
_nop_();
_nop_();
_nop_();
_nop_();
while(!(ADC_CONTR&ADC_FLAG));
ADC_CONTR &= ~ADC_FLAG;
TenData = ((uint)ADC_RES<<2)| ADC_RESL;
fan = MathPower(TenData);
return fan;
}
/*************延时函数******************
****************************************/
void Delayms(uchar ms)
{
uchar i, j;
while(ms--)
{
i = 11;
j = 190;
do
{
while(--j);
}
while(--i);
}
}
void Delayus(uchar us)
{
uchar i;
while(us--)
{
_nop_();
_nop_();
i = 24;
while(--i);
}
}
/************各项初始化函数**************
*****************************************/
void Int0Init()
{
IT0 = 1;
EX0 = 1;
}
void InitADC()
{
P1ASF = 0xff; //设置P1口为AD转换接口
ADC_RES = 0; //清楚结果寄存器
ADC_CONTR = ADC_POWER | ADC_SPEEDLL ;
Delayms(2); //ADC上电延迟
}
void InitUart()
{
AUXR = 0X40;
SCON = 0x50;
TMOD = 0x20; //定时器1为8位自动装载模式
TH1=TL1=(256-(FOSC/32/BAUD)); //自动装载值
TR1 = 1;
TI = 1;
EA = 1;
}
void lcdinit()
{
WrCmd(0x38);
Delayms(1);
WrCmd(0x0c);
Delayms(1);
WrCmd(0x06);
Delayms(1);
WrCmd(0x01);
Delayms(10);
}
/****************输出函数*******************
********************************************/
void SendData(uchar dat)
{
while(!TI);
TI = 0;
SBUF = dat;
}
void ShowResult(uchar ch)
{
double temp;
temp = ADCResult(ch);
LCDShow(temp);
// sprintf (Psend,"%d",temp);
} //*/
void LCDShow(double a)
{
double b;
uchar i,Str_dB[16],Str_mW[16];
i = 0;
b = exp(a/10);
sprintf(Str_dB,"Power:%.3f dBm",a);
sprintf(Str_mW,"%.4f mW ",b);
WrCmd(0x80);
while(Str_dB[i]!=0)
{
WrData(Str_dB[i]);
i++;
}
i =0;
WrCmd(0x80+0x40);
while(Str_mW[i] != 0)
{
WrData(Str_mW[i]);
i++;
}
}
/***************中断服务程序***********
***************************************/
void exint0() interrupt 0
{
uchar j;
j = 0;
Delayms(5);
if (0 == P32)
{
while(testA[j] != 0)
{
SendData(testA[j]);
j++;
}
j = 0;
while(0 == P32);
}
}
/*void exint2() interrupt 10
{
//int temp;
uchar Ptest[6],j;
Delayms(5); //去按键抖动
if(0 == P36)
{
temp = 0;
temp = testh*4+testl;
result = temp*5.0/1024;
sprintf(*Parray,"%.4f",result); //*
//SendData(temp);
ti = 0;
while ( *Parray[ti] !=0)
{
SendData(*Parray[ti]);
ti++;
}
ti = 0;
while (Ptest[j]!=0)
{
SendData(Ptest[j]);
j++;
}
j = 0;
//Delay(200);
//SendData(ADC_RESL);
//Delay(200);
while(0 == P36);
}
} */
void main()
{
InitUart();
Int0Init();
InitADC();
lcdinit();
SendData(20);
// INT_CLKO |= 0x10;
while(1)
{
ShowResult(0);
}
} |