FoxDie
Newbie level 5
- Joined
- Apr 7, 2013
- Messages
- 10
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1,281
- Activity points
- 1,365
//DS18B20 Code: DQ = P3.3
#include<intrins.h>
#include<AT89X51.H>
#include<math.h>
#include<stdio.h>
//LCD code: RS = P3.6 -- RW = P3.5 – E = P3.7 – DataLine = P1
sbit RS = P3^6;
sbit RW = P3^5;
sbit E = P3^7;
sbit DQ = P0^1;
void init(void);
void writeCmd( unsigned char a );
void writedat(unsigned char b );
void Delaylcd(void);
void sendstring(unsigned char *b);
void init()
{
writeCmd(0x01);
writeCmd(0x38);
writeCmd(0x0C);
writeCmd(0x06);
}
void writeCmd(unsigned char a)
{
RS = 0;
P1 = a;
RW = 0;
E = 1;
Delaylcd();
E = 0;
}
void writedat(unsigned char b)
{
RS = 1;
P1 = b;
RW = 0;
E = 1;
Delaylcd();
E = 0;
}
void Delaylcd()
{
unsigned char j,k;
for (j=0; j<255; j++)
for (k=0; k<5; k++);
}
void sendstring(unsigned char *c)
{
while(*c) //till string ends
writedat(*c++); //send characters one by one
}
//sbit DQ=P3^3;
unsigned char tmp1, tmp2;
unsigned char buffer[16];
unsigned char cdis1[16] = {" TEMPERATURE: "};
unsigned char cdis2[16] = {"C"};
void delay(int useconds);
unsigned char ow_reset(void);
unsigned char read_bit(void);
void write_bit(char bitval);
unsigned char read_byte(void);
void write_byte(char val);
void get_temp(void);
float Temp_convert ();
void delay(int useconds)
{
int s;
for (s=0; s<useconds;s++);
}
unsigned char ow_reset(void)
{
unsigned char presence=0;
DQ = 0; //pull DQ line low
delay(29); // leave it low for 480us
DQ = 1; // allow line to return high
delay(3); // wait for presence
presence = DQ; // get presence signal
delay(25); // wait for end of timeslot
return(presence); // presence signal returned
} // 0=presence, 1 = no part
unsigned char read_bit(void)
{
unsigned char i;
DQ = 0; // pull DQ low to start timeslot
DQ = 1; // then return high
for (i=0; i<3; i++); // delay 15us from start of timeslot
return(DQ); // return value of DQ line
}
void write_bit(char bitval)
{
DQ = 0; // pull DQ low to start timeslot
if(bitval==1)
{
DQ =1; // return DQ high if write 1
}
delay(5); // hold value for remainder of timeslot
DQ = 1;
}
unsigned char read_byte(void)
{
unsigned char i;
unsigned char value = 0;
for (i=0;i<8;i++)
{
if(read_bit())
{
value|=0x01<<i; // reads byte in, one byte at a time and then shifts it left.
// If DQ=0, skip, if DQ=1 execute shifting of "1" then OR.
}
delay(6); // wait for rest of timeslot
}
return(value);
}// Delay provides 16us per loop, plus 24us. Therefore delay(5) = 104us
void write_byte(char val)
{
unsigned char i, temp2;
for (i=0; i<8; i++) // writes byte, one bit at a time
{
temp2 = val>>i; // shifts val right 'i' spaces
temp2 &= 0x01; // copy that bit to temp
write_bit(temp2); // write bit in temp into
}
delay(5);
}
void get_temp(void)
{
ow_reset();
write_byte(0xCC);
write_byte(0x44);
delay(5);
ow_reset();
write_byte(0xCC);
write_byte(0xBE);
tmp1=read_byte();
tmp2=read_byte();
}
float Temp_convert ()
{
float f_temp1;
bit flag;
unsigned int uint_temp;
uint_temp=tmp2;
uint_temp=uint_temp<<8;
uint_temp=uint_temp|tmp1;
flag=uint_temp&&0xF800;
if(flag == 0xF800)
{
f_temp1=(~uint_temp+1)*0.0625;
f_temp1= f_temp1*-1;
}
else if(flag == 0)
{
f_temp1= uint_temp*0.0625;
}
return f_temp1;
}
void main()
{
float f_temp2;
unsigned int uint_temp2;
unsigned char j;
init();
writeCmd(0x80);
sendstring(cdis1);
writeCmd(0xCC);
writedat(0xDF);
writeCmd(0xCD);
sendstring(cdis2);
while(1)
{
get_temp();
f_temp2= Temp_convert();
uint_temp2 = f_temp2 * 100;
sprintf(buffer,"%3d.%2d",uint_temp2/100,uint_temp2%100);
//sprintf(buffer,"%3.4f\r\n",f_temp2);
writeCmd(0xC2);
sendstring(buffer);
for(j=0; j<1000; j++);
uint_temp2=0x00;
}
}
while(1)
{
get_temp();
f_temp2= Temp_convert();
uint_temp2 = f_temp2 * 100;
sprintf(buffer,"t2=%x",tmp2);
writeCmd(0xC0);
sendstring(buffer);
sprintf(buffer," t1=%x",tmp1);
sendstring(buffer);
for(j=0; j<1000; j++);
uint_temp2=0x00;
}
http://www.maximintegrated.com/app-notes/index.mvp/id/162
void get_temp(void)
{
ow_reset();
write_byte(0xCC); // Skip ROM
write_byte(0x44); // Start Conversion
delay(5);
ow_reset();
write_byte(0xCC); // Skip ROM
write_byte(0xBE); // Read Scratch Pad
tmp1=read_byte();
tmp2=read_byte();
}
void Read_ROMCode(void)
{
int n;
char dat[9];
sendstring(buffer);
ow_reset();
write_byte(0x33);
for (n=0;n<8;n++){dat[n]=read_byte();}
sprintf(buffer,"\n%X%X%X%X\n",dat[7],dat[6],dat[5],dat[4],dat[3],dat[2],dat[1],dat[0]);
sendstring(buffer);
}
unsigned char cdis1[16] = {" OK "};
unsigned char cdis2[16] = {" Fail "};
void main()
{
unsigned char chk=0;
unsigned char j;
init();
while(1)
{
writeCmd(0xC0);
chk=ow_reset();
if(!xchk)
{
sendstring(cdis1);
}
else
{
sendstring(cdis2);
}
for(j=0; j<1000; j++);
}
}
unsigned char ow_reset(void)
{
unsigned char presence=0;
DQ = 0; //pull DQ line low
delay(29); // leave it low for 480us
DQ = 1; // allow line to return high
delay(3); // wait for presence
presence = DQ; // get presence signal
delay(25); // wait for end of timeslot
return(presence); // presence signal returned
} // 0=presence, 1 = no part
unsigned char read_bit(void)
{
unsigned char i;
DQ = 0; // pull DQ low to start timeslot
DQ = 1; // then return high
for (i=0; i<3; i++); // delay 15us from start of timeslot
return(DQ); // return value of DQ line
}
Output data from the DS18B20 is valid for 15μs after the falling edge that initiated the read time slot. Therefore, the master must release the bus and then sample the bus state within 15μs from the start of the slot.
unsigned char i;
Set the pin connected to the 1-Wire as an output
Set the level of that pin to low (DQ = 0; // pull DQ low to start timeslot)
Delay of 2 us
Release the bus by making the pin an input
Delay of 8us
Read the data on the PIN
Delay enough to reach 60us as the datasheet says that all read time slots must last at least 60us. You can wait for example 50us (50 + 8 + 2 = 60). Make sure that your delay is correct again.
Return data line (return(DQ); // return value of DQ line)
ow_reset();
write_byte(0xCC); //Skip ROM
write_byte(0x44); // Start Conversion
delay(5);
ow_reset();
write_byte(0xCC); // Read Scratch Pad
Read_ScratchPad();
void Read_ScratchPad(void)
{
int j;
char pad[10];
write_byte(0xBE);
for (j=0;j<9;j++){pad[j]=read_byte();}
sprintf(buffer,"%X%X",pad[1],pad[0]);
sendstring(buffer);
}
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?