879 views|10 replies

69

Posts

0

Resources
The OP
 

The temperature read is 0 [Copy link]

Using 51 MCU and ds1822, the temperature read in the simulation is 0. The figure is a timing diagram on a single bus. Where is the problem? Please give me some advice.

This post is from 51mcu

Latest reply

Using 47 ohm for R2 doesn't seem right, try using 4.7K or 10K ohm. Because the resistor is too small, the 8051 GPIO cannot pull the voltage low. The sink current of 8051 should be less than 20mA. 5/47 = about 100mA   Details Published on 2023-8-22 14:59
 

1665

Posts

0

Resources
2
 

The problem is in the code

This post is from 51mcu
 
 
 

1w

Posts

25

Resources
3
 

The timing is incorrect, check the clock frequency

This post is from 51mcu
 
 
 

6027

Posts

6

Resources
4
 

The most important thing about a single bus is the timing. You can't see anything with this timing. Turn off some interrupts and try again.

This post is from 51mcu
 
Personal signature

在爱好的道路上不断前进,在生活的迷雾中播撒光引

 
 

69

Posts

0

Resources
5
 

Change to: temp=(temp*625/1);

Displays -62.5 degrees, but does not change with temperature adjustment.

This post is from 51mcu
 
 
 

69

Posts

0

Resources
6
 

#include <reg51.h>
#include <intrins.h>

#define uchar unsigned char // From now on, unsigned char can be replaced by uchar
#define uint unsigned int // From now on, unsigned int can be replaced by uint

sbit LcdRs_P = P3^5; // RS pin of 1602 LCD
sbit LcdRw_P = P3^6; // RW pin of 1602 LCD
sbit LcdEn_P = P3^7; // EN pin of 1602 LCD
sbit DQ= P3^4; // Pin definition of temperature sensor

/************************************************************/
// Millisecond delay function, time is the number of milliseconds to delay
/********************************************************/
void DelayMs(uint time)
{
uint i,j;
for(i=0;i<time;i++)
for(j=0;j<112;j++);
}


/****************************************************** ********/
// Delay 15 microseconds
/************************************ ************************/
void Delay15us(void)
{
_nop_();
_nop_();
_nop_();
_nop_();
_nop_() ;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_
(); _nop_();
_nop_()
; _nop_();
_nop_();
_nop_();
}


/************************************************************/
// 1602 LCD write command function, cmd is the command to be written
/********************************************************/
void LcdWriteCmd(uchar cmd)
{
LcdRs_P = 0;
LcdRw_P = 0;
LcdEn_P = 0;
P2=cmd;
DelayMs(2);
LcdEn_P = 1;
DelayMs(2);
LcdEn_P = 0;
}


/************************************************************/
// 1602 LCD write data function, dat is the data to be written
/********************************************************/
void LcdWriteData(uchar dat)
{
LcdRs_P = 1;
LcdRw_P = 0;
LcdEn_P = 0;
P2=dat;
DelayMs(2);
LcdEn_P = 1;
DelayMs(2);
LcdEn_P = 0;
}


/****************************************************************/
// 1602 LCD initialization function
/********************************************************/
void LcdInit()
{
LcdWriteCmd(0x38); // 16*2 display, 5*7 dot matrix, 8-bit data port
LcdWriteCmd(0x0C); // Turn on the display, do not display the cursor
LcdWriteCmd(0x06); // Add 1 to the address, and move the cursor right after writing data
LcdWriteCmd(0x01); // Clear the screen
}


/************************************************************/
// LCD cursor positioning function
/********************************************************/
void LcdGotoXY(uchar line,uchar column)
{
// First line
if(line==0)
LcdWriteCmd(0x80+column);
// Second line
if(line==1)
LcdWriteCmd(0x80+0x40+column);
}

/************************************************************/
// LCD output string function
/********************************************************/
void LcdPrintStr(uchar *str)
{
while(*str!='\0')
LcdWriteData(*str++);
}


/************************************************************/
// LCD output digital
/********************************************************/
void LcdPrintNum(uint num)
{
LcdWriteData(num/100+0x30); // Hundreds digit
LcdWriteData(num%100/10+0x30); // Tens digit
LcdWriteData(num%10+0x30); // Units digit
}


