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
I need your help and support, as I'm out of ideas.
I'm working on simple micro-temperature controller project, as this is the first one for me in uC.
I'm using at89s52 with 11.0592 MHz, plus the DS18b20 & LCD 16x2.
And I'm compiling c code using Keil uVision V4. Below is the whole code I'm using.
My hardware connection:
DS18B20: DQ = P3.3 with 4K7 connect to Power Vdd
LCD: RS = P3.6
RW = P3.5
E = P3.7
DataLine = P1
My problem here is with the temperature reading is stuck at too high reading 655.30 *C. And the reading doesn't change at all.
I wonder why? I have troubleshoot the coding, but nothing seems wrong with the code. I'm suspecting a hardware issue, but I'm not very experience with such connection.
So, please help resolve this problem. And thank you in advance.
I'm working on simple micro-temperature controller project, as this is the first one for me in uC.
I'm using at89s52 with 11.0592 MHz, plus the DS18b20 & LCD 16x2.
And I'm compiling c code using Keil uVision V4. Below is the whole code I'm using.
My hardware connection:
DS18B20: DQ = P3.3 with 4K7 connect to Power Vdd
LCD: RS = P3.6
RW = P3.5
E = P3.7
DataLine = P1
My problem here is with the temperature reading is stuck at too high reading 655.30 *C. And the reading doesn't change at all.
I wonder why? I have troubleshoot the coding, but nothing seems wrong with the code. I'm suspecting a hardware issue, but I'm not very experience with such connection.
So, please help resolve this problem. And thank you in advance.
Code:
//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;
}
}