新手!哪位高人帮我看看这段程序.
[color=#DC143C]本人做的是一个数字温度计,主要部份如下:放大滤波 AD转化,显示.
冷端补偿用DS18B20温环境温度,非线性较正用查表法进行,但是遇到一个问题,在主函数调用读环境温度,和调用读AD转化值时,怎么也不能显示正常结果.但是我把单单把DS18B20测温,得到一个电压值,送显示,或者,单单得到一个AD转换值,转化成电压,送显示,正常,也就是说各部份硬件电路和软件是正常的.
具体程序如下:
#include <reg51.h>
#include <intrins.h>
sfr p0=0x80;
sfr p1=0x90;
sfr p2=0xA0;
sfr p3=0xB0;
sbit CLOCK=P2^7;
sbit D_IN=P2^6;
sbit D_OUT=P2^5;
sbit _CS=P2^4;
sbit dp=p1^5;
sbit DQ=p0^6; //定义DS18B20的通信口
float code ldbc[]= {0,0.04,0.08,0.12,0.16,0.2,0.24,0.28,0.32,0.36,0.38,0.44,0.48,0.52,0.56,0.6,0.64,0.68,0.72,0.76, //K型热电偶在0-40度下对应的电压值,用于冷端补偿。
0.8,0.84,0.88,0.92,0.96,1.0,1.0,1.1,1.1,1.2,1.2,1.24,1.28,1.33,1.37,1.41,1.45,1.49,1.53,1.57};
float code E_temp[]={0,0.3,0.5,0.8,1,1.3,1.5,1.8,2,2.3,2.5,2.8,3,3.3,3.5,3.8,4.1,4.3,4.6,4.8,
5.1,5.3,5.6,5.8,6.1,6.3,6.6,6.8,7.1,7.3,7.6,7.8,8.1,8.3,8.6,8.8,9.1,9.3,9.6,9.8,
10.1,10.3,10.6,10.8,11.1,11.3,11.6,11.8,12.1,12.3,12.6,12.8,13.1,13.3,13.6,13.8,14.1,14.3,14.6,14.8, //K型热电偶:在电压值对应下的温度值,用于非线性较正。
15.1,15.3,15.6,15.8,16.1,16.3,16.6,16.8,17.1,17.3,17.6,17.8,18.1,18.3,18.6,18.8,19.1,19.3,19.6,19.8,
20.1,20.3,20.5,20.8,21.0,21.3,21.5,21.8,22.0,22.3,22.5,22.8,23.0,23.3,23.5,23.8,24.0,24.3,24.5,24.8,
25.0,25.2,25.5,25.7,26.0,26.2,26.5,26.7,27.0,27.2,27.5,27.7,28.0,28.2,28.4,28.7,28.9,29.2,29.4,29.7,
29.9,30.2,30.4,30.7,30.9,31.2,31.4,31.6,31.9,32.1,32.4,32.6,32.9,33.1,33.4,33.6,33.9,34.1,34.3,34.6,
34.8,35.1,35.3,35.6,35.8,36.1,36.3,36.5,36.8,37.0,37.3,37.5,37.8,38.0,38.3,38.5,38.7,39.0,39.2,39.5,
39.7,40.0,40.2,40.4,40.7,40.9,41.2,41.4,41.7,41.9,42.2,42.4,42.6,42.9,43.1,43.4,43.6,43.9,44.1,44.3,
44.6,44.8,45.1,45.3,45.6,45.8,46.0,42.3,46.5,46.8,47.0,47.3,47.5,47.7,48.0,48.2,48.5,48.7,49.0,49.2,
49.4,49.7,49.9,50.2,50.4,50.7,50.9,51.1,51.4,51.6,51.9,52.1,52.4,52.6,52.8,53.1,53.3,53.6,53.8,54.0,
54.3,54.5,54.8,55.0,55.3,55.5,55.7,56.0,56.2,56.5,56.7,57.0,57.2,57.4,57.7,57.9,58.2,58.4,58.6,58.9,
59.1,59.4,59.6,59.8,60.1,60.3,60.6,60.8,61.1,61.3,61.5,61.8,62.0,62.3,62.5,62.7,63.0,63.2,63.5,63.7,
64.0,64.2,64.4,64.7,64.9,65.2,65.4,65.6,65.9,66.1,66.4,66.6,66.8,67.1,67.3,67.6,67.8,68.0,68.3,68.5,
68.8,69.0,69.3,69.5,69.7,70.0,70.2,70.5,70.7,70.9,87.2,71.4,71.7,71.9,72.1,72.4,72.6,72.8,73.1,73.3,
73.6,73.8,74.1,74.3,74.5,74.8,75.0,75.3,75.5,75.8,76.0,76.2,76.5,76.7,77.0,77.2,77.4,77.7,77.9,78.2,
78.4,78.6,78.9,79.1,79.4,79.6,79.8,80.1,80.3,80.6,80.8,81.0,81.3,81.5,81.8,82.0,82.3,82.5,82.7,83.0,
83.2,83.5,83.7,83.9,84.2,84.4,84.7,84.9,85.1,85.4,85.6,85.9,86.1,86.3,86.6,86.8,87.1,87.3,87.6,87.8,
88.0,88.3,88.5,88.8,89.0,89.2,89.5,89.7,90.0,90.2,90.4,90.7,90.9,91.2,91.4,91.6,91.9,92.1,92.4,92.6,
92.9,93.1,93.3,93.6,93.8,94.1,94.3,94.5,94.8,95.0,95.3,95.5,95.7,96.0,96.2,96.5,96.7,97.0,97.2,97.4,
97.7,97.9,98.2,98.4,98.6,98.9,99.1,99.4,99.6,99.9,100.1,100.3,100.6,100.8,101.1,101.3,101.5,101.8,102.0,102.3,
102.5,102.8,103.0,103.2,103.5,103.7,104.0,104.2,104.5,104.7,104.9,105.2,105.4,105.7,105.9,106.1,106.4,106.6,106.9,107.1,
107.4,107.6,107.8,108.1,108.3,108.6,108.8,109.1,109.3,109.5,109.8,110.0,110.3,110.5,110.8,111.0,111.2,111.5,111.7,112.0,
112.2,112.5,112.7,112.9,113.0,113.2,113.4,113.7,113.9,114.2,114.4,114.6,114.9,115.1,115.4,115.6,115.9,116.1,116.6,116.8,
117.1,117.3,117.6,117.8,118.1,118.3,118.5,118.8,119.0,119.3,119.5,119.8,120.0,120.3,120.5,120.7,121.0,121.2,121.5,121.7,
122.0,122.2,122.5,122.7,122.9,123.2,123.4,123.7,123.9,124.2,124.4,124.7,124.9,125.1,125.4,125.6,125.9,126.1,126.4,126.6,
126.9,127.1,127.3,127.6,127.8,128.1,128.3,128.6,128.8,129.1,129.3,129.6,129.8,130.0,130.3,130.5,130.8,131.0,131.3,131.5,
131.8,132.0,132.3,132.5,132.7,133.0,133.2,133.5,133.7,134.0,134.2,134.5,134.7,135.0,135.2,135.5,135.7,135.9,136.2,136.4};
//延时函数
void delay(unsigned int i)
{
while(i--);
}
//启动TLC1543函数
unsigned int read1543(unsigned char port) //从TLC1543读取采样值,形参port是采样的通道号
{
unsigned int data ad;unsigned int data i;
unsigned char data al=0,ah=0;
CLOCK=0;
_CS=0;
port < <=4;
for (i=0;i <4;i++) //把通道号打入1543
{
D_IN=(bit)(port&0x80);CLOCK=1;CLOCK=0;
port < <=1;
}
for (i=0;i <6;i++) //填充6个CLOCK
{
CLOCK=1;CLOCK=0;
}
_CS=1;
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();
_CS=0; //等待AD转换
_nop_();_nop_();_nop_();
for (i=0;i <2;i++) //取D9,D8
{
D_OUT=1;
CLOCK=1;
ah < <=1;
if (D_OUT) ah|=0x01;
CLOCK=0;
}
for (i=0;i <8;i++) //取D7--D0
{
D_OUT=1;
CLOCK=1;
al < <=1;
if (D_OUT) al|=0x01;
CLOCK=0;
}
_CS=1;
ad=(unsigned int)ah;ad < <=8;ad|=(unsigned int)al; //得到AD值
return (ad);
}
//初始化函数
init_ds18b20(void)
{
unsigned char x=0;
DQ=1; //DQ复位
delay(8); //稍作延时
DQ=0; //拉低DQ
delay(80);
DQ=1;
delay(14);
x=DQ;
delay(20);
}
//读一个字节
Readonechar(void)
{
unsigned char i=0;
unsigned char dat=0;
for(i=8;i> 0;i--)
{
DQ=0; //给脉冲信号
dat> > =1;
DQ=1; //给脉冲信号
if(DQ)
dat|=0x80;
delay(4);
}
return(dat);
}
//写一字节
Writeonechar(unsigned char dat)
{
unsigned char i=0;
for(i=8;i> 0;i--)
{
DQ=0;
DQ=dat&0x01;
delay(5);
DQ=1;
dat> > =1;
}
delay(4);
}
//读温度函数
Readtemperature(void)
{
unsigned char a=0;
unsigned char b=0;
unsigned char t=0;
init_ds18b20();
Writeonechar(0xCC); // 跳过读序号列号的操作
Writeonechar(0x44); // 启动温度转换
init_ds18b20();
Writeonechar(0xCC); //跳过读序号列号的操作
Writeonechar(0xBE); //读取温度寄存器等(共可读9个寄存器) 前两个就是温度
a=Readonechar(); //读取温度值低位
b=Readonechar(); //读取温度值高位
a=a> > 4; //低位右移4位,舍弃小数部分
t=b < <4; //高位左移4位,舍弃符号
t=t|a;
return(t);
}
//显示函数
Display(float T)
{
unsigned char a[4];
int k,i,b;
unsigned char table[11]={0XD7,0X14,0XCD,0X9D,0X1E,0X9B,0XDB,0X15,0XDF,0X1F,0x00}; // 0-9数字加不显示的编码
unsigned char tablebit[4]={0xfe,0xfd,0xfb,0xf7}; //4位数码各位的控制端口
b=(int)(10*T);
a[0]=b%10; //a[0]:小数 a[1]:个位 a[2]:十位 a[3]:百位
b=b/10;
a[1]=b%10;
b=b/10;
a[2]=b%10; //p2.3 com1
a[3]=b/10; //p2.2 com2
//p2.1 com3
//p2.0 com4
if(a[3]==0)
{
a[3]=10; //百位为0不显示
if(a[2]==0)
a[2]=10; //十位为0不显示
}
for(k=0;k <4;k++)
{
p2=tablebit[k];
i=a[k];
p1=table[i];
if(k==1)
dp=1; //点亮小数点
delay(20);
}
}
main(void)
{
[color=#FF1493]float e,t,vol;
unsigned int ee,tem;
while(1)
{
ee= read1543(0x00); //读AD转化值
e=(float)ee;
e=e*0.0069; //e=ee*5/1024*(1000/710) 信号放大倍数为710;
tem=Readtemperature(); //DS18B20读环境温度值
vol=ldbc[tem]; //读补偿电压
e=e+vol; //冷端补偿
ee=(int)(100*e);
t=E_temp[ee]; //非线性较正(查表法)算温度
Display(t);[/color] }
}
[/color]
发表时间:2006年5月21日17:16:02