关于ds18b20 温度报警上限th设置问题,非常蛋疼。。。。。
void wirte_1()
{
chushihua();//对18b20初始化复位
delay1ms(1);//延时
write(0x4e);//这是写入th,tl和配置寄存器命令,
write(0x21);//写进th的内容为0x21(33)
}
uchar wirte_2()
{
uchar f=0;
chushihua();//复位
delay1ms(1);//延时
write(0xbe);//这是读取寄存器的命令,读取将从字节0开始,一只进行下去,知道第9字节(字节8,CRC)读完
f=read();//读取ls
f=read();//读取ms
f=read();//读取th
if(f==0x21) return 1;//如果读取到的th与写入的th一致,返回1
else return f;//否则返回0
}
void main()
{
uchar a;
wirte_1();
a=wirte_2();
while(1)
{
display(a);//为显示函数,可以正常运行,显示总为255(11111111)
}
}
这是源码:
#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define DSPORT p3_7
//#include"lcd.h"
unsigned char code DIG_CODE[17] = {
0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
unsigned char code DIG_CODE2[20]={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef,
0xf7,0xfc,0x99,0xde,0xf9,0xf1,0xf3,0xf6,0xee};
sbit p3_7=P3^7;
void delay700us(void) //误差 0us
{
unsigned char a,b;
for(b=41;b>0;b--)
for(a=7;a>0;a--);
}
void delay1ms(unsigned int y)
{
unsigned int x;
for( ; y>0; y--)
{
for(x=110; x>0; x--);
}
}
void delay500us(void) //误差 -0.868055555556us
{
unsigned char a,b;
for(b=1;b>0;b--)
for(a=227;a>0;a--);
}
void delay65us(void) //误差 -0.980902777778us
{
unsigned char a;
for(a=28;a>0;a--);
}
void delay500ms(void) //误差 -0.000000000114us
{
unsigned char a,b,c;
for(c=98;c>0;c--)
for(b=127;b>0;b--)
for(a=17;a>0;a--);
_nop_(); //if Keil,require use intrins.h
}
void display(unsigned int b)
{
unsigned char a;
a=b%10;
P0=DIG_CODE[a];
P2=0x07;
delay500us();
delay500us();
a=b%100/10;
P0=DIG_CODE[a];
P2=0x06;
delay500us();
delay500us();
a=b%1000/100;
P0=DIG_CODE2[a];
P2=0x05;
delay500us();
delay500us();
a=b%10000/1000;
P0=DIG_CODE[a];
P2=0x04;
delay500us();
delay500us();
a=b%100000/10000;
P0=DIG_CODE[a];
P2=0x03;
delay500us();
delay500us();
a=b%1000000/100000;
P0=DIG_CODE[a];
P2=0x02;
delay500us();
delay500us();
a=b%10000000/1000000;
P0=DIG_CODE[a];
P2=0x01;
delay500us();
delay500us();
a=b%100000000/10000000;
P0=DIG_CODE[a];
P2=0x00;
delay500us();
delay500us();
}
unsigned char chushihua(void)
{
p3_7=0;
delay700us();
p3_7=1;
while(1)
{
if(p3_7==0)
{
return 1;
break;
}
}
}
void write(unsigned char ba)//写一个字节
{
unsigned char i,j;
for(i=0;i<8;i++)
{
p3_7=1;
// delay65us();
p3_7=0;
j++;
p3_7=ba&0x01;
j=6;
while(j--);
ba=ba>>1;
p3_7=1;
}
}
unsigned char read(void)//读一个字节
{
unsigned char a=0,temp;
unsigned char i,j;
for(i=0;i<8;i++)
{
p3_7=0;
j++;
p3_7=1;
j++;
j++;
a=p3_7;
temp=(temp>>1)|(a<<7);
j=4;
while(j--);
}
return temp;
}
void com()
{
chushihua();
delay1ms(1);
write(0xcc);
write(0x44);
}
void date()
{
chushihua();
delay1ms(1);
write(0xcc);
write(0xbe);
}
void Read_1()
{
float zhi;
uint thl,thh;
thl=read();
thh=read();
thh=thh<<8;
thh=thl+thh;
zhi=thh;
thh=zhi*0.0625*100+0.5;
display(thh);
}
void wirte_1()
{
chushihua();
delay1ms(1);
write(0x4e);
write(0x21);
}
uchar wirte_2()
{
uchar f=0;
chushihua();
delay1ms(1);
write(0xbe);
f=read();
f=read();
f=read();
if(f==0x21) return 1;
else return f;
}
void main()
{
uchar a;
while(1)
{
com();
date();
Read_1();
wirte_1();
a=wirte_2();
while(1)
{
display(a);
}
}
}