有哪位大佬帮忙看看的
#include<reg51.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit ds=P2^7;
sbit CS=P3^4;
sbit SID=P3^5;
sbit SCLK=P3^6;
sbit PSB=P3^7;
char code table[]={"temperature:"};
char data disdate[8];
uchar tflag; //温度正负标志
uchar value=0; //温度
/******************************液晶显示程序******************************/
void delay(uchar z)
{
uchar i,j;
for(i=z;i>0;i--)
for(j=110;j>0;j--);
}
void sendbyte(uchar byte)
{
uchar i;
for(i=0;i<8;i++)
{
SCLK=1;
if(byte&0x80)
SID=1;
else
SID=0;
SCLK=0;
byte<<=1;
}
}
void write_cmd(uchar cmd)
{
CS=1;
sendbyte(0xf8);
sendbyte(cmd&0xf0);
sendbyte(cmd<<4&0xf0);
CS=0;
delay(10);
}
void write_data(uchar date)
{
CS=1;
sendbyte(0xfa);
sendbyte(date&0xf0);
sendbyte(date<<4&0xf0);
CS=0;
delay(10);
}
void dizhi(uchar x,uchar y)
{
uchar address;
switch(x)
{
case 1:address=0x80+y;break;
case 2:address=0x90+y;break;
case 3:address=0x88+y;break;
case 4:address=0x98+y;break;
}
write_cmd(address);
}
void guding(uchar l,uchar k,uchar *s)
{
dizhi(l,k);
while(*s)
{
write_data(*s);
s++;
}
}
void init()
{
PSB=0;
write_cmd(0x30);
write_cmd(0x01);
write_cmd(0x02);
write_cmd(0x06);
write_cmd(0x0e);
}
/******************************传感器ds1802程序*******************************/
void yanshi(uint i) //延时1微秒
{
while(i--);
}
uchar fuwei() //复位
{
bit ok;
ds=1;
yanshi(4);
ds=0;
yanshi(200);
yanshi(300); //延时时间在480us~960us
ds=1;
yanshi(50); //延时等待,确定传感器的存在
ok=ds;
yanshi(25);
return ok;
}
uchar read_date() //读数据
{
uchar i=0;
uchar date=0;
for(i=0;i<8;i++)
{
ds=0;
date>>=1;
ds=1;
if(ds) //执行到这电平已经稳定,不用再加延时
date|=0x80;
yanshi(30);
}
return(date);
}
void write_date(uchar wdata) //写数据
{
uchar i=0;
for(i=0;i>8;i++)
{
ds=0;
yanshi(15); //区分读写,让读的时间过去
ds=wdata&0x01;
yanshi(45); //让有效数据稳定
ds=1; //电平稳定后,释放总线
wdata>>=1;
}
}
int read_temp() //读取温度并转换
{
uchar a,b;
fuwei();
write_date(0xcc); //跳过读序列号
write_date(0x44); //启动温度转换
yanshi(10); //给芯片转换温度的时间
fuwei();
write_date(0xcc);
write_date(0xbe); //读温度
a=read_date(); //低八位
b=read_date(); //高八位
value=b;
value<<=8;
value=value|a;
if(value<0x0fff)
tflag=0;
else
{
value=~value+1;
tflag=1;
}
value=value*(0.625)+0.5;
return value;
}
void display()
{
disdate[0]=0x30; //正负号位
disdate[1]=value/1000+0x30; //百位
disdate[2]=value%1000/100+0x30; //十位
disdate[3]=value%100/10+0x30; //个位
disdate[4]=0x2e; //小数点
disdate[5]=value%10+0x30; //小数
if(tflag==0)
disdate[0]=0x2b; //如果温度为正,不显示
else
disdate[0]=0x2d;
if(disdate[1]==0x30) //如果百位为零不显示
{
disdate[1]=0x20;
if(disdate[2]==0x30) //如果百位,个位为零,也不显示
disdate[2]=0x20;
}
}
/******************************主程序*****************************/
void main()
{
init();
delay(5);
guding(1,0,table);
while(1)
{
read_temp(); //读温度
display(); //显示温度
guding(2,1,disdate);
}
}
|