写了一个DS18B20温度传感器的程序,1602液晶上显示。乱码
时间:10-02
整理:3721RD
点击:
#include<reg52.h> //一开始,液晶总是出问题,原来是table[]中的变量用了i,与延时函数中的i重复了!
#include<intrins.h>
#include <math.H>
#define uint unsigned int
#define uchar unsigned char
uchar code table[]= "i love mcu";
uchar code tab[]="0123456789";
uint i,j,x,date;
uchar s,TP1,TP2,TP;
sbit RS=P1^0;
sbit RW=P1^1;
sbit E=P2^5;
sbit PE=P3^6;
sbit DQ=P2^2;
sbit DU=P2^6;
sbit WE=P2^7;
void delay(uchar t)
{
for(i=t;i>0;i--);
for(j=110;j>0;j--);
}
void write_com(uint y)
{
RS=0;
RW=0;
//E=0;
P0=y;
delay(1);
E=1;
delay(5);
E=0;
}
void write_date(uint date)
{
RS=1;
RW=0;
//E=0;
P0=date;
delay(2);
E=1;
delay(2);
E=0;
}
void inite()
{
DU=0;
WE=0;
write_com(0x38);
write_com(0x0f);
write_com(0x06);
//无论用哪种都行!
write_com(0x01);
}
// ds18b20
void writeonechar(uchar dater)
{
uchar i;
for(i=0;i<8;i++)
{
DQ=0;
delay(5);
DQ=1;
DQ=dater&0x01;
delay(15);
DQ=1;
dater=dater>>1; //关键!
}
delay(5);
}
uchar Readonechar(void)
{
uchar dat,i=0;
for(i=8;i>0;i--)
{
DQ=0;
dat=dat>>1;
DQ=1;
if(DQ==1)
{
dat=dat/0x80;
}
delay(4);
}
return(dat);
}
void init_DS18b20()
{
DQ=0;
delay(5);
DQ=1;
delay(3);
}
uchar Read_temp() //读取温度值函数
{
uchar a=0,b=0,t=0;
init_DS18b20();
writeonechar(0xcc);
writeonechar(0x44);
init_DS18b20();
writeonechar(0xcc);
writeonechar(0xBE);
a=Readonechar();
b=Readonechar();
t=(b<<4)|(a>>4); //
if(t>128)
{
t=~t+1;
}
return(t);
}
void main(void)
{
PE=0;
inite(); //液晶初始化
delay(5); //有无没关系!
write_com(0x80+4);
for(s=0;s<10;s++)
{
write_date(table
delay(5);
}
while(1)
{
TP= Read_temp()*0.0625;
TP1=(TP)/10;
TP2=(TP)%10;
write_com(0x80+0x40+4);
write_date(tab[TP1]);
write_com(0x80+0x40+5);
write_date(tab[TP2]);
write_com(0x80+0x40+6);
write_date(0xdf);
write_com(0x80+0x40+7);
write_date('c');
}
}
What do you want to say ?
I don't know
你是求助还是?
我是这个程序不能采集温度值,望各位大侠指点小弟
zhege^
很难