Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] DS18B20 convert confuse

Status
Not open for further replies.

sherryliu

Member level 1
Member level 1
Joined
Jan 17, 2011
Messages
32
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Visit site
Activity points
1,535
#include<reg52.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit DATA = P1^1;
uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
int temp;
int ss;
int dd;
int j;
uchar data b;
uchar data buf[4];
int alarmH=320;
int alarmL=100;

sbit k1=P2^5;
sbit k2=P2^6;
sbit k3=P2^7;
sbit k4=P2^4;
sbit bell=P1^0;
sbit HLight=P1^2;
sbit LLight=P1^3;
sbit warn=P1^4;
sbit Red=P1^6;
sbit Green=P1^7;
bit set=0;
bit Flag=0;
int n;

void key_to1();
void key_to2();
void delay(uint);
void key();
void Show();

void delay(uint num)
{
while(num--) ;
}


Init_DS18B20(void)
{
uchar x=0;
DATA = 1;
delay(10);
DATA = 0;
delay(80);
DATA = 1;
delay(20);
x=DATA;
delay(30);
}
ReadOneChar(void)
{
uchar i=0;
uchar dat = 0;
for (i=8;i>0;i--)
{
DATA = 0;
dat>>=1;
DATA = 1;
if(DATA)
dat|=0x80;
delay(8);
}
return(dat);
}
WriteOneChar(unsigned char dat)
{
uchar i=0;
for (i=8; i>0; i--)
{
DATA = 0;
DATA = dat&0x01;
delay(10);
DATA = 1;
dat>>=1;
}
delay(8);
}
int ReadTemperature(void)
{
uchar a=0;
uchar b=0;
int t=0;
float tt=0;
Init_DS18B20();
WriteOneChar(0xCC);
WriteOneChar(0x44);
Init_DS18B20();
WriteOneChar(0xCC);
WriteOneChar(0xBE);
a=ReadOneChar();
b=ReadOneChar();
t=b;
t<<=8;
t=t|a;
tt=t*0.0625;
t= tt*10+0.5;
return(t);
}
void display00()
{
dd=-(temp-1);
buf[1]=dd/100;
buf[2]=dd/100;
buf[3]=dd%100/10;
buf[0]=dd%10;

for(j=0;j<5;j++)
{
P2=0xff;
P0=0x00;
P2=0xfd;
P0=0x80;
delay(100);
P2=0xff;
P0=0x00;
P2=0xf7;
P0=0x40;
delay(100);
P2=0xff;
P0=0x00;
P2=0xfb;
P0=table[buf[2]];
delay(100);
P2=0xff;
P0=0x00;
P2=0Xfd;
P0=table[buf[3]];
delay(100);
P2=0xff;
P0=0x00;
P2=0Xfe;
P0=table[buf[0]];
delay(100);
P2=0xff;
}
}

void display()
{
buf[1]=temp/1000;
buf[2]=temp/100%10;
buf[3]=temp%100/10;
buf[0]=temp%10;
for(j=0;j<3;j++)
{
P2=0xff;
P0=0x00;
P2=0xfd;
P0=0x80;
delay(300);
P2=0xff;
P0=0x00;
P2=0xf7;
P0=table[buf[1]];
delay(300);
P2=0xff;
P0=0x00;
P2=0xfb;
P0=table[buf[2]];
delay(300);
P2=0xff;
P0=0x00;
P2=0Xfd;
P0=table[buf[3]];
delay(300);
P2=0xff;
P0=0x00;
P2=0Xfe;
P0=table[buf[0]];
delay(300);
P2=0xff;
}
}
void key()
{ if(k1!=1)
{
delay(20);
if(k1!=1)
{
while(k1!=1)
{ key_to1();
for(n=0;n<8;n++)
Show();
}
}
}
if(k2!=1)
{
delay(20);
if(k2!=1)
{
while(k2!=1)
{ key_to2();
for(n=0;n<8;n++)
Show();
}

}
}
if(k3!=1)
{ TR0=1;
temp=ReadTemperature();
}
if(k4!=1)
{ delay(20);
if(k4!=1)
{
while(k4!=1);
set=!set;
if(set==0)
{
Red=0;Green=1;
}
else
{
Green=0;Red=1;
}
}
}
}
void key_to1()
{
TR0=0;
temp+=10;
if(temp>=1100)
{temp=-550;}
if(set==0)
{alarmH=temp;}
else {alarmL=temp;}
}
void key_to2()
{
TR0=0;

temp-=10;
if(temp<=-550)
{temp=1100;}
if(set==0)
{ alarmH=temp;}
else { alarmL=temp;}
}

void alarm(void)
{
if(temp>alarmH||temp<alarmL)
{ //bell=1;
//delay(50);
//bell=0;
Flag=1;
}else {Flag=0;}
}

logo()
{ P0=0x40;

P2=0xf7;
delay(50);
P2=0xfb;
delay(50);
P2=0Xfd;
delay(50);
P2=0Xfe;
delay(50);
P1 = 0xff;
}

void Show()
{ if(temp>=0)
{HLight=1;LLight=0;display();}
if(temp<0)
{HLight=0;LLight=1;display00();}
}
void main()
{
TCON=0x01;
TMOD=0X01;
TH0=0XD8;
TL0=0XF0;
EA=1;
ET0=1;
TR0=1;
EX0=1;

for(n=0;n<500;n++)
{bell=1;warn=1;logo();}
Red=0;
while(1)
{
key();
ss=ReadTemperature();
Show();
alarm();
if(Flag==1)
{bell=!bell;
warn=!warn;}
else {bell=1;
warn=1;}
}
}
void time0(void) interrupt 1 using 1
{ TH0=0X56;
TL0=0XDC;
temp=ss;
}

164404lmlamu0ll4dq4uk4.jpg08071387advf85usdv7zpo.jpg


Hi guys,could you help me explain this mean?Suppose ,measuring 85.C,first a=50,b=05,t=0550,then tt=550*0.0625=34.375 or tt=550H*0.0625=1360*0.0625=85,finally t=tt*10+0.5=34.375*10+0.5=344.25 or t=tt*10+0.5=85*10+0.5=850.5,Thanks!!!
 

The output od DS18B20 is 2 bytes

a = 0x50, b = 0x05, t = 0x0550

t = b << 8 puts the b in high byte of t

t = t | a adds a to low byte.

a and b contains the low and high bytes

If a = 0x02 and b = 0x03 then

t = 0x0302

tt = 0x0302 * 0.0625 = 48.125

t = tt * 10 + 0.5 = 481.25 + 0.5 = 481.75
 

Hi jayanth,i know what you are said,because t is int,from -32768to 32767,tt is float that tt=0x0550*0.0625=85.0000,finally t=tt*10+0.5=85*10+0.5=850.5.how to convert from float to int? Thanks for your reply.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top