/***************************************************************
*File: TestResistor.c
*Description: 1.采用的ADC是TLC548,电阻测量原理是用标准电阻与
被测电阻Rx相串,将标准电阻两端的电压Vo作为ADC的
参考电压,将Rx的两端电压作为ADC的被测量.那么待测
的大小Rx=Ux/(Uo/Ro)=Ro*Ux/Uo,经ADC后,其中Uo为满
值,则Ux/Uo=测量所得的8位数/256,则Rx=Value/265*Ro
2.由于TLC548输出的数据是串出的,本实验用的是I/O口
模拟串行口.
Author: WusouWei
Date: 2007/07/25
***************************************************************/
/***************************************************************
* 端口定义,文件包涵部分
***************************************************************/
#include"reg51.h"
#include"LCD1602.h"
#define uint unsigned int
#define uchar unsigned char
#define ulong unsigned long
sbit SD=P1^0;
sbit CS0=P1^1;
sbit SCLK=P1^2;
sbit LED=P3^0;
/***************************************************************
*Name: DelayNms()
*Description: 延迟n毫秒,其中n为入口参数
*Input: i 为整数,决定了延迟几毫秒
*Output: 无
***************************************************************/
void DelayNms(uint i)
{ uchar j;
for(;i>0;i--)
for(j=0;j<125;j++);
}
/***************************************************************
*Name: ReadValue()
*Description: 启动ADC(TLC548)进行转换,并将结果返回
*Input: 无
*Output: Value 被测模拟电压转换成8位数字的值
***************************************************************/
uchar ReadValue(void)
{ uchar Temp,i,Value;
uchar ArrayMap[8]={128,64,32,16,8,4,2,1};
//ADC选定
SCLK=0;
CS0=0;
CS0=0;
//接收8位数据
Value=0;
for(i=0;i<8;i++)
{
//下降沿读取数据
SCLK=1;
SCLK=0;
//数据位在Temp第0位
Temp=P1&0x01;
if(Temp==1)
Value=Value+ArrayMap[i];
}
CS0=1;
return Value;
}
ulong TestR(void)
{ uchar Temp;
ulong Referance;
//计算电阻的大小
//先判断当前参考电阻大小
//P2=0xFF;
Temp = P2 & 0xFC;
switch (Temp)
{
case 0x7C:
Referance = 10;
break;
case 0xBC:
Referance = 100;
break;
case 0xDC:
Referance = 1000;
break;
case 0xEC:
Referance = 10000;
break;
case 0xF4:
Referance = 100000;
break;
case 0xF8:
Referance = 1000000;
break;
//代表错误
other:
Referance = 0;
}
return Referance;
}
void Display(ulong R_Value)
{ uchar DispBuffer[10]={0};
uchar ArrTemp[9]={0};
uchar i;
ArrTemp[0] = R_Value / 10000000;
ArrTemp[1] = R_Value % 10000000 / 1000000;
ArrTemp[2] = R_Value % 10000000 % 1000000 / 100000;
ArrTemp[3] = R_Value % 10000000 % 1000000 % 100000 / 10000;
ArrTemp[4] = R_Value % 10000000 % 1000000 % 100000 % 10000 / 1000;
ArrTemp[5] = R_Value % 10000000 % 1000000 % 100000 % 10000 % 1000 / 100;
ArrTemp[6] = R_Value % 10000000 % 1000000 % 100000 % 10000 % 1000 % 100 /10;
ArrTemp[8] = R_Value % 10000000 % 1000000 % 100000 % 10000 % 1000 % 100 %10;
ArrTemp[7] = '0'; //无用
//变成ASCII码;
for(i=0;i<7;i++)
{
DispBuffer[i]=ArrTemp[i] + '0';
}
DispBuffer[8] = ArrTemp[8] + '0';
//去除头0;
i = 0;
while(DispBuffer[i]=='0' && i<7)
{ DispBuffer[i] = ' ';
i++;
}
//如果全为0;
if (i == 7)
DispBuffer[6]='0';
DispBuffer[7] = '.';
DispBuffer[9] = 'R';
//for(i=0;i<9;i++)
//{ DispBuffer[i]=i+'0';
//}
//显示
//清屏
while(LCD_BUSY());
LCD_CW =LCD_ClEAR_SCREEN;
LCD_W_Data(1,1,10,DispBuffer);
}
/***************************************************************
*Name: main()
*Description: 无
*Input: 无
*Output: 无
***************************************************************/
void main()
{ uchar Data;
ulong Referance;
uchar Temp;
// uchar *Point;
// Temp = 49;
// Point = &Temp;
LCD_Init();
// LCD_W_Data(1,1,1,Point);
// while(1);
while(1)
{ //提示灯先灭
LED=1;
Data=ReadValue();
//P2=Data;
//while(1);
//如果DATA刚好是256,则提示灯亮
if (Data==256)
LED=0;
//计算电阻大小
Referance = TestR();
Referance = Referance * 10 *Data / 256;
//进行显示
Display(Referance);
DelayNms(1000);
}
}
/***************************************************************
**************** END ********************
***************************************************************/