我用的51学习板,主要实现的功能是LM35温度传感器~~LM35需要经过运算放大器放大输出电压以后再传送到AD模块转换为数字数据,然后数字数据传到单片机,单片机去控制显示。之前写过DS18B20的温度传感器,不过它输出是数字数据,不用AD模块。
程序如下:
#include
#include
#define uchar unsigned char
#define uint unsigned int
#define Write_PCF8591 0x90
#define Read_PCF8591 0x91
#define Control_Byte 0x40
***it SCL=P2^0;
***it SDA=P2^1;
***it dula=P2^6;
***it wela=P2^7;
***it LM35=P2^2;
***it beep=P2^3;
unsigned char code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,
0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
unsigned char code table1[]={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,
0x87,0xff,0xef};
uchar num,temp;
void delayms(uint z)
{
uint x,y;
for(x=0;x
for(y=0;y<110;y++);
}
void delay(uint count)
{
uint i;
while(count)
{
i=200;
while(i>0)
i--;
count--;
}
}
void delay0()
{
_nop_();_nop_();
_nop_();_nop_();
_nop_();_nop_();
_nop_();_nop_();
}
void init_IIC()
{
SDA=1;
delay0();
SCL=1;
delay0();
}
void start()
{
SDA=1;
delay0();
SCL=1;
delay0();
SDA=0;
delay0();
}
void stop()
{
SDA=0;
delay0();
SCL=1;
delay0();
SDA=1;
delay0();
}
void ack()
{
uchar i=0;
SCL=1;
delay0();
while((SDA==1)&&(i<250)) i++;
SCL=0;
delay0();
}
void noack()
{
SDA=1;
delay0();
SCL=1;
delay0();
SCL=0;
delay0();
}
void Write_Byte(uchar dat)
{
uchar i;
for(i=0;i<8;i++)
{
SCL=0;
delay0();
if(dat&0x80)
{
SDA=1;
}
else
{
SDA=0;
}
dat=dat<<1;
SCL=1;
delay0();
}
SCL=0;
delay0();
SDA=1;
delay0();
}
uchar Read_Byte()
{
uchar i,dat;
SCL=0;
delay0();
SDA=1;
delay0();
for(i=0;i<8;i++)
{
SCL=1;
delay0();
dat=dat<<1;
if(SDA)
{
dat++;
}
SCL=0;
delay0();
}
return dat;
}
void DAC_PCF8591(uchar add,uchar dat)
{
start();
Write_Byte(add);
ack();
Write_Byte(Control_Byte);
ack();
Write_Byte(dat);
ack();
stop();
}
uchar ADC_Read(uchar add0,uchar add1,uchar chl)
{
uchar dat;
start();
Write_Byte(add0);
ack();
Write_Byte(Control_Byte|chl);
ack();
start();
Write_Byte(add1);
ack();
dat=Read_Byte();
noack();
stop();
return dat;
}
uint ADzhuanhuan ()
{
start();
while(1)
{
num=ADC_Read(Write_PCF8591,Read_PCF8591,0);
}
}
uint T()
{ temp=num*100/255;}
void display(uint temp) //显示程序
{
uchar A1,A2,A2t,A3;
A1=temp/100;
A2t=temp%100;
A2=A2t/10;
A3=A2t%10;
dula=0;
P0=table[A1]; //显示百位
dula=1;
dula=0;
wela=0;
P0=0x7e;
wela=1;
wela=0;
delay(1);
dula=0;
P0=table1[A2]; //显示十位
dula=1;
dula=0;
wela=0;
P0=0x7d;
wela=1;
wela=0;
delay(1);
P0=table[A3]; //显示个位
dula=1;
dula=0;
P0=0x7b;
wela=1;
wela=0;
delay(1);
}
void main()
{
LM35=1;
while(1)
{ADzhuanhuan();
T();
display(temp);
if(temp>35)beep=0;
else beep=1;
}
}
大家帮我看看,LM35输出是模拟数据,那其时序的控制是怎样的?求写过有经验的帮我修改一下。计算温度的准确值部分不用修改,其他部分希望指出错误所在,然后修正一下。十分感谢!
|