/************************************************************/
// Display temperature on LCD
/********************************************************/
void LcdPrintTemp(int temp)
{
if(temp<0)
{
LcdWriteData('-'); // minus sign
temp=0-temp; // negative number to positive number
}
if(temp>999)
{
LcdWriteData(temp/1000+0x30); // hundreds digit
}
LcdWriteData(temp%1000/100+0x30); // tens digit
LcdWriteData(temp%100/10+0x30); // ones digit
LcdWriteData('.'); // decimal point
LcdWriteData(temp%10+0x30); // one digit after the decimal
point LcdWriteData(0xdf); // Celsius symbol
LcdWriteData('C');
LcdWriteData(' ');
}

/****************************************************** ********/
// Reset DS18B20 (initialization)
/************************************ **********************/
void DS18B20_ReSet(void)
{
uchar i;
DQ=0;
i=240;
while(--i);
DQ =1;
i=30;
while(--i);
while(~DQ);
i=4;
while(--i);
}


/************************************************************/
// Write a byte to DS18B20
/********************************************************/
void DS18B20_WriteByte(uchar dat)
{
uchar j;
uchar btmp;

for(j=0;j<8;j++)
{
btmp=0x01;
btmp=btmp<<j;
btmp=btmp&dat;

if(btmp>0) // Write 1
{
DQ=0;//DQ=0
Delay15us();
DQ=1;
Delay15us(); Delay15us(); Delay15us(); Delay15us(); } else // Write 0 { DQ=0; Delay15us();
Delay15us (); Delay15us(); Delay15us(); DQ=1; Delay15us(); } } }















/************************************************************/
//Read temperature value
/********************************************************/
int DS18B20_ReadTemp(void)
{
uchar j;
int b,temp=0;

DS18B20_ReSet(); // Generate reset pulse
DS18B20_WriteByte(0xcc); // Ignore ROM instruction
DS18B20_WriteByte(0x44); // Start temperature conversion instruction

DS18B20_ReSet(); // Generate reset pulse
DS18B20_WriteByte(0xcc); // Ignore ROM command
DS18B20_WriteByte(0xbe); // Read temperature command

for(j=0;j<16;j++) //Read the temperature quantity
{
DQ=0;
_nop_();
_nop_();
DQ=1;
Delay15us();
b=DQ;
Delay15us();
Delay15us();
Delay15us();
b=b<<j;
temp=temp|b;
}

temp=(temp*625/1); //Synthesize the temperature value and amplify it by 10 timesreturn
(temp); //Return the detected temperature value
}


/************************************************************/
// Main function
/************************************************************/
void main()
{
int temp; // Save the results measured by the temperature sensor

LcdInit(); // Execute LCD initialization

LcdGotoXY(0,0); // Position to row 0, column 0
LcdPrintStr(" temp="); // Row 0 displays " temp="
// LcdGotoXY(1,0); // Position to row 1, column 0
// LcdPrintStr(" dist= cm "); // Row 1 displays " dist= cm "

while(DS18B20_ReadTemp()==850) // Wait for temperature sensor initialization to complete
{
DelayMs(10);
}

while(1)
{
temp=DS18B20_ReadTemp(); // Get the temperature value of the temperature sensor
LcdGotoXY(0,7); // Position to row 0, column 7
LcdPrintTemp(temp); // Display the current temperature value
LcdGotoXY(1,7); // Cursor positioning

}
}

Here is the modified download code.

This post is from 51mcu
 
 
 

69

Posts

0

Resources
7
 

This post is from 51mcu
 
 
 

4817

Posts

4

Resources
8
 

Make sure the timing diagram is consistent with the timing requirements of the DS1822, including parameters such as time delay and write pulse width.

This post is from 51mcu
 
 
 

114

Posts

6

Resources
9
 

Using 47 ohm for R2 doesn't seem right, try using 4.7K or 10K ohm.

Because the resistor is too small, the 8051 GPIO cannot pull the voltage low.

The sink current of 8051 should be less than 20mA.

5/47 = about 100mA

This post is from 51mcu
 
 
 

69

Posts

0

Resources
10
 

Yes, the resistance value of R2 is wrong, I also found it, the timing can read the temperature after adjusting the oscilloscope, the problem now is that the temperature read is inconsistent with that of DS1822. Thank you!

This post is from 51mcu
 
 
 

69

Posts

0

Resources
11
 

alright.

This post is from 51mcu
 
 
 

Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews

Room 1530, Zhongguancun MOOC Times Building, Block B, 18 Zhongguancun Street, Haidian District, Beijing 100190, China Tel:(010)82350740 Postcode:100190

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list