Musketeer
Newbie level 6
Hello everyone.
I'm trying to make a 8051 (AT89C51) to read temperature from DS18B20 and send it to a PC through the serial port (FT232).
I'm almost done, have the code alraedy, and it actually works in Proteus simulation.
this is my code:
when I make the actual board, it doesn't work. I mean it doesn't send anything to serial port, and I guess it's not reading anything from DS18B20 either. because I send the temp to P1 for diagnostic reasons, and when I test it P1 is just FFh (all the P1 are set to 1). I have tested simpler code, and I know this code can send data to serial port, it just doesn't work when I try to read the temperature.
So what am I missing? Any suggestion?
Thanks in advance.
I'm trying to make a 8051 (AT89C51) to read temperature from DS18B20 and send it to a PC through the serial port (FT232).
I'm almost done, have the code alraedy, and it actually works in Proteus simulation.
this is my code:
Code:
# include<reg51.h>
sbit dq = P3^5; // connect with DS1820 Data pin
sbit rs=P3^0;
sbit en=P3^1;
void delay_ms(int j)
{
unsigned char i;
for(;j;j--)
for(i=122;i<=0;i--);
}
void delayus(int us)
{
int i;
for (i=0; i<us; i++);
}
bit reset(void)
{
bit presence;
dq = 0;
delayus(29);
dq = 1;
delayus(3);
presence = dq;
delayus(25);
return(presence);
}
bit readbit(void)
{
unsigned char i=0;
dq = 0;
dq=1;
for (i=0; i < 3; i++);
return(dq);
}
void writebit(bit Dbit)
{
unsigned char i=0;
dq=0;
dq = Dbit?1:0;
delayus(5);
dq = 1;
}
unsigned char readbyte(void)
{
unsigned char i;
unsigned char din = 0;
for (i=0;i<8;i++)
{
din|=readbit()? 0x01<<i:din;
delayus(6);
}
return(din);
}
void writebyte(unsigned char dout)
{
unsigned char i;
for (i=0; i<8; i++)
{
writebit((bit)(dout & 0x1));
dout = dout >> 1;
}
delayus(5);
}
unsigned char * ReadTemp()
{
unsigned char n;
unsigned char buff[2]=0;
reset();
writebyte(0xcc);
writebyte(0x44);
while (readbyte()==0xff);
delay_ms(500);
reset();
writebyte(0xcc);
writebyte(0xbe);
for (n=0; n<9; n++)
buff[n]=readbyte();
return buff;
}
void delay_ms110(unsigned int k)
{
unsigned int i,j;
for (i=0;i<=k;i++)
for(j=0;j<110;j++);
}
void Serial_TX(char *str)
{
unsigned int j;
TMOD=0x20;
TH1=0xFD;
SCON=0x50;
TR1=1;
for(j=0;str[j]!=0;j++)
{
SBUF=str[j];
while(TI==0)
{}
TI=0;
}
delay_ms110(500);
}
void Serial_TX_temp(char tp)
{
unsigned int j;
TMOD=0x20;
TH1=0xFD;
SCON=0x50;
TR1=1;
SBUF=tp;
while(TI==0)
{}
TI=0;
delay_ms110(500);
}
void main()
{
unsigned char tp,*temp,t=0x00;
while(1)
{
dq = 0;
temp=ReadTemp();
temp[1]=temp[1]&0x07;
tp=temp[0]>>4;
temp[1]=temp[1]<<4;
tp=tp+temp[1];
P1 = tp;
Serial_TX(" tempis: ");
Serial_TX_temp(tp);
}
}
when I make the actual board, it doesn't work. I mean it doesn't send anything to serial port, and I guess it's not reading anything from DS18B20 either. because I send the temp to P1 for diagnostic reasons, and when I test it P1 is just FFh (all the P1 are set to 1). I have tested simpler code, and I know this code can send data to serial port, it just doesn't work when I try to read the temperature.
So what am I missing? Any suggestion?
Thanks in advance.
Last edited by a moderator: